@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,257 @@
1
+ #if canImport(ActivityKit) && canImport(WidgetKit)
2
+ import ActivityKit
3
+ import SwiftUI
4
+ import WidgetKit
5
+
6
+ @available(iOS 16.2, *)
7
+ public struct CapgoWorkoutLiveActivityWidget: Widget {
8
+ public init() {}
9
+
10
+ public var body: some WidgetConfiguration {
11
+ ActivityConfiguration(for: WorkoutActivityAttributes.self) { context in
12
+ WorkoutActivityRootView(
13
+ attributes: context.attributes,
14
+ contentState: context.state,
15
+ displayContext: .lockScreen
16
+ )
17
+ } dynamicIsland: { context in
18
+ DynamicIsland {
19
+ DynamicIslandExpandedRegion(.leading) {
20
+ WorkoutDynamicIslandLeadingView(attributes: context.attributes)
21
+ }
22
+
23
+ DynamicIslandExpandedRegion(.trailing) {
24
+ WorkoutDynamicIslandTrailingView(attributes: context.attributes, contentState: context.state)
25
+ }
26
+
27
+ DynamicIslandExpandedRegion(.center) {
28
+ WorkoutDynamicIslandCenterView(attributes: context.attributes)
29
+ }
30
+
31
+ DynamicIslandExpandedRegion(.bottom) {
32
+ WorkoutDynamicIslandBottomView(attributes: context.attributes, contentState: context.state)
33
+ }
34
+ } compactLeading: {
35
+ WorkoutCompactLeadingView(contentState: context.state)
36
+ } compactTrailing: {
37
+ WorkoutCompactTrailingView(contentState: context.state)
38
+ } minimal: {
39
+ WorkoutMinimalView(contentState: context.state)
40
+ }
41
+ }
42
+ }
43
+ }
44
+
45
+ private enum WorkoutDisplayContext {
46
+ case lockScreen
47
+ case dynamicIsland
48
+ }
49
+
50
+ @available(iOS 16.2, *)
51
+ private struct WorkoutActivityRootView: View {
52
+ let attributes: WorkoutActivityAttributes
53
+ let contentState: WorkoutActivityAttributes.ContentState
54
+ let displayContext: WorkoutDisplayContext
55
+
56
+ private var session: WorkoutSession? {
57
+ try? WorkoutActivityStore.make().loadEnvelope(sessionId: attributes.sessionId)?.session
58
+ }
59
+
60
+ private var widgetURL: URL? {
61
+ if let explicit = attributes.deepLinkUrl.flatMap(URL.init(string:)) {
62
+ return explicit
63
+ }
64
+ if let explicit = session?.deepLinkUrl.flatMap(URL.init(string:)) {
65
+ return explicit
66
+ }
67
+ return URL(string: "widgetkitdemo://session/\(attributes.sessionId)")
68
+ }
69
+
70
+ var body: some View {
71
+ Group {
72
+ if let session {
73
+ WorkoutLiveActivityView(
74
+ session: session,
75
+ contentState: contentState,
76
+ displayContext: displayContext
77
+ )
78
+ } else {
79
+ MissingSessionView()
80
+ }
81
+ }
82
+ .widgetURL(widgetURL)
83
+ }
84
+ }
85
+
86
+ @available(iOS 16.2, *)
87
+ private struct WorkoutLiveActivityView: View {
88
+ let session: WorkoutSession
89
+ let contentState: WorkoutActivityAttributes.ContentState
90
+ let displayContext: WorkoutDisplayContext
91
+
92
+ private var timer: WorkoutActiveTimer? {
93
+ session.activeTimer()
94
+ }
95
+
96
+ private var currentExercise: WorkoutExercise? {
97
+ session.currentExercise
98
+ }
99
+
100
+ private var currentSet: WorkoutSet? {
101
+ session.currentSet
102
+ }
103
+
104
+ var body: some View {
105
+ if session.isComplete {
106
+ CompletedWorkoutView(session: session)
107
+ } else {
108
+ TimelineView(.periodic(from: .now, by: 1)) { timeline in
109
+ let activeTimer = session.activeTimer(now: timeline.date)
110
+ ZStack(alignment: .leading) {
111
+ RoundedRectangle(cornerRadius: 22, style: .continuous)
112
+ .fill(Color.black)
113
+
114
+ if let activeTimer {
115
+ RestProgressBackground(timer: activeTimer, now: timeline.date)
116
+ }
117
+
118
+ VStack(alignment: .leading, spacing: 12) {
119
+ WorkoutIndicatorBar(session: session)
120
+ HStack(alignment: .top, spacing: 12) {
121
+ WorkoutExerciseThumbnail(exercise: currentExercise)
122
+
123
+ VStack(alignment: .leading, spacing: 4) {
124
+ Text(currentExercise?.title ?? session.title)
125
+ .font(.headline)
126
+ .foregroundStyle(.white)
127
+ .lineLimit(1)
128
+
129
+ if let subtitle = currentExercise?.subtitle, !subtitle.isEmpty {
130
+ Text(subtitle)
131
+ .font(.subheadline)
132
+ .foregroundStyle(.white.opacity(0.55))
133
+ .lineLimit(1)
134
+ }
135
+
136
+ if let recommendation = currentSet?.recommendation, !recommendation.isEmpty {
137
+ WorkoutRecommendationPill(text: recommendation)
138
+ }
139
+ }
140
+
141
+ Spacer(minLength: 8)
142
+
143
+ if let activeTimer {
144
+ WorkoutTimerLabel(timer: activeTimer, now: timeline.date)
145
+ }
146
+ }
147
+
148
+ HStack(alignment: .bottom, spacing: 12) {
149
+ Text(currentSet?.title ?? "No active set")
150
+ .font(.system(size: 28, weight: .semibold, design: .rounded))
151
+ .foregroundStyle(.white)
152
+ .minimumScaleFactor(0.7)
153
+ .lineLimit(2)
154
+
155
+ Spacer(minLength: 8)
156
+
157
+ WorkoutCompletionButton(sessionId: session.sessionId)
158
+ }
159
+ }
160
+ .padding(18)
161
+ }
162
+ }
163
+ }
164
+ }
165
+ }
166
+
167
+ @available(iOS 16.2, *)
168
+ private struct CompletedWorkoutView: View {
169
+ let session: WorkoutSession
170
+
171
+ var body: some View {
172
+ VStack(spacing: 16) {
173
+ WorkoutIndicatorBar(session: session)
174
+
175
+ Text(session.title)
176
+ .font(.headline)
177
+ .foregroundStyle(.white)
178
+
179
+ Text("All sets completed")
180
+ .font(.system(size: 26, weight: .bold, design: .rounded))
181
+ .foregroundStyle(Color.capgoMint)
182
+
183
+ Image(systemName: "checkmark")
184
+ .font(.system(size: 48, weight: .bold))
185
+ .foregroundStyle(Color.capgoMint)
186
+ }
187
+ .padding(18)
188
+ .frame(maxWidth: .infinity, maxHeight: .infinity)
189
+ .background(
190
+ RoundedRectangle(cornerRadius: 22, style: .continuous)
191
+ .fill(Color.black)
192
+ )
193
+ }
194
+ }
195
+
196
+ @available(iOS 16.2, *)
197
+ private struct WorkoutIndicatorBar: View {
198
+ let session: WorkoutSession
199
+
200
+ var body: some View {
201
+ HStack(spacing: 4) {
202
+ ForEach(Array(session.exercises.enumerated()), id: \.element.id) { index, exercise in
203
+ if index == session.activeExerciseIndex {
204
+ HStack(spacing: 4) {
205
+ ForEach(Array(exercise.sets.enumerated()), id: \.offset) { setIndex, set in
206
+ Capsule()
207
+ .fill(color(for: setIndex, set: set))
208
+ .frame(width: 10, height: 6)
209
+ }
210
+ }
211
+ .padding(.trailing, 2)
212
+ } else {
213
+ Capsule()
214
+ .fill(session.exerciseCompleted(at: index) ? Color.capgoMint : Color.white.opacity(0.18))
215
+ .frame(maxWidth: .infinity)
216
+ .frame(height: 6)
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ private func color(for setIndex: Int, set: WorkoutSet) -> Color {
223
+ if set.completedAt != nil {
224
+ return .capgoMint
225
+ }
226
+ if setIndex == session.activeSetIndex {
227
+ return .white
228
+ }
229
+ return Color.white.opacity(0.28)
230
+ }
231
+ }
232
+
233
+ @available(iOS 16.2, *)
234
+ private struct WorkoutExerciseThumbnail: View {
235
+ let exercise: WorkoutExercise?
236
+
237
+ var body: some View {
238
+ Group {
239
+ if let imageAssetName = exercise?.imageAssetName, !imageAssetName.isEmpty {
240
+ Image(imageAssetName)
241
+ .resizable()
242
+ .scaledToFill()
243
+ } else {
244
+ ZStack {
245
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
246
+ .fill(Color.white.opacity(0.08))
247
+ Image(systemName: exercise?.iconSystemName ?? "figure.strengthtraining.traditional")
248
+ .font(.system(size: 22, weight: .semibold))
249
+ .foregroundStyle(.white.opacity(0.85))
250
+ }
251
+ }
252
+ }
253
+ .frame(width: 56, height: 56)
254
+ .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
255
+ }
256
+ }
257
+ #endif
@@ -0,0 +1,24 @@
1
+ #if canImport(AppIntents)
2
+ import AppIntents
3
+
4
+ @available(iOS 17.0, *)
5
+ public struct CompleteSetIntent: LiveActivityIntent {
6
+ public static var title: LocalizedStringResource = "Complete Set"
7
+ public static var description = IntentDescription("Mark the current workout set as completed and advance the live activity.")
8
+ public static var openAppWhenRun = false
9
+
10
+ @Parameter(title: "Session ID")
11
+ public var sessionId: String
12
+
13
+ public init() {}
14
+
15
+ public init(sessionId: String) {
16
+ self.sessionId = sessionId
17
+ }
18
+
19
+ public func perform() async throws -> some IntentResult {
20
+ _ = try await WorkoutLiveActivityManager.shared.completeSet(sessionId: sessionId, activityId: nil)
21
+ return .result()
22
+ }
23
+ }
24
+ #endif
@@ -0,0 +1,56 @@
1
+ #if canImport(AppIntents)
2
+ import AppIntents
3
+ import Foundation
4
+
5
+ @available(iOS 17.0, *)
6
+ public struct CapgoTemplateActionIntent: LiveActivityIntent {
7
+ public static var title: LocalizedStringResource = "Run Widget Template Action"
8
+
9
+ @Parameter(title: "Activity Id")
10
+ public var activityId: String
11
+
12
+ @Parameter(title: "Action Id")
13
+ public var actionId: String
14
+
15
+ @Parameter(title: "Source Id")
16
+ public var sourceId: String?
17
+
18
+ @Parameter(title: "Payload JSON")
19
+ public var payloadJSON: String?
20
+
21
+ public init() {}
22
+
23
+ public init(activityId: String, actionId: String, sourceId: String? = nil, payloadJSON: String? = nil) {
24
+ self.activityId = activityId
25
+ self.actionId = actionId
26
+ self.sourceId = sourceId
27
+ self.payloadJSON = payloadJSON
28
+ }
29
+
30
+ public func perform() async throws -> some IntentResult {
31
+ let payloadObject: [String: Any]?
32
+ if let payloadJSON {
33
+ payloadObject = try parsePayload(payloadJSON)
34
+ } else {
35
+ payloadObject = nil
36
+ }
37
+
38
+ _ = try await TemplateLiveActivityManager.shared.performAction(
39
+ activityId: activityId,
40
+ actionId: actionId,
41
+ sourceId: sourceId,
42
+ payloadObject: payloadObject
43
+ )
44
+ return .result()
45
+ }
46
+
47
+ private func parsePayload(_ payloadJSON: String) throws -> [String: Any]? {
48
+ guard let data = payloadJSON.data(using: .utf8) else {
49
+ return nil
50
+ }
51
+
52
+ let object = try JSONSerialization.jsonObject(with: data, options: [])
53
+ return object as? [String: Any]
54
+ }
55
+ }
56
+ #endif
@@ -0,0 +1,239 @@
1
+ import Foundation
2
+
3
+ public struct StoredTemplateTimerState: Codable, Hashable, Sendable {
4
+ public var id: String
5
+ public var startedAtMs: Int64?
6
+ public var elapsedMs: Int64?
7
+ public var durationMs: Int64
8
+ public var status: String
9
+ public var updatedAtMs: Int64
10
+
11
+ public init(
12
+ id: String,
13
+ startedAtMs: Int64?,
14
+ durationMs: Int64,
15
+ status: String,
16
+ updatedAtMs: Int64,
17
+ elapsedMs: Int64? = nil
18
+ ) {
19
+ self.id = id
20
+ self.startedAtMs = startedAtMs
21
+ self.elapsedMs = elapsedMs
22
+ self.durationMs = durationMs
23
+ self.status = status
24
+ self.updatedAtMs = updatedAtMs
25
+ }
26
+ }
27
+
28
+ public struct StoredTemplateActivityEnvelope: Codable, Hashable, Sendable {
29
+ public var activityId: String
30
+ public var templateId: String
31
+ public var definitionData: Data
32
+ public var stateData: Data
33
+ public var timers: [String: StoredTemplateTimerState]
34
+ public var status: String
35
+ public var openUrl: String?
36
+ public var updatedAtMs: Int64
37
+ public var revision: Int
38
+ public var nativeActivityId: String?
39
+
40
+ public init(
41
+ activityId: String,
42
+ templateId: String,
43
+ definitionData: Data,
44
+ stateData: Data,
45
+ timers: [String: StoredTemplateTimerState],
46
+ status: String,
47
+ openUrl: String?,
48
+ updatedAtMs: Int64,
49
+ revision: Int,
50
+ nativeActivityId: String?
51
+ ) {
52
+ self.activityId = activityId
53
+ self.templateId = templateId
54
+ self.definitionData = definitionData
55
+ self.stateData = stateData
56
+ self.timers = timers
57
+ self.status = status
58
+ self.openUrl = openUrl
59
+ self.updatedAtMs = updatedAtMs
60
+ self.revision = revision
61
+ self.nativeActivityId = nativeActivityId
62
+ }
63
+ }
64
+
65
+ public struct StoredTemplateActionEvent: Codable, Hashable, Sendable {
66
+ public var eventId: String
67
+ public var activityId: String
68
+ public var actionId: String
69
+ public var eventName: String?
70
+ public var sourceId: String?
71
+ public var createdAtMs: Int64
72
+ public var acknowledgedAtMs: Int64?
73
+ public var payloadData: Data?
74
+ public var stateData: Data
75
+ public var timers: [String: StoredTemplateTimerState]
76
+
77
+ public init(
78
+ eventId: String,
79
+ activityId: String,
80
+ actionId: String,
81
+ eventName: String?,
82
+ sourceId: String?,
83
+ createdAtMs: Int64,
84
+ acknowledgedAtMs: Int64?,
85
+ payloadData: Data?,
86
+ stateData: Data,
87
+ timers: [String: StoredTemplateTimerState]
88
+ ) {
89
+ self.eventId = eventId
90
+ self.activityId = activityId
91
+ self.actionId = actionId
92
+ self.eventName = eventName
93
+ self.sourceId = sourceId
94
+ self.createdAtMs = createdAtMs
95
+ self.acknowledgedAtMs = acknowledgedAtMs
96
+ self.payloadData = payloadData
97
+ self.stateData = stateData
98
+ self.timers = timers
99
+ }
100
+ }
101
+
102
+ public final class TemplateActivityStore {
103
+ private static let activityIdsKey = "capgo.widgetkit.template.activity-ids"
104
+ private static let nativeActivityMapKey = "capgo.widgetkit.template.native-activity-map"
105
+
106
+ private let defaults: UserDefaults
107
+ private let encoder = JSONEncoder()
108
+ private let decoder = JSONDecoder()
109
+
110
+ private init(defaults: UserDefaults) {
111
+ self.defaults = defaults
112
+ }
113
+
114
+ public static func make(bundle: Bundle = .main) throws -> TemplateActivityStore {
115
+ TemplateActivityStore(defaults: try CapgoWidgetKitSharedStore.defaults(bundle: bundle))
116
+ }
117
+
118
+ public func loadEnvelope(activityId: String) -> StoredTemplateActivityEnvelope? {
119
+ guard let data = defaults.data(forKey: activityKey(activityId)) else {
120
+ return nil
121
+ }
122
+ return try? decoder.decode(StoredTemplateActivityEnvelope.self, from: data)
123
+ }
124
+
125
+ public func saveEnvelope(_ envelope: StoredTemplateActivityEnvelope) throws {
126
+ let data = try encoder.encode(envelope)
127
+ defaults.set(data, forKey: activityKey(envelope.activityId))
128
+ var ids = activityIds()
129
+ if !ids.contains(envelope.activityId) {
130
+ ids.append(envelope.activityId)
131
+ defaults.set(ids, forKey: Self.activityIdsKey)
132
+ }
133
+ defaults.synchronize()
134
+ }
135
+
136
+ public func deleteEnvelope(activityId: String) {
137
+ defaults.removeObject(forKey: activityKey(activityId))
138
+ defaults.set(activityIds().filter { $0 != activityId }, forKey: Self.activityIdsKey)
139
+ defaults.removeObject(forKey: eventsKey(activityId))
140
+
141
+ var mapping = nativeActivityMap()
142
+ mapping.removeValue(forKey: activityId)
143
+ defaults.set(mapping, forKey: Self.nativeActivityMapKey)
144
+ }
145
+
146
+ public func listEnvelopes() -> [StoredTemplateActivityEnvelope] {
147
+ activityIds().compactMap(loadEnvelope(activityId:))
148
+ .sorted { $0.updatedAtMs > $1.updatedAtMs }
149
+ }
150
+
151
+ public func appendEvent(_ event: StoredTemplateActionEvent) throws {
152
+ var events = loadEvents(activityId: event.activityId)
153
+ events.insert(event, at: 0)
154
+ let data = try encoder.encode(events)
155
+ defaults.set(data, forKey: eventsKey(event.activityId))
156
+ }
157
+
158
+ public func loadEvents(activityId: String) -> [StoredTemplateActionEvent] {
159
+ guard let data = defaults.data(forKey: eventsKey(activityId)) else {
160
+ return []
161
+ }
162
+ return (try? decoder.decode([StoredTemplateActionEvent].self, from: data)) ?? []
163
+ }
164
+
165
+ public func loadEvents(activityId: String?) -> [StoredTemplateActionEvent] {
166
+ if let activityId {
167
+ return loadEvents(activityId: activityId)
168
+ }
169
+
170
+ return activityIds()
171
+ .flatMap(loadEvents(activityId:))
172
+ .sorted { $0.createdAtMs > $1.createdAtMs }
173
+ }
174
+
175
+ public func acknowledgeEvents(eventIds: [String]?, activityId: String?, acknowledgedAtMs: Int64) throws {
176
+ let targetIds = Set(eventIds ?? [])
177
+
178
+ for currentActivityId in targetActivityIds(activityId: activityId, eventIds: eventIds) {
179
+ let updated = loadEvents(activityId: currentActivityId).map { event -> StoredTemplateActionEvent in
180
+ let matchesEventId = !targetIds.isEmpty && targetIds.contains(event.eventId)
181
+ let matchesActivity = activityId != nil && currentActivityId == activityId
182
+ guard matchesEventId || matchesActivity else {
183
+ return event
184
+ }
185
+
186
+ var acknowledgedEvent = event
187
+ acknowledgedEvent.acknowledgedAtMs = acknowledgedAtMs
188
+ return acknowledgedEvent
189
+ }
190
+ let data = try encoder.encode(updated)
191
+ defaults.set(data, forKey: eventsKey(currentActivityId))
192
+ }
193
+ }
194
+
195
+ public func nativeActivityId(for activityId: String) -> String? {
196
+ nativeActivityMap()[activityId]
197
+ }
198
+
199
+ public func setNativeActivityId(_ nativeActivityId: String, for activityId: String) {
200
+ var mapping = nativeActivityMap()
201
+ mapping[activityId] = nativeActivityId
202
+ defaults.set(mapping, forKey: Self.nativeActivityMapKey)
203
+ }
204
+
205
+ public func removeNativeActivityId(for activityId: String) {
206
+ var mapping = nativeActivityMap()
207
+ mapping.removeValue(forKey: activityId)
208
+ defaults.set(mapping, forKey: Self.nativeActivityMapKey)
209
+ }
210
+
211
+ private func targetActivityIds(activityId: String?, eventIds: [String]?) -> [String] {
212
+ if let activityId {
213
+ return [activityId]
214
+ }
215
+ guard let eventIds, !eventIds.isEmpty else {
216
+ return []
217
+ }
218
+ let targetIds = Set(eventIds)
219
+ return activityIds().filter { currentActivityId in
220
+ loadEvents(activityId: currentActivityId).contains(where: { targetIds.contains($0.eventId) })
221
+ }
222
+ }
223
+
224
+ private func activityIds() -> [String] {
225
+ defaults.stringArray(forKey: Self.activityIdsKey) ?? []
226
+ }
227
+
228
+ private func nativeActivityMap() -> [String: String] {
229
+ defaults.dictionary(forKey: Self.nativeActivityMapKey) as? [String: String] ?? [:]
230
+ }
231
+
232
+ private func activityKey(_ activityId: String) -> String {
233
+ "capgo.widgetkit.template.activity.\(activityId)"
234
+ }
235
+
236
+ private func eventsKey(_ activityId: String) -> String {
237
+ "capgo.widgetkit.template.events.\(activityId)"
238
+ }
239
+ }