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 retry = 0 @State private var busy = false private var location: Place? { guard let initialLocation else { return nil } return store.locations.first { $0.id == initialLocation.id } ?? initialLocation } var body: 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) } PhotoGallery(store: store, target: .location(location.id)) } 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("重试") { retry += 1 } } } .navigationTitle(location?.name ?? "位置") .navigationBarTitleDisplayMode(.inline) .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) } } .task(id: "\(retry):\(store.revision)") { do { try await store.refreshLocations(); error = nil } catch where !Task.isCancelled { self.error = error.localizedDescription } catch {} } .sheet(isPresented: $addingLocation) { PlaceEditor(store: store, parent: location?.id) { _ in } } .sheet(isPresented: $editingLocation) { if let location { PlaceEditor(store: store, existing: location) { _ in retry += 1 } } } .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("请先清空物品和子位置。删除这个位置时,里面的照片会一起删除。") } } } 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 } } }