96 lines
4.0 KiB
Swift
96 lines
4.0 KiB
Swift
import SwiftUI
|
|
|
|
struct InventorySymbol: Identifiable {
|
|
let id: String
|
|
let title: String
|
|
let image: String
|
|
|
|
static let locations: [Self] = [
|
|
.init(id: "FOLDER", title: "默认", image: "folder.fill"),
|
|
.init(id: "HOME", title: "房间", image: "house.fill"),
|
|
.init(id: "DESK", title: "书桌", image: "desktopcomputer"),
|
|
.init(id: "CABINET", title: "柜子", image: "cabinet.fill"),
|
|
.init(id: "DRAWER", title: "抽屉", image: "tray.fill"),
|
|
.init(id: "BASKET", title: "收纳篮", image: "basket.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")
|
|
]
|
|
|
|
static func location(_ value: String?) -> Self { resolve(value, in: locations) }
|
|
|
|
static func item(_ value: String?) -> Self { resolve(value, in: items) }
|
|
|
|
private static func resolve(_ value: String?, in symbols: [Self]) -> Self {
|
|
symbols.first { $0.id == value } ?? symbols[0]
|
|
}
|
|
}
|
|
|
|
struct SymbolPicker: View {
|
|
let symbols: [InventorySymbol]
|
|
let color: Color
|
|
@Binding var selection: String
|
|
|
|
var body: some View {
|
|
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), 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.allSatisfy({ $0 >= "0" && $0 <= "9" }),
|
|
fraction.allSatisfy({ $0 >= "0" && $0 <= "9" })
|
|
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"))
|
|
}
|
|
}
|