156 lines
6.1 KiB
Swift
156 lines
6.1 KiB
Swift
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 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 {
|
|
Label(location.path, systemImage: InventorySymbol.location(location.icon).image)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
Section(location == nil ? "房间和区域" : "子位置") {
|
|
ForEach(store.locations.filter { $0.parentId == location?.id }) { child in
|
|
NavigationLink { LocationContents(store: store, initialLocation: child) } label: {
|
|
Label(child.name, systemImage: InventorySymbol.location(child.icon).image)
|
|
.labelStyle(.titleAndIcon)
|
|
}
|
|
}
|
|
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) {
|
|
do { try await store.refreshLocations(); error = nil }
|
|
catch { self.error = error.localizedDescription }
|
|
}
|
|
.sheet(isPresented: $addingLocation) {
|
|
PlaceEditor(store: store, parent: location?.id) { _ in }
|
|
}
|
|
.sheet(isPresented: $editingLocation) {
|
|
if let location {
|
|
PlaceEditor(store: store, existing: location) { _ in retry += 1 }
|
|
}
|
|
}
|
|
.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 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 error: String?
|
|
@State private var busy = false
|
|
|
|
init(store: InventoryStore, parent: UUID? = nil, existing: Place? = nil,
|
|
saved: @escaping (UUID) -> Void) {
|
|
self.store = store
|
|
self.parent = parent
|
|
self.existing = existing
|
|
self.saved = saved
|
|
_name = State(initialValue: existing?.name ?? "")
|
|
_icon = State(initialValue: InventorySymbol.location(existing?.icon).id)
|
|
}
|
|
|
|
private var title: String {
|
|
if existing != nil { return "编辑位置" }
|
|
return parent == nil ? "新建位置" : "新建子位置"
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
TextField("名称", text: $name).focused($focused)
|
|
Section("位置图标") {
|
|
SymbolPicker(symbols: InventorySymbol.locations, color: .atlasBlue, selection: $icon)
|
|
}
|
|
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 || name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
}
|
|
|
|
private func save() async {
|
|
guard !busy else { return }
|
|
busy = true
|
|
defer { busy = false }
|
|
do {
|
|
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 }
|
|
}
|
|
}
|