import SwiftUI struct InventoryView: View { let store: InventoryStore var favoritesOnly = false @Environment(\.colorScheme) private var colorScheme @State private var query = "" @FocusState private var searching: Bool var body: some View { NavigationStack { VStack(spacing: 0) { VStack(alignment: .leading, spacing: 14) { HStack(alignment: .firstTextBaseline, spacing: 12) { Text(favoritesOnly ? "收藏" : "找物").font(.largeTitle.bold()) .accessibilityAddTraits(.isHeader) .layoutPriority(1) Spacer() Label(store.space.name, systemImage: "house") .font(.subheadline) .foregroundStyle(.secondary) .lineLimit(1) } HStack(spacing: 10) { Image(systemName: "magnifyingglass").foregroundStyle(.secondary).accessibilityHidden(true) TextField("搜物品、别名或位置", text: $query) .focused($searching).submitLabel(.search).onSubmit { searching = false } .textInputAutocapitalization(.never) if !query.isEmpty { Button { query = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(.secondary) } .accessibilityLabel("清除搜索") } if searching { Button { searching = false } label: { Image(systemName: "keyboard.chevron.compact.down") } .accessibilityLabel("收起键盘") } } .padding(.horizontal, 12) .frame(minHeight: 46) .background(Color(uiColor: .secondarySystemGroupedBackground), in: .rect(cornerRadius: 13)) } .padding(18) .background(Color.entryBackground(for: colorScheme)) List { ItemsSection(store: store, query: query, favorites: favoritesOnly) } .listStyle(.plain).scrollDismissesKeyboard(.interactively) } .toolbar(.hidden, for: .navigationBar) } } } struct ItemsSection: View { let store: InventoryStore var query = "" var favorites = false var location: UUID? = nil @State private var rows: [Item] = [] @State private var displayedQuery = "" @State private var loading = true @State private var more = false @State private var loadingMore = false @State private var error: String? @State private var retry = 0 @State private var showingAdd = false private var input: String { query.trimmingCharacters(in: .whitespacesAndNewlines) } var body: some View { Section { if loading { ProgressView("加载中") } else if rows.isEmpty && error == nil { ContentUnavailableView(emptyTitle, systemImage: favorites ? "star" : "archivebox", description: Text(emptyDescription)) } ForEach(rows) { item in NavigationLink { ItemDetailView(store: store, itemID: item.id) } label: { HStack(spacing: 12) { PhotoImage(store: store, item: item.id, photo: item.coverPhotoId, thumbnail: true) .frame(width: 46, height: 46).clipShape(.rect(cornerRadius: 8)).accessibilityHidden(true) VStack(alignment: .leading, spacing: 3) { Text(item.name).font(.headline) Text(item.locationPath).font(.subheadline).foregroundStyle(.secondary) } Spacer() if item.quantity > 1 { Text("×\(item.quantity)").font(.caption).foregroundStyle(.secondary) } } .padding(.vertical, 4) } } if more { Button(loadingMore ? "加载中" : "加载更多") { Task { await load(append: true) } }.disabled(loadingMore) } if let error { Text(error).foregroundStyle(.red) Button("重试") { retry += 1 } } if !favorites && store.canWrite && input.isEmpty { Button { showingAdd = true } label: { Label("记一件", systemImage: "plus") } } } .task(id: "\(store.revision):\(retry):\(input)") { await load() } .sheet(isPresented: $showingAdd) { ItemEditor(store: store, initialLocation: location) } } private var emptyTitle: String { if !input.isEmpty { return "没找到物品" } return favorites ? "还没有收藏" : "还没有物品" } private var emptyDescription: String { if !input.isEmpty { return "试试别名、房间或收纳盒名称。" } if favorites { return "可以在详情里收藏常用的东西。" } return store.canWrite ? "从手边的一件东西开始。" : "空间成员添加物品后会显示在这里。" } private func load(append: Bool = false) async { let requested = input let revision = store.revision if append { loadingMore = true } else if rows.isEmpty || displayedQuery != requested { loading = true rows = [] more = false } defer { if append { loadingMore = false } } do { if !append && !requested.isEmpty { try await Task.sleep(for: .milliseconds(240)) } let page = try await store.search(requested, favorites: favorites, location: location, offset: append ? rows.count : 0) try Task.checkCancellation() guard requested == input, revision == store.revision else { return } if append { let existing = Set(rows.map(\.id)) rows += page.filter { !existing.contains($0.id) } } else { rows = page; displayedQuery = requested } more = page.count == 50 && rows.count <= 10000 error = nil } catch where !Task.isCancelled { self.error = error.localizedDescription } catch { return } if !append { loading = false } } } struct ItemDetailView: View { let store: InventoryStore let itemID: UUID @Environment(\.dismiss) private var dismiss @State private var detail: ItemDetail? @State private var showingMove = false @State private var showingEdit = false @State private var confirmingDelete = false @State private var error: String? @State private var busy = false var body: some View { Group { if let detail { List { Section("当前位置") { Label(detail.item.locationPath, systemImage: "location.fill").foregroundStyle(Color.atlasBlue) if store.canWrite { Button("移位") { showingMove = true } } } Section("物品信息") { LabeledContent("数量", value: "\(detail.item.quantity)") if !detail.item.aliases.isEmpty { LabeledContent("别名", value: detail.item.aliases.joined(separator: "、")) } if !detail.item.notes.isEmpty { Text(detail.item.notes) } } ItemPhotos(store: store, item: itemID) Section("我的评价") { PreferenceEditor(store: store, detail: detail, saved: { self.detail = $0 }) } if !detail.relations.isEmpty { Section("关联物品") { ForEach(detail.relations) { relation in NavigationLink { ItemDetailView(store: store, itemID: relation.itemId) } label: { LabeledContent(relation.name, value: relation.locationPath) } } } } if !detail.moves.isEmpty { Section("移位历史") { ForEach(Array(detail.moves.enumerated()), id: \.offset) { _, move in VStack(alignment: .leading, spacing: 4) { Text("\(move.fromPath) → \(move.toPath)") Text(move.movedAt.prefix(10)).font(.caption).foregroundStyle(.secondary) } } } } if store.canWrite { Button("删除物品", role: .destructive) { confirmingDelete = true }.disabled(busy) } if let error { Text(error).foregroundStyle(.red); Button("刷新") { Task { await load() } } } } .navigationTitle(detail.item.name) .toolbar { ToolbarItem(placement: .topBarTrailing) { if store.canWrite { Button("编辑") { showingEdit = true }.disabled(busy) } } } .sheet(isPresented: $showingMove) { MoveEditor(store: store, item: detail.item) } .sheet(isPresented: $showingEdit) { ItemEditor(store: store, existing: detail.item) } .confirmationDialog("删除「\(detail.item.name)」?", isPresented: $confirmingDelete, titleVisibility: .visible) { Button("删除物品和图片", role: .destructive) { Task { await archive(detail.item) } } } } else if let error { ContentUnavailableView { Label("加载失败", systemImage: "exclamationmark.circle") } description: { Text(error) } actions: { Button("重试") { Task { await load() } } } } else { ProgressView("正在加载物品") } } .navigationBarTitleDisplayMode(.inline) .toolbar(.visible, for: .navigationBar) .task(id: store.revision) { await load() } } private func load() async { do { detail = try await store.get(store.itemPath(itemID)); error = nil } catch where !Task.isCancelled { self.error = error.localizedDescription } catch {} } private func archive(_ item: Item) async { busy = true defer { busy = false } do { try await store.delete(store.itemPath(item.id) + "?version=\(item.version)"); dismiss() } catch { self.error = error.localizedDescription } } } private struct PreferenceEditor: View { let store: InventoryStore let detail: ItemDetail let saved: (ItemDetail) -> Void @State private var error: String? @State private var busy = false var body: some View { Toggle("收藏", isOn: Binding(get: { detail.preference.favorite }, set: { value in var preference = detail.preference preference.favorite = value Task { await save(preference) } })).disabled(busy) Picker("评分", selection: Binding(get: { detail.preference.rating ?? 0 }, set: { value in var preference = detail.preference preference.rating = value == 0 ? nil : value Task { await save(preference) } })) { Text("未评分").tag(0) ForEach(1...5, id: \.self) { Text("\($0) 分").tag($0) } }.disabled(busy) if let error { Text(error).foregroundStyle(.red) } } private func save(_ preference: ItemDetail.Preference) async { busy = true defer { busy = false } do { let result: ItemDetail.Preference = try await store.send(store.itemPath(detail.item.id) + "/preference", method: "PUT", preference) saved(ItemDetail(item: detail.item, moves: detail.moves, relations: detail.relations, preference: result)) store.changed() error = nil } catch { self.error = error.localizedDescription } } }