@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,18 @@
|
|
|
1
|
+
package app.capgo.widgetkit;
|
|
2
|
+
|
|
3
|
+
public final class CapgoWidgetKitConstants {
|
|
4
|
+
|
|
5
|
+
public static final String PREFS_NAME = "capgo_widget_kit_templates_v1";
|
|
6
|
+
public static final String ACTION_TEMPLATE_STORE_CHANGED = "app.capgo.widgetkit.ACTION_TEMPLATE_STORE_CHANGED";
|
|
7
|
+
public static final String ACTION_NATIVE_WIDGET_BRIDGE_CHANGED = "app.capgo.widgetkit.ACTION_NATIVE_WIDGET_BRIDGE_CHANGED";
|
|
8
|
+
public static final String ACTION_PERFORM_TEMPLATE_ACTION = "app.capgo.widgetkit.ACTION_PERFORM_TEMPLATE_ACTION";
|
|
9
|
+
public static final String EXTRA_ACTIVITY_ID = "activityId";
|
|
10
|
+
public static final String EXTRA_ACTION_ID = "actionId";
|
|
11
|
+
public static final String EXTRA_SOURCE_ID = "sourceId";
|
|
12
|
+
public static final String EXTRA_PAYLOAD_JSON = "payloadJson";
|
|
13
|
+
public static final String EXTRA_WIDGET_ID = "widgetId";
|
|
14
|
+
public static final String EXTRA_MESSAGE_ID = "messageId";
|
|
15
|
+
public static final String ACTIVITY_IDS_KEY = "activityIds";
|
|
16
|
+
|
|
17
|
+
private CapgoWidgetKitConstants() {}
|
|
18
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
package app.capgo.widgetkit;
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.JSArray;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import com.getcapacitor.Plugin;
|
|
6
|
+
import com.getcapacitor.PluginCall;
|
|
7
|
+
import com.getcapacitor.PluginMethod;
|
|
8
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
9
|
+
import org.json.JSONArray;
|
|
10
|
+
import org.json.JSONException;
|
|
11
|
+
import org.json.JSONObject;
|
|
12
|
+
|
|
13
|
+
@CapacitorPlugin(name = "CapgoWidgetKit")
|
|
14
|
+
public class CapgoWidgetKitPlugin extends Plugin {
|
|
15
|
+
|
|
16
|
+
private CapgoWidgetKit implementation;
|
|
17
|
+
|
|
18
|
+
@Override
|
|
19
|
+
public void load() {
|
|
20
|
+
super.load();
|
|
21
|
+
implementation = new CapgoWidgetKit(getContext());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@PluginMethod
|
|
25
|
+
public void areActivitiesSupported(final PluginCall call) {
|
|
26
|
+
try {
|
|
27
|
+
call.resolve(toJsObject(implementation.areActivitiesSupported()));
|
|
28
|
+
} catch (JSONException exception) {
|
|
29
|
+
call.reject(exception.getMessage(), exception);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@PluginMethod
|
|
34
|
+
public void startTemplateActivity(final PluginCall call) {
|
|
35
|
+
final JSObject definition = call.getObject("definition");
|
|
36
|
+
final JSObject state = call.getObject("state");
|
|
37
|
+
|
|
38
|
+
if (definition == null) {
|
|
39
|
+
call.reject("The `definition` object is required.");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (state == null) {
|
|
43
|
+
call.reject("The `state` object is required.");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
final JSONObject activity = implementation.startTemplateActivity(
|
|
49
|
+
call.getString("activityId"),
|
|
50
|
+
definition,
|
|
51
|
+
state,
|
|
52
|
+
call.getString("openUrl")
|
|
53
|
+
);
|
|
54
|
+
final JSObject result = new JSObject();
|
|
55
|
+
result.put("activity", toJsObject(activity));
|
|
56
|
+
call.resolve(result);
|
|
57
|
+
} catch (JSONException exception) {
|
|
58
|
+
call.reject(exception.getMessage(), exception);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@PluginMethod
|
|
63
|
+
public void updateTemplateActivity(final PluginCall call) {
|
|
64
|
+
final String activityId = call.getString("activityId");
|
|
65
|
+
if (activityId == null) {
|
|
66
|
+
call.reject("The `activityId` is required.");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
final JSONObject activity = implementation.updateTemplateActivity(
|
|
72
|
+
activityId,
|
|
73
|
+
call.getObject("definition"),
|
|
74
|
+
call.getObject("state"),
|
|
75
|
+
call.getString("openUrl")
|
|
76
|
+
);
|
|
77
|
+
final JSObject result = new JSObject();
|
|
78
|
+
result.put("activity", activity == null ? JSObject.NULL : toJsObject(activity));
|
|
79
|
+
call.resolve(result);
|
|
80
|
+
} catch (JSONException exception) {
|
|
81
|
+
call.reject(exception.getMessage(), exception);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@PluginMethod
|
|
86
|
+
public void endTemplateActivity(final PluginCall call) {
|
|
87
|
+
final String activityId = call.getString("activityId");
|
|
88
|
+
if (activityId == null) {
|
|
89
|
+
call.reject("The `activityId` is required.");
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
implementation.endTemplateActivity(activityId, call.getObject("state"));
|
|
95
|
+
call.resolve();
|
|
96
|
+
} catch (JSONException exception) {
|
|
97
|
+
call.reject(exception.getMessage(), exception);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@PluginMethod
|
|
102
|
+
public void performTemplateAction(final PluginCall call) {
|
|
103
|
+
final String activityId = call.getString("activityId");
|
|
104
|
+
final String actionId = call.getString("actionId");
|
|
105
|
+
if (activityId == null) {
|
|
106
|
+
call.reject("The `activityId` is required.");
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (actionId == null) {
|
|
110
|
+
call.reject("The `actionId` is required.");
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
final TemplateRuntime.ActionResult result = implementation.performTemplateAction(
|
|
116
|
+
activityId,
|
|
117
|
+
actionId,
|
|
118
|
+
call.getString("sourceId"),
|
|
119
|
+
call.getObject("payload")
|
|
120
|
+
);
|
|
121
|
+
final JSObject payload = new JSObject();
|
|
122
|
+
payload.put("activity", toJsObject(result.activity));
|
|
123
|
+
payload.put("event", toJsObject(result.event));
|
|
124
|
+
call.resolve(payload);
|
|
125
|
+
} catch (JSONException exception) {
|
|
126
|
+
call.reject(exception.getMessage(), exception);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@PluginMethod
|
|
131
|
+
public void getTemplateActivity(final PluginCall call) {
|
|
132
|
+
final String activityId = call.getString("activityId");
|
|
133
|
+
if (activityId == null) {
|
|
134
|
+
call.reject("The `activityId` is required.");
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
final JSONObject activity = implementation.getTemplateActivity(activityId);
|
|
140
|
+
final JSObject result = new JSObject();
|
|
141
|
+
result.put("activity", activity == null ? JSObject.NULL : toJsObject(activity));
|
|
142
|
+
call.resolve(result);
|
|
143
|
+
} catch (JSONException exception) {
|
|
144
|
+
call.reject(exception.getMessage(), exception);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
@PluginMethod
|
|
149
|
+
public void listTemplateActivities(final PluginCall call) {
|
|
150
|
+
try {
|
|
151
|
+
final JSObject result = new JSObject();
|
|
152
|
+
result.put("activities", toJsArray(implementation.listTemplateActivities()));
|
|
153
|
+
call.resolve(result);
|
|
154
|
+
} catch (JSONException exception) {
|
|
155
|
+
call.reject(exception.getMessage(), exception);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@PluginMethod
|
|
160
|
+
public void listTemplateEvents(final PluginCall call) {
|
|
161
|
+
try {
|
|
162
|
+
final JSObject result = new JSObject();
|
|
163
|
+
result.put(
|
|
164
|
+
"events",
|
|
165
|
+
toJsArray(implementation.listTemplateEvents(call.getString("activityId"), call.getBoolean("unacknowledgedOnly", false)))
|
|
166
|
+
);
|
|
167
|
+
call.resolve(result);
|
|
168
|
+
} catch (JSONException exception) {
|
|
169
|
+
call.reject(exception.getMessage(), exception);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@PluginMethod
|
|
174
|
+
public void acknowledgeTemplateEvents(final PluginCall call) {
|
|
175
|
+
try {
|
|
176
|
+
implementation.acknowledgeTemplateEvents(call.getString("activityId"), toJsonArray(call.getArray("eventIds")));
|
|
177
|
+
call.resolve();
|
|
178
|
+
} catch (JSONException exception) {
|
|
179
|
+
call.reject(exception.getMessage(), exception);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
@PluginMethod
|
|
184
|
+
public void startWidgetSession(final PluginCall call) {
|
|
185
|
+
try {
|
|
186
|
+
final JSONObject session = implementation.startWidgetSession(
|
|
187
|
+
call.getString("widgetId"),
|
|
188
|
+
call.getString("kind"),
|
|
189
|
+
call.getObject("state"),
|
|
190
|
+
call.getObject("metadata")
|
|
191
|
+
);
|
|
192
|
+
final JSObject result = new JSObject();
|
|
193
|
+
result.put("session", toJsObject(session));
|
|
194
|
+
call.resolve(result);
|
|
195
|
+
} catch (JSONException exception) {
|
|
196
|
+
call.reject(exception.getMessage(), exception);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
@PluginMethod
|
|
201
|
+
public void updateWidgetSession(final PluginCall call) {
|
|
202
|
+
final String widgetId = call.getString("widgetId");
|
|
203
|
+
if (widgetId == null) {
|
|
204
|
+
call.reject("The `widgetId` is required.");
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
final JSONObject session = implementation.updateWidgetSession(
|
|
210
|
+
widgetId,
|
|
211
|
+
call.getObject("state"),
|
|
212
|
+
call.getObject("metadata"),
|
|
213
|
+
call.getBoolean("merge", false)
|
|
214
|
+
);
|
|
215
|
+
final JSObject result = new JSObject();
|
|
216
|
+
result.put("session", session == null ? JSObject.NULL : toJsObject(session));
|
|
217
|
+
call.resolve(result);
|
|
218
|
+
} catch (JSONException exception) {
|
|
219
|
+
call.reject(exception.getMessage(), exception);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
@PluginMethod
|
|
224
|
+
public void stopWidgetSession(final PluginCall call) {
|
|
225
|
+
final String widgetId = call.getString("widgetId");
|
|
226
|
+
if (widgetId == null) {
|
|
227
|
+
call.reject("The `widgetId` is required.");
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
implementation.stopWidgetSession(widgetId, call.getObject("state"));
|
|
233
|
+
call.resolve();
|
|
234
|
+
} catch (JSONException exception) {
|
|
235
|
+
call.reject(exception.getMessage(), exception);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
@PluginMethod
|
|
240
|
+
public void getWidgetSession(final PluginCall call) {
|
|
241
|
+
final String widgetId = call.getString("widgetId");
|
|
242
|
+
if (widgetId == null) {
|
|
243
|
+
call.reject("The `widgetId` is required.");
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
final JSONObject session = implementation.getWidgetSession(widgetId);
|
|
249
|
+
final JSObject result = new JSObject();
|
|
250
|
+
result.put("session", session == null ? JSObject.NULL : toJsObject(session));
|
|
251
|
+
call.resolve(result);
|
|
252
|
+
} catch (JSONException exception) {
|
|
253
|
+
call.reject(exception.getMessage(), exception);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
@PluginMethod
|
|
258
|
+
public void listWidgetSessions(final PluginCall call) {
|
|
259
|
+
try {
|
|
260
|
+
final JSObject result = new JSObject();
|
|
261
|
+
result.put("sessions", toJsArray(implementation.listWidgetSessions()));
|
|
262
|
+
call.resolve(result);
|
|
263
|
+
} catch (JSONException exception) {
|
|
264
|
+
call.reject(exception.getMessage(), exception);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
@PluginMethod
|
|
269
|
+
public void sendWidgetMessage(final PluginCall call) {
|
|
270
|
+
final String widgetId = call.getString("widgetId");
|
|
271
|
+
final String name = call.getString("name");
|
|
272
|
+
if (widgetId == null) {
|
|
273
|
+
call.reject("The `widgetId` is required.");
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (name == null) {
|
|
277
|
+
call.reject("The `name` is required.");
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
final JSONObject message = implementation.sendWidgetMessage(
|
|
283
|
+
widgetId,
|
|
284
|
+
call.getString("direction"),
|
|
285
|
+
name,
|
|
286
|
+
call.getObject("payload"),
|
|
287
|
+
call.getBoolean("expectsResponse", false)
|
|
288
|
+
);
|
|
289
|
+
final JSObject result = new JSObject();
|
|
290
|
+
result.put("message", toJsObject(message));
|
|
291
|
+
call.resolve(result);
|
|
292
|
+
} catch (JSONException exception) {
|
|
293
|
+
call.reject(exception.getMessage(), exception);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
@PluginMethod
|
|
298
|
+
public void listWidgetMessages(final PluginCall call) {
|
|
299
|
+
try {
|
|
300
|
+
final JSObject result = new JSObject();
|
|
301
|
+
result.put(
|
|
302
|
+
"messages",
|
|
303
|
+
toJsArray(
|
|
304
|
+
implementation.listWidgetMessages(
|
|
305
|
+
call.getString("widgetId"),
|
|
306
|
+
call.getString("direction"),
|
|
307
|
+
call.getBoolean("unacknowledgedOnly", false),
|
|
308
|
+
call.getBoolean("pendingOnly", false)
|
|
309
|
+
)
|
|
310
|
+
)
|
|
311
|
+
);
|
|
312
|
+
call.resolve(result);
|
|
313
|
+
} catch (JSONException exception) {
|
|
314
|
+
call.reject(exception.getMessage(), exception);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
@PluginMethod
|
|
319
|
+
public void acknowledgeWidgetMessages(final PluginCall call) {
|
|
320
|
+
try {
|
|
321
|
+
implementation.acknowledgeWidgetMessages(
|
|
322
|
+
toJsonArray(call.getArray("messageIds")),
|
|
323
|
+
call.getString("widgetId"),
|
|
324
|
+
call.getString("direction")
|
|
325
|
+
);
|
|
326
|
+
call.resolve();
|
|
327
|
+
} catch (JSONException exception) {
|
|
328
|
+
call.reject(exception.getMessage(), exception);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
@PluginMethod
|
|
333
|
+
public void completeWidgetMessage(final PluginCall call) {
|
|
334
|
+
final String messageId = call.getString("messageId");
|
|
335
|
+
if (messageId == null) {
|
|
336
|
+
call.reject("The `messageId` is required.");
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
try {
|
|
341
|
+
final JSONObject message = implementation.completeWidgetMessage(messageId, call.getObject("response"), call.getString("error"));
|
|
342
|
+
final JSObject result = new JSObject();
|
|
343
|
+
result.put("message", message == null ? JSObject.NULL : toJsObject(message));
|
|
344
|
+
call.resolve(result);
|
|
345
|
+
} catch (JSONException exception) {
|
|
346
|
+
call.reject(exception.getMessage(), exception);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
@PluginMethod
|
|
351
|
+
public void getPluginVersion(final PluginCall call) {
|
|
352
|
+
try {
|
|
353
|
+
call.resolve(toJsObject(implementation.getPluginVersion()));
|
|
354
|
+
} catch (JSONException exception) {
|
|
355
|
+
call.reject(exception.getMessage(), exception);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
private JSObject toJsObject(final JSONObject object) throws JSONException {
|
|
360
|
+
return new JSObject(object.toString());
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private JSArray toJsArray(final JSONArray array) throws JSONException {
|
|
364
|
+
return new JSArray(array.toString());
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
private JSONArray toJsonArray(final JSArray array) throws JSONException {
|
|
368
|
+
return array == null ? null : new JSONArray(array.toString());
|
|
369
|
+
}
|
|
370
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
package app.capgo.widgetkit;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.content.SharedPreferences;
|
|
5
|
+
import java.util.ArrayList;
|
|
6
|
+
import java.util.Collections;
|
|
7
|
+
import java.util.List;
|
|
8
|
+
import org.json.JSONArray;
|
|
9
|
+
import org.json.JSONException;
|
|
10
|
+
import org.json.JSONObject;
|
|
11
|
+
|
|
12
|
+
final class TemplateActivityStore {
|
|
13
|
+
|
|
14
|
+
private final SharedPreferences preferences;
|
|
15
|
+
|
|
16
|
+
TemplateActivityStore(final Context context) {
|
|
17
|
+
preferences = context.getApplicationContext().getSharedPreferences(CapgoWidgetKitConstants.PREFS_NAME, Context.MODE_PRIVATE);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
JSONObject loadActivity(final String activityId) throws JSONException {
|
|
21
|
+
final String raw = preferences.getString(activityKey(activityId), null);
|
|
22
|
+
return raw == null ? null : new JSONObject(raw);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void saveActivity(final JSONObject activity) {
|
|
26
|
+
final String activityId = activity.optString("activityId");
|
|
27
|
+
preferences.edit().putString(activityKey(activityId), activity.toString()).apply();
|
|
28
|
+
|
|
29
|
+
final List<String> ids = listActivityIds();
|
|
30
|
+
if (!ids.contains(activityId)) {
|
|
31
|
+
ids.add(activityId);
|
|
32
|
+
saveActivityIds(ids);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
void deleteActivity(final String activityId) {
|
|
37
|
+
preferences.edit().remove(activityKey(activityId)).remove(eventsKey(activityId)).apply();
|
|
38
|
+
final List<String> ids = listActivityIds();
|
|
39
|
+
ids.remove(activityId);
|
|
40
|
+
saveActivityIds(ids);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
JSONArray listActivities() throws JSONException {
|
|
44
|
+
final JSONArray activities = new JSONArray();
|
|
45
|
+
final List<JSONObject> records = new ArrayList<>();
|
|
46
|
+
for (String activityId : listActivityIds()) {
|
|
47
|
+
final JSONObject record = loadActivity(activityId);
|
|
48
|
+
if (record != null) {
|
|
49
|
+
records.add(record);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
records.sort((left, right) -> Long.compare(right.optLong("updatedAt"), left.optLong("updatedAt")));
|
|
54
|
+
for (JSONObject record : records) {
|
|
55
|
+
activities.put(record);
|
|
56
|
+
}
|
|
57
|
+
return activities;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
void appendEvent(final JSONObject event) throws JSONException {
|
|
61
|
+
final String activityId = event.optString("activityId");
|
|
62
|
+
final JSONArray existing = loadEvents(activityId);
|
|
63
|
+
final JSONArray updated = new JSONArray();
|
|
64
|
+
updated.put(event);
|
|
65
|
+
for (int index = 0; index < existing.length(); index += 1) {
|
|
66
|
+
updated.put(existing.opt(index));
|
|
67
|
+
}
|
|
68
|
+
preferences.edit().putString(eventsKey(activityId), updated.toString()).apply();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
JSONArray loadEvents(final String activityId) throws JSONException {
|
|
72
|
+
final String raw = preferences.getString(eventsKey(activityId), null);
|
|
73
|
+
return raw == null ? new JSONArray() : new JSONArray(raw);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
JSONArray loadEvents(final String activityId, final boolean unacknowledgedOnly) throws JSONException {
|
|
77
|
+
if (activityId != null) {
|
|
78
|
+
return filterEvents(loadEvents(activityId), unacknowledgedOnly);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
final JSONArray events = new JSONArray();
|
|
82
|
+
final List<JSONObject> collected = new ArrayList<>();
|
|
83
|
+
for (String currentActivityId : listActivityIds()) {
|
|
84
|
+
final JSONArray activityEvents = loadEvents(currentActivityId);
|
|
85
|
+
for (int index = 0; index < activityEvents.length(); index += 1) {
|
|
86
|
+
final JSONObject event = activityEvents.optJSONObject(index);
|
|
87
|
+
if (event != null && (!unacknowledgedOnly || event.isNull("acknowledgedAt"))) {
|
|
88
|
+
collected.add(event);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
collected.sort((left, right) -> Long.compare(right.optLong("createdAt"), left.optLong("createdAt")));
|
|
93
|
+
for (JSONObject event : collected) {
|
|
94
|
+
events.put(event);
|
|
95
|
+
}
|
|
96
|
+
return events;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
void acknowledgeEvents(final String activityId, final JSONArray eventIds, final long acknowledgedAt) throws JSONException {
|
|
100
|
+
if (activityId != null) {
|
|
101
|
+
final JSONArray updated = TemplateRuntime.acknowledgeEvents(loadEvents(activityId), activityId, eventIds, acknowledgedAt);
|
|
102
|
+
preferences.edit().putString(eventsKey(activityId), updated.toString()).apply();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (eventIds == null || eventIds.length() == 0) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (String currentActivityId : listActivityIds()) {
|
|
111
|
+
final JSONArray updated = TemplateRuntime.acknowledgeEvents(loadEvents(currentActivityId), null, eventIds, acknowledgedAt);
|
|
112
|
+
preferences.edit().putString(eventsKey(currentActivityId), updated.toString()).apply();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private JSONArray filterEvents(final JSONArray events, final boolean unacknowledgedOnly) {
|
|
117
|
+
if (!unacknowledgedOnly) {
|
|
118
|
+
return events;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
final JSONArray filtered = new JSONArray();
|
|
122
|
+
for (int index = 0; index < events.length(); index += 1) {
|
|
123
|
+
final JSONObject event = events.optJSONObject(index);
|
|
124
|
+
if (event != null && event.isNull("acknowledgedAt")) {
|
|
125
|
+
filtered.put(event);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return filtered;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private List<String> listActivityIds() {
|
|
132
|
+
final String raw = preferences.getString(CapgoWidgetKitConstants.ACTIVITY_IDS_KEY, null);
|
|
133
|
+
if (raw == null) {
|
|
134
|
+
return new ArrayList<>();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
final JSONArray ids = new JSONArray(raw);
|
|
139
|
+
final List<String> values = new ArrayList<>();
|
|
140
|
+
for (int index = 0; index < ids.length(); index += 1) {
|
|
141
|
+
final String value = ids.optString(index, null);
|
|
142
|
+
if (value != null && !value.isEmpty()) {
|
|
143
|
+
values.add(value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return values;
|
|
147
|
+
} catch (JSONException ignored) {
|
|
148
|
+
return new ArrayList<>();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private void saveActivityIds(final List<String> ids) {
|
|
153
|
+
final JSONArray values = new JSONArray();
|
|
154
|
+
for (String id : ids) {
|
|
155
|
+
values.put(id);
|
|
156
|
+
}
|
|
157
|
+
preferences.edit().putString(CapgoWidgetKitConstants.ACTIVITY_IDS_KEY, values.toString()).apply();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private String activityKey(final String activityId) {
|
|
161
|
+
return "activity:" + activityId;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private String eventsKey(final String activityId) {
|
|
165
|
+
return "events:" + activityId;
|
|
166
|
+
}
|
|
167
|
+
}
|