147 lines
6.8 KiB
Swift
147 lines
6.8 KiB
Swift
import SwiftUI
|
||
|
||
struct IntakeView: View {
|
||
let store: InventoryStore
|
||
@FocusState private var typing: Bool
|
||
@State private var text = ""
|
||
@State private var draft: Draft?
|
||
@State private var selections: [Int: UUID] = [:]
|
||
@State private var operation: Operation?
|
||
@State private var busy = false
|
||
@State private var showingAdd = false
|
||
@State private var error: String?
|
||
|
||
private struct Draft: Decodable {
|
||
let id: UUID
|
||
let candidates: [Candidate]
|
||
let version: Int
|
||
}
|
||
private struct Candidate: Decodable {
|
||
let name: String
|
||
let quantity: Int?
|
||
let action: String
|
||
let location: String?
|
||
}
|
||
private struct Operation: Decodable { let id: UUID; let status: String }
|
||
private struct Confirm: Encodable { let version: Int; let items: [ItemInput] }
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
if store.canWrite {
|
||
Form {
|
||
Button { showingAdd = true } label: { Label("手工记一件", systemImage: "plus") }
|
||
Section("批量录入") {
|
||
TextEditor(text: $text).frame(minHeight: 120).focused($typing).accessibilityLabel("整理原话")
|
||
Button("生成草稿") { typing = false; Task { await parse() } }
|
||
.disabled(busy || operation != nil || text.count > 4000 || text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
||
}
|
||
if let draft {
|
||
Section("核对物品和位置") {
|
||
ForEach(draft.candidates.indices, id: \.self) { index in
|
||
let candidate = draft.candidates[index]
|
||
if candidate.action == "SKIP" {
|
||
Text("\(candidate.name) · 跳过").foregroundStyle(.secondary)
|
||
} else {
|
||
VStack(alignment: .leading) {
|
||
Text("\(candidate.name) ×\(candidate.quantity ?? 1)")
|
||
if let hint = candidate.location, !hint.isEmpty {
|
||
Text("原话位置:\(hint)").font(.footnote).foregroundStyle(.secondary)
|
||
}
|
||
Picker("确认位置", selection: Binding(get: { selections[index] }, set: { selections[index] = $0 })) {
|
||
Text("不录入").tag(nil as UUID?)
|
||
ForEach(store.locations) { place in Text(place.path).tag(Optional(place.id)) }
|
||
}
|
||
}.disabled(operation != nil)
|
||
}
|
||
}
|
||
Button("确认录入") { Task { await submit(draft) } }
|
||
.disabled(busy || selections.isEmpty || operation != nil)
|
||
}
|
||
}
|
||
if let operation {
|
||
Section {
|
||
Text(statusText(operation.status))
|
||
if ["COMPLETED", "FAILED", "REJECTED"].contains(operation.status) {
|
||
Button("新的录入") { draft = nil; selections = [:]; text = ""; self.operation = nil }
|
||
} else if operation.status == "NEEDS_REVIEW" {
|
||
Button("重试交付") { Task { await retry(operation) } }.disabled(busy)
|
||
} else {
|
||
Button("刷新进度") { Task { await poll(operation) } }.disabled(busy)
|
||
}
|
||
}
|
||
}
|
||
if busy { ProgressView() }
|
||
if let error { Text(error).foregroundStyle(.red) }
|
||
}
|
||
.dismissibleKeyboard(active: typing) { typing = false }
|
||
.navigationTitle("录入")
|
||
.sheet(isPresented: $showingAdd) { ItemEditor(store: store) }
|
||
} else {
|
||
ContentUnavailableView("当前空间只有查看权限", systemImage: "lock").navigationTitle("录入")
|
||
}
|
||
}
|
||
}
|
||
|
||
private func parse() async {
|
||
busy = true
|
||
defer { busy = false }
|
||
error = nil
|
||
do {
|
||
try await store.refreshLocations()
|
||
draft = try await store.send(store.root + "/drafts", ["text": text])
|
||
selections = [:]
|
||
} catch { self.error = error.localizedDescription }
|
||
}
|
||
|
||
private func submit(_ draft: Draft) async {
|
||
let items = draft.candidates.enumerated().compactMap { index, candidate -> ItemInput? in
|
||
guard let location = selections[index], candidate.action != "SKIP" else { return nil }
|
||
return ItemInput(name: candidate.name, quantity: candidate.quantity ?? 1, notes: "", locationId: location, aliases: [])
|
||
}
|
||
busy = true
|
||
defer { busy = false }
|
||
do {
|
||
let accepted: Operation = try await store.send(store.root + "/drafts/\(draft.id)/confirm",
|
||
Confirm(version: draft.version, items: items))
|
||
operation = accepted
|
||
await poll(accepted)
|
||
} catch { self.error = error.localizedDescription }
|
||
}
|
||
|
||
private func retry(_ operation: Operation) async {
|
||
busy = true
|
||
defer { busy = false }
|
||
do {
|
||
let path = store.root + "/operations/\(operation.id)/retry"
|
||
let bytes = try await store.account.request(path, method: "POST")
|
||
let accepted = try JSONDecoder().decode(Operation.self, from: bytes)
|
||
self.operation = accepted
|
||
await poll(accepted)
|
||
} catch { self.error = error.localizedDescription }
|
||
}
|
||
|
||
private func poll(_ current: Operation) async {
|
||
busy = true
|
||
defer { busy = false }
|
||
error = nil
|
||
do {
|
||
for _ in 0..<12 {
|
||
let status: Operation = try await store.get(store.root + "/operations/\(current.id)")
|
||
operation = status
|
||
if status.status == "COMPLETED" { store.changed(); return }
|
||
if ["FAILED", "REJECTED", "NEEDS_REVIEW"].contains(status.status) { return }
|
||
try await Task.sleep(for: .seconds(1))
|
||
}
|
||
} catch { self.error = error.localizedDescription }
|
||
}
|
||
|
||
private func statusText(_ status: String) -> String {
|
||
switch status {
|
||
case "COMPLETED": "物品已录入"
|
||
case "FAILED", "REJECTED": "录入失败,请核对空间权限与位置"
|
||
case "NEEDS_REVIEW": "录入结果待核对,可以重试交付"
|
||
default: "正在录入"
|
||
}
|
||
}
|
||
}
|