item-atlas/apps/ios/ItemAtlas/LocationPicker.swift

201 lines
7.3 KiB
Swift

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
HStack(spacing: 6) {
Button { showingPicker = true } label: {
HStack(spacing: 8) {
VStack(alignment: .leading, spacing: 4) {
Text(title).foregroundStyle(.primary)
Text(path ?? (allowsClearing ? "未设置" : "请选择"))
.font(.subheadline).foregroundStyle(.secondary)
}
Spacer()
Image(systemName: "chevron.right")
.font(.footnote.weight(.semibold))
.foregroundStyle(.tertiary)
.accessibilityHidden(true)
}
.frame(minHeight: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel(title)
.accessibilityValue(path ?? "未设置")
if allowsClearing, selection != nil {
Button { selection = nil } label: {
Image(systemName: "xmark.circle.fill")
.font(.body)
.foregroundStyle(.secondary)
.frame(width: 44, height: 44)
.contentShape(Rectangle())
}
.buttonStyle(.borderless)
.accessibilityLabel("清除位置")
}
}
.sheet(isPresented: $showingPicker) {
LocationSelection(store: store, 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).mapValues(Place.displayOrder)
}
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 store: InventoryStore
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)
} header: {
LocationLevelHeading(level: .room)
}
} 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, place: parent,
checked: selected == parent.id)
}
.buttonStyle(.plain)
}
Section {
children(tree.children(of: parent.id), tree: tree)
} header: {
LocationLevelHeading(level: LocationLevel(depth: parent.levelDepth + 1))
}
}
.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, place: place, 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, place: place,
checked: selected == place.id)
}
.buttonStyle(.plain)
}
private func row(_ title: String, detail: String?, place: Place, checked: Bool) -> some View {
HStack(spacing: 11) {
LocationMark(store: store, place: place)
VStack(alignment: .leading, spacing: 4) {
Text(title).foregroundStyle(.primary)
if let detail {
Text(detail).font(.footnote).foregroundStyle(.secondary)
LocationLevelBadge(level: LocationLevel(depth: place.levelDepth))
}
}
Spacer(minLength: 0)
if checked {
Image(systemName: "checkmark").foregroundStyle(Color.atlasBlue).accessibilityHidden(true)
}
}
.contentShape(Rectangle())
.accessibilityAddTraits(checked ? .isSelected : [])
}
}