react-native-live-widgets 1.2.1 → 1.2.2
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/CHANGELOG.md +27 -0
- package/README.md +89 -49
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/assets/fonts/MaterialIcons-Regular.ttf +0 -0
- package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +61 -1
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +153 -17
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +1 -1
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +86 -13
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +53 -0
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +647 -104
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +3 -1
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicQuickSettingsTileService.kt +120 -0
- package/app.plugin.js +36 -0
- package/assets/MaterialIcons.json +2236 -0
- package/cli/codegen.js +789 -0
- package/cli/setup.js +323 -63
- package/docs/EXPO-GUIDE-EN.md +21 -3
- package/docs/EXPO-GUIDE-VI.md +21 -3
- package/docs/GUIDE-EN.md +203 -21
- package/docs/GUIDE-VI.md +207 -22
- package/docs/index.html +1328 -211
- package/docs/llms-full.txt +15 -0
- package/docs/llms.txt +16 -7
- package/ios/Bridge/LiveActivityModule.swift +333 -44
- package/ios/Intents/DynamicWidgetActionIntent.swift +57 -2
- package/ios/Models/DynamicLayoutModels.swift +114 -4
- package/ios/Renderer/DynamicNodeView.swift +15 -6
- package/ios/Storage/DynamicWidgetStateStore.swift +649 -38
- package/ios/Widgets/DynamicControlWidget.swift +63 -0
- package/ios/Widgets/DynamicHomeScreenWidget.swift +99 -26
- package/ios/Widgets/DynamicLiveActivityWidget.swift +25 -4
- package/lib/builder.d.ts +50 -7
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +326 -18
- package/lib/builder.js.map +1 -1
- package/lib/hooks.d.ts +13 -9
- package/lib/hooks.d.ts.map +1 -1
- package/lib/hooks.js +70 -12
- package/lib/hooks.js.map +1 -1
- package/lib/index.d.ts +80 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +303 -59
- package/lib/index.js.map +1 -1
- package/lib/ir.d.ts.map +1 -1
- package/lib/ir.js +9 -2
- package/lib/ir.js.map +1 -1
- package/lib/jsx.d.ts +11 -7
- package/lib/jsx.d.ts.map +1 -1
- package/lib/jsx.js +50 -13
- package/lib/jsx.js.map +1 -1
- package/lib/material-icons.generated.d.ts +8 -0
- package/lib/material-icons.generated.d.ts.map +1 -0
- package/lib/material-icons.generated.js +9 -0
- package/lib/material-icons.generated.js.map +1 -0
- package/lib/preview.d.ts +11 -4
- package/lib/preview.d.ts.map +1 -1
- package/lib/preview.js +150 -82
- package/lib/preview.js.map +1 -1
- package/lib/sf-symbols.generated.d.ts +8 -0
- package/lib/sf-symbols.generated.d.ts.map +1 -0
- package/lib/sf-symbols.generated.js +9 -0
- package/lib/sf-symbols.generated.js.map +1 -0
- package/lib/types.d.ts +302 -18
- package/lib/types.d.ts.map +1 -1
- package/package.json +6 -2
- package/scripts/build.js +98 -0
- package/scripts/generate-material-icons.js +93 -0
- package/scripts/generate-sf-symbols.js +95 -0
- package/scripts/release.js +115 -0
- package/scripts/test-ir-semantic.js +314 -0
- package/src/builder.ts +382 -24
- package/src/hooks.ts +88 -12
- package/src/index.ts +319 -56
- package/src/ir.ts +8 -2
- package/src/jsx.tsx +69 -21
- package/src/material-icons.generated.ts +3844 -0
- package/src/preview.tsx +235 -81
- package/src/sf-symbols.generated.ts +9533 -0
- package/src/types.ts +348 -18
package/src/index.ts
CHANGED
|
@@ -2,8 +2,10 @@ import React from 'react';
|
|
|
2
2
|
import { AppRegistry, NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
|
3
3
|
import {
|
|
4
4
|
ActiveActivityInfo,
|
|
5
|
+
ControlWidgetConfig,
|
|
5
6
|
EndActivityOptions,
|
|
6
7
|
LiveActivityPushTokens,
|
|
8
|
+
LiveActivityStateEvent,
|
|
7
9
|
StartActivityOptions,
|
|
8
10
|
UpdateActivityOptions,
|
|
9
11
|
WidgetCapabilities,
|
|
@@ -14,6 +16,7 @@ import {
|
|
|
14
16
|
WidgetIR,
|
|
15
17
|
} from './types';
|
|
16
18
|
import { compileToIR, inspectIR } from './ir';
|
|
19
|
+
import { UI, createDataBinding, flattenDataObject, resolveTemplateString } from './builder';
|
|
17
20
|
|
|
18
21
|
declare const __DEV__: boolean | undefined;
|
|
19
22
|
|
|
@@ -34,6 +37,7 @@ const DEVICE_TOKEN_CHANGED_EVENT = 'DevicePushTokenChanged';
|
|
|
34
37
|
const TOKEN_CHANGED_EVENT = 'LiveActivityTokenChanged';
|
|
35
38
|
const UPDATE_TOKEN_CHANGED_EVENT = 'LiveActivityUpdateTokenChanged';
|
|
36
39
|
const ACTION_CLICKED_EVENT = 'LiveActivityActionClicked';
|
|
40
|
+
const STATE_CHANGED_EVENT = 'LiveActivityStateChanged';
|
|
37
41
|
|
|
38
42
|
import {
|
|
39
43
|
ActionHandler,
|
|
@@ -63,17 +67,18 @@ if (liveActivityEmitter) {
|
|
|
63
67
|
liveActivityEmitter.addListener(ACTION_CLICKED_EVENT, async (event: any) => {
|
|
64
68
|
const actionId = event?.actionId || '';
|
|
65
69
|
const activityId = event?.activityId || '';
|
|
70
|
+
const payload = { ...(event || {}), actionId, activityId };
|
|
66
71
|
const handler = actionHandlers.get(actionId);
|
|
67
72
|
if (handler) {
|
|
68
73
|
try {
|
|
69
|
-
await handler(
|
|
74
|
+
await handler(payload);
|
|
70
75
|
} catch (e) {
|
|
71
76
|
console.warn(`[react-native-live-widgets] Error executing action '${actionId}':`, e);
|
|
72
77
|
}
|
|
73
78
|
}
|
|
74
79
|
for (const globalHandler of globalActionHandlers) {
|
|
75
80
|
try {
|
|
76
|
-
await globalHandler(actionId,
|
|
81
|
+
await globalHandler(actionId, payload);
|
|
77
82
|
} catch (e) {
|
|
78
83
|
console.warn(`[react-native-live-widgets] Error in global action handler for '${actionId}':`, e);
|
|
79
84
|
}
|
|
@@ -81,6 +86,106 @@ if (liveActivityEmitter) {
|
|
|
81
86
|
});
|
|
82
87
|
}
|
|
83
88
|
|
|
89
|
+
function convertIfJsx(node: any): any {
|
|
90
|
+
if (!node) return node;
|
|
91
|
+
if (React.isValidElement(node) || (node as any)?.$$typeof || typeof (node as any)?.type === 'function') {
|
|
92
|
+
return renderToWidget(node);
|
|
93
|
+
}
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function normalizeDynamicIsland(island: any): any {
|
|
98
|
+
if (!island || typeof island !== 'object') return island;
|
|
99
|
+
const normalized: any = { ...island };
|
|
100
|
+
if (normalized.compactLeading) normalized.compactLeading = convertIfJsx(normalized.compactLeading);
|
|
101
|
+
if (normalized.compactTrailing) normalized.compactTrailing = convertIfJsx(normalized.compactTrailing);
|
|
102
|
+
if (normalized.minimal) normalized.minimal = convertIfJsx(normalized.minimal);
|
|
103
|
+
if (normalized.expanded && typeof normalized.expanded === 'object') {
|
|
104
|
+
normalized.expanded = {
|
|
105
|
+
...normalized.expanded,
|
|
106
|
+
leading: convertIfJsx(normalized.expanded.leading),
|
|
107
|
+
trailing: convertIfJsx(normalized.expanded.trailing),
|
|
108
|
+
center: convertIfJsx(normalized.expanded.center),
|
|
109
|
+
bottom: convertIfJsx(normalized.expanded.bottom),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return normalized;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function normalizeActivityPayload(options: any): any {
|
|
116
|
+
const opts: any = { ...options };
|
|
117
|
+
if (opts.lockScreen) opts.lockScreen = convertIfJsx(opts.lockScreen);
|
|
118
|
+
if (opts.staleLockScreen) opts.staleLockScreen = convertIfJsx(opts.staleLockScreen);
|
|
119
|
+
if (opts.expiredLockScreen) opts.expiredLockScreen = convertIfJsx(opts.expiredLockScreen);
|
|
120
|
+
if (opts.dynamicIsland) opts.dynamicIsland = normalizeDynamicIsland(opts.dynamicIsland);
|
|
121
|
+
if (opts.staleDynamicIsland) opts.staleDynamicIsland = normalizeDynamicIsland(opts.staleDynamicIsland);
|
|
122
|
+
|
|
123
|
+
if (opts.alertConfig && typeof opts.alertConfig === 'object') {
|
|
124
|
+
if (opts.alertConfig.title && !opts.alertTitle) opts.alertTitle = opts.alertConfig.title;
|
|
125
|
+
if (opts.alertConfig.body && !opts.alertBody) opts.alertBody = opts.alertConfig.body;
|
|
126
|
+
if (opts.alertConfig.sound && !opts.alertSound) opts.alertSound = opts.alertConfig.sound;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof opts.autoEndAfterSeconds === 'number' && opts.autoEndAfterSeconds > 0 && !opts.scheduledEndTimestamp) {
|
|
130
|
+
opts.scheduledEndTimestamp = Date.now() / 1000.0 + opts.autoEndAfterSeconds;
|
|
131
|
+
}
|
|
132
|
+
return opts;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function normalizeSinglePageConfig(cfg: any): any {
|
|
136
|
+
if (!cfg || typeof cfg !== 'object') return cfg;
|
|
137
|
+
return {
|
|
138
|
+
...cfg,
|
|
139
|
+
small: convertIfJsx(cfg.small),
|
|
140
|
+
medium: convertIfJsx(cfg.medium),
|
|
141
|
+
large: convertIfJsx(cfg.large),
|
|
142
|
+
extraLarge: convertIfJsx(cfg.extraLarge),
|
|
143
|
+
standBy: convertIfJsx(cfg.standBy),
|
|
144
|
+
accessoryRectangular: convertIfJsx(cfg.accessoryRectangular),
|
|
145
|
+
accessoryCircular: convertIfJsx(cfg.accessoryCircular),
|
|
146
|
+
accessoryInline: convertIfJsx(cfg.accessoryInline),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function normalizeWidgetLayoutConfig(layoutConfig: any): any {
|
|
151
|
+
let finalLayout: any = layoutConfig;
|
|
152
|
+
if (
|
|
153
|
+
React.isValidElement(finalLayout) ||
|
|
154
|
+
(finalLayout as any)?.$$typeof ||
|
|
155
|
+
typeof (finalLayout as any)?.type === 'function'
|
|
156
|
+
) {
|
|
157
|
+
finalLayout = { medium: renderToWidget(finalLayout) };
|
|
158
|
+
} else if (finalLayout && typeof finalLayout === 'object') {
|
|
159
|
+
const c = normalizeSinglePageConfig(finalLayout);
|
|
160
|
+
if (c.pages && typeof c.pages === 'object') {
|
|
161
|
+
const convertedPages: Record<string, any> = {};
|
|
162
|
+
for (const [pageKey, pageCfg] of Object.entries(c.pages)) {
|
|
163
|
+
convertedPages[pageKey] = normalizeSinglePageConfig(pageCfg);
|
|
164
|
+
}
|
|
165
|
+
c.pages = convertedPages;
|
|
166
|
+
}
|
|
167
|
+
finalLayout = c;
|
|
168
|
+
}
|
|
169
|
+
return finalLayout;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function resolveLayoutObjectTemplates(value: any, flatVars: Record<string, string>): any {
|
|
173
|
+
if (typeof value === 'string') {
|
|
174
|
+
return resolveTemplateString(value, flatVars);
|
|
175
|
+
}
|
|
176
|
+
if (Array.isArray(value)) {
|
|
177
|
+
return value.map((item) => resolveLayoutObjectTemplates(item, flatVars));
|
|
178
|
+
}
|
|
179
|
+
if (value && typeof value === 'object') {
|
|
180
|
+
const out: Record<string, any> = {};
|
|
181
|
+
for (const [k, v] of Object.entries(value)) {
|
|
182
|
+
out[k] = resolveLayoutObjectTemplates(v, flatVars);
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
|
|
84
189
|
// ─── 1. LIVE ACTIVITY & LIVE UPDATE ──────────────────────────────────────────
|
|
85
190
|
|
|
86
191
|
export const LiveActivity = {
|
|
@@ -90,11 +195,7 @@ export const LiveActivity = {
|
|
|
90
195
|
start: async (options: StartActivityOptions): Promise<boolean> => {
|
|
91
196
|
if (!LiveActivityModule?.start) return false;
|
|
92
197
|
try {
|
|
93
|
-
const
|
|
94
|
-
if (React.isValidElement(opts.lockScreen)) {
|
|
95
|
-
opts.lockScreen = renderToWidget(opts.lockScreen);
|
|
96
|
-
}
|
|
97
|
-
const payload = JSON.stringify(opts);
|
|
198
|
+
const payload = JSON.stringify(normalizeActivityPayload(options));
|
|
98
199
|
return await LiveActivityModule.start(options.activityId, payload);
|
|
99
200
|
} catch (error) {
|
|
100
201
|
console.warn('[react-native-live-widgets] start error:', error);
|
|
@@ -108,11 +209,7 @@ export const LiveActivity = {
|
|
|
108
209
|
update: async (options: UpdateActivityOptions): Promise<boolean> => {
|
|
109
210
|
if (!LiveActivityModule?.update) return false;
|
|
110
211
|
try {
|
|
111
|
-
const
|
|
112
|
-
if (React.isValidElement(opts.lockScreen)) {
|
|
113
|
-
opts.lockScreen = renderToWidget(opts.lockScreen);
|
|
114
|
-
}
|
|
115
|
-
const payload = JSON.stringify(opts);
|
|
212
|
+
const payload = JSON.stringify(normalizeActivityPayload(options));
|
|
116
213
|
return await LiveActivityModule.update(options.activityId, payload);
|
|
117
214
|
} catch (error) {
|
|
118
215
|
console.warn('[react-native-live-widgets] update error:', error);
|
|
@@ -126,11 +223,13 @@ export const LiveActivity = {
|
|
|
126
223
|
end: async (activityId: string, options?: EndActivityOptions): Promise<boolean> => {
|
|
127
224
|
try {
|
|
128
225
|
if (options && (LiveActivityModule?.endWithOptions || LiveActivityModule?.endActivity)) {
|
|
129
|
-
const
|
|
226
|
+
const finalLock = options.finalLockScreen ? convertIfJsx(options.finalLockScreen) : undefined;
|
|
227
|
+
const finalIsland = options.finalDynamicIsland ? normalizeDynamicIsland(options.finalDynamicIsland) : undefined;
|
|
228
|
+
const payloadJson = finalLock || finalIsland
|
|
130
229
|
? JSON.stringify({
|
|
131
230
|
activityId,
|
|
132
|
-
lockScreen:
|
|
133
|
-
dynamicIsland:
|
|
231
|
+
lockScreen: finalLock,
|
|
232
|
+
dynamicIsland: finalIsland,
|
|
134
233
|
})
|
|
135
234
|
: null;
|
|
136
235
|
const nativeOptions = {
|
|
@@ -303,7 +402,7 @@ export const LiveActivity = {
|
|
|
303
402
|
},
|
|
304
403
|
|
|
305
404
|
/**
|
|
306
|
-
* Lấy danh sách các phiên Live Activity đang chạy trên thiết bị (
|
|
405
|
+
* Lấy danh sách các phiên Live Activity đang chạy trên thiết bị (kèm trạng thái Stale, RelevanceScore, ScheduledEnd và Push Token).
|
|
307
406
|
* Giúp ứng dụng khôi phục lại trạng thái giao diện khi mở lại App.
|
|
308
407
|
*/
|
|
309
408
|
getActiveActivities: async (): Promise<ActiveActivityInfo[]> => {
|
|
@@ -314,6 +413,12 @@ export const LiveActivity = {
|
|
|
314
413
|
const list = await LiveActivityModule.getActiveActivities();
|
|
315
414
|
return (list || []).map((item: any) => ({
|
|
316
415
|
activityId: item.activityId || '',
|
|
416
|
+
systemActivityId: item.systemActivityId,
|
|
417
|
+
activityState: item.activityState,
|
|
418
|
+
isStale: Boolean(item.isStale ?? item.activityState === 'stale'),
|
|
419
|
+
relevanceScore: typeof item.relevanceScore === 'number' ? item.relevanceScore : undefined,
|
|
420
|
+
staleTimestamp: typeof item.staleTimestamp === 'number' ? item.staleTimestamp : undefined,
|
|
421
|
+
scheduledEndTimestamp: typeof item.scheduledEndTimestamp === 'number' ? item.scheduledEndTimestamp : undefined,
|
|
317
422
|
pushToken: item.pushToken,
|
|
318
423
|
}));
|
|
319
424
|
} catch (e) {
|
|
@@ -322,6 +427,61 @@ export const LiveActivity = {
|
|
|
322
427
|
}
|
|
323
428
|
},
|
|
324
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Lấy thông tin chi tiết của một Live Activity cụ thể theo activityId.
|
|
432
|
+
*/
|
|
433
|
+
get: async (activityId: string): Promise<ActiveActivityInfo | null> => {
|
|
434
|
+
const list = await LiveActivity.getActiveActivities();
|
|
435
|
+
return list.find((a) => a.activityId === activityId || a.systemActivityId === activityId) ?? null;
|
|
436
|
+
},
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Kiểm tra nhanh xem một Live Activity cụ thể có đang chạy trên thiết bị hay không.
|
|
440
|
+
*/
|
|
441
|
+
isActive: async (activityId: string): Promise<boolean> => {
|
|
442
|
+
const info = await LiveActivity.get(activityId);
|
|
443
|
+
return Boolean(info && info.activityState !== 'ended' && info.activityState !== 'dismissed' && info.activityState !== 'expired');
|
|
444
|
+
},
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Lắng nghe sự kiện chuyển đổi vòng đời Live Activity (`active`, `started_by_push`, `stale`, `expired`, `ended`, `dismissed`).
|
|
448
|
+
*/
|
|
449
|
+
onStateChange: (listener: (event: LiveActivityStateEvent) => void): (() => void) => {
|
|
450
|
+
if (!liveActivityEmitter) return () => {};
|
|
451
|
+
const sub = liveActivityEmitter.addListener(STATE_CHANGED_EVENT, (event: any) => {
|
|
452
|
+
if (!event) return;
|
|
453
|
+
listener({
|
|
454
|
+
activityId: event.activityId || '',
|
|
455
|
+
systemActivityId: event.systemActivityId,
|
|
456
|
+
state: event.state || 'active',
|
|
457
|
+
timestamp: typeof event.timestamp === 'number' ? event.timestamp : Date.now() / 1000.0,
|
|
458
|
+
pushToken: event.pushToken,
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
return () => sub.remove();
|
|
462
|
+
},
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Kiểm tra và tự động kết thúc các Live Activity đã quá hạn `scheduledEndTimestamp` (dọn dẹp các phiên bị kẹt).
|
|
466
|
+
* Trả về danh sách các `activityId` đã được dọn dẹp.
|
|
467
|
+
*/
|
|
468
|
+
cleanupExpired: async (): Promise<string[]> => {
|
|
469
|
+
const list = await LiveActivity.getActiveActivities();
|
|
470
|
+
const nowSec = Date.now() / 1000.0;
|
|
471
|
+
const cleaned: string[] = [];
|
|
472
|
+
for (const item of list) {
|
|
473
|
+
const endTs = item.scheduledEndTimestamp;
|
|
474
|
+
if (typeof endTs === 'number' && endTs > 0) {
|
|
475
|
+
const normalizedEnd = endTs > 10_000_000_000 ? endTs / 1000.0 : endTs;
|
|
476
|
+
if (nowSec >= normalizedEnd) {
|
|
477
|
+
await LiveActivity.end(item.activityId, { dismissalPolicy: 'immediate' });
|
|
478
|
+
cleaned.push(item.activityId);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return cleaned;
|
|
483
|
+
},
|
|
484
|
+
|
|
325
485
|
/**
|
|
326
486
|
* Kiểm tra xem người dùng có cấp quyền Hoạt động trực tiếp (Live Activities) trong Cài đặt máy hay không.
|
|
327
487
|
*/
|
|
@@ -463,27 +623,7 @@ export const Widget = {
|
|
|
463
623
|
appGroup = typeof layoutConfigOrAppGroup === 'string' ? layoutConfigOrAppGroup : undefined;
|
|
464
624
|
}
|
|
465
625
|
|
|
466
|
-
|
|
467
|
-
const convertIfJsx = (node: any) => {
|
|
468
|
-
if (!node) return node;
|
|
469
|
-
if (React.isValidElement(node) || (node as any)?.$$typeof || typeof (node as any)?.type === 'function') {
|
|
470
|
-
return renderToWidget(node);
|
|
471
|
-
}
|
|
472
|
-
return node;
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
if (React.isValidElement(finalLayout) || (finalLayout as any)?.$$typeof || typeof (finalLayout as any)?.type === 'function') {
|
|
476
|
-
finalLayout = { medium: renderToWidget(finalLayout) };
|
|
477
|
-
} else if (finalLayout && typeof finalLayout === 'object') {
|
|
478
|
-
const c = { ...finalLayout };
|
|
479
|
-
c.small = convertIfJsx(c.small);
|
|
480
|
-
c.medium = convertIfJsx(c.medium);
|
|
481
|
-
c.large = convertIfJsx(c.large);
|
|
482
|
-
c.accessoryRectangular = convertIfJsx(c.accessoryRectangular);
|
|
483
|
-
c.accessoryCircular = convertIfJsx(c.accessoryCircular);
|
|
484
|
-
c.accessoryInline = convertIfJsx(c.accessoryInline);
|
|
485
|
-
finalLayout = c;
|
|
486
|
-
}
|
|
626
|
+
const finalLayout = normalizeWidgetLayoutConfig(layoutConfig);
|
|
487
627
|
|
|
488
628
|
const saved = await SharedStorage.set(`widget_layout_${widgetKey}`, finalLayout, appGroup);
|
|
489
629
|
if (!saved) return false;
|
|
@@ -516,15 +656,22 @@ export const Widget = {
|
|
|
516
656
|
appGroup?: string
|
|
517
657
|
): Promise<boolean> => {
|
|
518
658
|
try {
|
|
519
|
-
const formattedEntries = timelineConfig.entries.map((entry) =>
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
659
|
+
const formattedEntries = timelineConfig.entries.map((entry) => {
|
|
660
|
+
let entryLayout = normalizeWidgetLayoutConfig(entry.layout);
|
|
661
|
+
if (entry.data && typeof entry.data === 'object') {
|
|
662
|
+
const flatVars = flattenDataObject(entry.data);
|
|
663
|
+
entryLayout = resolveLayoutObjectTemplates(entryLayout, flatVars);
|
|
664
|
+
}
|
|
665
|
+
return {
|
|
666
|
+
date:
|
|
667
|
+
typeof entry.date === 'string'
|
|
668
|
+
? new Date(entry.date).getTime() / 1000.0
|
|
669
|
+
: entry.date > 10000000000
|
|
670
|
+
? entry.date / 1000.0
|
|
671
|
+
: entry.date,
|
|
672
|
+
layout: entryLayout,
|
|
673
|
+
};
|
|
674
|
+
});
|
|
528
675
|
const formattedConfig = {
|
|
529
676
|
entries: formattedEntries,
|
|
530
677
|
policy: timelineConfig.policy || 'atEnd',
|
|
@@ -589,13 +736,17 @@ export const Widget = {
|
|
|
589
736
|
},
|
|
590
737
|
|
|
591
738
|
/**
|
|
592
|
-
* Cấu hình
|
|
593
|
-
*
|
|
594
|
-
* kể cả khi
|
|
739
|
+
* Cấu hình Background Data Engine 2.0 để Widget Extension tự gọi API ngầm (REST, Custom Headers,
|
|
740
|
+
* Cache TTL, ETag/304, Retry/Backoff, Timeout, Multiple Sources, Data Transform, Schema Validation,
|
|
741
|
+
* Auth Token Refresh & Error State) kể cả khi App chính đã bị đóng hoàn toàn.
|
|
742
|
+
*
|
|
743
|
+
* Lưu ý hệ điều hành: Apple WidgetKit quản lý ngân sách reload động (~40–70 lần/ngày tùy mức sử dụng,
|
|
744
|
+
* `refreshIntervalMinutes` chỉ là mốc sớm nhất đề xuất chứ không cam kết chạy đúng mỗi 15 phút khi app đóng);
|
|
745
|
+
* Android cũng áp dụng Doze Mode / App Standby cho background task.
|
|
595
746
|
*
|
|
596
747
|
* Hỗ trợ gọi linh hoạt:
|
|
597
|
-
* - `Widget.setRemoteConfig('main',
|
|
598
|
-
* - `Widget.setRemoteConfig(
|
|
748
|
+
* - `Widget.setRemoteConfig('main', config, appGroup?)`
|
|
749
|
+
* - `Widget.setRemoteConfig(config, appGroup?)` (tự động gán widgetKey = 'main')
|
|
599
750
|
*/
|
|
600
751
|
setRemoteConfig: async (
|
|
601
752
|
widgetKeyOrConfig: string | WidgetRemoteConfig,
|
|
@@ -616,15 +767,25 @@ export const Widget = {
|
|
|
616
767
|
appGroup = typeof remoteConfigOrAppGroup === 'string' ? remoteConfigOrAppGroup : undefined;
|
|
617
768
|
}
|
|
618
769
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
770
|
+
const finalConfig: WidgetRemoteConfig = { ...remoteConfig };
|
|
771
|
+
if (
|
|
772
|
+
finalConfig.intervalMinutes !== undefined &&
|
|
773
|
+
finalConfig.refreshIntervalMinutes === undefined
|
|
774
|
+
) {
|
|
775
|
+
finalConfig.refreshIntervalMinutes = finalConfig.intervalMinutes;
|
|
776
|
+
} else if (
|
|
777
|
+
finalConfig.refreshIntervalMinutes !== undefined &&
|
|
778
|
+
finalConfig.intervalMinutes === undefined
|
|
779
|
+
) {
|
|
780
|
+
finalConfig.intervalMinutes = finalConfig.refreshIntervalMinutes;
|
|
781
|
+
}
|
|
782
|
+
if (finalConfig.errorFallback) {
|
|
783
|
+
finalConfig.errorFallback = convertIfJsx(finalConfig.errorFallback);
|
|
623
784
|
}
|
|
624
785
|
|
|
625
786
|
const saved = await SharedStorage.set(
|
|
626
787
|
`widget_remote_config_${widgetKey}`,
|
|
627
|
-
|
|
788
|
+
finalConfig,
|
|
628
789
|
appGroup
|
|
629
790
|
);
|
|
630
791
|
if (!saved) return false;
|
|
@@ -635,6 +796,53 @@ export const Widget = {
|
|
|
635
796
|
}
|
|
636
797
|
},
|
|
637
798
|
|
|
799
|
+
/**
|
|
800
|
+
* Đẩy dữ liệu có cấu trúc (Structured Data Binding) từ JS/TS xuống Native Shared Storage
|
|
801
|
+
* để Home Screen Widget cập nhật tức thì mà không cần dựng lại toàn bộ cây layout.
|
|
802
|
+
*
|
|
803
|
+
* Hỗ trợ gọi linh hoạt:
|
|
804
|
+
* - `Widget.setData('main', data, appGroup?)`
|
|
805
|
+
* - `Widget.setData(data, appGroup?)` (tự động gán widgetKey = 'main')
|
|
806
|
+
*/
|
|
807
|
+
setData: async (
|
|
808
|
+
widgetKeyOrData: string | Record<string, any>,
|
|
809
|
+
dataOrAppGroup?: Record<string, any> | string,
|
|
810
|
+
maybeAppGroup?: string
|
|
811
|
+
): Promise<boolean> => {
|
|
812
|
+
try {
|
|
813
|
+
let widgetKey = 'main';
|
|
814
|
+
let dataObj: Record<string, any>;
|
|
815
|
+
let appGroup: string | undefined;
|
|
816
|
+
|
|
817
|
+
if (typeof widgetKeyOrData === 'string') {
|
|
818
|
+
widgetKey = widgetKeyOrData;
|
|
819
|
+
dataObj = (dataOrAppGroup as Record<string, any>) || {};
|
|
820
|
+
appGroup = maybeAppGroup;
|
|
821
|
+
} else {
|
|
822
|
+
dataObj = widgetKeyOrData || {};
|
|
823
|
+
appGroup = typeof dataOrAppGroup === 'string' ? dataOrAppGroup : undefined;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const flattened = flattenDataObject(dataObj);
|
|
827
|
+
const saved = await SharedStorage.set(`widget_bound_data_${widgetKey}`, flattened, appGroup);
|
|
828
|
+
if (!saved) return false;
|
|
829
|
+
return await Widget.reloadAll();
|
|
830
|
+
} catch (error) {
|
|
831
|
+
console.warn('[react-native-live-widgets] setData error:', error);
|
|
832
|
+
return false;
|
|
833
|
+
}
|
|
834
|
+
},
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* Tạo Proxy Data Binding có cấu trúc & gợi ý kiểu TypeScript cho JSX/DSL Widget (`data.user.name`, `data.attendance.progress.divide(100)`).
|
|
838
|
+
*/
|
|
839
|
+
createDataBinding,
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Tạo chuỗi token binding (`{{path|pipe}}`) cho hệ thống Background Data Engine 2.0.
|
|
843
|
+
*/
|
|
844
|
+
bind: UI.bind,
|
|
845
|
+
|
|
638
846
|
/** Đăng ký lắng nghe tương tác nút bấm từ Widget theo `actionId` */
|
|
639
847
|
registerAction: registerAction,
|
|
640
848
|
|
|
@@ -709,6 +917,36 @@ export const Widget = {
|
|
|
709
917
|
};
|
|
710
918
|
},
|
|
711
919
|
|
|
920
|
+
/**
|
|
921
|
+
* 🎛️ Cấu hình Control Widget (iOS 18+ Control Center / Lock Screen Controls / Action Button & Android Quick Settings).
|
|
922
|
+
*/
|
|
923
|
+
setControl: async (
|
|
924
|
+
controlId: string,
|
|
925
|
+
config: ControlWidgetConfig,
|
|
926
|
+
appGroup?: string
|
|
927
|
+
): Promise<boolean> => {
|
|
928
|
+
try {
|
|
929
|
+
return await SharedStorage.set(`widget_control_${controlId}`, config, appGroup);
|
|
930
|
+
} catch (error) {
|
|
931
|
+
console.warn('[react-native-live-widgets] setControl error:', error);
|
|
932
|
+
return false;
|
|
933
|
+
}
|
|
934
|
+
},
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* Đọc cấu hình và trạng thái hiện tại của Control Widget từ Shared Storage.
|
|
938
|
+
*/
|
|
939
|
+
getControlState: async (
|
|
940
|
+
controlId: string,
|
|
941
|
+
appGroup?: string
|
|
942
|
+
): Promise<ControlWidgetConfig | null> => {
|
|
943
|
+
try {
|
|
944
|
+
return await SharedStorage.get<ControlWidgetConfig>(`widget_control_${controlId}`, appGroup);
|
|
945
|
+
} catch {
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
},
|
|
949
|
+
|
|
712
950
|
/**
|
|
713
951
|
* 🌲 Biên dịch cây DSL sang UI Intermediate Representation (IR & Semantic Tree - Phase 2).
|
|
714
952
|
*/
|
|
@@ -724,6 +962,17 @@ export const Widget = {
|
|
|
724
962
|
},
|
|
725
963
|
};
|
|
726
964
|
|
|
965
|
+
/**
|
|
966
|
+
* 🎛️ Namespace cấu hình & quản lý Control Widget đa nền tảng
|
|
967
|
+
* (iOS 18+ Control Center / Lock Screen Controls / Action Button & Android Quick Settings Tile).
|
|
968
|
+
*/
|
|
969
|
+
export const ControlWidget = {
|
|
970
|
+
set: Widget.setControl,
|
|
971
|
+
getState: Widget.getControlState,
|
|
972
|
+
button: UI.ControlButton,
|
|
973
|
+
toggle: UI.ControlToggle,
|
|
974
|
+
};
|
|
975
|
+
|
|
727
976
|
// ─── 3. SHARED STORAGE (App Groups & SharedPreferences) ─────────────────────
|
|
728
977
|
|
|
729
978
|
export const SharedStorage = {
|
|
@@ -876,3 +1125,17 @@ export * from './builder';
|
|
|
876
1125
|
export * from './ir';
|
|
877
1126
|
export { StyleSheet } from './builder';
|
|
878
1127
|
export * from './hooks';
|
|
1128
|
+
|
|
1129
|
+
/**
|
|
1130
|
+
* Định nghĩa cấu hình các Widget cho ứng dụng với đầy đủ Type Safety và gợi ý thuộc tính IDE.
|
|
1131
|
+
* Cấu hình này được công cụ Codegen (`npx react-native-live-widgets codegen`) đọc
|
|
1132
|
+
* để tự động tạo file LiveWidgetsBundle.swift cho iOS WidgetKit.
|
|
1133
|
+
*/
|
|
1134
|
+
export function defineWidgetsConfig<T extends import('./types').WidgetsProjectConfig>(config: T): T {
|
|
1135
|
+
return config;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* Alias cho `defineWidgetsConfig` (hỗ trợ cả dạng số ít và số nhiều)
|
|
1140
|
+
*/
|
|
1141
|
+
export const defineWidgetConfig = defineWidgetsConfig;
|
package/src/ir.ts
CHANGED
|
@@ -130,7 +130,8 @@ function buildSemanticNodes(node: any, path: string, result: SemanticNode[]): vo
|
|
|
130
130
|
role = 'progressbar';
|
|
131
131
|
if (!label) label = 'Tiến độ';
|
|
132
132
|
if (value === undefined && props.value !== undefined) {
|
|
133
|
-
|
|
133
|
+
const num = Number(props.value);
|
|
134
|
+
value = Number.isNaN(num) ? String(props.value) : `${Math.round(num * 100)}%`;
|
|
134
135
|
}
|
|
135
136
|
break;
|
|
136
137
|
}
|
|
@@ -139,7 +140,12 @@ function buildSemanticNodes(node: any, path: string, result: SemanticNode[]): vo
|
|
|
139
140
|
role = 'progressbar';
|
|
140
141
|
label = props.label || label || 'Chỉ số';
|
|
141
142
|
if (value === undefined) {
|
|
142
|
-
|
|
143
|
+
if (props.valueText) {
|
|
144
|
+
value = props.valueText;
|
|
145
|
+
} else if (props.value !== undefined) {
|
|
146
|
+
const num = Number(props.value);
|
|
147
|
+
value = Number.isNaN(num) ? String(props.value) : `${Math.round(num * 100)}%`;
|
|
148
|
+
}
|
|
143
149
|
}
|
|
144
150
|
break;
|
|
145
151
|
}
|