Cache-first refresh policy: auto-fetch at most twice a day (12h gate), no fetch on launch/foreground/location/fuel-switch, pull-to-refresh forces; geofences still follow location

This commit is contained in:
FuelBoard Contributor
2026-08-11 17:43:11 +01:00
parent 5a08a3a2fa
commit 8230daa249
3 changed files with 46 additions and 7 deletions
+24
View File
@@ -144,6 +144,7 @@ struct FuelStore {
static let favouritesKey = "fuelboard.favourites" // [FuelStation] JSON
static let alertsEnabledKey = "fuelboard.alertsEnabled" // Bool
static let alertsRadiusKey = "fuelboard.alertsRadius" // Double km
static let lastRefreshKey = "fuelboard.lastRefresh" // TimeInterval (seconds since 1970)
// MARK: Stations
@@ -280,6 +281,29 @@ struct FuelStore {
saveString(String(radius), service: alertsRadiusKey)
}
// MARK: Refresh policy data is cached; the app only auto-refreshes
// twice a day (pull-to-refresh is the manual override).
static let refreshInterval: TimeInterval = 12 * 60 * 60
static func loadLastRefresh() -> Date? {
if let raw = loadString(service: lastRefreshKey), let ts = TimeInterval(raw) {
return Date(timeIntervalSince1970: ts)
}
return nil
}
static func saveLastRefresh(_ date: Date = Date()) {
saveString(String(date.timeIntervalSince1970), service: lastRefreshKey)
}
/// True when the cached data is fresh enough that a scheduled auto-refresh
/// should be skipped (twice-a-day policy).
static var isCacheFresh: Bool {
guard let last = loadLastRefresh() else { return false }
return Date().timeIntervalSince(last) < refreshInterval
}
// MARK: Low-level keychain helpers
private static func keychainData(service: String) -> Data? {