105 lines
4.2 KiB
Swift
105 lines
4.2 KiB
Swift
import SwiftUI
|
|
|
|
struct LocationView: View {
|
|
let store: InventoryStore
|
|
var body: some View {
|
|
NavigationStack { LocationContents(store: store, location: nil) }
|
|
}
|
|
}
|
|
|
|
private struct LocationContents: View {
|
|
let store: InventoryStore
|
|
let location: Place?
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var items = ItemListResults()
|
|
@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
|
|
|
|
var body: some View {
|
|
List {
|
|
if let location { Text(location.path).foregroundStyle(.secondary) }
|
|
Section(location == nil ? "房间和区域" : "子位置") {
|
|
ForEach(store.locations.filter { $0.parentId == location?.id }) { child in
|
|
NavigationLink { LocationContents(store: store, location: child) } label: {
|
|
Label(child.name, systemImage: "folder")
|
|
}
|
|
}
|
|
if store.canWrite {
|
|
Button { editingLocation = 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)
|
|
.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: $editingLocation) { PlaceEditor(store: store, parent: location?.id) { _ 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
|
|
var parent: UUID? = nil
|
|
let saved: (UUID) -> Void
|
|
@Environment(\.dismiss) private var dismiss
|
|
@FocusState private var focused: Bool
|
|
@State private var name = ""
|
|
@State private var error: String?
|
|
@State private var busy = false
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
TextField("名称", text: $name).focused($focused)
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
}
|
|
.dismissibleKeyboard(active: focused) { focused = false }
|
|
.navigationTitle(parent == nil ? "新建位置" : "新建子位置")
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() }.disabled(busy) }
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("保存") { Task { await save() } }
|
|
.disabled(busy || name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
}
|
|
|
|
private func save() async {
|
|
busy = true
|
|
defer { busy = false }
|
|
do { saved(try await store.saveLocation(name: name, parent: parent)); dismiss() }
|
|
catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|