Harden watch sync and App Group storage

This commit is contained in:
FuelBoard Contributor
2026-09-26 20:03:29 +01:00
parent 004fe2f787
commit c3a975eb10
5 changed files with 95 additions and 16 deletions
+13 -4
View File
@@ -37,10 +37,11 @@ struct MirrorFuelProvider: FuelPriceProviding {
/// surfaced via `LiveChainProvider.latestMeta` in Settings → About.
static var latestMeta: RelayMeta?
/// App-group cache of the last fetched FULL dump, keyed by snapshot day.
/// App-group file cache of the last fetched FULL dump, keyed by snapshot day.
/// The mirror pushes once/day, so between pushes the app reuses the
/// cached dump instead of re-downloading ~2.8 MB at every 12 h gate.
static let liveDumpCacheKey = "fuelboard.liveDumpCache"
static let liveDumpCacheFileName = "fuelboard.liveDumpCache.json"
struct DumpCache: Codable {
let day: String
@@ -48,18 +49,26 @@ struct MirrorFuelProvider: FuelPriceProviding {
}
static func loadDumpCache() -> DumpCache? {
if let raw = FuelStore.loadAppGroupFile(liveDumpCacheFileName),
let cache = try? JSONDecoder().decode(DumpCache.self, from: raw) {
return cache
}
// Migrate the legacy UserDefaults cache when present.
guard let defaults = UserDefaults(suiteName: FuelStore.appGroupSuite),
let raw = defaults.data(forKey: liveDumpCacheKey),
let cache = try? JSONDecoder().decode(DumpCache.self, from: raw) else {
return nil
}
if FuelStore.saveAppGroupFile(raw, name: liveDumpCacheFileName) {
defaults.removeObject(forKey: liveDumpCacheKey)
}
return cache
}
static func saveDumpCache(day: String, data: Data) {
guard let defaults = UserDefaults(suiteName: FuelStore.appGroupSuite),
let raw = try? JSONEncoder().encode(DumpCache(day: day, data: data)) else { return }
defaults.set(raw, forKey: liveDumpCacheKey)
guard let raw = try? JSONEncoder().encode(DumpCache(day: day, data: data)) else { return }
_ = FuelStore.saveAppGroupFile(raw, name: liveDumpCacheFileName)
}
/// True when the cached dump is already the freshest the mirror has —