Files
jarvis/Jervis/Store/CacheMaintenance.swift
Kutesir bd632a1592
Some checks are pending
CI / Build · Debug (push) Waiting to run
CI / Build · Release (push) Waiting to run
rename: Jarvis -> Jervis target, scheme, and folder throughout
The bundle-ID and display-name renames weren't enough — the actual
Xcode target/product name was still "Jarvis", which drives CFBundleName,
Xcode's Organizer archive list, the .xcodeproj filename, and the scheme
name. Renamed all the way through:

- project.yml: top-level name, target key, PRODUCT_NAME, source/info paths
- Jarvis/ -> Jervis/ (source folder, git-tracked as renames, no content
  diffs on the moved files)
- .gitignore, CI workflows, README, CONTRIBUTING: Jarvis.xcodeproj /
  scheme Jarvis -> Jervis.xcodeproj / scheme Jervis

Verified: xcodebuild -scheme Jervis succeeds, produces Jervis.app,
CFBundleName/CFBundleExecutable/CFBundleDisplayName all read "Jervis".
CFBundleIdentifier intentionally stays com.kisani.jarvis (per prior
decision — bundle ID isn't user-facing anywhere including Organizer).

Swift type names (JarvisApp, JarvisWordmark, etc.) and the Gitea repo
name/URL are unchanged — pure internal source identifiers, not user or
developer-facing product identity.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-13 03:49:31 +03:00

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) ?? [])
}
}