- APIClient: generate and persist a stable device UUID (jarvis.device.id in UserDefaults); attach as X-Jarvis-Device-Id header on every request - Add markStoryRead / markStoryUnread — POST/DELETE /stories/:id/read with device_id, read_at, and source fields - Wire both calls into all three read-toggle sites: SignalFeedView, ArticleReaderView, StoryDetailView - Add postVoid helper for fire-and-forget POST calls (no response body needed) - Pass include_read param on stories fetch for future server-side filtering - CacheMaintenance: recentHours 84→72 (mirrors backend 72h story window); markerHours 96→192 so read suppression survives a full weekly news cycle Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
61 lines
2.7 KiB
Swift
61 lines
2.7 KiB
Swift
// CacheMaintenance.swift
|
|
// Jarvis — bounds the on-device SwiftData caches so they don't grow forever.
|
|
// Saved (bookmarked) stories are always protected.
|
|
|
|
import Foundation
|
|
import SwiftData
|
|
|
|
@MainActor
|
|
enum CacheMaintenance {
|
|
private static let hour: TimeInterval = 3_600
|
|
private static let day: TimeInterval = 86_400
|
|
|
|
private static let recentHours = 72.0 // 3 days: mirrors backend active story window
|
|
private static let markerHours = 192.0 // 8 days: read suppression survives weekly repeats
|
|
private static let articleDays = 14.0 // cached article bodies (backend article retention)
|
|
|
|
/// Age-based eviction. Run on launch. Cheap; protects saved stories.
|
|
static func prune(_ context: ModelContext) {
|
|
let now = Date()
|
|
let seenCutoff = now.addingTimeInterval(-markerHours * hour)
|
|
let readCutoff = now.addingTimeInterval(-markerHours * hour)
|
|
let articleCutoff = now.addingTimeInterval(-articleDays * day)
|
|
let storyCutoff = now.addingTimeInterval(-recentHours * hour)
|
|
|
|
// Markers: simple age batch deletes.
|
|
try? context.delete(model: SeenStory.self, where: #Predicate { $0.seenAt < seenCutoff })
|
|
try? context.delete(model: ReadStory.self, where: #Predicate { $0.readAt < readCutoff })
|
|
|
|
// Offline content: drop old entries that aren't saved.
|
|
let saved = savedIDs(context)
|
|
if let arts = try? context.fetch(
|
|
FetchDescriptor<CachedArticle>(predicate: #Predicate { $0.cachedAt < articleCutoff })) {
|
|
for a in arts where !saved.contains(a.storyId) { context.delete(a) }
|
|
}
|
|
if let stories = try? context.fetch(
|
|
FetchDescriptor<CachedStory>(predicate: #Predicate { $0.cachedAt < storyCutoff })) {
|
|
for s in stories where !saved.contains(s.id) { context.delete(s) }
|
|
}
|
|
try? context.save()
|
|
}
|
|
|
|
/// Manual "Clear cache" — wipes offline stories/articles + seen/read state,
|
|
/// keeps your saved bookmarks (and their articles).
|
|
static func clear(_ context: ModelContext) {
|
|
try? context.delete(model: SeenStory.self)
|
|
try? context.delete(model: ReadStory.self)
|
|
let saved = savedIDs(context)
|
|
if let arts = try? context.fetch(FetchDescriptor<CachedArticle>()) {
|
|
for a in arts where !saved.contains(a.storyId) { context.delete(a) }
|
|
}
|
|
if let stories = try? context.fetch(FetchDescriptor<CachedStory>()) {
|
|
for s in stories where !saved.contains(s.id) { context.delete(s) }
|
|
}
|
|
try? context.save()
|
|
}
|
|
|
|
private static func savedIDs(_ context: ModelContext) -> Set<String> {
|
|
Set((try? context.fetch(FetchDescriptor<SavedStory>()))?.map(\.id) ?? [])
|
|
}
|
|
}
|