import SwiftUI struct InventorySymbol: Identifiable { let id: String let title: String let image: String private static let folder = Self(id: "FOLDER", title: "默认", image: "folder.fill") private static let rooms: [Self] = [ folder, .init(id: "HOME", title: "房间", image: "house.fill"), .init(id: "LIVING_ROOM", title: "客厅", image: "sofa.fill"), .init(id: "BEDROOM", title: "卧室", image: "bed.double.fill"), .init(id: "KITCHEN", title: "厨房", image: "cooktop.fill"), .init(id: "BATHROOM", title: "浴室", image: "bathtub.fill"), .init(id: "STUDY", title: "书房", image: "books.vertical.fill"), .init(id: "BALCONY", title: "阳台", image: "sun.max.fill"), .init(id: "ENTRY", title: "玄关", image: "door.left.hand.open"), .init(id: "GARAGE", title: "车库", image: "car.fill"), .init(id: "LAUNDRY", title: "洗衣房", image: "washer.fill") ] private static let furniture: [Self] = [ folder, .init(id: "CABINET", title: "柜子", image: "cabinet.fill"), .init(id: "WARDROBE", title: "衣柜", image: "hanger"), .init(id: "SHELF", title: "置物架", image: "squareshape.split.3x3"), .init(id: "DESK", title: "书桌", image: "desktopcomputer"), .init(id: "TV_STAND", title: "电视柜", image: "tv.fill"), .init(id: "COUNTER", title: "台面", image: "rectangle.grid.1x2.fill"), .init(id: "BOOKCASE", title: "书柜", image: "books.vertical"), .init(id: "TROLLEY", title: "推车", image: "cart.fill"), .init(id: "FRIDGE", title: "冰箱", image: "refrigerator.fill"), .init(id: "SHOE_CABINET", title: "鞋柜", image: "shoe.fill") ] private static let containers: [Self] = [ folder, .init(id: "DRAWER", title: "抽屉", image: "tray.fill"), .init(id: "BASKET", title: "收纳篮", image: "basket.fill"), .init(id: "TRAY", title: "托盘", image: "tray"), .init(id: "BIN", title: "收纳桶", image: "archivebox"), .init(id: "CASE", title: "收纳箱", image: "case.fill"), .init(id: "BAG", title: "袋子", image: "bag.fill"), .init(id: "BOX", title: "小盒", image: "shippingbox"), .init(id: "DIVIDER", title: "格子", image: "square.grid.2x2.fill"), .init(id: "POUCH", title: "小包", image: "wallet.bifold.fill"), .init(id: "TUBE", title: "筒", image: "cylinder.fill"), .init(id: "JAR", title: "罐子", image: "mug.fill") ] static let items: [Self] = [ .init(id: "BOX", title: "默认", image: "shippingbox.fill"), .init(id: "CABLE", title: "线材", image: "cable.connector"), .init(id: "BOOK", title: "书本", image: "book.closed.fill"), .init(id: "TOOL", title: "工具", image: "wrench.and.screwdriver.fill"), .init(id: "CLOTHING", title: "衣物", image: "tshirt.fill"), .init(id: "KEY", title: "钥匙", image: "key.fill"), .init(id: "HEADPHONES", title: "耳机", image: "headphones"), .init(id: "CAMERA", title: "相机", image: "camera.fill"), .init(id: "COMPUTER", title: "电脑", image: "laptopcomputer"), .init(id: "GAME", title: "游戏", image: "gamecontroller.fill"), .init(id: "KITCHENWARE", title: "餐厨", image: "fork.knife"), .init(id: "SPORT", title: "运动", image: "basketball.fill"), .init(id: "PET", title: "宠物", image: "pawprint.fill"), .init(id: "STATIONERY", title: "文具", image: "pencil"), .init(id: "MEDICINE", title: "药品", image: "pills.fill"), .init(id: "BOTTLE", title: "水杯", image: "waterbottle.fill"), .init(id: "WATCH", title: "手表", image: "watchface.applewatch.case"), .init(id: "UMBRELLA", title: "雨伞", image: "umbrella.fill"), .init(id: "GLASSES", title: "眼镜", image: "eyeglasses"), .init(id: "TOY", title: "玩具", image: "teddybear.fill") ] static func location(_ value: String?) -> Self { (rooms + furniture + containers).first { $0.id == value } ?? folder } static func palette(level: LocationLevel, including current: String?) -> [Self] { let options = switch level { case .room: rooms case .furniture: furniture case .container, .nested: containers } guard let current, !options.contains(where: { $0.id == current }) else { return options } let old = location(current) return old.id == current ? options + [old] : options } static func item(_ value: String?) -> Self { items.first { $0.id == value } ?? items[0] } } enum LocationLevel { case room, furniture, container, nested init(depth: Int) { self = switch depth { case ...1: .room case 2: .furniture case 3: .container default: .nested } } var title: String { switch self { case .room: "一级 · 房间和区域" case .furniture: "二级 · 家具和收纳区" case .container: "三级 · 抽屉和容器" case .nested: "内层容器" } } var tint: Color { switch self { case .room: .indigo case .furniture: .blue case .container: .orange case .nested: .secondary } } } struct LocationMark: View { let store: InventoryStore let place: Place var size: CGFloat = 31 var body: some View { let tint = LocationLevel(depth: place.levelDepth).tint let symbol = InventorySymbol.location(place.displayIcon ?? place.icon).image Group { if let photo = place.iconPhotoId { PhotoImage(store: store, target: .location(place.id), photo: photo, thumbnail: true, placeholderSymbol: symbol, placeholderTint: tint) } else { Image(systemName: symbol) .font(.subheadline.weight(.medium)) .foregroundStyle(tint) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(tint.opacity(0.10)) } } .frame(width: size, height: size) .clipShape(.rect(cornerRadius: 8)) .accessibilityHidden(true) } } struct SymbolPicker: View { let symbols: [InventorySymbol] let color: Color @Binding var selection: String @ScaledMetric(relativeTo: .caption) private var cellWidth = 56.0 var body: some View { LazyVGrid(columns: [GridItem(.adaptive(minimum: cellWidth, maximum: 120), spacing: 8)], spacing: 12) { ForEach(symbols) { symbol in let selected = selection == symbol.id Button { selection = symbol.id } label: { VStack(spacing: 5) { Image(systemName: symbol.image) .font(.title3) .frame(width: 42, height: 38) .foregroundStyle(color) .background(color.opacity(selected ? 0.15 : 0.06), in: .rect(cornerRadius: 10)) .overlay { RoundedRectangle(cornerRadius: 10) .strokeBorder(selected ? color : .clear, lineWidth: 1.5) } .accessibilityHidden(true) Text(symbol.title).font(.caption).foregroundStyle(.primary) } .frame(maxWidth: .infinity) } .buttonStyle(.plain) .accessibilityAddTraits(selected ? .isSelected : []) } } .padding(.vertical, 6) } } enum PurchasePrice { static func cents(from text: String, decimalSeparator: String = Locale.current.decimalSeparator ?? ".") -> Int64? { let normalized = text.trimmingCharacters(in: .whitespacesAndNewlines) .folding(options: .widthInsensitive, locale: .current) .replacingOccurrences(of: decimalSeparator, with: ".") let parts = normalized.split(separator: ".", omittingEmptySubsequences: false) guard let whole = parts.first, parts.count <= 2 else { return nil } let fraction = parts.count == 2 ? parts[1] : "" guard !whole.isEmpty || !fraction.isEmpty, whole.count <= 8, fraction.count <= 2, whole.utf8.allSatisfy({ (48...57).contains($0) }), fraction.utf8.allSatisfy({ (48...57).contains($0) }) else { return nil } let yuan = Int64(whole) ?? 0 let minor = (Int64(fraction) ?? 0) * (fraction.count == 1 ? 10 : 1) return yuan * 100 + minor } static func input(_ cents: Int64) -> String { let fraction = cents % 100 let separator = Locale.current.decimalSeparator ?? "." return "\(cents / 100)\(separator)\(fraction < 10 ? "0" : "")\(fraction)" } static func display(_ cents: Int64) -> String { (Decimal(cents) / 100).formatted(.currency(code: "CNY")) } }