Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Persistence/ValueHistory.swift3// Description: The portfolio's estimated total as THIS Mac observed it over time (the server keeps no such series). Each point is an estimate (balances × displayed prices) taken when the app was open; gaps are real gaps. Stored in the sandbox container, excluded from backups.4import Foundation5import SPWalletCore67struct ValuePoint: Codable, Sendable, Identifiable { let at: Date; let value: Double; let currency: String; let complete: Bool; var id: Date { at } }89actor ValueHistory {10 static let shared = ValueHistory()11 private var points: [ValuePoint] = []12 private var loaded = false13 private var url: URL { AppEnvironment.supportDirectory.appendingPathComponent("snapshots/value-history.json") }1415 private func load() {16 guard !loaded else { return }; loaded = true17 if let d = try? Data(contentsOf: url), let p = try? APICoding.decoder().decode([ValuePoint].self, from: d) { points = p }18 }19 /// One point per 10 minutes at most; two years kept.20 func record(_ p: Portfolio, currency: FiatCurrency) {21 load()22 guard p.prices != nil else { return }23 let t = p.estimatedTotal(in: currency)24 if let last = points.last, last.currency == currency.rawValue, p.asOf.timeIntervalSince(last.at) < 600 { return }25 points.append(ValuePoint(at: p.asOf, value: t.value, currency: currency.rawValue, complete: t.complete))26 let cutoff = Date().addingTimeInterval(-730 * 86400); points.removeAll { $0.at < cutoff }27 try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)28 if let d = try? APICoding.encoder().encode(points) { try? d.write(to: url, options: [.atomic, .completeFileProtection]) }29 }30 func series(currency: FiatCurrency, since: Date) -> [ValuePoint] { load(); return points.filter { $0.currency == currency.rawValue && $0.at >= since } }31}32