@capgo/capacitor-widget-kit 8.1.2 → 8.1.3
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.
- package/README.md +116 -22
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKit.java +23 -3
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitConstants.java +1 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitPlugin.java +15 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateRuntime.java +10 -2
- package/dist/docs.json +116 -2
- package/dist/esm/definitions.d.ts +60 -5
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/helpers/workout.js +28 -0
- package/dist/esm/helpers/workout.js.map +1 -1
- package/dist/esm/runtime.js +3 -3
- package/dist/esm/runtime.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +8 -1
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +38 -4
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +38 -4
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoTemplateHomeWidget.swift +117 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoTemplateWidgetBridge.swift +37 -2
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoTemplateWidgetViews.swift +288 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKit.swift +27 -2
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitPlugin.swift +17 -1
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateLiveActivityManager.swift +19 -2
- package/package.json +3 -2
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import Foundation
|
|
3
3
|
|
|
4
4
|
public enum CapgoTemplateSurface: String, CaseIterable, Sendable {
|
|
5
|
+
case homeScreen
|
|
5
6
|
case lockScreen
|
|
6
7
|
case dynamicIslandExpanded
|
|
7
8
|
case dynamicIslandCompactLeading
|
|
@@ -102,6 +103,19 @@ public final class CapgoTemplateWidgetBridge {
|
|
|
102
103
|
try TemplateActivityStore.make(bundle: bundle).listEnvelopes()
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
public func latestActivity(status: String? = "active", bundle: Bundle = .main) throws -> StoredTemplateActivityEnvelope? {
|
|
107
|
+
try listActivities(bundle: bundle)
|
|
108
|
+
.filter { envelope in
|
|
109
|
+
guard let status else {
|
|
110
|
+
return true
|
|
111
|
+
}
|
|
112
|
+
return envelope.status == status
|
|
113
|
+
}
|
|
114
|
+
.max { left, right in
|
|
115
|
+
left.updatedAtMs < right.updatedAtMs
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
105
119
|
public func resolveLayout(
|
|
106
120
|
activityId: String,
|
|
107
121
|
surface: CapgoTemplateSurface,
|
|
@@ -113,10 +127,9 @@ public final class CapgoTemplateWidgetBridge {
|
|
|
113
127
|
}
|
|
114
128
|
|
|
115
129
|
let record = try TemplateRuntimeRecord(envelope: envelope)
|
|
116
|
-
let layoutKey = surface.rawValue
|
|
117
130
|
guard
|
|
118
131
|
let layouts = record.definition["layouts"] as? [String: Any],
|
|
119
|
-
let layoutObject =
|
|
132
|
+
let layoutObject = layoutObject(for: surface, in: layouts)
|
|
120
133
|
else {
|
|
121
134
|
return nil
|
|
122
135
|
}
|
|
@@ -143,6 +156,28 @@ public final class CapgoTemplateWidgetBridge {
|
|
|
143
156
|
)
|
|
144
157
|
}
|
|
145
158
|
|
|
159
|
+
public func resolveLatestLayout(
|
|
160
|
+
surface: CapgoTemplateSurface,
|
|
161
|
+
status: String? = "active",
|
|
162
|
+
bundle: Bundle = .main,
|
|
163
|
+
now: Date = Date()
|
|
164
|
+
) throws -> CapgoResolvedTemplateLayout? {
|
|
165
|
+
guard let envelope = try latestActivity(status: status, bundle: bundle) else {
|
|
166
|
+
return nil
|
|
167
|
+
}
|
|
168
|
+
return try resolveLayout(activityId: envelope.activityId, surface: surface, bundle: bundle, now: now)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private func layoutObject(for surface: CapgoTemplateSurface, in layouts: [String: Any]) -> [String: Any]? {
|
|
172
|
+
if let layout = layouts[surface.rawValue] as? [String: Any] {
|
|
173
|
+
return layout
|
|
174
|
+
}
|
|
175
|
+
if surface == .homeScreen {
|
|
176
|
+
return layouts[CapgoTemplateSurface.lockScreen.rawValue] as? [String: Any]
|
|
177
|
+
}
|
|
178
|
+
return nil
|
|
179
|
+
}
|
|
180
|
+
|
|
146
181
|
private func parseHotspots(_ value: Any?) -> [CapgoTemplateResolvedHotspot] {
|
|
147
182
|
guard let hotspots = value as? [[String: Any]] else {
|
|
148
183
|
return []
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
#if canImport(SwiftUI)
|
|
2
|
+
import Foundation
|
|
3
|
+
import OSLog
|
|
4
|
+
import SwiftUI
|
|
5
|
+
#if canImport(AppIntents)
|
|
6
|
+
import AppIntents
|
|
7
|
+
#endif
|
|
8
|
+
|
|
9
|
+
@available(iOS 16.0, *)
|
|
10
|
+
public struct CapgoTemplateClearHotspotLabel: View {
|
|
11
|
+
public init() {}
|
|
12
|
+
|
|
13
|
+
public var body: some View {
|
|
14
|
+
Rectangle()
|
|
15
|
+
.fill(Color.clear)
|
|
16
|
+
.contentShape(Rectangle())
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@available(iOS 16.0, *)
|
|
21
|
+
public struct CapgoTemplateHotspotButton<Label: View>: View {
|
|
22
|
+
public let activityId: String
|
|
23
|
+
public let hotspot: CapgoTemplateResolvedHotspot
|
|
24
|
+
private let label: () -> Label
|
|
25
|
+
|
|
26
|
+
public init(
|
|
27
|
+
activityId: String,
|
|
28
|
+
hotspot: CapgoTemplateResolvedHotspot,
|
|
29
|
+
@ViewBuilder label: @escaping () -> Label
|
|
30
|
+
) {
|
|
31
|
+
self.activityId = activityId
|
|
32
|
+
self.hotspot = hotspot
|
|
33
|
+
self.label = label
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public var body: some View {
|
|
37
|
+
Group {
|
|
38
|
+
#if canImport(AppIntents)
|
|
39
|
+
if #available(iOS 17.0, *) {
|
|
40
|
+
Button(
|
|
41
|
+
intent: CapgoTemplateActionIntent(
|
|
42
|
+
activityId: activityId,
|
|
43
|
+
actionId: hotspot.actionId,
|
|
44
|
+
sourceId: hotspot.id,
|
|
45
|
+
payloadJSON: hotspot.payloadJSON
|
|
46
|
+
)
|
|
47
|
+
) {
|
|
48
|
+
label()
|
|
49
|
+
}
|
|
50
|
+
.buttonStyle(.plain)
|
|
51
|
+
} else {
|
|
52
|
+
fallbackLabel
|
|
53
|
+
}
|
|
54
|
+
#else
|
|
55
|
+
fallbackLabel
|
|
56
|
+
#endif
|
|
57
|
+
}
|
|
58
|
+
.accessibilityLabel(Text(hotspot.label ?? hotspot.actionId))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private var fallbackLabel: some View {
|
|
62
|
+
label()
|
|
63
|
+
.allowsHitTesting(false)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@available(iOS 16.0, *)
|
|
68
|
+
public extension CapgoTemplateHotspotButton where Label == CapgoTemplateClearHotspotLabel {
|
|
69
|
+
init(activityId: String, hotspot: CapgoTemplateResolvedHotspot) {
|
|
70
|
+
self.init(activityId: activityId, hotspot: hotspot) {
|
|
71
|
+
CapgoTemplateClearHotspotLabel()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@available(iOS 16.0, *)
|
|
77
|
+
public struct CapgoTemplateSurfaceView<SVGContent: View, Placeholder: View>: View {
|
|
78
|
+
public let layout: CapgoResolvedTemplateLayout?
|
|
79
|
+
public let showsHotspots: Bool
|
|
80
|
+
private let svgContent: (CapgoResolvedTemplateLayout) -> SVGContent
|
|
81
|
+
private let placeholder: () -> Placeholder
|
|
82
|
+
|
|
83
|
+
public init(
|
|
84
|
+
layout: CapgoResolvedTemplateLayout?,
|
|
85
|
+
showsHotspots: Bool = true,
|
|
86
|
+
@ViewBuilder svgContent: @escaping (CapgoResolvedTemplateLayout) -> SVGContent,
|
|
87
|
+
@ViewBuilder placeholder: @escaping () -> Placeholder
|
|
88
|
+
) {
|
|
89
|
+
self.layout = layout
|
|
90
|
+
self.showsHotspots = showsHotspots
|
|
91
|
+
self.svgContent = svgContent
|
|
92
|
+
self.placeholder = placeholder
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public var body: some View {
|
|
96
|
+
Group {
|
|
97
|
+
if let layout {
|
|
98
|
+
surface(layout)
|
|
99
|
+
} else {
|
|
100
|
+
placeholder()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private func surface(_ layout: CapgoResolvedTemplateLayout) -> some View {
|
|
106
|
+
GeometryReader { proxy in
|
|
107
|
+
ZStack(alignment: .topLeading) {
|
|
108
|
+
svgContent(layout)
|
|
109
|
+
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
110
|
+
|
|
111
|
+
if showsHotspots {
|
|
112
|
+
ForEach(layout.hotspots, id: \.id) { hotspot in
|
|
113
|
+
CapgoTemplateHotspotButton(activityId: layout.activityId, hotspot: hotspot)
|
|
114
|
+
.frame(
|
|
115
|
+
width: hotspotWidth(hotspot, in: proxy.size, layout: layout),
|
|
116
|
+
height: hotspotHeight(hotspot, in: proxy.size, layout: layout)
|
|
117
|
+
)
|
|
118
|
+
.position(
|
|
119
|
+
x: hotspotCenterX(hotspot, in: proxy.size, layout: layout),
|
|
120
|
+
y: hotspotCenterY(hotspot, in: proxy.size, layout: layout)
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
.aspectRatio(CGFloat(layout.width / max(layout.height, 1)), contentMode: .fit)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private func hotspotWidth(
|
|
130
|
+
_ hotspot: CapgoTemplateResolvedHotspot,
|
|
131
|
+
in size: CGSize,
|
|
132
|
+
layout: CapgoResolvedTemplateLayout
|
|
133
|
+
) -> CGFloat {
|
|
134
|
+
max(1, size.width * CGFloat(hotspot.width / max(layout.width, 1)))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private func hotspotHeight(
|
|
138
|
+
_ hotspot: CapgoTemplateResolvedHotspot,
|
|
139
|
+
in size: CGSize,
|
|
140
|
+
layout: CapgoResolvedTemplateLayout
|
|
141
|
+
) -> CGFloat {
|
|
142
|
+
max(1, size.height * CGFloat(hotspot.height / max(layout.height, 1)))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private func hotspotCenterX(
|
|
146
|
+
_ hotspot: CapgoTemplateResolvedHotspot,
|
|
147
|
+
in size: CGSize,
|
|
148
|
+
layout: CapgoResolvedTemplateLayout
|
|
149
|
+
) -> CGFloat {
|
|
150
|
+
size.width * CGFloat((hotspot.x + hotspot.width / 2) / max(layout.width, 1))
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private func hotspotCenterY(
|
|
154
|
+
_ hotspot: CapgoTemplateResolvedHotspot,
|
|
155
|
+
in size: CGSize,
|
|
156
|
+
layout: CapgoResolvedTemplateLayout
|
|
157
|
+
) -> CGFloat {
|
|
158
|
+
size.height * CGFloat((hotspot.y + hotspot.height / 2) / max(layout.height, 1))
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@available(iOS 16.0, *)
|
|
163
|
+
public struct CapgoTemplateWidgetSurface<SVGContent: View, Placeholder: View>: View {
|
|
164
|
+
private let activityId: String
|
|
165
|
+
private let surface: CapgoTemplateSurface
|
|
166
|
+
private let bundle: Bundle
|
|
167
|
+
private let showsHotspots: Bool
|
|
168
|
+
private let now: () -> Date
|
|
169
|
+
private let svgContent: (CapgoResolvedTemplateLayout) -> SVGContent
|
|
170
|
+
private let placeholder: () -> Placeholder
|
|
171
|
+
|
|
172
|
+
public init(
|
|
173
|
+
activityId: String,
|
|
174
|
+
surface: CapgoTemplateSurface = .lockScreen,
|
|
175
|
+
bundle: Bundle = .main,
|
|
176
|
+
showsHotspots: Bool = true,
|
|
177
|
+
now: @escaping () -> Date = { Date() },
|
|
178
|
+
@ViewBuilder svgContent: @escaping (CapgoResolvedTemplateLayout) -> SVGContent,
|
|
179
|
+
@ViewBuilder placeholder: @escaping () -> Placeholder
|
|
180
|
+
) {
|
|
181
|
+
self.activityId = activityId
|
|
182
|
+
self.surface = surface
|
|
183
|
+
self.bundle = bundle
|
|
184
|
+
self.showsHotspots = showsHotspots
|
|
185
|
+
self.now = now
|
|
186
|
+
self.svgContent = svgContent
|
|
187
|
+
self.placeholder = placeholder
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public var body: some View {
|
|
191
|
+
CapgoTemplateSurfaceView(
|
|
192
|
+
layout: resolvedLayout,
|
|
193
|
+
showsHotspots: showsHotspots,
|
|
194
|
+
svgContent: svgContent,
|
|
195
|
+
placeholder: placeholder
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private var resolvedLayout: CapgoResolvedTemplateLayout? {
|
|
200
|
+
do {
|
|
201
|
+
return try CapgoTemplateWidgetBridge().resolveLayout(
|
|
202
|
+
activityId: activityId,
|
|
203
|
+
surface: surface,
|
|
204
|
+
bundle: bundle,
|
|
205
|
+
now: now()
|
|
206
|
+
)
|
|
207
|
+
} catch {
|
|
208
|
+
logTemplateWidgetResolutionFailure(error, surface: surface, bundle: bundle, activityId: activityId)
|
|
209
|
+
return nil
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@available(iOS 16.0, *)
|
|
215
|
+
public struct CapgoTemplateLatestWidgetSurface<SVGContent: View, Placeholder: View>: View {
|
|
216
|
+
private let surface: CapgoTemplateSurface
|
|
217
|
+
private let status: String?
|
|
218
|
+
private let bundle: Bundle
|
|
219
|
+
private let showsHotspots: Bool
|
|
220
|
+
private let now: () -> Date
|
|
221
|
+
private let svgContent: (CapgoResolvedTemplateLayout) -> SVGContent
|
|
222
|
+
private let placeholder: () -> Placeholder
|
|
223
|
+
|
|
224
|
+
public init(
|
|
225
|
+
surface: CapgoTemplateSurface = .lockScreen,
|
|
226
|
+
status: String? = "active",
|
|
227
|
+
bundle: Bundle = .main,
|
|
228
|
+
showsHotspots: Bool = true,
|
|
229
|
+
now: @escaping () -> Date = { Date() },
|
|
230
|
+
@ViewBuilder svgContent: @escaping (CapgoResolvedTemplateLayout) -> SVGContent,
|
|
231
|
+
@ViewBuilder placeholder: @escaping () -> Placeholder
|
|
232
|
+
) {
|
|
233
|
+
self.surface = surface
|
|
234
|
+
self.status = status
|
|
235
|
+
self.bundle = bundle
|
|
236
|
+
self.showsHotspots = showsHotspots
|
|
237
|
+
self.now = now
|
|
238
|
+
self.svgContent = svgContent
|
|
239
|
+
self.placeholder = placeholder
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
public var body: some View {
|
|
243
|
+
CapgoTemplateSurfaceView(
|
|
244
|
+
layout: resolvedLayout,
|
|
245
|
+
showsHotspots: showsHotspots,
|
|
246
|
+
svgContent: svgContent,
|
|
247
|
+
placeholder: placeholder
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private var resolvedLayout: CapgoResolvedTemplateLayout? {
|
|
252
|
+
do {
|
|
253
|
+
return try CapgoTemplateWidgetBridge().resolveLatestLayout(
|
|
254
|
+
surface: surface,
|
|
255
|
+
status: status,
|
|
256
|
+
bundle: bundle,
|
|
257
|
+
now: now()
|
|
258
|
+
)
|
|
259
|
+
} catch {
|
|
260
|
+
logTemplateWidgetResolutionFailure(error, surface: surface, bundle: bundle, status: status)
|
|
261
|
+
return nil
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
@available(iOS 16.0, *)
|
|
267
|
+
private enum CapgoTemplateWidgetSurfaceLog {
|
|
268
|
+
static let logger = Logger(subsystem: "app.capgo.widgetkit", category: "TemplateWidgetSurface")
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
@available(iOS 16.0, *)
|
|
272
|
+
private func logTemplateWidgetResolutionFailure(
|
|
273
|
+
_ error: Error,
|
|
274
|
+
surface: CapgoTemplateSurface,
|
|
275
|
+
bundle: Bundle,
|
|
276
|
+
activityId: String? = nil,
|
|
277
|
+
status: String? = nil
|
|
278
|
+
) {
|
|
279
|
+
let activityContext = activityId ?? "latest"
|
|
280
|
+
let statusContext = status ?? "any"
|
|
281
|
+
let bundleIdentifier = bundle.bundleIdentifier ?? "unknown"
|
|
282
|
+
let context =
|
|
283
|
+
"activityId=\(activityContext) status=\(statusContext) surface=\(surface.rawValue) bundle=\(bundleIdentifier)"
|
|
284
|
+
CapgoTemplateWidgetSurfaceLog.logger.error(
|
|
285
|
+
"Failed to resolve template widget surface \(context, privacy: .public): \(error.localizedDescription, privacy: .public)"
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
#endif
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import Foundation
|
|
2
|
+
#if canImport(WidgetKit)
|
|
3
|
+
import WidgetKit
|
|
4
|
+
#endif
|
|
2
5
|
|
|
3
6
|
enum CapgoWidgetKitBridgeError: LocalizedError {
|
|
4
7
|
case missingObject(String)
|
|
@@ -30,13 +33,15 @@ public final class CapgoWidgetKit {
|
|
|
30
33
|
activityId: String?,
|
|
31
34
|
definitionObject: [String: Any],
|
|
32
35
|
stateObject: [String: Any],
|
|
33
|
-
openUrl: String
|
|
36
|
+
openUrl: String?,
|
|
37
|
+
startLiveActivity: Bool
|
|
34
38
|
) async throws -> [String: Any] {
|
|
35
39
|
let envelope = try await TemplateLiveActivityManager.shared.start(
|
|
36
40
|
activityId: activityId,
|
|
37
41
|
definitionObject: definitionObject,
|
|
38
42
|
stateObject: stateObject,
|
|
39
|
-
openUrl: openUrl
|
|
43
|
+
openUrl: openUrl,
|
|
44
|
+
startLiveActivity: startLiveActivity
|
|
40
45
|
)
|
|
41
46
|
return ["activity": try TemplateRuntime.serializeActivity(envelope)]
|
|
42
47
|
}
|
|
@@ -120,6 +125,7 @@ public final class CapgoWidgetKit {
|
|
|
120
125
|
stateObject: stateObject ?? [:],
|
|
121
126
|
metadataObject: metadataObject
|
|
122
127
|
)
|
|
128
|
+
reloadWidgets(kind: nil)
|
|
123
129
|
return ["session": try CapgoNativeWidgetBridge.serializeSession(session)]
|
|
124
130
|
}
|
|
125
131
|
|
|
@@ -138,11 +144,13 @@ public final class CapgoWidgetKit {
|
|
|
138
144
|
guard let session else {
|
|
139
145
|
return ["session": NSNull()]
|
|
140
146
|
}
|
|
147
|
+
reloadWidgets(kind: nil)
|
|
141
148
|
return ["session": try CapgoNativeWidgetBridge.serializeSession(session)]
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
public func stopWidgetSession(widgetId: String, stateObject: [String: Any]?) throws {
|
|
145
152
|
try CapgoNativeWidgetBridge().stopSession(widgetId: widgetId, stateObject: stateObject)
|
|
153
|
+
reloadWidgets(kind: nil)
|
|
146
154
|
}
|
|
147
155
|
|
|
148
156
|
public func getWidgetSession(widgetId: String) throws -> [String: Any] {
|
|
@@ -172,6 +180,7 @@ public final class CapgoWidgetKit {
|
|
|
172
180
|
payloadObject: payloadObject,
|
|
173
181
|
expectsResponse: expectsResponse
|
|
174
182
|
)
|
|
183
|
+
reloadWidgets(kind: nil)
|
|
175
184
|
return ["message": try CapgoNativeWidgetBridge.serializeMessage(message)]
|
|
176
185
|
}
|
|
177
186
|
|
|
@@ -196,6 +205,7 @@ public final class CapgoWidgetKit {
|
|
|
196
205
|
widgetId: widgetId,
|
|
197
206
|
direction: direction.flatMap(CapgoWidgetMessageDirection.init(rawValue:))
|
|
198
207
|
)
|
|
208
|
+
reloadWidgets(kind: nil)
|
|
199
209
|
}
|
|
200
210
|
|
|
201
211
|
public func completeWidgetMessage(messageId: String, responseObject: [String: Any]?, error: String?) throws -> [String: Any] {
|
|
@@ -207,9 +217,24 @@ public final class CapgoWidgetKit {
|
|
|
207
217
|
guard let message else {
|
|
208
218
|
return ["message": NSNull()]
|
|
209
219
|
}
|
|
220
|
+
reloadWidgets(kind: nil)
|
|
210
221
|
return ["message": try CapgoNativeWidgetBridge.serializeMessage(message)]
|
|
211
222
|
}
|
|
212
223
|
|
|
224
|
+
public func reloadWidgets(kind: String?) {
|
|
225
|
+
#if canImport(WidgetKit)
|
|
226
|
+
if #available(iOS 14.0, *) {
|
|
227
|
+
if let kind, !kind.isEmpty {
|
|
228
|
+
WidgetCenter.shared.reloadTimelines(ofKind: kind)
|
|
229
|
+
} else {
|
|
230
|
+
WidgetCenter.shared.reloadAllTimelines()
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
#else
|
|
234
|
+
_ = kind
|
|
235
|
+
#endif
|
|
236
|
+
}
|
|
237
|
+
|
|
213
238
|
public func getPluginVersion() -> [String: Any] {
|
|
214
239
|
[
|
|
215
240
|
"version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "ios"
|
|
@@ -8,6 +8,7 @@ public class CapgoWidgetKitPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
8
8
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
9
9
|
CAPPluginMethod(name: "areActivitiesSupported", returnType: CAPPluginReturnPromise),
|
|
10
10
|
CAPPluginMethod(name: "startTemplateActivity", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "startTemplateWidget", returnType: CAPPluginReturnPromise),
|
|
11
12
|
CAPPluginMethod(name: "updateTemplateActivity", returnType: CAPPluginReturnPromise),
|
|
12
13
|
CAPPluginMethod(name: "endTemplateActivity", returnType: CAPPluginReturnPromise),
|
|
13
14
|
CAPPluginMethod(name: "performTemplateAction", returnType: CAPPluginReturnPromise),
|
|
@@ -24,6 +25,7 @@ public class CapgoWidgetKitPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
24
25
|
CAPPluginMethod(name: "listWidgetMessages", returnType: CAPPluginReturnPromise),
|
|
25
26
|
CAPPluginMethod(name: "acknowledgeWidgetMessages", returnType: CAPPluginReturnPromise),
|
|
26
27
|
CAPPluginMethod(name: "completeWidgetMessage", returnType: CAPPluginReturnPromise),
|
|
28
|
+
CAPPluginMethod(name: "reloadWidgets", returnType: CAPPluginReturnPromise),
|
|
27
29
|
CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise)
|
|
28
30
|
]
|
|
29
31
|
|
|
@@ -34,6 +36,14 @@ public class CapgoWidgetKitPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
@objc func startTemplateActivity(_ call: CAPPluginCall) {
|
|
39
|
+
startTemplateRecord(call, startLiveActivity: call.getBool("startLiveActivity") ?? true)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@objc func startTemplateWidget(_ call: CAPPluginCall) {
|
|
43
|
+
startTemplateRecord(call, startLiveActivity: false)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private func startTemplateRecord(_ call: CAPPluginCall, startLiveActivity: Bool) {
|
|
37
47
|
guard let definition = call.getObject("definition") else {
|
|
38
48
|
call.reject(CapgoWidgetKitBridgeError.missingObject("definition").localizedDescription)
|
|
39
49
|
return
|
|
@@ -50,7 +60,8 @@ public class CapgoWidgetKitPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
50
60
|
activityId: call.getString("activityId"),
|
|
51
61
|
definitionObject: definition,
|
|
52
62
|
stateObject: state,
|
|
53
|
-
openUrl: call.getString("openUrl")
|
|
63
|
+
openUrl: call.getString("openUrl"),
|
|
64
|
+
startLiveActivity: startLiveActivity
|
|
54
65
|
)
|
|
55
66
|
call.resolve(payload)
|
|
56
67
|
} catch {
|
|
@@ -316,6 +327,11 @@ public class CapgoWidgetKitPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
316
327
|
}
|
|
317
328
|
}
|
|
318
329
|
|
|
330
|
+
@objc func reloadWidgets(_ call: CAPPluginCall) {
|
|
331
|
+
implementation.reloadWidgets(kind: call.getString("kind"))
|
|
332
|
+
call.resolve()
|
|
333
|
+
}
|
|
334
|
+
|
|
319
335
|
@objc func getPluginVersion(_ call: CAPPluginCall) {
|
|
320
336
|
call.resolve(implementation.getPluginVersion())
|
|
321
337
|
}
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
import ActivityKit
|
|
3
3
|
#endif
|
|
4
4
|
import Foundation
|
|
5
|
+
#if canImport(WidgetKit)
|
|
6
|
+
import WidgetKit
|
|
7
|
+
#endif
|
|
5
8
|
|
|
6
9
|
public struct TemplateActivityInfo: Hashable, Sendable {
|
|
7
10
|
public let activityId: String
|
|
@@ -55,7 +58,8 @@ public final class TemplateLiveActivityManager {
|
|
|
55
58
|
activityId requestedActivityId: String?,
|
|
56
59
|
definitionObject: [String: Any],
|
|
57
60
|
stateObject: [String: Any],
|
|
58
|
-
openUrl: String
|
|
61
|
+
openUrl: String?,
|
|
62
|
+
startLiveActivity: Bool = true
|
|
59
63
|
) async throws -> StoredTemplateActivityEnvelope {
|
|
60
64
|
let nowMs = Int64(Date().timeIntervalSince1970 * 1000)
|
|
61
65
|
let activityId: String
|
|
@@ -74,7 +78,7 @@ public final class TemplateLiveActivityManager {
|
|
|
74
78
|
let store = try TemplateActivityStore.make()
|
|
75
79
|
|
|
76
80
|
#if canImport(ActivityKit)
|
|
77
|
-
if #available(iOS 16.2, *) {
|
|
81
|
+
if #available(iOS 16.2, *), startLiveActivity {
|
|
78
82
|
let activity = try Activity.request(
|
|
79
83
|
attributes: CapgoTemplateActivityAttributes(
|
|
80
84
|
activityId: activityId,
|
|
@@ -91,6 +95,7 @@ public final class TemplateLiveActivityManager {
|
|
|
91
95
|
|
|
92
96
|
let envelope = try record.toEnvelope()
|
|
93
97
|
try store.saveEnvelope(envelope)
|
|
98
|
+
reloadWidgetTimelines()
|
|
94
99
|
return envelope
|
|
95
100
|
}
|
|
96
101
|
|
|
@@ -123,6 +128,7 @@ public final class TemplateLiveActivityManager {
|
|
|
123
128
|
let envelope = try record.toEnvelope()
|
|
124
129
|
try store.saveEnvelope(envelope)
|
|
125
130
|
try await updateNativeActivity(for: envelope)
|
|
131
|
+
reloadWidgetTimelines()
|
|
126
132
|
return envelope
|
|
127
133
|
}
|
|
128
134
|
|
|
@@ -150,6 +156,7 @@ public final class TemplateLiveActivityManager {
|
|
|
150
156
|
#endif
|
|
151
157
|
|
|
152
158
|
store.removeNativeActivityId(for: activityId)
|
|
159
|
+
reloadWidgetTimelines()
|
|
153
160
|
}
|
|
154
161
|
|
|
155
162
|
public func performAction(
|
|
@@ -177,6 +184,7 @@ public final class TemplateLiveActivityManager {
|
|
|
177
184
|
try store.saveEnvelope(envelope)
|
|
178
185
|
try store.appendEvent(event)
|
|
179
186
|
try await updateNativeActivity(for: envelope)
|
|
187
|
+
reloadWidgetTimelines()
|
|
180
188
|
return (envelope, event)
|
|
181
189
|
}
|
|
182
190
|
|
|
@@ -209,6 +217,15 @@ public final class TemplateLiveActivityManager {
|
|
|
209
217
|
activityId: activityId,
|
|
210
218
|
acknowledgedAtMs: Int64(Date().timeIntervalSince1970 * 1000)
|
|
211
219
|
)
|
|
220
|
+
reloadWidgetTimelines()
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private func reloadWidgetTimelines() {
|
|
224
|
+
#if canImport(WidgetKit)
|
|
225
|
+
if #available(iOS 14.0, *) {
|
|
226
|
+
WidgetCenter.shared.reloadAllTimelines()
|
|
227
|
+
}
|
|
228
|
+
#endif
|
|
212
229
|
}
|
|
213
230
|
|
|
214
231
|
#if canImport(ActivityKit)
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/capacitor-widget-kit",
|
|
3
|
-
"version": "8.1.
|
|
3
|
+
"version": "8.1.3",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "Capacitor plugin for generic iOS WidgetKit and Live Activities using SVG templates, declarative actions, and shared App Group persistence.",
|
|
5
|
+
"description": "Capacitor plugin for generic iOS Home Screen widgets, WidgetKit, and Live Activities using SVG templates, declarative actions, and shared App Group persistence.",
|
|
6
6
|
"main": "dist/plugin.cjs.js",
|
|
7
7
|
"module": "dist/esm/index.js",
|
|
8
8
|
"types": "dist/esm/index.d.ts",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"capacitor",
|
|
31
31
|
"plugin",
|
|
32
32
|
"widgetkit",
|
|
33
|
+
"home-screen-widgets",
|
|
33
34
|
"live-activities",
|
|
34
35
|
"activitykit",
|
|
35
36
|
"ios"
|