Files
rapira-market-widget/crypto/MarketSnapshotCache.swift
2026-08-12 07:06:01 +07:00

47 lines
1.6 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
import os
/// Последний удачный ответ API.
struct MarketSnapshot: Codable, Sendable {
let date: Date
let thumbs: [SymbolThumb]
let histories: [String: [Double]]
}
/// Дисковый кэш снимка рынка в контейнере App Group.
///
/// Офлайн-фолбэк: без сети виджет показывает последние реальные котировки
/// с честной меткой времени, а не фиктивные данные.
struct MarketSnapshotCache: Sendable {
private let fileURL: URL?
private static let logger = Logger(subsystem: AppGroup.id, category: "snapshot-cache")
init() {
fileURL = AppGroup.cachesURL?.appendingPathComponent("market-snapshot.json")
}
func load() -> MarketSnapshot? {
guard let fileURL, let data = try? Data(contentsOf: fileURL) else { return nil }
do {
return try JSONDecoder().decode(MarketSnapshot.self, from: data)
} catch {
Self.logger.error("Кэш повреждён: \(String(describing: error), privacy: .public)")
return nil
}
}
func save(_ snapshot: MarketSnapshot) {
guard let fileURL else { return }
do {
try FileManager.default.createDirectory(
at: fileURL.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try JSONEncoder().encode(snapshot).write(to: fileURL, options: .atomic)
} catch {
Self.logger.error("Кэш не сохранён: \(String(describing: error), privacy: .public)")
}
}
}