import SwiftUI struct LocationPicker: View { let store: InventoryStore @Binding var selection: UUID? var title = "位置" var allowsClearing = false @State private var showingPicker = false var body: some View { let path = store.locations.first { $0.id == selection }?.path Button { showingPicker = true } label: { HStack(spacing: 8) { VStack(alignment: .leading, spacing: 4) { Text(title).foregroundStyle(.primary) Text(path ?? "请选择").font(.subheadline).foregroundStyle(.secondary) } Spacer() Image(systemName: "chevron.right") .font(.footnote.weight(.semibold)) .foregroundStyle(.tertiary) .accessibilityHidden(true) } .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel(title) .accessibilityValue(path ?? "未选择") .sheet(isPresented: $showingPicker) { LocationSelection(places: store.locations, selected: selection, allowsClearing: allowsClearing) { chosen in selection = chosen showingPicker = false } } } } struct LocationTree { private let places: [Place] private let byParent: [UUID?: [Place]] init(_ places: [Place]) { self.places = places byParent = Dictionary(grouping: places, by: \.parentId) } var isEmpty: Bool { places.isEmpty } func place(_ id: UUID) -> Place? { places.first { $0.id == id } } func children(of parent: UUID?) -> [Place] { byParent[parent] ?? [] } func matching(_ query: String) -> [Place] { let words = Self.words(in: query) guard !words.isEmpty else { return [] } return places.filter { place in let pathWords = Self.words(in: place.path) let spacedPath = pathWords.joined(separator: " ") return words.allSatisfy { spacedPath.contains($0) } || pathWords.joined().contains(words.joined()) } } private static func words(in value: String) -> [String] { value.folding(options: [.caseInsensitive, .widthInsensitive, .diacriticInsensitive], locale: .current) .split { $0.isWhitespace || $0 == "/" } .map(String.init) } } struct LocationSelection: View { let places: [Place] let selected: UUID? let allowsClearing: Bool let pick: (UUID?) -> Void @Environment(\.dismiss) private var dismiss @State private var query = "" var body: some View { let tree = LocationTree(places) NavigationStack { root(tree) .navigationDestination(for: UUID.self) { id in if let parent = tree.place(id) { level(parent, tree: tree) } } .navigationTitle("选择位置") .navigationBarTitleDisplayMode(.inline) .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always), prompt: "搜位置或完整路径") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() } } } } } private func root(_ tree: LocationTree) -> some View { List { if tree.isEmpty { ContentUnavailableView("还没有位置", systemImage: "folder", description: Text("请先在「位置」里新建一个存放位置。")) } else if query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { Section("房间和区域") { children(tree.children(of: nil), tree: tree) } } else { let matches = tree.matching(query) if matches.isEmpty { ContentUnavailableView.search(text: query) } else { ForEach(matches) { place in choice(place, showsPath: true) } } } if allowsClearing && selected != nil { Button("清除位置选择") { pick(nil) } } } } private func level(_ parent: Place, tree: LocationTree) -> some View { List { Section { Button { pick(parent.id) } label: { row("选这个位置", detail: parent.path, symbol: "location", checked: selected == parent.id) } .buttonStyle(.plain) } Section("子位置") { children(tree.children(of: parent.id), tree: tree) } } .navigationTitle(parent.name) .navigationBarTitleDisplayMode(.inline) } private func children(_ places: [Place], tree: LocationTree) -> some View { ForEach(places) { place in if tree.children(of: place.id).isEmpty { choice(place) } else { NavigationLink(value: place.id) { row(place.name, detail: nil, symbol: InventorySymbol.location(place.icon).image, checked: selected == place.id) } } } } private func choice(_ place: Place, showsPath: Bool = false) -> some View { Button { pick(place.id) } label: { row(place.name, detail: showsPath ? place.path : nil, symbol: InventorySymbol.location(place.icon).image, checked: selected == place.id) } .buttonStyle(.plain) } private func row(_ title: String, detail: String?, symbol: String, checked: Bool) -> some View { HStack(spacing: 11) { Image(systemName: symbol).foregroundStyle(Color.atlasBlue).accessibilityHidden(true) VStack(alignment: .leading, spacing: 4) { Text(title).foregroundStyle(.primary) if let detail { Text(detail).font(.footnote).foregroundStyle(.secondary) } } Spacer(minLength: 0) if checked { Image(systemName: "checkmark").foregroundStyle(Color.atlasBlue).accessibilityHidden(true) } } .contentShape(Rectangle()) .accessibilityAddTraits(checked ? .isSelected : []) } }