import SwiftUI struct ItemLocationPlanRoute { struct Layer { let parent: Place? let marker: Place } let layers: [Layer] init?(locationID: UUID, locations: [Place]) { let locationsByID = Dictionary(locations.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first }) var visited: Set = [] var chain: [Place] = [] var current: UUID? = locationID while let id = current { guard visited.insert(id).inserted, let location = locationsByID[id] else { return nil } chain.append(location) current = location.parentId } guard let root = chain.last else { return nil } let overview = Layer(parent: nil, marker: root) // A room plan draws only its immediate children, even when the item is stored deeper. if let inside = chain.dropLast().last { layers = [Layer(parent: root, marker: inside), overview] } else { layers = [overview] } } } struct ItemLocationPlanView: View { let store: InventoryStore private let itemID: UUID @State private var item: Item? @State private var state = ViewState.loading @State private var requestID = UUID() private enum ViewState { case loading case mapped(ItemLocationPlanRoute.Layer, LocationLayoutSnapshot) case unplaced case noLocation case unavailable case failed(String) } init(store: InventoryStore, item: Item) { self.store = store itemID = item.id _item = State(initialValue: item) } var body: some View { GeometryReader { viewport in ScrollView { VStack(alignment: .leading, spacing: 16) { if let item { VStack(alignment: .leading, spacing: 6) { Text(item.name).font(.headline) Label(item.locationLabel, systemImage: "location.fill") .font(.subheadline) .foregroundStyle(Color.atlasBlue) } .frame(maxWidth: .infinity, alignment: .leading) .padding(16) .background(Color(uiColor: .secondarySystemGroupedBackground), in: .rect(cornerRadius: 14)) .accessibilityElement(children: .combine) } switch state { case .loading: ProgressView("正在读取平面图") .frame(maxWidth: .infinity).padding(.vertical, 36) case let .mapped(layer, snapshot): plan(layer, snapshot: snapshot, height: max(280, viewport.size.height * 0.65)) case .unplaced: ContentUnavailableView("还未摆放到平面图", systemImage: "map", description: Text("可以按上方的路径找到这件物品。")) case .noLocation: ContentUnavailableView("物品暂无位置", systemImage: "location.slash", description: Text("返回物品详情可以查看或设置位置。")) case .unavailable: ContentUnavailableView("位置不可用", systemImage: "location.slash", description: Text("物品或位置可能已有变动,请返回详情或下拉刷新。")) case let .failed(message): ContentUnavailableView { Label("平面图加载失败", systemImage: "map") } description: { Text(message) } actions: { Button("重试") { Task { await load(refresh: true) } } } } } .padding(.horizontal, 16) .padding(.vertical, 12) } .refreshable { await load(refresh: true) } } .background(Color(uiColor: .systemGroupedBackground)) .navigationTitle("平面图定位") .navigationBarTitleDisplayMode(.inline) .toolbar(.visible, for: .navigationBar) .task(id: store.revision) { await load() } } private func plan(_ layer: ItemLocationPlanRoute.Layer, snapshot: LocationLayoutSnapshot, height: CGFloat) -> some View { VStack(alignment: .leading, spacing: 8) { Text(layer.parent.map { "\($0.name)平面图" } ?? "空间平面图") .font(.headline) Label("图中标出「\(layer.marker.name)」", systemImage: "mappin.circle.fill") .font(.subheadline) .foregroundStyle(Color.atlasBlue) LocationPlanCanvas(width: snapshot.widthMm, depth: snapshot.depthMm, locations: snapshot.locations.filter { $0.geometry != nil }, selected: layer.marker.id, editing: false, open: nil, expanded: true) .frame(height: height) .id(layer.marker.id) } } private func load(refresh: Bool = false) async { let revision = store.revision let request = UUID() requestID = request @MainActor func checkCurrent() throws { guard request == requestID, revision == store.revision, !Task.isCancelled else { throw CancellationError() } } do { let detail: ItemDetail = try await store.account.read(store.itemPath(itemID), refresh: refresh) try checkCurrent() if item?.locationId != detail.item.locationId || item?.locationPath != detail.item.locationPath { state = .loading } item = detail.item guard let locationID = detail.item.locationId else { state = .noLocation return } try await store.refreshLocations(refresh: refresh) try checkCurrent() guard let route = ItemLocationPlanRoute(locationID: locationID, locations: store.locations) else { state = .unavailable return } for layer in route.layers { let snapshot = try await store.locationLayout(parent: layer.parent?.id, refresh: refresh) try checkCurrent() if snapshot.locations.contains(where: { $0.id == layer.marker.id && $0.geometry != nil }) { state = .mapped(layer, snapshot) return } } state = .unplaced } catch is CancellationError { } catch where request == requestID && revision == store.revision && !Task.isCancelled { if (error as? AtlasError)?.resourceUnavailable == true { item = nil state = .unavailable } else { state = .failed(error.localizedDescription) } } catch {} } }