414 lines
17 KiB
Swift
414 lines
17 KiB
Swift
import SwiftUI
|
|
|
|
struct LocationView: View {
|
|
let store: InventoryStore
|
|
let onIntake: (Place?) -> Void
|
|
|
|
var body: some View {
|
|
NavigationStack { LocationContents(store: store, initialLocation: nil, onIntake: onIntake) }
|
|
}
|
|
}
|
|
|
|
private struct LocationContents: View {
|
|
let store: InventoryStore
|
|
let initialLocation: Place?
|
|
let onIntake: (Place?) -> Void
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var items: ItemListResults
|
|
@State private var addingLocation = false
|
|
@State private var editingLocation = false
|
|
@State private var editingAppearance = false
|
|
@State private var sortingLocations = false
|
|
@State private var confirmingDelete = false
|
|
@State private var error: String?
|
|
@State private var busy = false
|
|
|
|
init(store: InventoryStore, initialLocation: Place?, onIntake: @escaping (Place?) -> Void) {
|
|
self.store = store
|
|
self.initialLocation = initialLocation
|
|
self.onIntake = onIntake
|
|
let preview = initialLocation.flatMap { store.cachedItems(favorites: false, location: $0.id) }
|
|
_items = State(initialValue: ItemListResults(firstPage: preview))
|
|
}
|
|
|
|
private var location: Place? {
|
|
guard let initialLocation, !store.locationAccessLost else { return nil }
|
|
if store.locationsReady { return store.locations.first { $0.id == initialLocation.id } }
|
|
return initialLocation
|
|
}
|
|
|
|
private var children: [Place] {
|
|
Place.displayOrder(store.locations.filter { $0.parentId == location?.id })
|
|
}
|
|
|
|
var body: some View {
|
|
Group {
|
|
if store.locationAccessLost || (initialLocation != nil && store.locationsReady && location == nil) {
|
|
ContentUnavailableView("位置不可用", systemImage: "archivebox",
|
|
description: Text("这个位置可能已被删除,或当前账号无法访问。"))
|
|
} else {
|
|
contents
|
|
}
|
|
}
|
|
.navigationTitle(location?.name ?? "位置")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.task(id: store.revision) { await reloadLocations() }
|
|
}
|
|
|
|
private var contents: some View {
|
|
List {
|
|
if let location {
|
|
Section {
|
|
locationOverview(location)
|
|
}
|
|
}
|
|
if !store.locationsReady && error == nil {
|
|
ProgressView("正在加载位置")
|
|
}
|
|
Section {
|
|
ForEach(children) { child in
|
|
NavigationLink { LocationContents(store: store, initialLocation: child, onIntake: onIntake) } label: {
|
|
HStack(spacing: 11) {
|
|
LocationMark(store: store, place: child)
|
|
Text(child.name)
|
|
}
|
|
}
|
|
}
|
|
if store.canWrite {
|
|
Button { addingLocation = true } label: {
|
|
Label(location == nil ? "新建房间或区域" : "新建子位置", systemImage: "plus")
|
|
}
|
|
}
|
|
} header: {
|
|
HStack {
|
|
LocationLevelHeading(level: LocationLevel(depth: (location?.levelDepth ?? 0) + 1))
|
|
if store.canWrite && children.count > 1 {
|
|
Spacer()
|
|
Button("排序") { sortingLocations = true }
|
|
.buttonStyle(.borderless)
|
|
.font(.subheadline.weight(.semibold))
|
|
.frame(minHeight: 44)
|
|
}
|
|
}
|
|
.textCase(nil)
|
|
}
|
|
if let location {
|
|
ItemsSection(store: store, results: items, location: location.id, onIntake: { onIntake(location) })
|
|
if store.canWrite { Button("删除位置", role: .destructive) { confirmingDelete = true }.disabled(busy) }
|
|
}
|
|
if let error {
|
|
Text(error).foregroundStyle(.red)
|
|
Button("重试") { Task { await reloadLocations(refresh: true) } }
|
|
}
|
|
}
|
|
.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) }
|
|
}
|
|
.refreshable {
|
|
async let places: Void = reloadLocations(refresh: true)
|
|
if let location { await items.load(store: store, location: location.id, refresh: true) }
|
|
await places
|
|
}
|
|
.sheet(isPresented: $addingLocation) {
|
|
PlaceEditor(store: store, parent: location?.id) { _ in }
|
|
}
|
|
.sheet(isPresented: $editingLocation) {
|
|
if let location {
|
|
PlaceEditor(store: store, existing: location) { _ in }
|
|
}
|
|
}
|
|
.sheet(isPresented: $editingAppearance) {
|
|
if let location {
|
|
PlaceEditor(store: store, existing: location, personal: true) { _ in }
|
|
}
|
|
}
|
|
.sheet(isPresented: $sortingLocations) {
|
|
LocationOrderEditor(store: store, parent: location ?? initialLocation, siblings: children)
|
|
}
|
|
.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("请先清空物品和子位置。删除这个位置时,里面的照片会一起删除。") }
|
|
}
|
|
|
|
private func locationOverview(_ location: Place) -> some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
HStack(spacing: 12) {
|
|
Button { editingAppearance = true } label: {
|
|
LocationMark(store: store, place: location, size: 44)
|
|
.overlay(alignment: .bottomTrailing) {
|
|
Image(systemName: "pencil")
|
|
.font(.system(size: 9, weight: .semibold))
|
|
.foregroundStyle(.primary)
|
|
.frame(width: 18, height: 18)
|
|
.background(Color(uiColor: .secondarySystemGroupedBackground), in: Circle())
|
|
.overlay { Circle().strokeBorder(.quaternary, lineWidth: 1) }
|
|
.offset(x: 2, y: 2)
|
|
}
|
|
.contentShape(Rectangle())
|
|
.accessibilityHidden(true)
|
|
}
|
|
.buttonStyle(.borderless)
|
|
.accessibilityLabel("编辑位置图标")
|
|
.accessibilityHint("选择图标或照片缩略图")
|
|
.disabled(busy)
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
Text(location.path).font(.subheadline)
|
|
LocationLevelBadge(level: LocationLevel(depth: location.levelDepth))
|
|
}
|
|
}
|
|
Divider()
|
|
PhotoGallery(store: store, target: .location(location.id), embedded: true)
|
|
}
|
|
.padding(.vertical, 5)
|
|
}
|
|
|
|
private func reloadLocations(refresh: Bool = false) async {
|
|
do { try await store.refreshLocations(refresh: refresh); error = nil }
|
|
catch is CancellationError {}
|
|
catch where !Task.isCancelled { self.error = error.localizedDescription }
|
|
catch {}
|
|
}
|
|
}
|
|
|
|
private struct LocationOrderEditor: View {
|
|
let store: InventoryStore
|
|
let parent: Place?
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var siblings: [Place]
|
|
@State private var snapshot: LocationOrder?
|
|
@State private var loading = true
|
|
@State private var saving = false
|
|
@State private var error: String?
|
|
|
|
init(store: InventoryStore, parent: Place?, siblings: [Place]) {
|
|
self.store = store
|
|
self.parent = parent
|
|
_siblings = State(initialValue: siblings)
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
if let error {
|
|
Section {
|
|
Text(error).font(.footnote).foregroundStyle(.secondary)
|
|
if snapshot == nil {
|
|
Button("加载最新顺序") { Task { await load() } }.disabled(loading)
|
|
}
|
|
}
|
|
}
|
|
Section {
|
|
ForEach(siblings) { sibling in
|
|
HStack(spacing: 10) {
|
|
LocationMark(store: store, place: sibling)
|
|
Text(sibling.name)
|
|
}
|
|
.moveDisabled(snapshot == nil || loading || saving)
|
|
}
|
|
.onMove { from, to in siblings.move(fromOffsets: from, toOffset: to) }
|
|
} header: {
|
|
if loading { ProgressView("正在读取顺序") }
|
|
else { Text(parent?.path ?? "空间内的一级位置") }
|
|
}
|
|
}
|
|
.environment(\.editMode, .constant(.active))
|
|
.navigationTitle("位置排序")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("取消") { dismiss() }.disabled(saving)
|
|
}
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("保存") { Task { await save() } }
|
|
.disabled(snapshot == nil || loading || saving || snapshot?.ids == siblings.map(\.id))
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(saving)
|
|
.task { await load() }
|
|
}
|
|
|
|
private func load() async {
|
|
loading = true
|
|
error = nil
|
|
defer { loading = false }
|
|
do {
|
|
let order = try await store.locationOrder(parent: parent?.id)
|
|
try await store.refreshLocations(refresh: true)
|
|
let byID = Dictionary(uniqueKeysWithValues: store.locations.map { ($0.id, $0) })
|
|
let latest = order.ids.compactMap { byID[$0] }
|
|
guard latest.count == order.ids.count else { throw AtlasError("同级位置有变化,请重新加载。") }
|
|
siblings = latest
|
|
snapshot = order
|
|
} catch is CancellationError {
|
|
} catch {
|
|
self.error = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
private func save() async {
|
|
guard let snapshot, !saving else { return }
|
|
saving = true
|
|
error = nil
|
|
defer { saving = false }
|
|
do {
|
|
try await store.saveLocationOrder(parent: parent?.id, version: snapshot.version, ids: siblings.map(\.id))
|
|
dismiss()
|
|
} catch {
|
|
if (error as? AtlasError)?.status == 409 { self.snapshot = nil }
|
|
self.error = error.localizedDescription
|
|
}
|
|
}
|
|
}
|
|
|
|
struct PlaceEditor: View {
|
|
let store: InventoryStore
|
|
let parent: UUID?
|
|
let existing: Place?
|
|
let personal: Bool
|
|
let saved: (UUID) -> Void
|
|
@Environment(\.dismiss) private var dismiss
|
|
@Environment(\.colorScheme) private var colorScheme
|
|
@FocusState private var focused: Bool
|
|
@State private var name: String
|
|
@State private var icon: String
|
|
@State private var iconPhoto: UUID?
|
|
@State private var defaultDisplay: Bool
|
|
@State private var photos: [Photo] = []
|
|
@State private var error: String?
|
|
@State private var busy = false
|
|
|
|
init(store: InventoryStore, parent: UUID? = nil, existing: Place? = nil,
|
|
personal: Bool = false, saved: @escaping (UUID) -> Void) {
|
|
self.store = store
|
|
self.parent = parent
|
|
self.existing = existing
|
|
self.personal = personal
|
|
self.saved = saved
|
|
_name = State(initialValue: existing?.name ?? "")
|
|
_icon = State(initialValue: InventorySymbol.location(personal ? existing?.displayIcon : existing?.icon).id)
|
|
_iconPhoto = State(initialValue: personal ? existing?.iconPhotoId : nil)
|
|
_defaultDisplay = State(initialValue: personal && existing?.displayIcon == existing?.icon && existing?.iconPhotoId == nil)
|
|
}
|
|
|
|
private var level: LocationLevel {
|
|
LocationLevel(depth: existing?.levelDepth ?? ((store.locations.first { $0.id == parent }?.levelDepth ?? 0) + 1))
|
|
}
|
|
|
|
private var title: String {
|
|
if existing != nil { return personal ? "图标与缩略图" : "编辑位置" }
|
|
return parent == nil ? "新建房间或区域" : "新建子位置"
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
if !personal {
|
|
TextField("名称", text: $name).focused($focused)
|
|
}
|
|
Section {
|
|
SymbolPicker(symbols: InventorySymbol.palette(level: level, including: icon),
|
|
color: level.tint(for: colorScheme), selection: Binding(
|
|
get: { iconPhoto == nil && !defaultDisplay ? icon : "" },
|
|
set: { icon = $0; iconPhoto = nil; defaultDisplay = false }
|
|
))
|
|
if personal {
|
|
Button("使用空间默认图标") {
|
|
iconPhoto = nil
|
|
defaultDisplay = true
|
|
}
|
|
}
|
|
} header: {
|
|
LocationLevelHeading(level: level)
|
|
}
|
|
if personal, let existing, !photos.isEmpty {
|
|
Section {
|
|
ScrollView(.horizontal) {
|
|
HStack(spacing: 11) {
|
|
ForEach(photos) { photo in
|
|
Button {
|
|
iconPhoto = photo.id
|
|
defaultDisplay = false
|
|
} label: {
|
|
PhotoImage(store: store, target: .location(existing.id), photo: photo.id,
|
|
thumbnail: true, placeholderTint: level.tint(for: colorScheme))
|
|
.frame(width: 44, height: 44)
|
|
.clipShape(.rect(cornerRadius: 7))
|
|
.overlay {
|
|
RoundedRectangle(cornerRadius: 7)
|
|
.strokeBorder(iconPhoto == photo.id ? level.tint(for: colorScheme) : .clear, lineWidth: 2)
|
|
}
|
|
}
|
|
.buttonStyle(.plain)
|
|
.accessibilityLabel("选为我的缩略图")
|
|
.accessibilityAddTraits(iconPhoto == photo.id ? .isSelected : [])
|
|
}
|
|
}
|
|
}
|
|
} header: {
|
|
Text("我的照片缩略图")
|
|
} footer: {
|
|
Text("只影响你看到的位置列表图标。")
|
|
}
|
|
}
|
|
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 || (!personal && name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty))
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
.task {
|
|
guard personal, let existing else { return }
|
|
do { photos = try await store.get(store.root + PhotoTarget.location(existing.id).path) }
|
|
catch where !Task.isCancelled { self.error = error.localizedDescription }
|
|
catch {}
|
|
}
|
|
}
|
|
|
|
private func save() async {
|
|
guard !busy else { return }
|
|
busy = true
|
|
error = nil
|
|
defer { busy = false }
|
|
do {
|
|
if personal, let existing {
|
|
let personalIcon = defaultDisplay || iconPhoto != nil ? nil : icon
|
|
try await store.saveLocationDisplay(.init(photoId: iconPhoto, icon: personalIcon), location: existing.id)
|
|
saved(existing.id)
|
|
} else {
|
|
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 }
|
|
}
|
|
}
|