Root cause: the widget was opening the shared local.store with a 2-entity schema (VocabCard, CourseDeck), causing SwiftData to destructively migrate the file and drop the 4 entities the widget didn't know about (Verb, VerbForm, IrregularSpan, TenseGuide). The main app would then re-seed on next launch, and the cycle repeated forever. Fix: move Verb, VerbForm, IrregularSpan, TenseGuide from the app target into SharedModels so both the main app and the widget use the exact same types from the same module. Both now declare all 6 local entities in their ModelContainer, producing identical schema hashes and eliminating the destructive migration. Other changes bundled in this commit (accumulated during debugging): - Split ModelContainer into localContainer + cloudContainer (no more CloudKit + non-CloudKit configs in one container) - Add SharedStore.localStoreURL() helper and a global reference for bypass-environment fetches - One-time store reset mechanism to wipe stale schema metadata from previous broken iterations - Bootstrap/maintenance split so only seeding gates the UI; dedup and cloud repair run in the background - Sync status toast that shows "Syncing" while background maintenance runs (network-aware, auto-dismisses) - Background app refresh task to keep the widget word-of-day fresh - Speaker icon on VerbDetailView for TTS - Grammar notes navigation fix (nested NavigationStack was breaking detail pane on iPhone) - Word-of-day widget swaps front/back when the deck is reversed so the Spanish word always shows in bold - StoreInspector diagnostic helper for raw SQLite table inspection - Add Conjuga scheme explicitly to project.yml so xcodegen doesn't drop it Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
91 lines
3.2 KiB
Swift
91 lines
3.2 KiB
Swift
import Foundation
|
|
import SharedModels
|
|
import SwiftData
|
|
import WidgetKit
|
|
|
|
/// Writes widget data to shared App Group container.
|
|
@MainActor
|
|
struct WidgetDataService {
|
|
static let suiteName = "group.com.conjuga.app"
|
|
static let dataKey = "widgetData"
|
|
|
|
static func update(localContainer: ModelContainer, cloudContainer: ModelContainer) {
|
|
let localContext = ModelContext(localContainer)
|
|
let cloudContext = ModelContext(cloudContainer)
|
|
update(localContext: localContext, cloudContext: cloudContext)
|
|
}
|
|
|
|
static func update(localContext: ModelContext, cloudContext: ModelContext) {
|
|
guard let shared = UserDefaults(suiteName: suiteName) else { return }
|
|
|
|
let progress = ReviewStore.fetchOrCreateUserProgress(context: cloudContext)
|
|
|
|
let now = Date()
|
|
let dueDescriptor = FetchDescriptor<ReviewCard>(
|
|
predicate: #Predicate<ReviewCard> { $0.dueDate <= now }
|
|
)
|
|
let dueCount = (try? cloudContext.fetchCount(dueDescriptor)) ?? 0
|
|
|
|
var wordOfDay: WordOfDay?
|
|
let wordOffset = shared.integer(forKey: "wordOffset")
|
|
if let card = CourseCardStore.fetchWordOfDayCard(
|
|
for: now,
|
|
wordOffset: wordOffset,
|
|
context: localContext
|
|
) {
|
|
let deckId = card.deckId
|
|
let deckDescriptor = FetchDescriptor<CourseDeck>(
|
|
predicate: #Predicate<CourseDeck> { $0.id == deckId }
|
|
)
|
|
let deck = (try? localContext.fetch(deckDescriptor))?.first
|
|
wordOfDay = WordOfDay(
|
|
spanish: card.front,
|
|
english: card.back,
|
|
weekNumber: deck?.weekNumber ?? 1
|
|
)
|
|
}
|
|
|
|
let testDescriptor = FetchDescriptor<TestResult>(
|
|
sortBy: [SortDescriptor(\TestResult.dateTaken, order: .reverse)]
|
|
)
|
|
let latestTest = (try? cloudContext.fetch(testDescriptor))?.first
|
|
let currentWeek = latestTest?.weekNumber ?? 1
|
|
|
|
let previousData = shared.data(forKey: dataKey)
|
|
.flatMap { try? JSONDecoder().decode(WidgetData.self, from: $0) }
|
|
|
|
var data = WidgetData(
|
|
todayCount: progress.todayCount,
|
|
dailyGoal: progress.dailyGoal,
|
|
currentStreak: progress.currentStreak,
|
|
dueCardCount: dueCount,
|
|
wordOfTheDay: wordOfDay,
|
|
latestTestScore: latestTest?.scorePercent,
|
|
latestTestWeek: latestTest?.weekNumber,
|
|
currentWeek: currentWeek,
|
|
lastUpdated: previousData?.lastUpdated ?? now
|
|
)
|
|
|
|
if previousData == data {
|
|
return
|
|
}
|
|
|
|
data.lastUpdated = now
|
|
|
|
if let encoded = try? JSONEncoder().encode(data) {
|
|
shared.set(encoded, forKey: dataKey)
|
|
WidgetCenter.shared.reloadAllTimelines()
|
|
}
|
|
}
|
|
|
|
/// Read widget data (used by widget extension).
|
|
static func read() -> WidgetData {
|
|
guard let shared = UserDefaults(suiteName: suiteName),
|
|
let data = shared.data(forKey: dataKey),
|
|
let decoded = try? JSONDecoder().decode(WidgetData.self, from: data) else {
|
|
return WidgetData.placeholder
|
|
}
|
|
return decoded
|
|
}
|
|
}
|