diff --git a/ios/App/App/CoachWidgetBridge.swift b/ios/App/App/CoachWidgetBridge.swift index e189f41..3af4b1a 100644 --- a/ios/App/App/CoachWidgetBridge.swift +++ b/ios/App/App/CoachWidgetBridge.swift @@ -24,6 +24,7 @@ public class CoachWidgetBridgePlugin: CAPPlugin, CAPBridgedPlugin { public let pluginMethods: [CAPPluginMethod] = [ CAPPluginMethod(name: "setSnapshot", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "clear", returnType: CAPPluginReturnPromise), + CAPPluginMethod(name: "getSnapshot", returnType: CAPPluginReturnPromise), ] @objc func setSnapshot(_ call: CAPPluginCall) { @@ -118,6 +119,31 @@ public class CoachWidgetBridgePlugin: CAPPlugin, CAPBridgedPlugin { try? session.updateApplicationContext(["widgetSnapshot": data]) } + /// Relit le snapshot RÉELLEMENT stocké dans l'App Group. + /// + /// Sans cette lecture, un widget figé laisse trois hypothèses ouvertes + /// (rien d'écrit, écrit sans le champ, écrit mais pas réaffiché) et aucun + /// moyen de trancher depuis le web : l'App Group est invisible du JS. + @objc func getSnapshot(_ call: CAPPluginCall) { + guard let s = CoachWidgetStore.load() else { + call.resolve(["stored": false]) + return + } + var out: [String: Any] = [ + "stored": true, + "updatedAt": ISO8601DateFormatter().string(from: s.updatedAt), + "hasToday": s.today != nil, + "hasDoneToday": s.doneToday != nil, + ] + if let d = s.doneToday { + out["doneToday"] = ["sport": d.sport, + "title": d.title ?? "", + "subtitle": d.subtitle ?? ""] + } + if let r = s.routine { out["routine"] = ["done": r.done, "total": r.total] } + call.resolve(out) + } + @objc func clear(_ call: CAPPluginCall) { CoachWidgetStore.clear() if #available(iOS 14.0, *) { WidgetCenter.shared.reloadAllTimelines() }