@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.
- package/CapgoCapacitorWidgetKit.podspec +17 -0
- package/LICENSE +373 -0
- package/Package.swift +28 -0
- package/README.md +1119 -0
- package/android/build.gradle +59 -0
- package/android/src/main/AndroidManifest.xml +8 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoNativeWidgetBridge.java +311 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateActionReceiver.java +36 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateSvgBitmapRenderer.java +25 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateWidgetBridge.java +31 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKit.java +218 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitConstants.java +18 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitPlugin.java +370 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateActivityStore.java +167 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateJsonUtils.java +369 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateRuntime.java +676 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +2223 -0
- package/dist/esm/definitions.d.ts +1117 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/helpers/workout.d.ts +32 -0
- package/dist/esm/helpers/workout.js +129 -0
- package/dist/esm/helpers/workout.js.map +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/runtime.d.ts +25 -0
- package/dist/esm/runtime.js +653 -0
- package/dist/esm/runtime.js.map +1 -0
- package/dist/esm/web.d.ts +23 -0
- package/dist/esm/web.js +292 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +1096 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +1099 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoNativeWidgetBridge.swift +372 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoTemplateWidgetBridge.swift +199 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKit.swift +218 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitPlugin.swift +322 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitSharedStore.swift +47 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWorkoutLiveActivityComponents.swift +246 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWorkoutLiveActivityWidget.swift +257 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CompleteSetIntent.swift +24 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateActionIntent.swift +56 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateActivityStore.swift +239 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateLiveActivityManager.swift +251 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateRuntime.swift +1067 -0
- package/ios/Sources/CapgoWidgetKitPlugin/WorkoutActivityStore.swift +111 -0
- package/ios/Sources/CapgoWidgetKitPlugin/WorkoutLiveActivityManager.swift +263 -0
- package/ios/Sources/CapgoWidgetKitPlugin/WorkoutSession.swift +271 -0
- package/ios/Tests/CapgoWidgetKitPluginTests/CapgoWidgetKitPluginTests.swift +130 -0
- package/package.json +93 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
package app.capgo.widgetkit;
|
|
2
|
+
|
|
3
|
+
import java.util.Iterator;
|
|
4
|
+
import java.util.UUID;
|
|
5
|
+
import org.json.JSONArray;
|
|
6
|
+
import org.json.JSONException;
|
|
7
|
+
import org.json.JSONObject;
|
|
8
|
+
|
|
9
|
+
final class TemplateRuntime {
|
|
10
|
+
|
|
11
|
+
static final class ActionResult {
|
|
12
|
+
|
|
13
|
+
final JSONObject activity;
|
|
14
|
+
final JSONObject event;
|
|
15
|
+
|
|
16
|
+
ActionResult(final JSONObject activity, final JSONObject event) {
|
|
17
|
+
this.activity = activity;
|
|
18
|
+
this.event = event;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private TemplateRuntime() {}
|
|
23
|
+
|
|
24
|
+
static JSONObject createActivityRecord(
|
|
25
|
+
final String activityId,
|
|
26
|
+
final JSONObject definition,
|
|
27
|
+
final JSONObject state,
|
|
28
|
+
final String openUrl,
|
|
29
|
+
final long nowMs
|
|
30
|
+
) throws JSONException {
|
|
31
|
+
final JSONObject record = new JSONObject()
|
|
32
|
+
.put("activityId", activityId)
|
|
33
|
+
.put("definition", TemplateJsonUtils.deepCopyObject(definition))
|
|
34
|
+
.put("state", TemplateJsonUtils.deepCopyObject(state))
|
|
35
|
+
.put("timers", new JSONObject())
|
|
36
|
+
.put("status", "active")
|
|
37
|
+
.put("openUrl", openUrl == null ? JSONObject.NULL : openUrl)
|
|
38
|
+
.put("updatedAt", nowMs)
|
|
39
|
+
.put("revision", 1);
|
|
40
|
+
record.put("timers", reconcileTimerStates(record, nowMs));
|
|
41
|
+
return record;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static JSONObject updateActivity(
|
|
45
|
+
final JSONObject current,
|
|
46
|
+
final JSONObject nextDefinition,
|
|
47
|
+
final JSONObject nextState,
|
|
48
|
+
final String openUrl,
|
|
49
|
+
final long nowMs
|
|
50
|
+
) throws JSONException {
|
|
51
|
+
final JSONObject updated = TemplateJsonUtils.deepCopyObject(current);
|
|
52
|
+
if (nextDefinition != null) {
|
|
53
|
+
updated.put("definition", TemplateJsonUtils.deepCopyObject(nextDefinition));
|
|
54
|
+
}
|
|
55
|
+
if (nextState != null) {
|
|
56
|
+
updated.put("state", TemplateJsonUtils.deepCopyObject(nextState));
|
|
57
|
+
}
|
|
58
|
+
if (openUrl != null) {
|
|
59
|
+
updated.put("openUrl", openUrl);
|
|
60
|
+
}
|
|
61
|
+
updated.put("updatedAt", nowMs);
|
|
62
|
+
updated.put("revision", updated.optInt("revision") + 1);
|
|
63
|
+
updated.put("timers", reconcileTimerStates(updated, nowMs));
|
|
64
|
+
return updated;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static JSONObject endActivity(final JSONObject current, final JSONObject finalState, final long nowMs) throws JSONException {
|
|
68
|
+
final JSONObject ended = TemplateJsonUtils.deepCopyObject(current);
|
|
69
|
+
if (finalState != null) {
|
|
70
|
+
ended.put("state", TemplateJsonUtils.deepCopyObject(finalState));
|
|
71
|
+
}
|
|
72
|
+
ended.put("status", "ended");
|
|
73
|
+
ended.put("updatedAt", nowMs);
|
|
74
|
+
ended.put("revision", ended.optInt("revision") + 1);
|
|
75
|
+
ended.put("timers", reconcileTimerStates(ended, nowMs));
|
|
76
|
+
return ended;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static ActionResult applyAction(
|
|
80
|
+
final String actionId,
|
|
81
|
+
final String sourceId,
|
|
82
|
+
final JSONObject payload,
|
|
83
|
+
final JSONObject current,
|
|
84
|
+
final long nowMs
|
|
85
|
+
) throws JSONException {
|
|
86
|
+
final JSONObject action = findActionDefinition(current.getJSONObject("definition"), actionId);
|
|
87
|
+
if (action == null) {
|
|
88
|
+
throw new JSONException(
|
|
89
|
+
"Action " + actionId + " is not defined on template " + current.getJSONObject("definition").optString("id")
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
final JSONObject nextActivity = TemplateJsonUtils.deepCopyObject(current);
|
|
94
|
+
final JSONObject actionScope = new JSONObject()
|
|
95
|
+
.put("id", actionId)
|
|
96
|
+
.put("sourceId", sourceId == null ? JSONObject.NULL : sourceId)
|
|
97
|
+
.put("payload", payload == null ? JSONObject.NULL : TemplateJsonUtils.deepCopyObject(payload));
|
|
98
|
+
|
|
99
|
+
final JSONArray patches = action.optJSONArray("patches");
|
|
100
|
+
if (patches != null) {
|
|
101
|
+
for (int index = 0; index < patches.length(); index += 1) {
|
|
102
|
+
final JSONObject patch = patches.optJSONObject(index);
|
|
103
|
+
if (patch != null) {
|
|
104
|
+
applyStatePatch(nextActivity, patch, nowMs, actionScope);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
final JSONArray frameMutations = action.optJSONArray("frameMutations");
|
|
110
|
+
if (frameMutations != null) {
|
|
111
|
+
for (int index = 0; index < frameMutations.length(); index += 1) {
|
|
112
|
+
final JSONObject mutation = frameMutations.optJSONObject(index);
|
|
113
|
+
if (mutation != null) {
|
|
114
|
+
applyFrameMutation(nextActivity, mutation, nowMs, actionScope);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
final JSONArray timerMutations = action.optJSONArray("timerMutations");
|
|
120
|
+
if (timerMutations != null) {
|
|
121
|
+
for (int index = 0; index < timerMutations.length(); index += 1) {
|
|
122
|
+
final JSONObject mutation = timerMutations.optJSONObject(index);
|
|
123
|
+
if (mutation != null) {
|
|
124
|
+
applyTimerMutation(nextActivity, mutation, nowMs, actionScope);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
nextActivity.put("timers", reconcileTimerStates(nextActivity, nowMs));
|
|
130
|
+
nextActivity.put("updatedAt", nowMs);
|
|
131
|
+
nextActivity.put("revision", nextActivity.optInt("revision") + 1);
|
|
132
|
+
|
|
133
|
+
final JSONObject event = new JSONObject()
|
|
134
|
+
.put("eventId", createIdentifier("event"))
|
|
135
|
+
.put("activityId", nextActivity.optString("activityId"))
|
|
136
|
+
.put("actionId", actionId)
|
|
137
|
+
.put("eventName", action.has("eventName") ? action.opt("eventName") : JSONObject.NULL)
|
|
138
|
+
.put("sourceId", sourceId == null ? JSONObject.NULL : sourceId)
|
|
139
|
+
.put("createdAt", nowMs)
|
|
140
|
+
.put("acknowledgedAt", JSONObject.NULL)
|
|
141
|
+
.put("payload", payload == null ? JSONObject.NULL : TemplateJsonUtils.deepCopyObject(payload))
|
|
142
|
+
.put("state", TemplateJsonUtils.deepCopyObject(nextActivity.getJSONObject("state")))
|
|
143
|
+
.put("timers", TemplateJsonUtils.deepCopyObject(nextActivity.getJSONObject("timers")));
|
|
144
|
+
|
|
145
|
+
return new ActionResult(nextActivity, event);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static JSONArray acknowledgeEvents(final JSONArray events, final String activityId, final JSONArray eventIds, final long acknowledgedAt)
|
|
149
|
+
throws JSONException {
|
|
150
|
+
final JSONArray nextEvents = new JSONArray();
|
|
151
|
+
for (int index = 0; index < events.length(); index += 1) {
|
|
152
|
+
final JSONObject event = events.optJSONObject(index);
|
|
153
|
+
if (event == null) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
final boolean matchesEventId = eventIds != null && containsString(eventIds, event.optString("eventId"));
|
|
158
|
+
final boolean matchesActivity = activityId != null && activityId.equals(event.optString("activityId"));
|
|
159
|
+
final JSONObject nextEvent = TemplateJsonUtils.deepCopyObject(event);
|
|
160
|
+
if (matchesEventId || matchesActivity) {
|
|
161
|
+
nextEvent.put("acknowledgedAt", acknowledgedAt);
|
|
162
|
+
}
|
|
163
|
+
nextEvents.put(nextEvent);
|
|
164
|
+
}
|
|
165
|
+
return nextEvents;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static JSONObject resolveSurface(final JSONObject record, final String surface, final long nowMs) throws JSONException {
|
|
169
|
+
final JSONObject layouts = record.getJSONObject("definition").getJSONObject("layouts");
|
|
170
|
+
final JSONObject layout = layouts.optJSONObject(surface);
|
|
171
|
+
if (layout == null) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
final JSONObject resolvedLayout = resolveLayoutFrame(layout, record, nowMs);
|
|
176
|
+
|
|
177
|
+
return new JSONObject()
|
|
178
|
+
.put("surface", surface)
|
|
179
|
+
.put("activityId", record.optString("activityId"))
|
|
180
|
+
.put("templateId", record.getJSONObject("definition").optString("id"))
|
|
181
|
+
.put("frameId", resolvedLayout.has("frameId") ? resolvedLayout.opt("frameId") : JSONObject.NULL)
|
|
182
|
+
.put("svg", TemplateJsonUtils.resolveTemplateString(resolvedLayout.optString("svg", ""), record, nowMs, null))
|
|
183
|
+
.put(
|
|
184
|
+
"width",
|
|
185
|
+
TemplateJsonUtils.coerceDouble(layout.opt("width")) != null ? TemplateJsonUtils.coerceDouble(layout.opt("width")) : 1D
|
|
186
|
+
)
|
|
187
|
+
.put(
|
|
188
|
+
"height",
|
|
189
|
+
TemplateJsonUtils.coerceDouble(layout.opt("height")) != null ? TemplateJsonUtils.coerceDouble(layout.opt("height")) : 1D
|
|
190
|
+
)
|
|
191
|
+
.put(
|
|
192
|
+
"hotspots",
|
|
193
|
+
TemplateJsonUtils.deepCopyValue(
|
|
194
|
+
resolvedLayout.optJSONArray("hotspots") != null ? resolvedLayout.optJSONArray("hotspots") : new JSONArray()
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
.put("openUrl", record.has("openUrl") ? record.opt("openUrl") : JSONObject.NULL)
|
|
198
|
+
.put("status", record.optString("status"))
|
|
199
|
+
.put("revision", record.optInt("revision"))
|
|
200
|
+
.put("updatedAt", record.optLong("updatedAt"));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private static JSONObject resolveLayoutFrame(final JSONObject layout, final JSONObject record, final long nowMs) throws JSONException {
|
|
204
|
+
final JSONArray frames = TemplateJsonUtils.arrayOrEmpty(layout, "frames");
|
|
205
|
+
final String requestedFrameId = resolveFrameId(layout.optString("frameIdPath", null), record, nowMs, null, true);
|
|
206
|
+
final String defaultFrameId = layout.optString("defaultFrameId", null);
|
|
207
|
+
JSONObject selectedFrame = null;
|
|
208
|
+
|
|
209
|
+
for (int index = 0; index < frames.length(); index += 1) {
|
|
210
|
+
final JSONObject frame = frames.optJSONObject(index);
|
|
211
|
+
if (frame != null && frame.optString("id").equals(requestedFrameId)) {
|
|
212
|
+
selectedFrame = frame;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (selectedFrame == null && defaultFrameId != null) {
|
|
217
|
+
for (int index = 0; index < frames.length(); index += 1) {
|
|
218
|
+
final JSONObject frame = frames.optJSONObject(index);
|
|
219
|
+
if (frame != null && frame.optString("id").equals(defaultFrameId)) {
|
|
220
|
+
selectedFrame = frame;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
final boolean hasBaseSvg = layout.has("svg") && !layout.isNull("svg");
|
|
226
|
+
if (selectedFrame == null && !hasBaseSvg && frames.length() > 0) {
|
|
227
|
+
selectedFrame = frames.optJSONObject(0);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
final JSONObject resolved = new JSONObject()
|
|
231
|
+
.put("frameId", selectedFrame == null ? JSONObject.NULL : selectedFrame.optString("id"))
|
|
232
|
+
.put("svg", selectedFrame == null ? layout.optString("svg", "") : selectedFrame.optString("svg", layout.optString("svg", "")));
|
|
233
|
+
final JSONArray hotspots =
|
|
234
|
+
selectedFrame != null && selectedFrame.optJSONArray("hotspots") != null
|
|
235
|
+
? selectedFrame.optJSONArray("hotspots")
|
|
236
|
+
: layout.optJSONArray("hotspots");
|
|
237
|
+
resolved.put("hotspots", hotspots != null ? hotspots : new JSONArray());
|
|
238
|
+
return resolved;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static String createIdentifier(final String prefix) {
|
|
242
|
+
return prefix + "-" + UUID.randomUUID().toString().toLowerCase();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private static JSONObject reconcileTimerStates(final JSONObject record, final long nowMs) throws JSONException {
|
|
246
|
+
final JSONObject nextTimers = new JSONObject();
|
|
247
|
+
final JSONObject existingTimers = record.optJSONObject("timers") != null ? record.optJSONObject("timers") : new JSONObject();
|
|
248
|
+
final JSONArray timerDefinitions = TemplateJsonUtils.arrayOrEmpty(record.getJSONObject("definition"), "timers");
|
|
249
|
+
|
|
250
|
+
for (int index = 0; index < timerDefinitions.length(); index += 1) {
|
|
251
|
+
final JSONObject timerDefinition = timerDefinitions.optJSONObject(index);
|
|
252
|
+
if (timerDefinition == null) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
final String timerId = timerDefinition.optString("id", null);
|
|
257
|
+
if (timerId == null || timerId.isEmpty()) {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
final JSONObject previousTimer = existingTimers.optJSONObject(timerId);
|
|
262
|
+
final Long resolvedDuration = resolveTimerDuration(timerDefinition, record, nowMs);
|
|
263
|
+
final long durationMs =
|
|
264
|
+
resolvedDuration != null
|
|
265
|
+
? resolvedDuration
|
|
266
|
+
: TemplateJsonUtils.coerceLong(previousTimer != null ? previousTimer.opt("durationMs") : null) != null
|
|
267
|
+
? TemplateJsonUtils.coerceLong(previousTimer.opt("durationMs"))
|
|
268
|
+
: 0L;
|
|
269
|
+
final Long previousStartedAt = TemplateJsonUtils.coerceLong(previousTimer != null ? previousTimer.opt("startedAt") : null);
|
|
270
|
+
final String startAtPath = timerDefinition.optString("startAtPath", "");
|
|
271
|
+
final boolean shouldResolveStartAt = previousTimer == null || (previousStartedAt == null && !startAtPath.isEmpty());
|
|
272
|
+
final Long startedAt =
|
|
273
|
+
previousStartedAt != null
|
|
274
|
+
? previousStartedAt
|
|
275
|
+
: (shouldResolveStartAt ? resolveTimerStartAt(timerDefinition, record, nowMs) : null);
|
|
276
|
+
final String initialStatus =
|
|
277
|
+
previousTimer != null
|
|
278
|
+
? previousTimer.optString("status", startedAt == null ? "idle" : "running")
|
|
279
|
+
: (startedAt == null ? "idle" : "running");
|
|
280
|
+
final Long elapsedMs = TemplateJsonUtils.coerceLong(previousTimer != null ? previousTimer.opt("elapsedMs") : null);
|
|
281
|
+
|
|
282
|
+
final JSONObject timer = new JSONObject()
|
|
283
|
+
.put("id", timerId)
|
|
284
|
+
.put("startedAt", startedAt == null ? JSONObject.NULL : startedAt)
|
|
285
|
+
.put("elapsedMs", elapsedMs == null ? 0L : Math.max(0L, elapsedMs))
|
|
286
|
+
.put("durationMs", durationMs)
|
|
287
|
+
.put("status", initialStatus)
|
|
288
|
+
.put("updatedAt", nowMs);
|
|
289
|
+
timer.put("status", TemplateJsonUtils.timerStatus(timer, nowMs));
|
|
290
|
+
if ("finished".equals(timer.optString("status"))) {
|
|
291
|
+
timer.put("startedAt", JSONObject.NULL);
|
|
292
|
+
timer.put("elapsedMs", durationMs);
|
|
293
|
+
}
|
|
294
|
+
nextTimers.put(timerId, timer);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return nextTimers;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private static Long resolveTimerDuration(final JSONObject timerDefinition, final JSONObject record, final long nowMs)
|
|
301
|
+
throws JSONException {
|
|
302
|
+
final Long explicitDuration = TemplateJsonUtils.coerceLong(timerDefinition.opt("durationMs"));
|
|
303
|
+
if (explicitDuration != null) {
|
|
304
|
+
return Math.max(0L, explicitDuration);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
final String durationPath = timerDefinition.optString("durationPath", null);
|
|
308
|
+
if (durationPath != null && !durationPath.isEmpty()) {
|
|
309
|
+
final String resolvedPath = TemplateJsonUtils.resolveTemplateString(durationPath, record, nowMs, null);
|
|
310
|
+
final Long resolvedValue = TemplateJsonUtils.coerceLong(TemplateJsonUtils.resolveReference(resolvedPath, record, nowMs, null));
|
|
311
|
+
return Math.max(0L, resolvedValue != null ? resolvedValue : 0L);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private static Long resolveTimerStartAt(final JSONObject timerDefinition, final JSONObject record, final long nowMs)
|
|
318
|
+
throws JSONException {
|
|
319
|
+
final String startAtPath = timerDefinition.optString("startAtPath", null);
|
|
320
|
+
if (startAtPath != null && !startAtPath.isEmpty()) {
|
|
321
|
+
final String resolvedPath = TemplateJsonUtils.resolveTemplateString(startAtPath, record, nowMs, null);
|
|
322
|
+
final Long resolvedValue = TemplateJsonUtils.coerceLong(TemplateJsonUtils.resolveReference(resolvedPath, record, nowMs, null));
|
|
323
|
+
if (resolvedValue != null) {
|
|
324
|
+
return resolvedValue;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return timerDefinition.optBoolean("autoStart", false) ? nowMs : null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private static void applyStatePatch(final JSONObject activity, final JSONObject patch, final long nowMs, final JSONObject actionScope)
|
|
332
|
+
throws JSONException {
|
|
333
|
+
final String targetPath = TemplateJsonUtils.resolveRuntimePath(patch.optString("path", ""), activity, nowMs, actionScope);
|
|
334
|
+
if (targetPath.isEmpty()) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
final JSONObject state = activity.getJSONObject("state");
|
|
339
|
+
final String operation = patch.optString("op", "");
|
|
340
|
+
switch (operation) {
|
|
341
|
+
case "set":
|
|
342
|
+
case "timestamp":
|
|
343
|
+
TemplateJsonUtils.setValueAtPath(state, targetPath, resolvePatchValue(activity, patch, nowMs, actionScope));
|
|
344
|
+
break;
|
|
345
|
+
case "increment":
|
|
346
|
+
final Long currentValue = TemplateJsonUtils.coerceLong(TemplateJsonUtils.getValueAtPath(state, targetPath));
|
|
347
|
+
final long amount =
|
|
348
|
+
TemplateJsonUtils.coerceLong(patch.opt("amount")) != null ? TemplateJsonUtils.coerceLong(patch.opt("amount")) : 1L;
|
|
349
|
+
TemplateJsonUtils.setValueAtPath(state, targetPath, (currentValue != null ? currentValue : 0L) + amount);
|
|
350
|
+
break;
|
|
351
|
+
case "toggle":
|
|
352
|
+
final Object currentFlag = TemplateJsonUtils.getValueAtPath(state, targetPath);
|
|
353
|
+
final boolean nextValue = !(currentFlag instanceof Boolean && (Boolean) currentFlag);
|
|
354
|
+
TemplateJsonUtils.setValueAtPath(state, targetPath, nextValue);
|
|
355
|
+
break;
|
|
356
|
+
case "unset":
|
|
357
|
+
TemplateJsonUtils.deleteValueAtPath(state, targetPath);
|
|
358
|
+
break;
|
|
359
|
+
default:
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private static Object resolvePatchValue(
|
|
365
|
+
final JSONObject activity,
|
|
366
|
+
final JSONObject patch,
|
|
367
|
+
final long nowMs,
|
|
368
|
+
final JSONObject actionScope
|
|
369
|
+
) throws JSONException {
|
|
370
|
+
final String operation = patch.optString("op", "");
|
|
371
|
+
if ("timestamp".equals(operation)) {
|
|
372
|
+
return nowMs;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
final String valuePath = patch.optString("valuePath", null);
|
|
376
|
+
if (valuePath != null && !valuePath.isEmpty()) {
|
|
377
|
+
final String resolvedPath = TemplateJsonUtils.resolveTemplateString(valuePath, activity, nowMs, actionScope);
|
|
378
|
+
return TemplateJsonUtils.deepCopyValue(TemplateJsonUtils.resolveReference(resolvedPath, activity, nowMs, actionScope));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
final String valueTemplate = patch.optString("valueTemplate", null);
|
|
382
|
+
if (valueTemplate != null && !valueTemplate.isEmpty()) {
|
|
383
|
+
final Object exact = TemplateJsonUtils.exactTokenValue(valueTemplate, activity, nowMs, actionScope);
|
|
384
|
+
if (exact != null) {
|
|
385
|
+
return TemplateJsonUtils.deepCopyValue(exact);
|
|
386
|
+
}
|
|
387
|
+
return TemplateJsonUtils.resolveTemplateString(valueTemplate, activity, nowMs, actionScope);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (patch.has("value")) {
|
|
391
|
+
return TemplateJsonUtils.deepCopyValue(patch.opt("value"));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return JSONObject.NULL;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private static String resolveFrameId(
|
|
398
|
+
final String frameIdTemplate,
|
|
399
|
+
final JSONObject activity,
|
|
400
|
+
final long nowMs,
|
|
401
|
+
final JSONObject actionScope,
|
|
402
|
+
final boolean dereferenceReferences
|
|
403
|
+
) throws JSONException {
|
|
404
|
+
if (frameIdTemplate == null || frameIdTemplate.isEmpty()) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
final Object exact = TemplateJsonUtils.exactTokenValue(frameIdTemplate, activity, nowMs, actionScope);
|
|
409
|
+
if (exact != null) {
|
|
410
|
+
return TemplateJsonUtils.stringifyValue(exact);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
final String resolved = TemplateJsonUtils.resolveTemplateString(frameIdTemplate, activity, nowMs, actionScope);
|
|
414
|
+
if (dereferenceReferences) {
|
|
415
|
+
final Object referenced = TemplateJsonUtils.resolveReference(resolved, activity, nowMs, actionScope);
|
|
416
|
+
if (referenced != null) {
|
|
417
|
+
return TemplateJsonUtils.stringifyValue(referenced);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return resolved.isEmpty() ? null : resolved;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private static JSONArray frameIdsForMutation(final JSONObject activity, final JSONObject mutation) throws JSONException {
|
|
424
|
+
final JSONArray explicitFrameIds = mutation.optJSONArray("frameIds");
|
|
425
|
+
if (explicitFrameIds != null && explicitFrameIds.length() > 0) {
|
|
426
|
+
return explicitFrameIds;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
final String surface = mutation.optString("surface", null);
|
|
430
|
+
if (surface == null || surface.isEmpty()) {
|
|
431
|
+
return new JSONArray();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
final JSONObject layout = activity.getJSONObject("definition").getJSONObject("layouts").optJSONObject(surface);
|
|
435
|
+
final JSONArray frames = layout != null ? TemplateJsonUtils.arrayOrEmpty(layout, "frames") : new JSONArray();
|
|
436
|
+
final JSONArray frameIds = new JSONArray();
|
|
437
|
+
for (int index = 0; index < frames.length(); index += 1) {
|
|
438
|
+
final JSONObject frame = frames.optJSONObject(index);
|
|
439
|
+
if (frame != null && !frame.optString("id").isEmpty()) {
|
|
440
|
+
frameIds.put(frame.optString("id"));
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return frameIds;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private static int indexOfString(final JSONArray values, final String target) {
|
|
447
|
+
for (int index = 0; index < values.length(); index += 1) {
|
|
448
|
+
if (target.equals(values.optString(index))) {
|
|
449
|
+
return index;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return -1;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private static String normalizeFrameMutationId(final String frameId, final JSONArray frameIds) {
|
|
456
|
+
if (frameId == null || frameId.isEmpty()) {
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
return frameIds.length() == 0 || indexOfString(frameIds, frameId) >= 0 ? frameId : null;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private static void applyFrameMutation(
|
|
463
|
+
final JSONObject activity,
|
|
464
|
+
final JSONObject mutation,
|
|
465
|
+
final long nowMs,
|
|
466
|
+
final JSONObject actionScope
|
|
467
|
+
) throws JSONException {
|
|
468
|
+
final String targetPath = TemplateJsonUtils.resolveRuntimePath(mutation.optString("path", ""), activity, nowMs, actionScope);
|
|
469
|
+
if (targetPath.isEmpty()) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
final JSONArray frameIds = frameIdsForMutation(activity, mutation);
|
|
474
|
+
final String currentValue = TemplateJsonUtils.stringifyValue(
|
|
475
|
+
TemplateJsonUtils.getValueAtPath(activity.getJSONObject("state"), targetPath)
|
|
476
|
+
);
|
|
477
|
+
final int currentIndex = indexOfString(frameIds, currentValue);
|
|
478
|
+
final boolean wraps = !mutation.has("wrap") || mutation.optBoolean("wrap", true);
|
|
479
|
+
String nextFrameId = null;
|
|
480
|
+
|
|
481
|
+
final String operation = mutation.optString("op", "");
|
|
482
|
+
switch (operation) {
|
|
483
|
+
case "set":
|
|
484
|
+
nextFrameId = resolveFrameId(mutation.optString("frameId", null), activity, nowMs, actionScope, false);
|
|
485
|
+
break;
|
|
486
|
+
case "toggle": {
|
|
487
|
+
final String alternateFrameId = resolveFrameId(mutation.optString("frameId", null), activity, nowMs, actionScope, false);
|
|
488
|
+
if (frameIds.length() >= 2) {
|
|
489
|
+
nextFrameId = currentValue.equals(frameIds.optString(0)) ? frameIds.optString(1) : frameIds.optString(0);
|
|
490
|
+
} else if (alternateFrameId != null) {
|
|
491
|
+
nextFrameId = currentValue.equals(alternateFrameId) && frameIds.length() > 0 ? frameIds.optString(0) : alternateFrameId;
|
|
492
|
+
}
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
case "next": {
|
|
496
|
+
if (frameIds.length() == 0) {
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
final int nextIndex = currentIndex < 0 ? 0 : currentIndex + 1;
|
|
500
|
+
nextFrameId =
|
|
501
|
+
nextIndex < frameIds.length() ? frameIds.optString(nextIndex) : (wraps ? frameIds.optString(0) : currentValue);
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
case "previous": {
|
|
505
|
+
if (frameIds.length() == 0) {
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
final int nextIndex = currentIndex < 0 ? frameIds.length() - 1 : currentIndex - 1;
|
|
509
|
+
nextFrameId =
|
|
510
|
+
nextIndex >= 0 ? frameIds.optString(nextIndex) : (wraps ? frameIds.optString(frameIds.length() - 1) : currentValue);
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
default:
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
nextFrameId = normalizeFrameMutationId(nextFrameId, frameIds);
|
|
518
|
+
if (nextFrameId != null && !nextFrameId.isEmpty()) {
|
|
519
|
+
TemplateJsonUtils.setValueAtPath(activity.getJSONObject("state"), targetPath, nextFrameId);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
private static void pauseTimer(final JSONObject timer, final long nowMs) throws JSONException {
|
|
524
|
+
final Long durationMs = TemplateJsonUtils.coerceLong(timer.opt("durationMs"));
|
|
525
|
+
timer.put("elapsedMs", Math.min(durationMs != null ? durationMs : 0L, TemplateJsonUtils.timerElapsedMs(timer, nowMs)));
|
|
526
|
+
timer.put("startedAt", JSONObject.NULL);
|
|
527
|
+
timer.put("status", TemplateJsonUtils.timerStatus(timer, nowMs));
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
private static void resumeTimer(final JSONObject timer, final long nowMs) throws JSONException {
|
|
531
|
+
final String status = timer.optString("status");
|
|
532
|
+
if ("stopped".equals(status)) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (!"paused".equals(status)) {
|
|
536
|
+
timer.put("elapsedMs", 0L);
|
|
537
|
+
}
|
|
538
|
+
final Long durationMs = TemplateJsonUtils.coerceLong(timer.opt("durationMs"));
|
|
539
|
+
timer.put("startedAt", nowMs);
|
|
540
|
+
timer.put("status", durationMs != null && durationMs > 0L ? "running" : "idle");
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
private static void applyTimerMutation(
|
|
544
|
+
final JSONObject activity,
|
|
545
|
+
final JSONObject mutation,
|
|
546
|
+
final long nowMs,
|
|
547
|
+
final JSONObject actionScope
|
|
548
|
+
) throws JSONException {
|
|
549
|
+
final String timerId = mutation.optString("timerId", null);
|
|
550
|
+
if (timerId == null || timerId.isEmpty()) {
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
final JSONObject timers = activity.optJSONObject("timers") != null ? activity.optJSONObject("timers") : new JSONObject();
|
|
555
|
+
final JSONObject timer =
|
|
556
|
+
timers.optJSONObject(timerId) != null
|
|
557
|
+
? TemplateJsonUtils.deepCopyObject(timers.optJSONObject(timerId))
|
|
558
|
+
: new JSONObject()
|
|
559
|
+
.put("id", timerId)
|
|
560
|
+
.put("startedAt", JSONObject.NULL)
|
|
561
|
+
.put("elapsedMs", 0L)
|
|
562
|
+
.put("durationMs", 0L)
|
|
563
|
+
.put("status", "idle")
|
|
564
|
+
.put("updatedAt", nowMs);
|
|
565
|
+
|
|
566
|
+
final Long explicitDuration = TemplateJsonUtils.coerceLong(mutation.opt("durationMs"));
|
|
567
|
+
final Long pathDuration;
|
|
568
|
+
final String durationPath = mutation.optString("durationPath", null);
|
|
569
|
+
if (durationPath != null && !durationPath.isEmpty()) {
|
|
570
|
+
final String resolvedPath = TemplateJsonUtils.resolveTemplateString(durationPath, activity, nowMs, actionScope);
|
|
571
|
+
pathDuration = TemplateJsonUtils.coerceLong(TemplateJsonUtils.resolveReference(resolvedPath, activity, nowMs, actionScope));
|
|
572
|
+
} else {
|
|
573
|
+
pathDuration = null;
|
|
574
|
+
}
|
|
575
|
+
final JSONObject timerDefinition = findTimerDefinition(activity.getJSONObject("definition"), timerId);
|
|
576
|
+
final Long definitionDuration = timerDefinition != null ? resolveTimerDuration(timerDefinition, activity, nowMs) : null;
|
|
577
|
+
final Long existingDuration = TemplateJsonUtils.coerceLong(timer.opt("durationMs"));
|
|
578
|
+
final long durationMs =
|
|
579
|
+
explicitDuration != null
|
|
580
|
+
? explicitDuration
|
|
581
|
+
: pathDuration != null
|
|
582
|
+
? pathDuration
|
|
583
|
+
: definitionDuration != null
|
|
584
|
+
? definitionDuration
|
|
585
|
+
: existingDuration != null
|
|
586
|
+
? existingDuration
|
|
587
|
+
: 0L;
|
|
588
|
+
|
|
589
|
+
timer.put("durationMs", Math.max(0L, durationMs));
|
|
590
|
+
timer.put(
|
|
591
|
+
"elapsedMs",
|
|
592
|
+
Math.max(
|
|
593
|
+
0L,
|
|
594
|
+
TemplateJsonUtils.coerceLong(timer.opt("elapsedMs")) != null ? TemplateJsonUtils.coerceLong(timer.opt("elapsedMs")) : 0L
|
|
595
|
+
)
|
|
596
|
+
);
|
|
597
|
+
timer.put("updatedAt", nowMs);
|
|
598
|
+
|
|
599
|
+
final String operation = mutation.optString("op", "");
|
|
600
|
+
switch (operation) {
|
|
601
|
+
case "start":
|
|
602
|
+
case "restart":
|
|
603
|
+
timer.put("elapsedMs", 0L);
|
|
604
|
+
timer.put("startedAt", nowMs);
|
|
605
|
+
timer.put("status", durationMs > 0 ? "running" : "idle");
|
|
606
|
+
break;
|
|
607
|
+
case "pause":
|
|
608
|
+
pauseTimer(timer, nowMs);
|
|
609
|
+
break;
|
|
610
|
+
case "resume":
|
|
611
|
+
resumeTimer(timer, nowMs);
|
|
612
|
+
break;
|
|
613
|
+
case "toggle":
|
|
614
|
+
if ("running".equals(TemplateJsonUtils.timerStatus(timer, nowMs))) {
|
|
615
|
+
pauseTimer(timer, nowMs);
|
|
616
|
+
} else {
|
|
617
|
+
resumeTimer(timer, nowMs);
|
|
618
|
+
}
|
|
619
|
+
break;
|
|
620
|
+
case "stop":
|
|
621
|
+
timer.put("elapsedMs", 0L);
|
|
622
|
+
timer.put("startedAt", JSONObject.NULL);
|
|
623
|
+
timer.put("status", "stopped");
|
|
624
|
+
break;
|
|
625
|
+
case "reset":
|
|
626
|
+
timer.put("elapsedMs", 0L);
|
|
627
|
+
timer.put("startedAt", JSONObject.NULL);
|
|
628
|
+
timer.put("status", "idle");
|
|
629
|
+
break;
|
|
630
|
+
case "setDuration":
|
|
631
|
+
timer.put("status", TemplateJsonUtils.timerStatus(timer, nowMs));
|
|
632
|
+
break;
|
|
633
|
+
default:
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
timer.put("status", TemplateJsonUtils.timerStatus(timer, nowMs));
|
|
638
|
+
if ("finished".equals(timer.optString("status"))) {
|
|
639
|
+
timer.put("elapsedMs", timer.optLong("durationMs"));
|
|
640
|
+
timer.put("startedAt", JSONObject.NULL);
|
|
641
|
+
}
|
|
642
|
+
timers.put(timerId, timer);
|
|
643
|
+
activity.put("timers", timers);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
private static JSONObject findActionDefinition(final JSONObject definition, final String actionId) {
|
|
647
|
+
final JSONArray actions = TemplateJsonUtils.arrayOrEmpty(definition, "actions");
|
|
648
|
+
for (int index = 0; index < actions.length(); index += 1) {
|
|
649
|
+
final JSONObject action = actions.optJSONObject(index);
|
|
650
|
+
if (action != null && actionId.equals(action.optString("id"))) {
|
|
651
|
+
return action;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return null;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
private static JSONObject findTimerDefinition(final JSONObject definition, final String timerId) {
|
|
658
|
+
final JSONArray timers = TemplateJsonUtils.arrayOrEmpty(definition, "timers");
|
|
659
|
+
for (int index = 0; index < timers.length(); index += 1) {
|
|
660
|
+
final JSONObject timer = timers.optJSONObject(index);
|
|
661
|
+
if (timer != null && timerId.equals(timer.optString("id"))) {
|
|
662
|
+
return timer;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private static boolean containsString(final JSONArray values, final String target) {
|
|
669
|
+
for (int index = 0; index < values.length(); index += 1) {
|
|
670
|
+
if (target.equals(values.optString(index))) {
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
File without changes
|