react-native-live-widgets 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +21 -0
  3. package/README.md +221 -2
  4. package/android/build.gradle +49 -0
  5. package/android/src/main/AndroidManifest.xml +38 -0
  6. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +440 -0
  7. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityPackage.kt +17 -0
  8. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityHeadlessTaskService.kt +24 -0
  9. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +410 -0
  10. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +67 -0
  11. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicNodeRegistry.kt +64 -0
  12. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +1223 -0
  13. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +120 -0
  14. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +281 -0
  15. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +229 -0
  16. package/android/src/main/res/layout/rn_live_widget_host.xml +113 -0
  17. package/android/src/main/res/xml/rn_live_widget_info.xml +14 -0
  18. package/assets/dashboard_features.png +0 -0
  19. package/assets/dynamic_island_expanded.png +0 -0
  20. package/assets/home_widget_interactive.png +0 -0
  21. package/assets/lock_screen_live_activity.png +0 -0
  22. package/cli/setup.js +94 -0
  23. package/docs/GUIDE-EN.md +804 -0
  24. package/docs/GUIDE-VI.md +802 -0
  25. package/docs/widgets.md +52 -0
  26. package/ios/Bridge/LiveActivityModule.m +105 -0
  27. package/ios/Bridge/LiveActivityModule.swift +957 -0
  28. package/ios/Intents/DynamicWidgetActionIntent.swift +38 -0
  29. package/ios/Models/DynamicActivityAttributes.swift +22 -0
  30. package/ios/Models/DynamicLayoutModels.swift +243 -0
  31. package/ios/Renderer/ColorHexExtension.swift +49 -0
  32. package/ios/Renderer/DynamicNodeRegistry.swift +54 -0
  33. package/ios/Renderer/DynamicNodeView.swift +1060 -0
  34. package/ios/Storage/DynamicWidgetImageCache.swift +137 -0
  35. package/ios/Storage/DynamicWidgetStateStore.swift +162 -0
  36. package/ios/Widgets/DynamicHomeScreenWidget.swift +435 -0
  37. package/ios/Widgets/DynamicLiveActivityWidget.swift +79 -0
  38. package/lib/builder.d.ts +60 -0
  39. package/lib/builder.d.ts.map +1 -0
  40. package/lib/builder.js +296 -0
  41. package/lib/builder.js.map +1 -0
  42. package/lib/dispatcher.d.ts +16 -0
  43. package/lib/dispatcher.d.ts.map +1 -0
  44. package/lib/dispatcher.js +23 -0
  45. package/lib/dispatcher.js.map +1 -0
  46. package/lib/index.d.ts +199 -0
  47. package/lib/index.d.ts.map +1 -0
  48. package/lib/index.js +634 -0
  49. package/lib/index.js.map +1 -0
  50. package/lib/types.d.ts +489 -0
  51. package/lib/types.d.ts.map +1 -0
  52. package/lib/types.js +10 -0
  53. package/lib/types.js.map +1 -0
  54. package/package.json +68 -4
  55. package/react-native-live-widgets.podspec +38 -0
  56. package/src/builder.ts +409 -0
  57. package/src/dispatcher.ts +25 -0
  58. package/src/index.ts +681 -0
  59. package/src/types.ts +595 -0
package/src/builder.ts ADDED
@@ -0,0 +1,409 @@
1
+ import {
2
+ BadgeNode,
3
+ ButtonNode,
4
+ ChartNode,
5
+ ContainerProps,
6
+ CustomNode,
7
+ DividerNode,
8
+ EqualizerNode,
9
+ EqualizerProps,
10
+ GaugeNode,
11
+ GridNode,
12
+ GridProps,
13
+ HStackNode,
14
+ ImageNode,
15
+ LayoutNode,
16
+ LinkNode,
17
+ LinkProps,
18
+ ProgressNode,
19
+ SpacerNode,
20
+ StyleProp,
21
+ TextNode,
22
+ TextStyle,
23
+ TimerNode,
24
+ ToggleNode,
25
+ VStackNode,
26
+ ZStackNode,
27
+ ZStackProps,
28
+ } from './types';
29
+ import { registerAction } from './dispatcher';
30
+
31
+ let autoActionCounter = 0;
32
+
33
+ export function flattenStyle<T extends object>(...styles: StyleProp<T>[]): T | undefined {
34
+ let merged: any = undefined;
35
+
36
+ function walk(item: any) {
37
+ if (!item) return;
38
+ if (Array.isArray(item)) {
39
+ for (const sub of item) walk(sub);
40
+ } else if (typeof item === 'object') {
41
+ if (!merged) merged = {};
42
+ Object.assign(merged, item);
43
+ }
44
+ }
45
+
46
+ for (const s of styles) {
47
+ walk(s);
48
+ }
49
+ return merged as T | undefined;
50
+ }
51
+
52
+ export type AnyWidgetStyle = TextStyle | ContainerProps | Record<string, any>;
53
+
54
+ export type NamedStyles<T> = {
55
+ [P in keyof T]: AnyWidgetStyle;
56
+ };
57
+
58
+ export const StyleSheet = {
59
+ create: <T extends NamedStyles<T> | NamedStyles<any>>(styles: T & NamedStyles<any>): T => styles,
60
+ flatten: flattenStyle,
61
+ };
62
+
63
+ function isLayoutNode(item: any): item is LayoutNode {
64
+ return item && typeof item === 'object' && typeof item.type === 'string';
65
+ }
66
+
67
+ function parseContainerArgs<P extends object>(
68
+ first?: StyleProp<P> | LayoutNode | LayoutNode[],
69
+ rest?: LayoutNode[]
70
+ ): { props?: P; children: LayoutNode[] } {
71
+ if (!first) {
72
+ return { props: undefined, children: [] };
73
+ }
74
+ if (Array.isArray(first)) {
75
+ if (first.length > 0 && isLayoutNode(first[0])) {
76
+ return { props: undefined, children: first as LayoutNode[] };
77
+ }
78
+ const flattened = flattenStyle<P>(first as StyleProp<P>);
79
+ return { props: flattened, children: rest || [] };
80
+ }
81
+ if (isLayoutNode(first)) {
82
+ return { props: undefined, children: [first, ...(rest || [])] };
83
+ }
84
+ return { props: first as P, children: rest || [] };
85
+ }
86
+
87
+ /**
88
+ * Các hàm helper tạo node cây giao diện trực quan cho TypeScript/JavaScript
89
+ */
90
+ export const UI = {
91
+ StyleSheet,
92
+
93
+ VStack: (
94
+ propsOrChildren?: StyleProp<ContainerProps> | LayoutNode | LayoutNode[],
95
+ ...children: LayoutNode[]
96
+ ): VStackNode => {
97
+ const { props, children: parsedChildren } = parseContainerArgs<ContainerProps>(propsOrChildren, children);
98
+ return {
99
+ type: 'VStack',
100
+ props,
101
+ children: parsedChildren,
102
+ };
103
+ },
104
+
105
+ HStack: (
106
+ propsOrChildren?: StyleProp<ContainerProps> | LayoutNode | LayoutNode[],
107
+ ...children: LayoutNode[]
108
+ ): HStackNode => {
109
+ const { props, children: parsedChildren } = parseContainerArgs<ContainerProps>(propsOrChildren, children);
110
+ return {
111
+ type: 'HStack',
112
+ props,
113
+ children: parsedChildren,
114
+ };
115
+ },
116
+
117
+ Text: (
118
+ text: string,
119
+ ...styles: StyleProp<TextStyle>[]
120
+ ): TextNode => {
121
+ const merged = flattenStyle(...styles);
122
+ return {
123
+ type: 'Text',
124
+ props: {
125
+ text,
126
+ ...(merged || {}),
127
+ },
128
+ };
129
+ },
130
+
131
+ Timer: (
132
+ targetTimestamp: number,
133
+ options?: {
134
+ mode?: 'countdown' | 'countup';
135
+ color?: string;
136
+ fontSize?: number;
137
+ fontWeight?: 'regular' | 'medium' | 'semibold' | 'bold';
138
+ prefix?: string;
139
+ suffix?: string;
140
+ }
141
+ ): TimerNode => ({
142
+ type: 'Timer',
143
+ props: {
144
+ targetTimestamp,
145
+ mode: options?.mode ?? 'countdown',
146
+ ...options,
147
+ },
148
+ }),
149
+
150
+ Progress: (
151
+ valueOrOptions: number | ProgressNode['props'],
152
+ options?: Omit<ProgressNode['props'], 'value'>
153
+ ): ProgressNode => {
154
+ if (typeof valueOrOptions === 'number') {
155
+ return {
156
+ type: 'Progress',
157
+ props: {
158
+ value: valueOrOptions,
159
+ ...options,
160
+ },
161
+ };
162
+ }
163
+ return {
164
+ type: 'Progress',
165
+ props: valueOrOptions,
166
+ };
167
+ },
168
+
169
+ Image: (
170
+ urlOrOptions: string | ImageNode['props'],
171
+ options?: Omit<ImageNode['props'], 'url'>
172
+ ): ImageNode => {
173
+ if (typeof urlOrOptions === 'string') {
174
+ const isHttp = urlOrOptions.startsWith('http://') || urlOrOptions.startsWith('https://');
175
+ const isBase64 = urlOrOptions.startsWith('data:image/');
176
+ return {
177
+ type: 'Image',
178
+ props: {
179
+ ...(isHttp
180
+ ? { url: urlOrOptions }
181
+ : isBase64
182
+ ? { base64: urlOrOptions }
183
+ : { systemName: urlOrOptions }),
184
+ ...options,
185
+ },
186
+ };
187
+ }
188
+ return {
189
+ type: 'Image',
190
+ props: urlOrOptions,
191
+ };
192
+ },
193
+
194
+ Spacer: (minLength?: number): SpacerNode => ({
195
+ type: 'Spacer',
196
+ props: { minLength },
197
+ }),
198
+
199
+ Button: (
200
+ titleOrProps: string | ButtonNode['props'],
201
+ options?: Omit<ButtonNode['props'], 'title'>
202
+ ): ButtonNode => {
203
+ if (typeof titleOrProps === 'object' && titleOrProps !== null) {
204
+ if (titleOrProps.onPress && !titleOrProps.actionId) {
205
+ autoActionCounter += 1;
206
+ titleOrProps.actionId = `act_${Date.now()}_${autoActionCounter}`;
207
+ registerAction(titleOrProps.actionId, titleOrProps.onPress);
208
+ } else if (titleOrProps.onPress && titleOrProps.actionId) {
209
+ registerAction(titleOrProps.actionId, titleOrProps.onPress);
210
+ }
211
+ return {
212
+ type: 'Button',
213
+ props: titleOrProps,
214
+ };
215
+ }
216
+
217
+ let finalActionId = options?.actionId;
218
+ if (options?.onPress) {
219
+ if (!finalActionId) {
220
+ autoActionCounter += 1;
221
+ finalActionId = `act_${Date.now()}_${autoActionCounter}`;
222
+ }
223
+ registerAction(finalActionId, options.onPress);
224
+ }
225
+
226
+ return {
227
+ type: 'Button',
228
+ props: {
229
+ title: titleOrProps,
230
+ ...options,
231
+ actionId: finalActionId,
232
+ },
233
+ };
234
+ },
235
+
236
+ /**
237
+ * Tạo nút bấm kèm logic hàm TypeScript trực tiếp.
238
+ * Thư viện tự động sinh ActionId và kết nối bộ lắng nghe Native -> hàm async TypeScript của bạn!
239
+ */
240
+ ActionButton: (
241
+ title: string,
242
+ onPress: (payload?: any) => Promise<any> | any,
243
+ options?: Omit<ButtonNode['props'], 'title' | 'actionId' | 'onPress'>
244
+ ): ButtonNode => {
245
+ autoActionCounter += 1;
246
+ const actionId = `act_${Date.now()}_${autoActionCounter}`;
247
+ registerAction(actionId, onPress);
248
+ return {
249
+ type: 'Button',
250
+ props: {
251
+ title,
252
+ actionId,
253
+ onPress,
254
+ ...options,
255
+ },
256
+ };
257
+ },
258
+
259
+ ZStack: (
260
+ propsOrChildren?: ZStackProps | LayoutNode | LayoutNode[],
261
+ ...children: LayoutNode[]
262
+ ): ZStackNode => {
263
+ const { props, children: parsedChildren } = parseContainerArgs(propsOrChildren, children);
264
+ return {
265
+ type: 'ZStack',
266
+ props: props as ZStackProps | undefined,
267
+ children: parsedChildren,
268
+ };
269
+ },
270
+
271
+ Grid: (
272
+ propsOrChildren?: GridProps | LayoutNode | LayoutNode[],
273
+ ...children: LayoutNode[]
274
+ ): GridNode => {
275
+ const { props, children: parsedChildren } = parseContainerArgs(propsOrChildren, children);
276
+ return {
277
+ type: 'Grid',
278
+ props: props as GridProps | undefined,
279
+ children: parsedChildren,
280
+ };
281
+ },
282
+
283
+ Divider: (options?: DividerNode['props']): DividerNode => ({
284
+ type: 'Divider',
285
+ props: options,
286
+ }),
287
+
288
+ Gauge: (
289
+ valueOrOptions: number | GaugeNode['props'],
290
+ options?: Omit<GaugeNode['props'], 'value'>
291
+ ): GaugeNode => {
292
+ if (typeof valueOrOptions === 'number') {
293
+ return {
294
+ type: 'Gauge',
295
+ props: {
296
+ value: valueOrOptions,
297
+ ...options,
298
+ },
299
+ };
300
+ }
301
+ return {
302
+ type: 'Gauge',
303
+ props: valueOrOptions,
304
+ };
305
+ },
306
+
307
+ Chart: (
308
+ valuesOrOptions: number[] | ChartNode['props'],
309
+ options?: Omit<ChartNode['props'], 'values'>
310
+ ): ChartNode => {
311
+ if (Array.isArray(valuesOrOptions)) {
312
+ return {
313
+ type: 'Chart',
314
+ props: {
315
+ values: valuesOrOptions,
316
+ ...options,
317
+ },
318
+ };
319
+ }
320
+ return {
321
+ type: 'Chart',
322
+ props: valuesOrOptions,
323
+ };
324
+ },
325
+
326
+ Toggle: (
327
+ labelOrOptions: string | ToggleNode['props'],
328
+ options?: Omit<ToggleNode['props'], 'label'>
329
+ ): ToggleNode => {
330
+ if (typeof labelOrOptions === 'string') {
331
+ return {
332
+ type: 'Toggle',
333
+ props: {
334
+ label: labelOrOptions,
335
+ actionId: options?.actionId ?? 'toggle:default',
336
+ ...options,
337
+ },
338
+ };
339
+ }
340
+ return {
341
+ type: 'Toggle',
342
+ props: labelOrOptions,
343
+ };
344
+ },
345
+
346
+ Badge: (
347
+ textOrOptions: string | BadgeNode['props'],
348
+ options?: Omit<BadgeNode['props'], 'text'>
349
+ ): BadgeNode => {
350
+ if (typeof textOrOptions === 'string') {
351
+ return {
352
+ type: 'Badge',
353
+ props: {
354
+ text: textOrOptions,
355
+ ...options,
356
+ },
357
+ };
358
+ }
359
+ return {
360
+ type: 'Badge',
361
+ props: textOrOptions,
362
+ };
363
+ },
364
+
365
+ Link: (
366
+ props: LinkProps,
367
+ ...children: LayoutNode[]
368
+ ): LinkNode => ({
369
+ type: 'Link',
370
+ props,
371
+ children,
372
+ }),
373
+
374
+ /**
375
+ * Khởi tạo một Component Native tùy chỉnh đã được đăng ký qua:
376
+ * - iOS (Swift): `DynamicNodeRegistry.shared.register("MyComponent") { node in ... }`
377
+ * - Android (Kotlin): `DynamicNodeRegistry.register("MyComponent", ...)`
378
+ */
379
+ Custom: (
380
+ componentName: string,
381
+ props?: Record<string, any>,
382
+ ...children: LayoutNode[]
383
+ ): CustomNode => ({
384
+ type: 'Custom',
385
+ props: {
386
+ ...props,
387
+ componentName,
388
+ },
389
+ children: children.length > 0 ? children : undefined,
390
+ }),
391
+
392
+ /**
393
+ * Component sóng nhạc chuyển động (Equalizer / Sound Visualizer):
394
+ * Tự động chạy animation nhịp sóng nhạc mượt mà trên Dynamic Island và Lock Screen (iOS)
395
+ * và hiển thị trực quan trên Android.
396
+ */
397
+ Equalizer: (options?: EqualizerProps): EqualizerNode => ({
398
+ type: 'Equalizer',
399
+ props: {
400
+ isPlaying: true,
401
+ barCount: 4,
402
+ tintColor: '#10B981',
403
+ height: 16,
404
+ barWidth: 3,
405
+ spacing: 2,
406
+ ...options,
407
+ },
408
+ }),
409
+ };
@@ -0,0 +1,25 @@
1
+ export type ActionHandler = (event: {
2
+ actionId: string;
3
+ activityId?: string;
4
+ [key: string]: any;
5
+ }) => Promise<any> | any;
6
+
7
+ export const actionHandlers = new Map<string, ActionHandler>();
8
+
9
+ /**
10
+ * Đăng ký một hàm xử lý logic TypeScript khi người dùng bấm nút có `actionId` tương ứng ngoài Widget/Live Activity.
11
+ * Trả về hàm hủy đăng ký (unsubscribe).
12
+ */
13
+ export function registerAction(actionId: string, handler: ActionHandler): () => void {
14
+ actionHandlers.set(actionId, handler);
15
+ return () => {
16
+ actionHandlers.delete(actionId);
17
+ };
18
+ }
19
+
20
+ /**
21
+ * Hủy đăng ký action handler.
22
+ */
23
+ export function unregisterAction(actionId: string): void {
24
+ actionHandlers.delete(actionId);
25
+ }