import SwiftUI struct LocationPlanPicker: View { let store: InventoryStore let parent: Place? let pick: (UUID?) -> Void @State private var focusID: UUID? @State private var snapshot: LocationLayoutSnapshot? @State private var error: String? @State private var unavailable = false @State private var attempt = 0 init(store: InventoryStore, parent: Place?, selected: UUID?, pick: @escaping (UUID?) -> Void) { self.store = store self.parent = parent self.pick = pick let route = selected.flatMap { ItemLocationPlanRoute(locationID: $0, locations: store.locations) } _focusID = State(initialValue: route?.layers.first { $0.parent?.id == parent?.id }?.marker.id) } private var choices: [Place] { Place.displayOrder(store.locations.filter { $0.parentId == parent?.id }) } private var placed: [LayoutLocation] { let choiceIDs = Set(choices.map(\.id)) return snapshot?.locations.filter { $0.geometry != nil && choiceIDs.contains($0.id) } ?? [] } private var canChoose: Bool { !store.locationAccessLost && !unavailable } var body: some View { GeometryReader { viewport in List { Section { canvas(height: max(250, viewport.size.height * 0.55)) } header: { Text(parent?.path ?? store.space.name) .textCase(nil) } if canChoose && !choices.isEmpty { let placedIDs = Set(placed.map(\.id)) Section("位置列表") { ForEach(choices) { place in row(place, offPlan: snapshot != nil && !placedIDs.contains(place.id)) } } } } .safeAreaInset(edge: .bottom, spacing: 0) { if canChoose, let focus = choices.first(where: { $0.id == focusID }) { selectionPanel(focus) } } } .navigationTitle(parent == nil ? "从平面图选择" : "选择室内位置") .navigationBarTitleDisplayMode(.inline) .task(id: [store.revision, attempt]) { await load(refresh: attempt > 0) } } @ViewBuilder private func canvas(height: CGFloat) -> some View { if let error { VStack(alignment: .leading, spacing: 8) { Text(error).font(.subheadline).foregroundStyle(.secondary) Button("重新读取平面图") { attempt += 1 } } } else if let snapshot { if placed.isEmpty { Text(choices.isEmpty ? "这里还没有下级位置。" : "这张图还没摆放位置,可以在下面选择。") .font(.subheadline).foregroundStyle(.secondary) } else { LocationPlanCanvas(width: snapshot.widthMm, depth: snapshot.depthMm, locations: placed, selected: focusID, editing: false, open: { focusID = $0 }, expanded: true) .frame(height: height) .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) .listRowBackground(Color.clear) } } else { ProgressView("正在读取平面图") } } private func row(_ place: Place, offPlan: Bool) -> some View { Button { focusID = place.id } label: { HStack(spacing: 11) { LocationMark(store: store, place: place) VStack(alignment: .leading, spacing: 3) { Text(place.name).foregroundStyle(.primary) if offPlan { Text("未摆放在图中").font(.caption).foregroundStyle(.secondary) } } Spacer() if focusID == place.id { Image(systemName: "checkmark") .foregroundStyle(Color.atlasBlue) .accessibilityHidden(true) } } .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityAddTraits(focusID == place.id ? .isSelected : []) } private func selectionPanel(_ place: Place) -> some View { VStack(alignment: .leading, spacing: 10) { Label(place.path, systemImage: "location.fill") .font(.subheadline.weight(.semibold)) .foregroundStyle(Color.atlasBlue) Button { pick(place.id) } label: { Text("选这个位置").frame(maxWidth: .infinity, minHeight: 30) } .buttonStyle(.borderedProminent) .tint(Color.atlasBlue) if store.locations.contains(where: { $0.parentId == place.id }) { if parent == nil { NavigationLink(value: LocationSelectionDestination.plan(place.id)) { Text("查看室内位置").frame(maxWidth: .infinity, minHeight: 30) } .buttonStyle(.bordered) } else { NavigationLink(value: LocationSelectionDestination.level(place.id)) { Text("查看下级位置").frame(maxWidth: .infinity, minHeight: 30) } .buttonStyle(.bordered) } } } .padding(.horizontal, 16) .padding(.vertical, 10) .background(.regularMaterial) } private func load(refresh: Bool) async { let revision = store.revision error = nil do { try await store.refreshLocations(refresh: refresh) guard !Task.isCancelled, revision == store.revision else { return } if let parent, !store.locations.contains(where: { $0.id == parent.id && $0.parentId == nil }) { snapshot = nil unavailable = true error = "这个位置可能已被移除,请返回后重新选择。" return } let layout = try await store.locationLayout(parent: parent?.id, refresh: refresh) guard !Task.isCancelled, revision == store.revision else { return } snapshot = layout unavailable = false } catch is CancellationError { } catch where !Task.isCancelled && revision == store.revision { if (error as? AtlasError)?.resourceUnavailable == true { snapshot = nil unavailable = true } self.error = error.localizedDescription } catch {} } }