import Foundation import SwiftData import WidgetKit enum WidgetSync { /// Writes the current habits to the shared store and reloads widget timelines. static func refresh(_ context: ModelContext) { let descriptor = FetchDescriptor(sortBy: [SortDescriptor(\.createdAt)]) let habits = (try? context.fetch(descriptor)) ?? [] let items = habits.map { habit in WidgetSnapshot.Item( id: habit.uuid, name: habit.name, symbolName: habit.symbolName, colorHex: habit.colorHex, isDoneToday: habit.isCompletedToday ) } SharedStore.save(WidgetSnapshot(generatedAt: .now, items: items)) WidgetCenter.shared.reloadAllTimelines() } }