import Foundation /// Reads and writes WidgetData from the shared App Group UserDefaults container. /// Compiled into both the main app and widget extension targets. final class WidgetDataManager { static let shared = WidgetDataManager() private let suiteName = "group.com.frenocorp.kordant" private let dataKey = "com.frenocorp.kordant.widgetData" private init() {} /// Persist widget data to the shared container. func save(_ data: WidgetData) { guard let defaults = UserDefaults(suiteName: suiteName) else { assertionFailure("WidgetDataManager: Unable to access App Group UserDefaults") return } do { let encoded = try JSONEncoder().encode(data) defaults.set(encoded, forKey: dataKey) } catch { assertionFailure("WidgetDataManager: Failed to encode widget data: \(error)") } } /// Load widget data from the shared container. /// Returns `nil` if no data has been written yet. func load() -> WidgetData? { guard let defaults = UserDefaults(suiteName: suiteName), let data = defaults.data(forKey: dataKey) else { return nil } do { return try JSONDecoder().decode(WidgetData.self, from: data) } catch { assertionFailure("WidgetDataManager: Failed to decode widget data: \(error)") return nil } } /// Remove all stored widget data (e.g. on logout). func clear() { UserDefaults(suiteName: suiteName)?.removeObject(forKey: dataKey) } }