import SwiftUI struct LocationView: View { let store: InventoryStore var body: some View { NavigationStack { LocationContents(store: store, initialLocation: nil) } } } private struct LocationContents: View { let store: InventoryStore let initialLocation: Place? @Environment(\.dismiss) private var dismiss @State private var items: ItemListResults @State private var addingLocation = false @State private var editingLocation = false @State private var editingAppearance = false @State private var confirmingDelete = false @State private var error: String? @State private var busy = false init(store: InventoryStore, initialLocation: Place?) { self.store = store self.initialLocation = initialLocation let preview = initialLocation.flatMap { store.cachedItems(favorites: false, location: $0.id) } _items = State(initialValue: ItemListResults(firstPage: preview)) } private var location: Place? { guard let initialLocation, !store.locationAccessLost else { return nil } if store.locationsReady { return store.locations.first { $0.id == initialLocation.id } } return initialLocation } var body: some View { Group { if store.locationAccessLost || (initialLocation != nil && store.locationsReady && location == nil) { ContentUnavailableView("位置不可用", systemImage: "archivebox", description: Text("这个位置可能已被删除,或当前账号无法访问。")) } else { contents } } .navigationTitle(location?.name ?? "位置") .navigationBarTitleDisplayMode(.inline) .task(id: store.revision) { await reloadLocations() } } private var contents: some View { List { if let location { Section { HStack(spacing: 11) { LocationMark(store: store, place: location, size: 38) VStack(alignment: .leading, spacing: 4) { Text(location.path).font(.subheadline) Text(LocationLevel(depth: location.levelDepth).title) .font(.caption).foregroundStyle(.secondary) } } Button { editingAppearance = true } label: { Label("图标与缩略图", systemImage: "square.on.square") } .disabled(busy) } } if store.canWrite { Section { NavigationLink { IntakeView(store: store, initialLocation: location) } label: { Label { VStack(alignment: .leading, spacing: 4) { Text(location == nil ? "录入物品" : "在这里录入") .font(.headline) .foregroundStyle(.primary) Text("语音录入或手动添加") .font(.subheadline) .foregroundStyle(.secondary) } } icon: { Image(systemName: "plus") .font(.headline.weight(.semibold)) .foregroundStyle(.white) .frame(width: 37, height: 37) .background(Color.atlasBlue, in: .rect(cornerRadius: 11)) .accessibilityHidden(true) } .padding(.vertical, 5) } } } if let location { PhotoGallery(store: store, target: .location(location.id)) } if !store.locationsReady && error == nil { ProgressView("正在加载位置") } Section(LocationLevel(depth: (location?.levelDepth ?? 0) + 1).title) { ForEach(store.locations.filter { $0.parentId == location?.id }) { child in NavigationLink { LocationContents(store: store, initialLocation: child) } label: { HStack(spacing: 11) { LocationMark(store: store, place: child) Text(child.name) } } } if store.canWrite { Button { addingLocation = true } label: { Label(location == nil ? "新建房间或区域" : "新建子位置", systemImage: "plus") } } } if let location { ItemsSection(store: store, results: items, location: location.id) if store.canWrite { Button("删除位置", role: .destructive) { confirmingDelete = true }.disabled(busy) } } if let error { Text(error).foregroundStyle(.red) Button("重试") { Task { await reloadLocations(refresh: true) } } } } .toolbar { if location != nil && store.canWrite { ToolbarItem(placement: .topBarTrailing) { Button("编辑") { editingLocation = true }.disabled(busy) } } } .task(id: items.loadID(query: "", revision: store.revision)) { if let location { await items.load(store: store, location: location.id) } } .refreshable { async let places: Void = reloadLocations(refresh: true) if let location { await items.load(store: store, location: location.id, refresh: true) } await places } .sheet(isPresented: $addingLocation) { PlaceEditor(store: store, parent: location?.id) { _ in } } .sheet(isPresented: $editingLocation) { if let location { PlaceEditor(store: store, existing: location) { _ in } } } .sheet(isPresented: $editingAppearance) { if let location { PlaceEditor(store: store, existing: location, personal: true) { _ in } } } .confirmationDialog("删除这个位置?", isPresented: $confirmingDelete, titleVisibility: .visible) { Button("删除位置", role: .destructive) { guard let location else { return } Task { busy = true defer { busy = false } do { try await store.delete(store.root + "/locations/\(location.id)") try await store.refreshLocations() dismiss() } catch { self.error = error.localizedDescription } } } } message: { Text("请先清空物品和子位置。删除这个位置时,里面的照片会一起删除。") } } private func reloadLocations(refresh: Bool = false) async { do { try await store.refreshLocations(refresh: refresh); error = nil } catch is CancellationError {} catch where !Task.isCancelled { self.error = error.localizedDescription } catch {} } } struct PlaceEditor: View { let store: InventoryStore let parent: UUID? let existing: Place? let personal: Bool let saved: (UUID) -> Void @Environment(\.dismiss) private var dismiss @FocusState private var focused: Bool @State private var name: String @State private var icon: String @State private var iconPhoto: UUID? @State private var defaultDisplay: Bool @State private var photos: [Photo] = [] @State private var error: String? @State private var busy = false init(store: InventoryStore, parent: UUID? = nil, existing: Place? = nil, personal: Bool = false, saved: @escaping (UUID) -> Void) { self.store = store self.parent = parent self.existing = existing self.personal = personal self.saved = saved _name = State(initialValue: existing?.name ?? "") _icon = State(initialValue: InventorySymbol.location(personal ? existing?.displayIcon : existing?.icon).id) _iconPhoto = State(initialValue: personal ? existing?.iconPhotoId : nil) _defaultDisplay = State(initialValue: personal && existing?.displayIcon == existing?.icon && existing?.iconPhotoId == nil) } private var level: LocationLevel { LocationLevel(depth: existing?.levelDepth ?? ((store.locations.first { $0.id == parent }?.levelDepth ?? 0) + 1)) } private var title: String { if existing != nil { return personal ? "图标与缩略图" : "编辑位置" } return parent == nil ? "新建房间或区域" : "新建子位置" } var body: some View { NavigationStack { Form { if !personal { TextField("名称", text: $name).focused($focused) } Section(level.title) { SymbolPicker(symbols: InventorySymbol.palette(level: level, including: icon), color: level.tint, selection: Binding( get: { iconPhoto == nil && !defaultDisplay ? icon : "" }, set: { icon = $0; iconPhoto = nil; defaultDisplay = false } )) if personal { Button("使用空间默认图标") { iconPhoto = nil defaultDisplay = true } } } if personal, let existing, !photos.isEmpty { Section { ScrollView(.horizontal) { HStack(spacing: 11) { ForEach(photos) { photo in Button { iconPhoto = photo.id defaultDisplay = false } label: { PhotoImage(store: store, target: .location(existing.id), photo: photo.id, thumbnail: true, placeholderTint: level.tint) .frame(width: 44, height: 44) .clipShape(.rect(cornerRadius: 7)) .overlay { RoundedRectangle(cornerRadius: 7) .strokeBorder(iconPhoto == photo.id ? level.tint : .clear, lineWidth: 2) } } .buttonStyle(.plain) .accessibilityLabel("选为我的缩略图") .accessibilityAddTraits(iconPhoto == photo.id ? .isSelected : []) } } } } header: { Text("我的照片缩略图") } footer: { Text("只影响你看到的位置列表图标。") } } if let error { Text(error).foregroundStyle(.red) } } .disabled(busy) .dismissibleKeyboard(active: focused) { focused = false } .navigationTitle(title) .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() }.disabled(busy) } ToolbarItem(placement: .confirmationAction) { Button("保存") { focused = false; Task { await save() } } .disabled(busy || (!personal && name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)) } } } .interactiveDismissDisabled(busy) .task { guard personal, let existing else { return } do { photos = try await store.get(store.root + PhotoTarget.location(existing.id).path) } catch where !Task.isCancelled { self.error = error.localizedDescription } catch {} } } private func save() async { guard !busy else { return } busy = true error = nil defer { busy = false } do { if personal, let existing { let personalIcon = defaultDisplay || iconPhoto != nil ? nil : icon try await store.saveLocationDisplay(.init(photoId: iconPhoto, icon: personalIcon), location: existing.id) saved(existing.id) } else { let id = try await store.saveLocation(name: name, parent: existing?.parentId ?? parent, icon: icon, id: existing?.id) saved(id) } dismiss() } catch { self.error = error.localizedDescription } } }