react-native-live-widgets 1.1.0 → 1.2.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/CHANGELOG.md +20 -0
- package/README.md +76 -10
- package/android/build.gradle +14 -20
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +5 -1
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityHeadlessTaskService.kt +8 -0
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +4 -1
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +29 -3
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +26 -1
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +51 -15
- package/android/src/main/res/layout/rn_live_widget_host.xml +64 -74
- package/docs/GUIDE-EN.md +109 -7
- package/docs/GUIDE-VI.md +109 -7
- package/docs/index.html +440 -0
- package/docs/llms-full.txt +132 -0
- package/docs/llms.txt +88 -0
- package/ios/Models/DynamicLayoutModels.swift +5 -0
- package/ios/Widgets/DynamicHomeScreenWidget.swift +2 -3
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +29 -19
- package/lib/builder.js.map +1 -1
- package/lib/dispatcher.d.ts +7 -0
- package/lib/dispatcher.d.ts.map +1 -1
- package/lib/dispatcher.js +11 -0
- package/lib/dispatcher.js.map +1 -1
- package/lib/hooks.d.ts +66 -0
- package/lib/hooks.d.ts.map +1 -0
- package/lib/hooks.js +166 -0
- package/lib/hooks.js.map +1 -0
- package/lib/index.d.ts +32 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +115 -8
- package/lib/index.js.map +1 -1
- package/lib/jsx.d.ts +79 -0
- package/lib/jsx.d.ts.map +1 -0
- package/lib/jsx.js +196 -0
- package/lib/jsx.js.map +1 -0
- package/lib/preview.d.ts +54 -0
- package/lib/preview.d.ts.map +1 -0
- package/lib/preview.js +466 -0
- package/lib/preview.js.map +1 -0
- package/lib/types.d.ts +8 -0
- package/lib/types.d.ts.map +1 -1
- package/package.json +3 -1
- package/src/builder.ts +29 -19
- package/src/dispatcher.ts +17 -0
- package/src/hooks.ts +223 -0
- package/src/index.ts +137 -13
- package/src/jsx.tsx +296 -0
- package/src/preview.tsx +719 -0
- package/src/react-ambient.d.ts +10 -0
- package/src/react-native-env.d.ts +43 -0
- package/src/types.ts +8 -0
package/src/preview.tsx
ADDED
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
View,
|
|
4
|
+
Text as RNText,
|
|
5
|
+
Image as RNImage,
|
|
6
|
+
TouchableOpacity,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
ViewStyle,
|
|
9
|
+
TextStyle as RNTextStyle,
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
import {
|
|
12
|
+
LayoutNode,
|
|
13
|
+
WidgetSinglePageConfig,
|
|
14
|
+
VStackNode,
|
|
15
|
+
HStackNode,
|
|
16
|
+
TextNode,
|
|
17
|
+
ButtonNode,
|
|
18
|
+
BadgeNode,
|
|
19
|
+
ProgressNode,
|
|
20
|
+
TimerNode,
|
|
21
|
+
ImageNode,
|
|
22
|
+
} from './types';
|
|
23
|
+
import { renderToWidget, extractPropsAndStyle } from './jsx';
|
|
24
|
+
|
|
25
|
+
export type WidgetPreviewSize = 'small' | 'medium' | 'large' | 'lockScreen' | 'dynamicIsland';
|
|
26
|
+
|
|
27
|
+
export interface WidgetPreviewProps {
|
|
28
|
+
/**
|
|
29
|
+
* Kích thước tiện ích cần xem trước:
|
|
30
|
+
* - 'small' (158 × 158 dp)
|
|
31
|
+
* - 'medium' (338 × 158 dp)
|
|
32
|
+
* - 'large' (338 × 354 dp)
|
|
33
|
+
* - 'lockScreen' (350 × 80 dp)
|
|
34
|
+
* - 'dynamicIsland' (350 × 160 dp)
|
|
35
|
+
* Mặc định là 'medium'.
|
|
36
|
+
*/
|
|
37
|
+
size?: WidgetPreviewSize;
|
|
38
|
+
/** Cây LayoutNode hoặc đối tượng chứa { small, medium, large } */
|
|
39
|
+
layout?: LayoutNode | WidgetSinglePageConfig;
|
|
40
|
+
/** Cú pháp JSX trực tiếp truyền vào bên trong thẻ `<WidgetPreview>` */
|
|
41
|
+
children?: React.ReactNode;
|
|
42
|
+
/** Bắt sự kiện khi người dùng bấm vào các nút tương tác trên Widget xem trước */
|
|
43
|
+
onAction?: (actionId: string) => void;
|
|
44
|
+
/** Hiển thị nhãn kích thước chuẩn ở góc (mặc định true) */
|
|
45
|
+
showBadge?: boolean;
|
|
46
|
+
/** Cho phép bấm thanh tab nhỏ ở trên để chuyển nhanh qua lại giữa các kích thước (mặc định false) */
|
|
47
|
+
interactiveSizeSwitch?: boolean;
|
|
48
|
+
/** Style tùy biến cho wrapper bên ngoài */
|
|
49
|
+
style?: ViewStyle;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const SIZE_DIMENSIONS: Record<WidgetPreviewSize, { width: number; height: number; label: string }> = {
|
|
53
|
+
small: { width: 158, height: 158, label: 'Small • 158×158' },
|
|
54
|
+
medium: { width: 338, height: 158, label: 'Medium • 338×158' },
|
|
55
|
+
large: { width: 338, height: 354, label: 'Large • 338×354' },
|
|
56
|
+
lockScreen: { width: 350, height: 80, label: 'Lock Screen • 350×80' },
|
|
57
|
+
dynamicIsland: { width: 350, height: 160, label: 'Dynamic Island' },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function parseDynamicColor(c: any): string | undefined {
|
|
61
|
+
if (!c) return undefined;
|
|
62
|
+
if (typeof c === 'string') return c;
|
|
63
|
+
if (typeof c === 'object') return c.dark || c.light;
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 📱 Bộ Component xem trước tiện ích trực tiếp trong ứng dụng React Native (In-App Widget Preview).
|
|
69
|
+
* Cho phép lập trình viên và người dùng trải nghiệm giao diện & bấm thử tương tác của Widget
|
|
70
|
+
* ngay trong màn hình App mà không cần ra ngoài Home Screen.
|
|
71
|
+
*
|
|
72
|
+
* Cách dùng 1 (Truyền layout prop):
|
|
73
|
+
* ```tsx
|
|
74
|
+
* <WidgetPreview size="medium" layout={myLayout} onAction={(act) => alert(act)} />
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Cách dùng 2 (Truyền JSX children trực tiếp):
|
|
78
|
+
* ```tsx
|
|
79
|
+
* <WidgetPreview size="medium">
|
|
80
|
+
* <VStack padding={16} backgroundColor="#0F172A" borderRadius={16}>
|
|
81
|
+
* <HStack justifyContent="spaceBetween">
|
|
82
|
+
* <Text fontSize={14} fontWeight="bold" color="#FFFFFF">Vào ca sáng</Text>
|
|
83
|
+
* <Badge text="07:54" backgroundColor="#064E3B" textColor="#6EE7B7" />
|
|
84
|
+
* </HStack>
|
|
85
|
+
* <Button title="Làm mới" actionId="refresh" />
|
|
86
|
+
* </VStack>
|
|
87
|
+
* </WidgetPreview>
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export const WidgetPreview: React.FC<WidgetPreviewProps> = ({
|
|
91
|
+
size: initialSize = 'medium',
|
|
92
|
+
layout,
|
|
93
|
+
children,
|
|
94
|
+
onAction,
|
|
95
|
+
showBadge = true,
|
|
96
|
+
interactiveSizeSwitch = false,
|
|
97
|
+
style,
|
|
98
|
+
}: WidgetPreviewProps) => {
|
|
99
|
+
const [activeSize, setActiveSize] = useState<WidgetPreviewSize>(initialSize);
|
|
100
|
+
|
|
101
|
+
// Trích xuất LayoutNode từ props hoặc JSX children
|
|
102
|
+
let rootNode: LayoutNode | null = null;
|
|
103
|
+
let rawTarget: any = null;
|
|
104
|
+
|
|
105
|
+
if (children) {
|
|
106
|
+
rawTarget = children;
|
|
107
|
+
} else if (layout) {
|
|
108
|
+
if (React.isValidElement(layout) || (layout as any)?.$$typeof) {
|
|
109
|
+
rawTarget = layout;
|
|
110
|
+
} else if ((layout as any).type && typeof (layout as any).type === 'string' && (layout as any).props !== undefined) {
|
|
111
|
+
rawTarget = layout;
|
|
112
|
+
} else {
|
|
113
|
+
const pageConfig = layout as WidgetSinglePageConfig;
|
|
114
|
+
rawTarget =
|
|
115
|
+
(pageConfig as any)[activeSize] ||
|
|
116
|
+
pageConfig.medium ||
|
|
117
|
+
pageConfig.large ||
|
|
118
|
+
pageConfig.small ||
|
|
119
|
+
pageConfig.accessoryRectangular ||
|
|
120
|
+
null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (rawTarget) {
|
|
125
|
+
if (React.isValidElement(rawTarget) || (rawTarget as any)?.$$typeof || typeof (rawTarget as any)?.type === 'function') {
|
|
126
|
+
rootNode = renderToWidget(rawTarget);
|
|
127
|
+
} else {
|
|
128
|
+
rootNode = rawTarget as LayoutNode;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const dim =
|
|
133
|
+
SIZE_DIMENSIONS[(activeSize as WidgetPreviewSize) || 'medium'] || SIZE_DIMENSIONS.medium;
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<View style={[styles.container, style]}>
|
|
137
|
+
{interactiveSizeSwitch && (
|
|
138
|
+
<View style={styles.tabBar}>
|
|
139
|
+
{(['small', 'medium', 'large'] as WidgetPreviewSize[]).map((s) => (
|
|
140
|
+
<TouchableOpacity
|
|
141
|
+
key={s}
|
|
142
|
+
onPress={() => setActiveSize(s)}
|
|
143
|
+
style={[styles.tabItem, activeSize === s && styles.tabItemActive]}
|
|
144
|
+
>
|
|
145
|
+
<RNText style={[styles.tabText, activeSize === s && styles.tabTextActive]}>
|
|
146
|
+
{s.toUpperCase()}
|
|
147
|
+
</RNText>
|
|
148
|
+
</TouchableOpacity>
|
|
149
|
+
))}
|
|
150
|
+
</View>
|
|
151
|
+
)}
|
|
152
|
+
|
|
153
|
+
{showBadge && (
|
|
154
|
+
<View style={styles.badgeRow}>
|
|
155
|
+
<View style={styles.specBadge}>
|
|
156
|
+
<RNText style={styles.specBadgeText}>{dim.label}</RNText>
|
|
157
|
+
</View>
|
|
158
|
+
</View>
|
|
159
|
+
)}
|
|
160
|
+
|
|
161
|
+
{/* Khung mô phỏng Widget iOS Continuous Curve */}
|
|
162
|
+
<View
|
|
163
|
+
style={[
|
|
164
|
+
styles.widgetFrame,
|
|
165
|
+
{
|
|
166
|
+
width: dim.width,
|
|
167
|
+
height: dim.height,
|
|
168
|
+
},
|
|
169
|
+
]}
|
|
170
|
+
>
|
|
171
|
+
{rootNode ? (
|
|
172
|
+
renderPreviewNode(rootNode, onAction, true, 'column')
|
|
173
|
+
) : (
|
|
174
|
+
<View style={styles.emptyContainer}>
|
|
175
|
+
<RNText style={styles.emptyText}>Chưa có cấu hình layout cho kích thước {activeSize}</RNText>
|
|
176
|
+
</View>
|
|
177
|
+
)}
|
|
178
|
+
</View>
|
|
179
|
+
</View>
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** Alias tương thích */
|
|
184
|
+
export const VoltraWidgetPreview = WidgetPreview;
|
|
185
|
+
|
|
186
|
+
// ─── RECURSIVE NODE RENDERER ─────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
function renderPreviewNode(
|
|
189
|
+
node: any,
|
|
190
|
+
onAction?: (actionId: string) => void,
|
|
191
|
+
isRoot: boolean = false,
|
|
192
|
+
parentDirection: 'row' | 'column' = 'column'
|
|
193
|
+
): React.ReactNode {
|
|
194
|
+
if (!node) return null;
|
|
195
|
+
|
|
196
|
+
if (React.isValidElement(node) || (node as any)?.$$typeof || typeof (node as any)?.type === 'function') {
|
|
197
|
+
const converted = renderToWidget(node);
|
|
198
|
+
if (!converted) return null;
|
|
199
|
+
return renderPreviewNode(converted, onAction, isRoot, parentDirection);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
switch (node.type) {
|
|
203
|
+
case 'VStack': {
|
|
204
|
+
const vnode = node as VStackNode;
|
|
205
|
+
const p = extractPropsAndStyle(vnode.props);
|
|
206
|
+
const containerStyle: ViewStyle = {
|
|
207
|
+
flexDirection: 'column',
|
|
208
|
+
padding: p.padding ?? (p.paddingHorizontal || p.paddingVertical ? undefined : 0),
|
|
209
|
+
paddingHorizontal: p.paddingHorizontal,
|
|
210
|
+
paddingVertical: p.paddingVertical,
|
|
211
|
+
backgroundColor: parseDynamicColor(p.backgroundColor) || 'transparent',
|
|
212
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 0,
|
|
213
|
+
borderWidth: p.borderWidth,
|
|
214
|
+
borderColor: parseDynamicColor(p.borderColor),
|
|
215
|
+
rowGap: p.spacing ?? 0,
|
|
216
|
+
alignItems:
|
|
217
|
+
p.alignment === 'center'
|
|
218
|
+
? 'center'
|
|
219
|
+
: p.alignment === 'trailing'
|
|
220
|
+
? 'flex-end'
|
|
221
|
+
: 'flex-start',
|
|
222
|
+
justifyContent:
|
|
223
|
+
p.justifyContent === 'center'
|
|
224
|
+
? 'center'
|
|
225
|
+
: p.justifyContent === 'spaceBetween'
|
|
226
|
+
? 'space-between'
|
|
227
|
+
: p.justifyContent === 'spaceAround'
|
|
228
|
+
? 'space-around'
|
|
229
|
+
: p.justifyContent === 'flexEnd'
|
|
230
|
+
? 'flex-end'
|
|
231
|
+
: 'flex-start',
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
if (isRoot) {
|
|
235
|
+
containerStyle.width = '100%';
|
|
236
|
+
containerStyle.height = '100%';
|
|
237
|
+
containerStyle.flex = 1;
|
|
238
|
+
} else if (parentDirection === 'row') {
|
|
239
|
+
if (p.flex !== undefined) {
|
|
240
|
+
containerStyle.flex = p.flex;
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
containerStyle.alignSelf = 'stretch';
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<View style={containerStyle}>
|
|
248
|
+
{vnode.children?.map((child, idx) => (
|
|
249
|
+
<React.Fragment key={idx}>
|
|
250
|
+
{renderPreviewNode(child, onAction, false, 'column')}
|
|
251
|
+
</React.Fragment>
|
|
252
|
+
))}
|
|
253
|
+
</View>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
case 'HStack': {
|
|
258
|
+
const hnode = node as HStackNode;
|
|
259
|
+
const p = extractPropsAndStyle(hnode.props);
|
|
260
|
+
const containerStyle: ViewStyle = {
|
|
261
|
+
flexDirection: 'row',
|
|
262
|
+
padding: p.padding,
|
|
263
|
+
paddingHorizontal: p.paddingHorizontal,
|
|
264
|
+
paddingVertical: p.paddingVertical,
|
|
265
|
+
backgroundColor: parseDynamicColor(p.backgroundColor) || 'transparent',
|
|
266
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 0,
|
|
267
|
+
borderWidth: p.borderWidth,
|
|
268
|
+
borderColor: parseDynamicColor(p.borderColor),
|
|
269
|
+
columnGap: p.spacing ?? 0,
|
|
270
|
+
alignItems:
|
|
271
|
+
p.alignment === 'center'
|
|
272
|
+
? 'center'
|
|
273
|
+
: p.alignment === 'trailing'
|
|
274
|
+
? 'flex-end'
|
|
275
|
+
: p.alignment === 'top'
|
|
276
|
+
? 'flex-start'
|
|
277
|
+
: 'center',
|
|
278
|
+
justifyContent:
|
|
279
|
+
p.justifyContent === 'center'
|
|
280
|
+
? 'center'
|
|
281
|
+
: p.justifyContent === 'spaceBetween'
|
|
282
|
+
? 'space-between'
|
|
283
|
+
: p.justifyContent === 'spaceAround'
|
|
284
|
+
? 'space-around'
|
|
285
|
+
: p.justifyContent === 'flexEnd'
|
|
286
|
+
? 'flex-end'
|
|
287
|
+
: 'flex-start',
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
if (isRoot) {
|
|
291
|
+
containerStyle.width = '100%';
|
|
292
|
+
containerStyle.height = '100%';
|
|
293
|
+
} else if (parentDirection === 'column') {
|
|
294
|
+
containerStyle.alignSelf = 'stretch';
|
|
295
|
+
} else if (p.flex !== undefined) {
|
|
296
|
+
containerStyle.flex = p.flex;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return (
|
|
300
|
+
<View style={containerStyle}>
|
|
301
|
+
{hnode.children?.map((child, idx) => (
|
|
302
|
+
<React.Fragment key={idx}>
|
|
303
|
+
{renderPreviewNode(child, onAction, false, 'row')}
|
|
304
|
+
</React.Fragment>
|
|
305
|
+
))}
|
|
306
|
+
</View>
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
case 'ZStack': {
|
|
311
|
+
const znode = node as any;
|
|
312
|
+
const p = extractPropsAndStyle(znode.props);
|
|
313
|
+
return (
|
|
314
|
+
<View
|
|
315
|
+
style={{
|
|
316
|
+
position: 'relative',
|
|
317
|
+
width: isRoot ? '100%' : undefined,
|
|
318
|
+
height: isRoot ? '100%' : undefined,
|
|
319
|
+
alignSelf: isRoot ? undefined : 'stretch',
|
|
320
|
+
alignItems: 'center',
|
|
321
|
+
justifyContent: 'center',
|
|
322
|
+
backgroundColor: parseDynamicColor(p.backgroundColor) || 'transparent',
|
|
323
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 0,
|
|
324
|
+
}}
|
|
325
|
+
>
|
|
326
|
+
{znode.children?.map((child: any, idx: number) => (
|
|
327
|
+
<View
|
|
328
|
+
key={idx}
|
|
329
|
+
style={
|
|
330
|
+
idx === 0
|
|
331
|
+
? { width: '100%', height: '100%' }
|
|
332
|
+
: { ...StyleSheet.absoluteFillObject, alignItems: 'center', justifyContent: 'center' }
|
|
333
|
+
}
|
|
334
|
+
>
|
|
335
|
+
{renderPreviewNode(child, onAction, false, parentDirection)}
|
|
336
|
+
</View>
|
|
337
|
+
))}
|
|
338
|
+
</View>
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
case 'Grid': {
|
|
343
|
+
const gnode = node as any;
|
|
344
|
+
const p = extractPropsAndStyle(gnode.props);
|
|
345
|
+
const cols = p.columns ?? 2;
|
|
346
|
+
return (
|
|
347
|
+
<View
|
|
348
|
+
style={{
|
|
349
|
+
flexDirection: 'row',
|
|
350
|
+
flexWrap: 'wrap',
|
|
351
|
+
gap: p.spacing ?? 6,
|
|
352
|
+
width: '100%',
|
|
353
|
+
alignSelf: 'stretch',
|
|
354
|
+
}}
|
|
355
|
+
>
|
|
356
|
+
{gnode.children?.map((child: any, idx: number) => (
|
|
357
|
+
<View key={idx} style={{ flexBasis: `${Math.floor(100 / cols) - 3}%` }}>
|
|
358
|
+
{renderPreviewNode(child, onAction, false, 'column')}
|
|
359
|
+
</View>
|
|
360
|
+
))}
|
|
361
|
+
</View>
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
case 'Text': {
|
|
366
|
+
const tnode = node as TextNode;
|
|
367
|
+
const p = extractPropsAndStyle(tnode.props);
|
|
368
|
+
const textStyle: RNTextStyle = {
|
|
369
|
+
color: parseDynamicColor(p.color) || '#FFFFFF',
|
|
370
|
+
fontSize: p.fontSize ?? 14,
|
|
371
|
+
fontWeight:
|
|
372
|
+
p.fontWeight === 'bold' || p.fontWeight === 'heavy'
|
|
373
|
+
? 'bold'
|
|
374
|
+
: p.fontWeight === 'semibold'
|
|
375
|
+
? '600'
|
|
376
|
+
: p.fontWeight === 'medium'
|
|
377
|
+
? '500'
|
|
378
|
+
: 'normal',
|
|
379
|
+
textAlign: p.textAlign ?? 'left',
|
|
380
|
+
};
|
|
381
|
+
return (
|
|
382
|
+
<RNText
|
|
383
|
+
numberOfLines={p.numberOfLines}
|
|
384
|
+
style={[
|
|
385
|
+
textStyle,
|
|
386
|
+
parentDirection === 'row' ? { flexShrink: 1 } : undefined,
|
|
387
|
+
]}
|
|
388
|
+
>
|
|
389
|
+
{p.text}
|
|
390
|
+
</RNText>
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
case 'Button': {
|
|
395
|
+
const bnode = node as ButtonNode;
|
|
396
|
+
const p = bnode.props;
|
|
397
|
+
const btnStyle: ViewStyle = {
|
|
398
|
+
backgroundColor: parseDynamicColor(p.backgroundColor) || '#2563EB',
|
|
399
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 8,
|
|
400
|
+
paddingHorizontal: p.paddingHorizontal ?? p.padding ?? 12,
|
|
401
|
+
paddingVertical: p.paddingVertical ?? p.padding ?? 6,
|
|
402
|
+
alignItems: 'center',
|
|
403
|
+
justifyContent: 'center',
|
|
404
|
+
alignSelf: p.fullWidth ? 'stretch' : 'auto',
|
|
405
|
+
width: p.fullWidth ? '100%' : undefined,
|
|
406
|
+
};
|
|
407
|
+
const textStyle: RNTextStyle = {
|
|
408
|
+
color: parseDynamicColor(p.textColor) || '#FFFFFF',
|
|
409
|
+
fontSize: p.fontSize ?? 12,
|
|
410
|
+
fontWeight: (p.fontWeight as any) ?? '600',
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
return (
|
|
414
|
+
<TouchableOpacity
|
|
415
|
+
activeOpacity={0.7}
|
|
416
|
+
onPress={() => {
|
|
417
|
+
if (p.actionId && onAction) {
|
|
418
|
+
onAction(p.actionId);
|
|
419
|
+
}
|
|
420
|
+
}}
|
|
421
|
+
style={btnStyle}
|
|
422
|
+
>
|
|
423
|
+
<RNText numberOfLines={1} style={textStyle}>
|
|
424
|
+
{p.title}
|
|
425
|
+
</RNText>
|
|
426
|
+
</TouchableOpacity>
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
case 'Badge': {
|
|
431
|
+
const bnode = node as BadgeNode;
|
|
432
|
+
const p = bnode.props;
|
|
433
|
+
return (
|
|
434
|
+
<View
|
|
435
|
+
style={{
|
|
436
|
+
backgroundColor: parseDynamicColor(p.backgroundColor) || '#064E3B',
|
|
437
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 6,
|
|
438
|
+
paddingHorizontal: p.paddingHorizontal ?? 6,
|
|
439
|
+
paddingVertical: p.paddingVertical ?? 2,
|
|
440
|
+
flexShrink: 0,
|
|
441
|
+
alignSelf: 'center',
|
|
442
|
+
}}
|
|
443
|
+
>
|
|
444
|
+
<RNText
|
|
445
|
+
numberOfLines={1}
|
|
446
|
+
style={{
|
|
447
|
+
color: parseDynamicColor(p.textColor) || '#6EE7B7',
|
|
448
|
+
fontSize: p.fontSize ?? 10,
|
|
449
|
+
fontWeight: '600',
|
|
450
|
+
}}
|
|
451
|
+
>
|
|
452
|
+
{p.text}
|
|
453
|
+
</RNText>
|
|
454
|
+
</View>
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
case 'Progress': {
|
|
459
|
+
const pnode = node as ProgressNode;
|
|
460
|
+
const p = pnode.props || {};
|
|
461
|
+
const val = Math.max(0, Math.min(1, p.value ?? 0.5));
|
|
462
|
+
const height = p.height ?? 6;
|
|
463
|
+
const tint = parseDynamicColor(p.tintColor) || '#2563EB';
|
|
464
|
+
const track = parseDynamicColor(p.trackColor) || '#334155';
|
|
465
|
+
|
|
466
|
+
if (p.style === 'segmented' && (p.segments ?? 4) > 1) {
|
|
467
|
+
const segs = p.segments ?? 4;
|
|
468
|
+
const filled = Math.round(val * segs);
|
|
469
|
+
return (
|
|
470
|
+
<View style={{ flexDirection: 'row', gap: 4, width: '100%', height, alignSelf: 'stretch' }}>
|
|
471
|
+
{Array.from({ length: segs }).map((_, idx) => (
|
|
472
|
+
<View
|
|
473
|
+
key={idx}
|
|
474
|
+
style={{
|
|
475
|
+
flex: 1,
|
|
476
|
+
height,
|
|
477
|
+
backgroundColor: idx < filled ? tint : track,
|
|
478
|
+
borderRadius: height / 2,
|
|
479
|
+
}}
|
|
480
|
+
/>
|
|
481
|
+
))}
|
|
482
|
+
</View>
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return (
|
|
487
|
+
<View style={{ width: '100%', height, backgroundColor: track, borderRadius: height / 2, alignSelf: 'stretch' }}>
|
|
488
|
+
<View
|
|
489
|
+
style={{
|
|
490
|
+
width: `${val * 100}%`,
|
|
491
|
+
height,
|
|
492
|
+
backgroundColor: tint,
|
|
493
|
+
borderRadius: height / 2,
|
|
494
|
+
}}
|
|
495
|
+
/>
|
|
496
|
+
</View>
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
case 'Timer': {
|
|
501
|
+
const tnode = node as TimerNode;
|
|
502
|
+
const p = tnode.props || {};
|
|
503
|
+
return (
|
|
504
|
+
<RNText
|
|
505
|
+
style={[
|
|
506
|
+
{ color: parseDynamicColor(p.color) || '#2563EB', fontSize: p.fontSize ?? 14, fontWeight: 'bold' },
|
|
507
|
+
parentDirection === 'row' ? { flexShrink: 0 } : undefined,
|
|
508
|
+
]}
|
|
509
|
+
>
|
|
510
|
+
{p.prefix || ''}1:29:55{p.suffix || ''}
|
|
511
|
+
</RNText>
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
case 'Image': {
|
|
516
|
+
const inode = node as ImageNode;
|
|
517
|
+
const p = inode.props || {};
|
|
518
|
+
const width = p.width ?? 32;
|
|
519
|
+
const height = p.height ?? 32;
|
|
520
|
+
const uri = p.url || (p.base64 ? `data:image/png;base64,${p.base64}` : null);
|
|
521
|
+
|
|
522
|
+
if (uri) {
|
|
523
|
+
return (
|
|
524
|
+
<RNImage
|
|
525
|
+
source={{ uri }}
|
|
526
|
+
style={{
|
|
527
|
+
width,
|
|
528
|
+
height,
|
|
529
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 0,
|
|
530
|
+
}}
|
|
531
|
+
/>
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
return (
|
|
535
|
+
<View
|
|
536
|
+
style={{
|
|
537
|
+
width,
|
|
538
|
+
height,
|
|
539
|
+
backgroundColor: '#334155',
|
|
540
|
+
borderRadius: p.borderRadius ?? p.cornerRadius ?? 4,
|
|
541
|
+
alignItems: 'center',
|
|
542
|
+
justifyContent: 'center',
|
|
543
|
+
}}
|
|
544
|
+
>
|
|
545
|
+
<RNText style={{ color: '#94A3B8', fontSize: 10 }}>IMG</RNText>
|
|
546
|
+
</View>
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
case 'Spacer': {
|
|
551
|
+
const snode = node as any;
|
|
552
|
+
const p = snode.props || {};
|
|
553
|
+
const minLen = p?.minLength ?? 4;
|
|
554
|
+
return (
|
|
555
|
+
<View
|
|
556
|
+
style={
|
|
557
|
+
parentDirection === 'row'
|
|
558
|
+
? { minWidth: minLen, flex: 1 }
|
|
559
|
+
: { minHeight: minLen, flex: 1 }
|
|
560
|
+
}
|
|
561
|
+
/>
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
case 'Divider': {
|
|
566
|
+
const p = (node as any).props || {};
|
|
567
|
+
const color = parseDynamicColor(p.color) || '#334155';
|
|
568
|
+
return (
|
|
569
|
+
<View
|
|
570
|
+
style={
|
|
571
|
+
parentDirection === 'row'
|
|
572
|
+
? { width: 1, height: '100%', backgroundColor: color }
|
|
573
|
+
: { width: '100%', height: 1, backgroundColor: color, alignSelf: 'stretch' }
|
|
574
|
+
}
|
|
575
|
+
/>
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
case 'Link': {
|
|
580
|
+
const lnode = node as any;
|
|
581
|
+
const p = lnode.props || {};
|
|
582
|
+
return (
|
|
583
|
+
<TouchableOpacity
|
|
584
|
+
activeOpacity={0.7}
|
|
585
|
+
onPress={() => {
|
|
586
|
+
if (p.url && onAction) onAction(p.url);
|
|
587
|
+
}}
|
|
588
|
+
style={{ flexDirection: 'row', alignItems: 'center' }}
|
|
589
|
+
>
|
|
590
|
+
{lnode.children?.map((child: any, idx: number) => (
|
|
591
|
+
<React.Fragment key={idx}>
|
|
592
|
+
{renderPreviewNode(child, onAction, false, parentDirection)}
|
|
593
|
+
</React.Fragment>
|
|
594
|
+
))}
|
|
595
|
+
</TouchableOpacity>
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
case 'Equalizer': {
|
|
600
|
+
const enode = node as any;
|
|
601
|
+
const p = enode.props || {};
|
|
602
|
+
const tint = parseDynamicColor(p.color) || '#38BDF8';
|
|
603
|
+
return (
|
|
604
|
+
<View style={{ flexDirection: 'row', gap: 3, alignItems: 'flex-end', height: 16 }}>
|
|
605
|
+
{[12, 16, 8, 14].map((h, i) => (
|
|
606
|
+
<View key={i} style={{ width: 3, height: h, backgroundColor: tint, borderRadius: 1.5 }} />
|
|
607
|
+
))}
|
|
608
|
+
</View>
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
case 'Gauge': {
|
|
613
|
+
const gnode = node as any;
|
|
614
|
+
const p = gnode.props || {};
|
|
615
|
+
const val = Math.round((p.value ?? 0.75) * 100);
|
|
616
|
+
const tint = parseDynamicColor(p.tintColor) || '#10B981';
|
|
617
|
+
return (
|
|
618
|
+
<View style={{ alignItems: 'center', justifyContent: 'center' }}>
|
|
619
|
+
<RNText style={{ color: tint, fontSize: 13, fontWeight: 'bold' }}>{val}%</RNText>
|
|
620
|
+
</View>
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
case 'Chart': {
|
|
625
|
+
const cnode = node as any;
|
|
626
|
+
const p = cnode.props || {};
|
|
627
|
+
const values: number[] = Array.isArray(p.values) ? p.values : [20, 45, 60, 80, 50];
|
|
628
|
+
const maxVal = Math.max(...values, 1);
|
|
629
|
+
const tint = parseDynamicColor(p.color) || '#38BDF8';
|
|
630
|
+
return (
|
|
631
|
+
<View style={{ flexDirection: 'row', gap: 4, alignItems: 'flex-end', height: 28, width: '100%', alignSelf: 'stretch' }}>
|
|
632
|
+
{values.map((v, i) => (
|
|
633
|
+
<View
|
|
634
|
+
key={i}
|
|
635
|
+
style={{
|
|
636
|
+
flex: 1,
|
|
637
|
+
height: `${Math.max(15, (v / maxVal) * 100)}%`,
|
|
638
|
+
backgroundColor: tint,
|
|
639
|
+
borderRadius: 2,
|
|
640
|
+
}}
|
|
641
|
+
/>
|
|
642
|
+
))}
|
|
643
|
+
</View>
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
default:
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const styles = StyleSheet.create({
|
|
653
|
+
container: {
|
|
654
|
+
alignItems: 'center',
|
|
655
|
+
marginVertical: 12,
|
|
656
|
+
},
|
|
657
|
+
tabBar: {
|
|
658
|
+
flexDirection: 'row',
|
|
659
|
+
backgroundColor: '#1E293B',
|
|
660
|
+
borderRadius: 8,
|
|
661
|
+
padding: 3,
|
|
662
|
+
marginBottom: 8,
|
|
663
|
+
},
|
|
664
|
+
tabItem: {
|
|
665
|
+
paddingHorizontal: 12,
|
|
666
|
+
paddingVertical: 4,
|
|
667
|
+
borderRadius: 6,
|
|
668
|
+
},
|
|
669
|
+
tabItemActive: {
|
|
670
|
+
backgroundColor: '#2563EB',
|
|
671
|
+
},
|
|
672
|
+
tabText: {
|
|
673
|
+
color: '#94A3B8',
|
|
674
|
+
fontSize: 11,
|
|
675
|
+
fontWeight: 'bold',
|
|
676
|
+
},
|
|
677
|
+
tabTextActive: {
|
|
678
|
+
color: '#FFFFFF',
|
|
679
|
+
},
|
|
680
|
+
badgeRow: {
|
|
681
|
+
marginBottom: 6,
|
|
682
|
+
},
|
|
683
|
+
specBadge: {
|
|
684
|
+
backgroundColor: 'rgba(15, 23, 42, 0.6)',
|
|
685
|
+
paddingHorizontal: 8,
|
|
686
|
+
paddingVertical: 2,
|
|
687
|
+
borderRadius: 10,
|
|
688
|
+
borderWidth: 1,
|
|
689
|
+
borderColor: 'rgba(255, 255, 255, 0.1)',
|
|
690
|
+
},
|
|
691
|
+
specBadgeText: {
|
|
692
|
+
color: '#94A3B8',
|
|
693
|
+
fontSize: 10,
|
|
694
|
+
fontWeight: '600',
|
|
695
|
+
},
|
|
696
|
+
widgetFrame: {
|
|
697
|
+
borderRadius: 22,
|
|
698
|
+
overflow: 'hidden',
|
|
699
|
+
backgroundColor: '#0F172A',
|
|
700
|
+
shadowColor: '#000',
|
|
701
|
+
shadowOffset: { width: 0, height: 6 },
|
|
702
|
+
shadowOpacity: 0.25,
|
|
703
|
+
shadowRadius: 12,
|
|
704
|
+
elevation: 8,
|
|
705
|
+
borderWidth: 1,
|
|
706
|
+
borderColor: 'rgba(255, 255, 255, 0.12)',
|
|
707
|
+
},
|
|
708
|
+
emptyContainer: {
|
|
709
|
+
flex: 1,
|
|
710
|
+
alignItems: 'center',
|
|
711
|
+
justifyContent: 'center',
|
|
712
|
+
padding: 16,
|
|
713
|
+
},
|
|
714
|
+
emptyText: {
|
|
715
|
+
color: '#64748B',
|
|
716
|
+
fontSize: 12,
|
|
717
|
+
textAlign: 'center',
|
|
718
|
+
},
|
|
719
|
+
});
|