Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Models/Formatting.swift3// Description: Display helpers. Crypto amounts are the server's exact decimal strings (grouped for reading, never rounded through floating point); fiat values are estimates and always rendered with the ≈ sign.4import Foundation5import SPWalletCore67enum Fmt {8 /// Groups the integer part of an exact decimal string ("1234567.89" → "1 234 567,89" in fr). No digit is added or removed.9 static func amount(_ exact: String?, maxFraction: Int? = nil) -> String {10 guard let exact, !exact.isEmpty else { return "—" }11 let neg = exact.hasPrefix("-"); let body = neg ? String(exact.dropFirst()) : exact12 let parts = body.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: false)13 var intPart = String(parts[0]); var frac = parts.count > 1 ? String(parts[1]) : ""14 var truncated = false15 if let m = maxFraction, frac.count > m { frac = String(frac.prefix(m)); truncated = true }16 var grouped = ""; for (i, ch) in intPart.reversed().enumerated() { if i > 0, i % 3 == 0 { grouped.append("\u{202F}") }; grouped.append(ch) }17 intPart = String(grouped.reversed())18 let sep = Locale.current.decimalSeparator ?? ","19 return (neg ? "−" : "") + intPart + (frac.isEmpty ? "" : sep + frac) + (truncated ? "…" : "")20 }2122 static func fiat(_ v: Double?, _ c: FiatCurrency, estimate: Bool = true) -> String {23 guard let v else { return "—" }24 let s = v.formatted(.currency(code: c.rawValue).precision(.fractionLength(abs(v) < 1 && v != 0 ? 4 : 2)))25 return estimate ? "≈ " + s : s26 }27 static func price(_ v: Double?, _ c: FiatCurrency) -> String {28 guard let v else { return "—" }29 let digits = v >= 100 ? 2 : v >= 1 ? 4 : 630 return v.formatted(.currency(code: c.rawValue).precision(.fractionLength(2...digits)))31 }32 static func percent(_ v: Double?) -> String {33 guard let v else { return "—" }34 return (v >= 0 ? "+" : "−") + abs(v).formatted(.number.precision(.fractionLength(2))) + " %"35 }36 static func compact(_ v: Double?, _ c: FiatCurrency) -> String {37 guard let v else { return "—" }38 return v.formatted(.currency(code: c.rawValue).notation(.compactName).precision(.fractionLength(0...2)))39 }40 static func date(_ d: Date?) -> String { d.map { $0.formatted(date: .abbreviated, time: .shortened) } ?? "—" }41 static func dateSeconds(_ d: Date?) -> String { d.map { $0.formatted(date: .numeric, time: .standard) } ?? "—" }42 static func relative(_ d: Date?) -> String { d.map { $0.formatted(.relative(presentation: .named)) } ?? "—" }4344 /// "0x1234…abcd" — for lists only. Review screens always show the full address.45 static func short(_ address: String?, head: Int = 8, tail: Int = 6) -> String {46 guard let a = address else { return "—" }47 return a.count <= head + tail + 1 ? a : "\(a.prefix(head))…\(a.suffix(tail))"48 }49 /// A full address in groups of four characters, for character-by-character verification.50 static func chunked(_ address: String) -> String {51 var out = ""; for (i, ch) in address.enumerated() { if i > 0, i % 4 == 0 { out.append(" ") }; out.append(ch) }52 return out53 }54}5556extension JSONValue {57 /// A compact human rendering of unknown server data (policy values, audit details): shown verbatim, never interpreted.58 var displayText: String {59 switch self {60 case .string(let s): return s61 case .null: return "—"62 default:63 guard let d = try? APICoding.encoder().encode(self), let s = String(data: d, encoding: .utf8) else { return "?" }64 return s65 }66 }67 func str(_ k: String) -> String? { self[k]?.stringValue }68}69