138 lines
5.6 KiB
Swift
138 lines
5.6 KiB
Swift
import SwiftUI
|
|
|
|
struct ItemEditor: View {
|
|
let store: InventoryStore
|
|
var initialLocation: UUID? = nil
|
|
var existing: Item? = nil
|
|
@Environment(\.dismiss) private var dismiss
|
|
@FocusState private var focus: Field?
|
|
@State private var name = ""
|
|
@State private var aliases = ""
|
|
@State private var quantity = 1
|
|
@State private var notes = ""
|
|
@State private var locationID: UUID?
|
|
@State private var showingAddLocation = false
|
|
@State private var loadingLocations = true
|
|
@State private var error: String?
|
|
@State private var busy = false
|
|
|
|
private enum Field { case name, aliases, notes }
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section("物品") {
|
|
TextField("名称", text: $name).focused($focus, equals: .name)
|
|
TextField("别名,用逗号隔开", text: $aliases).focused($focus, equals: .aliases)
|
|
Stepper("数量:\(quantity)", value: $quantity, in: 1...9999)
|
|
TextField("备注", text: $notes, axis: .vertical).focused($focus, equals: .notes)
|
|
}
|
|
if existing == nil {
|
|
Section("放在哪") {
|
|
LocationPicker(store: store, selection: $locationID)
|
|
if !loadingLocations && store.locations.isEmpty {
|
|
Button("新建位置") { focus = nil; showingAddLocation = true }
|
|
}
|
|
}
|
|
}
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
}
|
|
.dismissibleKeyboard(active: focus != nil) { focus = nil }
|
|
.navigationTitle(existing == nil ? "添加物品" : "编辑物品")
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() }.disabled(busy) }
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("保存") { focus = nil; Task { await save() } }
|
|
.disabled(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || locationID == nil || busy)
|
|
}
|
|
}
|
|
.sheet(isPresented: $showingAddLocation) {
|
|
PlaceEditor(store: store) { locationID = $0 }
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
.task {
|
|
name = existing?.name ?? ""
|
|
aliases = existing?.aliases.joined(separator: "、") ?? ""
|
|
quantity = existing?.quantity ?? 1
|
|
notes = existing?.notes ?? ""
|
|
locationID = existing?.locationId ?? initialLocation
|
|
do { if existing == nil { try await store.refreshLocations() } }
|
|
catch { self.error = error.localizedDescription }
|
|
loadingLocations = false
|
|
}
|
|
}
|
|
|
|
private func save() async {
|
|
guard let locationID else { return }
|
|
let aliasList = aliases.split(whereSeparator: { ",,、".contains($0) })
|
|
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }
|
|
busy = true
|
|
defer { busy = false }
|
|
do {
|
|
_ = try await store.save(ItemInput(name: name, quantity: quantity, notes: notes, locationId: locationID,
|
|
aliases: aliasList, version: existing?.version), id: existing?.id)
|
|
dismiss()
|
|
} catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|
|
|
|
struct MoveEditor: View {
|
|
let store: InventoryStore
|
|
let item: Item
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var locationID: UUID?
|
|
@State private var error: String?
|
|
@State private var busy = false
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section("原位置") { Text(item.locationPath) }
|
|
Section("新位置") { LocationPicker(store: store, selection: $locationID) }
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
}
|
|
.navigationTitle("移位")
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() }.disabled(busy) }
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("确认移位") { Task { await save() } }.disabled(locationID == nil || locationID == item.locationId || busy)
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
.task {
|
|
do { try await store.refreshLocations() }
|
|
catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|
|
|
|
private func save() async {
|
|
guard let locationID else { return }
|
|
struct Move: Encodable { let locationId: UUID; let version: Int }
|
|
busy = true
|
|
defer { busy = false }
|
|
do {
|
|
let _: Item = try await store.send(store.itemPath(item.id) + "/moves", Move(locationId: locationID, version: item.version))
|
|
store.changed()
|
|
dismiss()
|
|
} catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|
|
|
|
extension View {
|
|
func dismissibleKeyboard(active: Bool, action: @escaping () -> Void) -> some View {
|
|
scrollDismissesKeyboard(.interactively)
|
|
.safeAreaInset(edge: .bottom, spacing: 0) {
|
|
if active {
|
|
HStack {
|
|
Spacer()
|
|
Button(action: action) { Label("收起键盘", systemImage: "keyboard.chevron.compact.down") }
|
|
}
|
|
.padding(.horizontal).padding(.vertical, 8)
|
|
.background(.bar)
|
|
}
|
|
}
|
|
}
|
|
}
|