import InventoryCore import SwiftUI struct InventoryView: View { let store: InventoryStore var favoritesOnly = false @State private var query = "" @State private var showingAdd = false private var rows: [Item] { store.inventory.search(query, favoritesOnly: favoritesOnly) } var body: some View { NavigationStack { VStack(spacing: 0) { searchHeader if rows.isEmpty { ContentUnavailableView( favoritesOnly && query.isEmpty ? "还没有收藏" : "没找到物品", systemImage: favoritesOnly ? "star" : "magnifyingglass", description: Text(favoritesOnly && query.isEmpty ? "可以在物品详情收藏经常用的东西。" : "试试别名、房间或收纳盒名称。") ) } else { List { Section { ForEach(rows) { item in NavigationLink { ItemDetailView(store: store, itemID: item.id) } label: { ItemRow(item: item, path: store.inventory.path(to: item.locationID)) } } } header: { Text("\(rows.count) 条记录") } } .listStyle(.plain) .scrollDismissesKeyboard(.interactively) } } .toolbar(.hidden, for: .navigationBar) .sheet(isPresented: $showingAdd) { ItemEditor(store: store) } } } private var searchHeader: some View { VStack(alignment: .leading, spacing: 14) { HStack { Label("Item Atlas", systemImage: "square.grid.2x2.fill") .font(.subheadline.weight(.semibold)) Spacer() Text("本机演示").font(.caption) } HStack(alignment: .center) { Text(favoritesOnly ? "我的收藏" : "找东西") .font(.largeTitle.weight(.bold)) .accessibilityAddTraits(.isHeader) Spacer() if !favoritesOnly { Button { showingAdd = true } label: { Label("记一件", systemImage: "plus") .font(.subheadline.weight(.semibold)) } .buttonStyle(.bordered) .tint(.white) } } HStack(spacing: 10) { Image(systemName: "magnifyingglass").accessibilityHidden(true) TextField("搜物品、别名或位置", text: $query) .submitLabel(.search) .textInputAutocapitalization(.never) if !query.isEmpty { Button { query = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(.secondary) } .accessibilityLabel("清除搜索") } } .padding(12) .foregroundStyle(Color.atlasNavy) .environment(\.colorScheme, .light) .background(.white, in: RoundedRectangle(cornerRadius: 10)) } .padding(18) .foregroundStyle(.white) .background(Color.atlasNavy) } } struct ItemRow: View { let item: Item let path: String var body: some View { HStack(spacing: 12) { Image(systemName: "archivebox") .font(.title3) .foregroundStyle(Color.atlasBlue) .frame(width: 43, height: 43) .background(Color.atlasBlue.opacity(0.10), in: RoundedRectangle(cornerRadius: 10)) .accessibilityHidden(true) VStack(alignment: .leading, spacing: 3) { HStack { Text(item.name).font(.headline) if item.quantity > 1 { Text("×\(item.quantity)").font(.caption).foregroundStyle(.secondary) } } Text(path).font(.subheadline).foregroundStyle(.secondary) } .frame(maxWidth: .infinity, alignment: .leading) } .padding(.vertical, 4) } } struct ItemDetailView: View { let store: InventoryStore let itemID: UUID @State private var showingMove = false @State private var errorMessage: String? var body: some View { Group { if let item = store.inventory.item(id: itemID) { List { Section("当前位置") { Label(store.inventory.path(to: item.locationID), systemImage: "location.fill") .foregroundStyle(Color.atlasBlue) Button("移位") { showingMove = true } } Section("物品信息") { LabeledContent("数量", value: "\(item.quantity)") if !item.aliases.isEmpty { LabeledContent("别名", value: item.aliases.joined(separator: "、")) } if !item.notes.isEmpty { Text(item.notes) } } if !item.moves.isEmpty { Section("移位历史") { ForEach(item.moves) { move in VStack(alignment: .leading, spacing: 4) { Text("\(move.from) → \(move.to)") Text(move.date.formatted(date: .abbreviated, time: .omitted)) .font(.caption).foregroundStyle(.secondary) } } } } } .navigationTitle(item.name) .navigationBarTitleDisplayMode(.inline) .toolbar(.visible, for: .navigationBar) .toolbar { ToolbarItem(placement: .topBarTrailing) { Button { do { try store.update { try $0.toggleFavorite(id: item.id) } } catch { errorMessage = error.localizedDescription } } label: { Image(systemName: item.favorite ? "star.fill" : "star") } .accessibilityLabel(item.favorite ? "取消收藏" : "收藏") } } .sheet(isPresented: $showingMove) { MoveEditor(store: store, item: item) } .alert("保存失败", isPresented: Binding( get: { errorMessage != nil }, set: { if !$0 { errorMessage = nil } } )) { Button("好", role: .cancel) {} } message: { Text(errorMessage ?? "") } } else { ContentUnavailableView("找不到这件物品", systemImage: "archivebox") } } } }