- SnapshotModels.swift: CachedQuote (slug-unique, themes/regions
als CSV), SnapshotMeta (singleton mit lastSyncedAt + totalCount),
SnapshotContainer.make() mit App-Group-Store-URL (Fallback auf
App-Container für Dev ohne Apple-Dev-Portal-Setup)
- SnapshotSync (actor) mit injectable Loader für Tests: refresh /
refreshIfStale / tryRefresh (fail-soft). Re-konsolidiert beim Pull
(Update + Insert + Delete entzogene Slugs). 24h-Staleness-Default.
- DailyQuoteWidget: Hash-of-Day-Picker aus SwiftData, drei Sizes,
Mitternacht-Refresh-Policy, Placeholder bei leerem Store. Widget-
Target zieht SnapshotModels.swift mit (project.yml).
- ZitareNativeApp triggert SnapshotSync.tryRefresh() bei Launch +
WidgetCenter.reloadAllTimelines() danach.
- AppConfig.snapshotURL = webBaseURL/index-min.json (Web-Endpoint
noch nicht live, fail-soft).
- DeepLinkRouter Substring-Guard fix (`/t` statt `/t/` im
Prefix-Array, sonst greift hasPrefix("/t//") nicht).
- 22 Tests grün (6 AppConfig + 11 DeepLinkRouter + 3 SnapshotSync +
1 UI + 1 Widget-Compile-Smoke), swiftlint 0 violations in 22 Files
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
112 lines
3.9 KiB
Swift
112 lines
3.9 KiB
Swift
import Foundation
|
|
import SwiftData
|
|
|
|
/// SwiftData-Model für ein Quote aus dem `index-min.json`-Snapshot.
|
|
/// Lebt in einem App-Group-`ModelContainer`, damit Widget +
|
|
/// ShareExtension lesend zugreifen können.
|
|
@Model
|
|
final class CachedQuote {
|
|
/// Stabiler Slug, dient als Primary-Key (eindeutig via Unique-Index).
|
|
@Attribute(.unique) var slug: String
|
|
var text: String
|
|
var authorSlug: String?
|
|
var authorName: String?
|
|
var language: String?
|
|
/// Komma-getrennte Slug-Liste (SwiftData mag arrays of String mäßig).
|
|
var themesCSV: String
|
|
var regionsCSV: String
|
|
/// Wann zuletzt aus dem Snapshot importiert.
|
|
var importedAt: Date
|
|
|
|
init(
|
|
slug: String,
|
|
text: String,
|
|
authorSlug: String?,
|
|
authorName: String?,
|
|
language: String?,
|
|
themes: [String],
|
|
regions: [String],
|
|
importedAt: Date = Date()
|
|
) {
|
|
self.slug = slug
|
|
self.text = text
|
|
self.authorSlug = authorSlug
|
|
self.authorName = authorName
|
|
self.language = language
|
|
themesCSV = themes.joined(separator: ",")
|
|
regionsCSV = regions.joined(separator: ",")
|
|
self.importedAt = importedAt
|
|
}
|
|
|
|
var themes: [String] {
|
|
themesCSV.isEmpty ? [] : themesCSV.split(separator: ",").map(String.init)
|
|
}
|
|
|
|
var regions: [String] {
|
|
regionsCSV.isEmpty ? [] : regionsCSV.split(separator: ",").map(String.init)
|
|
}
|
|
}
|
|
|
|
/// SwiftData-Marker für „wann zuletzt erfolgreich gesynct" + Total-
|
|
/// Count. Einzeiliger Singleton — ein einziges Objekt im Container.
|
|
@Model
|
|
final class SnapshotMeta {
|
|
@Attribute(.unique) var id: String
|
|
var generatedAt: Date?
|
|
var lastSyncedAt: Date?
|
|
var totalCount: Int
|
|
|
|
init(
|
|
id: String = "default",
|
|
generatedAt: Date? = nil,
|
|
lastSyncedAt: Date? = nil,
|
|
totalCount: Int = 0
|
|
) {
|
|
self.id = id
|
|
self.generatedAt = generatedAt
|
|
self.lastSyncedAt = lastSyncedAt
|
|
self.totalCount = totalCount
|
|
}
|
|
}
|
|
|
|
/// Schema-Helper für ModelContainer-Setup. App + Widget + ShareExt
|
|
/// rufen `SnapshotContainer.make()` auf und teilen so denselben
|
|
/// SwiftData-Store unter der App-Group.
|
|
///
|
|
/// Der App-Group-Identifier ist hier hartkodiert, damit das File ohne
|
|
/// AppConfig-Dependency auch von der Widget-Extension konsumierbar
|
|
/// ist (Widget-Target kompiliert nur Source-File-Whitelist aus
|
|
/// `project.yml`).
|
|
enum SnapshotContainer {
|
|
static let appGroup = "group.ev.mana.zitare"
|
|
|
|
/// Default-URL für den Store: in der App-Group, damit alle drei
|
|
/// Extensions ihn sehen. Fällt zurück auf den App-Container, wenn
|
|
/// die App-Group (noch) nicht aktiviert ist — siehe Apple-Dev-
|
|
/// Portal-Blocker in `PLAN.md`.
|
|
static func defaultStoreURL(appGroup: String = appGroup) -> URL {
|
|
let fm = FileManager.default
|
|
if let groupURL = fm.containerURL(forSecurityApplicationGroupIdentifier: appGroup) {
|
|
return groupURL.appendingPathComponent("snapshot.store")
|
|
}
|
|
// Fallback: App-eigener Documents-Container (Widget sieht das
|
|
// dann nicht — wird in Release mit funktionierender App-Group
|
|
// automatisch übersprungen).
|
|
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first
|
|
?? URL(fileURLWithPath: NSTemporaryDirectory())
|
|
return docs.appendingPathComponent("snapshot.store")
|
|
}
|
|
|
|
/// Baut einen `ModelContainer` für die `CachedQuote` + `SnapshotMeta`-
|
|
/// Models. `inMemory: true` für Unit-Tests.
|
|
static func make(inMemory: Bool = false) throws -> ModelContainer {
|
|
let schema = Schema([CachedQuote.self, SnapshotMeta.self])
|
|
let storeURL = defaultStoreURL()
|
|
let config = if inMemory {
|
|
ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
|
|
} else {
|
|
ModelConfiguration("snapshot", schema: schema, url: storeURL)
|
|
}
|
|
return try ModelContainer(for: schema, configurations: [config])
|
|
}
|
|
}
|