Repository Public
1// SP Wallet iOS2// File: WidgetShared/WidgetSnapshot.swift3// Description: What the app hands to its widgets through the App Group: a small read-only summary with its date. Amounts are4// included only when the owner allowed them in Settings; otherwise the widgets show state without figures.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910struct WidgetSnapshot: Codable, Sendable, Equatable {11 struct Asset: Codable, Sendable, Equatable, Identifiable {12 let symbol: String13 let name: String14 /** Estimated fiat value, already formatted; nil when amounts are not allowed. */15 let value: String?16 let share: Double17 let change24h: Double?18 var id: String { symbol }19 }20 struct Transaction: Codable, Sendable, Equatable, Identifiable {21 let id: String22 /** `send`, `receive`, `swap`, `bridge`… */23 let kind: String24 let symbol: String25 let amount: String?26 let state: String27 let settled: Bool28 let at: Date29 }30 let savedAt: Date31 let signedIn: Bool32 let amountsAllowed: Bool33 /** Estimated total, formatted with its currency; nil when amounts are not allowed or the total is unknown. */34 let total: String?35 /** False when at least one balance or price was missing: the total is then a lower bound and is labelled as such. */36 let totalComplete: Bool37 let change24h: Double?38 let pricesStale: Bool39 let assets: [Asset]40 let transactions: [Transaction]41 let unreadAlerts: Int?42 let criticalAlert: String?43 let halted: Bool?4445 static let fileName = "widget-snapshot.json"46 static let kind = "ai.spboucher.spwallet.ios.widgets"4748 static func url(appGroup: String) -> URL? {49 FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?.appendingPathComponent(fileName)50 }51 static func read(appGroup: String) -> WidgetSnapshot? {52 guard let u = url(appGroup: appGroup), let d = try? Data(contentsOf: u) else { return nil }53 let dec = JSONDecoder(); dec.dateDecodingStrategy = .secondsSince197054 return try? dec.decode(WidgetSnapshot.self, from: d)55 }56 func write(appGroup: String) {57 guard let u = Self.url(appGroup: appGroup) else { return }58 let enc = JSONEncoder(); enc.dateEncodingStrategy = .secondsSince197059 guard let d = try? enc.encode(self) else { return }60 try? d.write(to: u, options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])61 }62 static func erase(appGroup: String) {63 guard let u = url(appGroup: appGroup) else { return }64 try? FileManager.default.removeItem(at: u)65 }66 static var appGroup: String { Bundle.main.infoDictionary?["SPWalletAppGroup"] as? String ?? "group.ai.spboucher.spwallet.ios" }67}68