@capgo/capacitor-widget-kit 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CapgoCapacitorWidgetKit.podspec +17 -0
  2. package/LICENSE +373 -0
  3. package/Package.swift +28 -0
  4. package/README.md +1119 -0
  5. package/android/build.gradle +59 -0
  6. package/android/src/main/AndroidManifest.xml +8 -0
  7. package/android/src/main/java/app/capgo/widgetkit/CapgoNativeWidgetBridge.java +311 -0
  8. package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateActionReceiver.java +36 -0
  9. package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateSvgBitmapRenderer.java +25 -0
  10. package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateWidgetBridge.java +31 -0
  11. package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKit.java +218 -0
  12. package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitConstants.java +18 -0
  13. package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitPlugin.java +370 -0
  14. package/android/src/main/java/app/capgo/widgetkit/TemplateActivityStore.java +167 -0
  15. package/android/src/main/java/app/capgo/widgetkit/TemplateJsonUtils.java +369 -0
  16. package/android/src/main/java/app/capgo/widgetkit/TemplateRuntime.java +676 -0
  17. package/android/src/main/res/.gitkeep +0 -0
  18. package/dist/docs.json +2223 -0
  19. package/dist/esm/definitions.d.ts +1117 -0
  20. package/dist/esm/definitions.js +2 -0
  21. package/dist/esm/definitions.js.map +1 -0
  22. package/dist/esm/helpers/workout.d.ts +32 -0
  23. package/dist/esm/helpers/workout.js +129 -0
  24. package/dist/esm/helpers/workout.js.map +1 -0
  25. package/dist/esm/index.d.ts +6 -0
  26. package/dist/esm/index.js +9 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/runtime.d.ts +25 -0
  29. package/dist/esm/runtime.js +653 -0
  30. package/dist/esm/runtime.js.map +1 -0
  31. package/dist/esm/web.d.ts +23 -0
  32. package/dist/esm/web.js +292 -0
  33. package/dist/esm/web.js.map +1 -0
  34. package/dist/plugin.cjs.js +1096 -0
  35. package/dist/plugin.cjs.js.map +1 -0
  36. package/dist/plugin.js +1099 -0
  37. package/dist/plugin.js.map +1 -0
  38. package/ios/Sources/CapgoWidgetKitPlugin/CapgoNativeWidgetBridge.swift +372 -0
  39. package/ios/Sources/CapgoWidgetKitPlugin/CapgoTemplateWidgetBridge.swift +199 -0
  40. package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKit.swift +218 -0
  41. package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitPlugin.swift +322 -0
  42. package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitSharedStore.swift +47 -0
  43. package/ios/Sources/CapgoWidgetKitPlugin/CapgoWorkoutLiveActivityComponents.swift +246 -0
  44. package/ios/Sources/CapgoWidgetKitPlugin/CapgoWorkoutLiveActivityWidget.swift +257 -0
  45. package/ios/Sources/CapgoWidgetKitPlugin/CompleteSetIntent.swift +24 -0
  46. package/ios/Sources/CapgoWidgetKitPlugin/TemplateActionIntent.swift +56 -0
  47. package/ios/Sources/CapgoWidgetKitPlugin/TemplateActivityStore.swift +239 -0
  48. package/ios/Sources/CapgoWidgetKitPlugin/TemplateLiveActivityManager.swift +251 -0
  49. package/ios/Sources/CapgoWidgetKitPlugin/TemplateRuntime.swift +1067 -0
  50. package/ios/Sources/CapgoWidgetKitPlugin/WorkoutActivityStore.swift +111 -0
  51. package/ios/Sources/CapgoWidgetKitPlugin/WorkoutLiveActivityManager.swift +263 -0
  52. package/ios/Sources/CapgoWidgetKitPlugin/WorkoutSession.swift +271 -0
  53. package/ios/Tests/CapgoWidgetKitPluginTests/CapgoWidgetKitPluginTests.swift +130 -0
  54. package/package.json +93 -0
@@ -0,0 +1,251 @@
1
+ #if canImport(ActivityKit)
2
+ import ActivityKit
3
+ #endif
4
+ import Foundation
5
+
6
+ public struct TemplateActivityInfo: Hashable, Sendable {
7
+ public let activityId: String
8
+ public let templateId: String
9
+ public let status: String
10
+ public let updatedAtMs: Int64
11
+ }
12
+
13
+ #if canImport(ActivityKit)
14
+ @available(iOS 16.2, *)
15
+ public struct CapgoTemplateActivityAttributes: ActivityAttributes, Sendable {
16
+ public struct ContentState: Codable, Hashable, Sendable {
17
+ public var revision: Int
18
+ public var status: String
19
+ public var updatedAtMs: Int64
20
+ }
21
+
22
+ public var activityId: String
23
+ public var templateId: String
24
+ public var openUrl: String?
25
+
26
+ public init(activityId: String, templateId: String, openUrl: String?) {
27
+ self.activityId = activityId
28
+ self.templateId = templateId
29
+ self.openUrl = openUrl
30
+ }
31
+ }
32
+ #endif
33
+
34
+ public final class TemplateLiveActivityManager {
35
+ public static let shared = TemplateLiveActivityManager()
36
+
37
+ private init() {}
38
+
39
+ public func areActivitiesSupported() -> (Bool, String?) {
40
+ #if canImport(ActivityKit)
41
+ if #available(iOS 16.2, *) {
42
+ let authorizationInfo = ActivityAuthorizationInfo()
43
+ guard authorizationInfo.areActivitiesEnabled else {
44
+ return (false, "Live Activities are disabled in Settings.")
45
+ }
46
+ return (true, nil)
47
+ }
48
+ return (false, "iOS 16.2 or later is required for the native template activity bridge.")
49
+ #else
50
+ return (false, "ActivityKit is unavailable in this build configuration.")
51
+ #endif
52
+ }
53
+
54
+ public func start(
55
+ activityId requestedActivityId: String?,
56
+ definitionObject: [String: Any],
57
+ stateObject: [String: Any],
58
+ openUrl: String?
59
+ ) async throws -> StoredTemplateActivityEnvelope {
60
+ let nowMs = Int64(Date().timeIntervalSince1970 * 1000)
61
+ let activityId: String
62
+ if let requestedActivityId, !requestedActivityId.isEmpty {
63
+ activityId = requestedActivityId
64
+ } else {
65
+ activityId = TemplateRuntime.createIdentifier(prefix: "activity")
66
+ }
67
+ var record = TemplateRuntime.makeRecord(
68
+ activityId: activityId,
69
+ definitionObject: definitionObject,
70
+ stateObject: stateObject,
71
+ openUrl: openUrl,
72
+ nowMs: nowMs
73
+ )
74
+ let store = try TemplateActivityStore.make()
75
+
76
+ #if canImport(ActivityKit)
77
+ if #available(iOS 16.2, *) {
78
+ let activity = try Activity.request(
79
+ attributes: CapgoTemplateActivityAttributes(
80
+ activityId: activityId,
81
+ templateId: record.templateId,
82
+ openUrl: openUrl
83
+ ),
84
+ content: ActivityContent(state: contentState(from: record), staleDate: nil),
85
+ pushType: nil
86
+ )
87
+ record.nativeActivityId = activity.id
88
+ store.setNativeActivityId(activity.id, for: activityId)
89
+ }
90
+ #endif
91
+
92
+ let envelope = try record.toEnvelope()
93
+ try store.saveEnvelope(envelope)
94
+ return envelope
95
+ }
96
+
97
+ public func update(
98
+ activityId: String,
99
+ definitionObject: [String: Any]?,
100
+ stateObject: [String: Any]?,
101
+ openUrl: String?
102
+ ) async throws -> StoredTemplateActivityEnvelope? {
103
+ let store = try TemplateActivityStore.make()
104
+ guard var record = try store.loadEnvelope(activityId: activityId).map(TemplateRuntimeRecord.init(envelope:)) else {
105
+ return nil
106
+ }
107
+
108
+ let nowMs = Int64(Date().timeIntervalSince1970 * 1000)
109
+ if let definitionObject {
110
+ record.definition = definitionObject
111
+ record.templateId = (definitionObject["id"] as? String) ?? record.templateId
112
+ }
113
+ if let stateObject {
114
+ record.state = stateObject
115
+ }
116
+ if let openUrl {
117
+ record.openUrl = openUrl
118
+ }
119
+ record.updatedAtMs = nowMs
120
+ record.revision += 1
121
+ record.timers = TemplateRuntime.reconcileTimers(for: record, nowMs: nowMs)
122
+
123
+ let envelope = try record.toEnvelope()
124
+ try store.saveEnvelope(envelope)
125
+ try await updateNativeActivity(for: envelope)
126
+ return envelope
127
+ }
128
+
129
+ public func end(activityId: String, finalStateObject: [String: Any]?) async throws {
130
+ let store = try TemplateActivityStore.make()
131
+ guard var record = try store.loadEnvelope(activityId: activityId).map(TemplateRuntimeRecord.init(envelope:)) else {
132
+ return
133
+ }
134
+
135
+ let nowMs = Int64(Date().timeIntervalSince1970 * 1000)
136
+ if let finalStateObject {
137
+ record.state = finalStateObject
138
+ }
139
+ record.status = "ended"
140
+ record.updatedAtMs = nowMs
141
+ record.revision += 1
142
+ record.timers = TemplateRuntime.reconcileTimers(for: record, nowMs: nowMs)
143
+ let envelope = try record.toEnvelope()
144
+ try store.saveEnvelope(envelope)
145
+
146
+ #if canImport(ActivityKit)
147
+ if #available(iOS 16.2, *), let nativeActivity = nativeActivity(for: activityId, store: store) {
148
+ await nativeActivity.end(ActivityContent(state: contentState(from: record), staleDate: nil), dismissalPolicy: .immediate)
149
+ }
150
+ #endif
151
+
152
+ store.removeNativeActivityId(for: activityId)
153
+ }
154
+
155
+ public func performAction(
156
+ activityId: String,
157
+ actionId: String,
158
+ sourceId: String?,
159
+ payloadObject: [String: Any]?
160
+ ) async throws -> (StoredTemplateActivityEnvelope, StoredTemplateActionEvent) {
161
+ let store = try TemplateActivityStore.make()
162
+ guard let currentEnvelope = store.loadEnvelope(activityId: activityId) else {
163
+ throw CapgoWidgetKitBridgeError.invalidObject("No stored template activity found for \(activityId).")
164
+ }
165
+
166
+ let record = try TemplateRuntimeRecord(envelope: currentEnvelope)
167
+ let nowMs = Int64(Date().timeIntervalSince1970 * 1000)
168
+ let (nextRecord, event) = try TemplateRuntime.applyAction(
169
+ actionId: actionId,
170
+ sourceId: sourceId,
171
+ payloadObject: payloadObject,
172
+ record: record,
173
+ nowMs: nowMs
174
+ )
175
+
176
+ let envelope = try nextRecord.toEnvelope()
177
+ try store.saveEnvelope(envelope)
178
+ try store.appendEvent(event)
179
+ try await updateNativeActivity(for: envelope)
180
+ return (envelope, event)
181
+ }
182
+
183
+ public func activity(activityId: String) throws -> StoredTemplateActivityEnvelope? {
184
+ try TemplateActivityStore.make().loadEnvelope(activityId: activityId)
185
+ }
186
+
187
+ public func listActivities() throws -> [TemplateActivityInfo] {
188
+ try TemplateActivityStore.make().listEnvelopes().map { envelope in
189
+ TemplateActivityInfo(
190
+ activityId: envelope.activityId,
191
+ templateId: envelope.templateId,
192
+ status: envelope.status,
193
+ updatedAtMs: envelope.updatedAtMs
194
+ )
195
+ }
196
+ }
197
+
198
+ public func listEvents(activityId: String?, unacknowledgedOnly: Bool) throws -> [StoredTemplateActionEvent] {
199
+ try TemplateActivityStore.make()
200
+ .loadEvents(activityId: activityId)
201
+ .filter { !unacknowledgedOnly || $0.acknowledgedAtMs == nil }
202
+ .sorted { $0.createdAtMs > $1.createdAtMs }
203
+ }
204
+
205
+ public func acknowledgeEvents(activityId: String?, eventIds: [String]?) throws {
206
+ try TemplateActivityStore.make()
207
+ .acknowledgeEvents(
208
+ eventIds: eventIds,
209
+ activityId: activityId,
210
+ acknowledgedAtMs: Int64(Date().timeIntervalSince1970 * 1000)
211
+ )
212
+ }
213
+
214
+ #if canImport(ActivityKit)
215
+ @available(iOS 16.2, *)
216
+ private func contentState(from record: TemplateRuntimeRecord) -> CapgoTemplateActivityAttributes.ContentState {
217
+ CapgoTemplateActivityAttributes.ContentState(
218
+ revision: record.revision,
219
+ status: record.status,
220
+ updatedAtMs: record.updatedAtMs
221
+ )
222
+ }
223
+
224
+ @available(iOS 16.2, *)
225
+ private func nativeActivity(for activityId: String, store: TemplateActivityStore) -> Activity<CapgoTemplateActivityAttributes>? {
226
+ guard let nativeActivityId = store.nativeActivityId(for: activityId) else {
227
+ return nil
228
+ }
229
+ return Activity<CapgoTemplateActivityAttributes>.activities.first(where: { $0.id == nativeActivityId })
230
+ }
231
+
232
+ private func updateNativeActivity(for envelope: StoredTemplateActivityEnvelope) async throws {
233
+ guard #available(iOS 16.2, *) else {
234
+ return
235
+ }
236
+
237
+ let store = try TemplateActivityStore.make()
238
+ guard let record = try store.loadEnvelope(activityId: envelope.activityId).map(TemplateRuntimeRecord.init(envelope:)),
239
+ let nativeActivity = nativeActivity(for: envelope.activityId, store: store)
240
+ else {
241
+ return
242
+ }
243
+
244
+ await nativeActivity.update(ActivityContent(state: contentState(from: record), staleDate: nil))
245
+ }
246
+ #else
247
+ private func updateNativeActivity(for envelope: StoredTemplateActivityEnvelope) async throws {
248
+ _ = envelope
249
+ }
250
+ #endif
251
+ }