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
@@ -0,0 +1,1223 @@
1
+ package com.reactnativeliveactivity
2
+
3
+ import android.content.Context
4
+ import android.graphics.Bitmap
5
+ import android.graphics.BitmapShader
6
+ import android.graphics.Canvas
7
+ import android.graphics.Color
8
+ import android.graphics.Matrix
9
+ import android.graphics.Paint
10
+ import android.graphics.Path
11
+ import android.graphics.RectF
12
+ import android.graphics.Shader
13
+ import android.graphics.Typeface
14
+ import org.json.JSONObject
15
+ import java.util.Locale
16
+ import kotlin.math.abs
17
+ import kotlin.math.max
18
+ import kotlin.math.min
19
+ import kotlin.math.roundToInt
20
+
21
+ data class ButtonActionBinding(
22
+ val actionId: String,
23
+ val deepLink: String?,
24
+ val zone: String // "top", "mid", "bot"
25
+ )
26
+
27
+ object DynamicWidgetCanvasRenderer {
28
+
29
+ private fun isFlexibleSpacer(node: JSONObject?): Boolean {
30
+ if (node == null || node.optString("type") != "Spacer") return false
31
+ val props = node.optJSONObject("props") ?: return true
32
+ return !props.has("minLength")
33
+ }
34
+
35
+ fun renderWidgetBitmap(
36
+ context: Context,
37
+ rootNode: JSONObject?,
38
+ widthPx: Int,
39
+ heightPx: Int,
40
+ baseDensity: Float,
41
+ family: String,
42
+ bindings: MutableList<ButtonActionBinding>
43
+ ): Bitmap {
44
+ val bmp = Bitmap.createBitmap(widthPx, heightPx, Bitmap.Config.ARGB_8888)
45
+ val canvas = Canvas(bmp)
46
+ val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
47
+
48
+ val rootBgHex = rootNode?.optJSONObject("props")?.optString("backgroundColor", "#FFFFFF")
49
+ ?.ifEmpty { "#FFFFFF" } ?: "#FFFFFF"
50
+ paint.color = parseHexColor(rootBgHex, Color.WHITE)
51
+ val cardRect = RectF(0f, 0f, widthPx.toFloat(), heightPx.toFloat())
52
+ val outerRadius = (if (family == "small") 22f else 24f) * baseDensity
53
+ canvas.drawRoundRect(cardRect, outerRadius, outerRadius, paint)
54
+
55
+ if (rootNode == null) {
56
+ paint.color = Color.parseColor("#0F172A")
57
+ paint.textSize = 15f * baseDensity
58
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
59
+ paint.textAlign = Paint.Align.CENTER
60
+ canvas.drawText("Live Widgets", widthPx / 2f, heightPx / 2f - 6f * baseDensity, paint)
61
+
62
+ paint.color = Color.parseColor("#64748B")
63
+ paint.textSize = 11f * baseDensity
64
+ paint.typeface = Typeface.create("sans-serif", Typeface.NORMAL)
65
+ canvas.drawText("Chọn 1 mẫu UI trong app", widthPx / 2f, heightPx / 2f + 14f * baseDensity, paint)
66
+ return bmp
67
+ }
68
+
69
+ val naturalH = measureNodeHeight(context, rootNode, widthPx.toFloat(), baseDensity)
70
+ val fitScale = if (naturalH > heightPx * 0.96f && naturalH > 0f) {
71
+ ((heightPx * 0.95f) / naturalH).coerceIn(0.62f, 1.0f)
72
+ } else {
73
+ 1.0f
74
+ }
75
+ val effectiveDensity = baseDensity * fitScale
76
+ val scaledNaturalH = measureNodeHeight(context, rootNode, widthPx.toFloat(), effectiveDensity)
77
+
78
+ var hasRootFlexSpacer = false
79
+ val rootChildren = rootNode.optJSONArray("children")
80
+ if (rootChildren != null) {
81
+ for (i in 0 until rootChildren.length()) {
82
+ if (isFlexibleSpacer(rootChildren.optJSONObject(i))) {
83
+ hasRootFlexSpacer = true
84
+ break
85
+ }
86
+ }
87
+ }
88
+ val startTop = if (!hasRootFlexSpacer && heightPx > scaledNaturalH) {
89
+ ((heightPx - scaledNaturalH) / 2f).coerceAtLeast(0f)
90
+ } else {
91
+ 0f
92
+ }
93
+
94
+ drawNode(
95
+ context = context,
96
+ canvas = canvas,
97
+ node = rootNode,
98
+ left = 0f,
99
+ top = startTop,
100
+ availWidth = widthPx.toFloat(),
101
+ availHeight = (heightPx - startTop).coerceAtLeast(scaledNaturalH),
102
+ totalHeight = heightPx.toFloat(),
103
+ density = effectiveDensity,
104
+ isRoot = true,
105
+ bindings = bindings
106
+ )
107
+ return bmp
108
+ }
109
+
110
+ private fun drawNode(
111
+ context: Context,
112
+ canvas: Canvas,
113
+ node: JSONObject,
114
+ left: Float,
115
+ top: Float,
116
+ availWidth: Float,
117
+ availHeight: Float,
118
+ totalHeight: Float,
119
+ density: Float,
120
+ isRoot: Boolean,
121
+ bindings: MutableList<ButtonActionBinding>
122
+ ): Float {
123
+ val type = node.optString("type", "")
124
+ val props = node.optJSONObject("props")
125
+ val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
126
+
127
+ return when (type) {
128
+ "VStack" -> {
129
+ val pad = (props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density
130
+ val spacing = (props?.optDouble("spacing", 6.0) ?: 6.0).toFloat() * density
131
+ val bgHex = props?.optString("backgroundColor", "") ?: ""
132
+ val borderHex = props?.optString("borderColor", "") ?: ""
133
+ val borderW = (props?.optDouble("borderWidth", 0.0) ?: 0.0).toFloat() * density
134
+ val radius = (props?.optDouble("borderRadius", 10.0) ?: 10.0).toFloat() * density
135
+
136
+ val children = node.optJSONArray("children")
137
+ val innerW = (availWidth - pad * 2).coerceAtLeast(10f)
138
+ val measuredH = measureNodeHeight(context, node, availWidth, density)
139
+ val boxH = if (isRoot) max(availHeight, measuredH) else (measuredH + 2f * density)
140
+
141
+ if (!isRoot && bgHex.isNotEmpty()) {
142
+ val rect = RectF(left, top, left + availWidth, top + boxH)
143
+ paint.style = Paint.Style.FILL
144
+ paint.color = parseHexColor(bgHex, Color.TRANSPARENT)
145
+ canvas.drawRoundRect(rect, radius, radius, paint)
146
+
147
+ if (borderW > 0f && borderHex.isNotEmpty()) {
148
+ paint.style = Paint.Style.STROKE
149
+ paint.strokeWidth = borderW
150
+ paint.color = parseHexColor(borderHex, Color.LTGRAY)
151
+ canvas.drawRoundRect(rect, radius, radius, paint)
152
+ }
153
+ }
154
+
155
+ var flexSpacerCount = 0
156
+ if (isRoot && children != null && availHeight > measuredH) {
157
+ for (i in 0 until children.length()) {
158
+ if (isFlexibleSpacer(children.optJSONObject(i))) {
159
+ flexSpacerCount++
160
+ }
161
+ }
162
+ }
163
+ val extraPerSpacer = if (flexSpacerCount > 0) {
164
+ ((availHeight - measuredH) / flexSpacerCount).coerceAtLeast(0f)
165
+ } else 0f
166
+
167
+ var curY = top + pad
168
+ if (children != null) {
169
+ val count = children.length()
170
+ for (i in 0 until count) {
171
+ val child = children.optJSONObject(i) ?: continue
172
+ val childType = child.optString("type")
173
+ if (childType == "Spacer") {
174
+ val cProps = child.optJSONObject("props")
175
+ val minLen = (cProps?.optDouble("minLength", 4.0) ?: 4.0).toFloat() * density
176
+ val isFlex = isRoot && isFlexibleSpacer(child)
177
+ curY += minLen + (if (isFlex) extraPerSpacer else 0f)
178
+ if (i < count - 1) curY += spacing
179
+ } else {
180
+ val usedH = drawNode(
181
+ context = context,
182
+ canvas = canvas,
183
+ node = child,
184
+ left = left + pad,
185
+ top = curY,
186
+ availWidth = innerW,
187
+ availHeight = measuredH,
188
+ totalHeight = totalHeight,
189
+ density = density,
190
+ isRoot = false,
191
+ bindings = bindings
192
+ )
193
+ curY += usedH
194
+ if (i < count - 1) curY += spacing
195
+ }
196
+ }
197
+ }
198
+ if (isRoot) availHeight else boxH
199
+ }
200
+
201
+ "HStack" -> {
202
+ val pad = (props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density
203
+ val spacing = (props?.optDouble("spacing", 6.0) ?: 6.0).toFloat() * density
204
+ val bgHex = props?.optString("backgroundColor", "") ?: ""
205
+ val radius = (props?.optDouble("borderRadius", 8.0) ?: 8.0).toFloat() * density
206
+ val rowH = measureNodeHeight(context, node, availWidth, density)
207
+
208
+ if (!isRoot && bgHex.isNotEmpty()) {
209
+ paint.style = Paint.Style.FILL
210
+ paint.color = parseHexColor(bgHex, Color.TRANSPARENT)
211
+ canvas.drawRoundRect(
212
+ RectF(left, top, left + availWidth, top + rowH),
213
+ radius,
214
+ radius,
215
+ paint
216
+ )
217
+ }
218
+
219
+ val children = node.optJSONArray("children")
220
+ if (children != null && children.length() > 0) {
221
+ val innerW = (availWidth - pad * 2).coerceAtLeast(10f)
222
+ val innerH = (rowH - pad * 2).coerceAtLeast(10f)
223
+
224
+ var flexSpacerIndex = -1
225
+ for (i in 0 until children.length()) {
226
+ if (isFlexibleSpacer(children.optJSONObject(i))) {
227
+ flexSpacerIndex = i
228
+ break
229
+ }
230
+ }
231
+
232
+ if (flexSpacerIndex >= 0 && children.length() >= 2) {
233
+ val rightWidths = mutableMapOf<Int, Float>()
234
+ for (i in children.length() - 1 downTo flexSpacerIndex + 1) {
235
+ val c = children.optJSONObject(i) ?: continue
236
+ rightWidths[i] = measureNodeWidth(context, c, innerW, density)
237
+ }
238
+
239
+ var curRight = left + availWidth - pad
240
+ for (i in children.length() - 1 downTo flexSpacerIndex + 1) {
241
+ val c = children.optJSONObject(i) ?: continue
242
+ if (c.optString("type") == "Spacer") {
243
+ val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
244
+ curRight -= gapW + spacing
245
+ continue
246
+ }
247
+ val w = rightWidths[i] ?: 0f
248
+ val childH = measureNodeHeight(context, c, w, density)
249
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
250
+ curRight -= w
251
+ drawNode(context, canvas, c, curRight, childTop, w, childH, totalHeight, density, false, bindings)
252
+ curRight -= spacing
253
+ }
254
+
255
+ val maxLeftAreaW = (curRight - (left + pad)).coerceAtLeast(30f)
256
+ var curLeft = left + pad
257
+ for (i in 0 until flexSpacerIndex) {
258
+ val c = children.optJSONObject(i) ?: continue
259
+ if (c.optString("type") == "Spacer") {
260
+ val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
261
+ curLeft += gapW + spacing
262
+ continue
263
+ }
264
+ val remainingForLeft = (left + pad + maxLeftAreaW - curLeft).coerceAtLeast(20f)
265
+ val w = if (i == flexSpacerIndex - 1 && c.optString("type") == "VStack") {
266
+ remainingForLeft
267
+ } else {
268
+ min(remainingForLeft, measureNodeWidth(context, c, maxLeftAreaW, density))
269
+ }
270
+ val childH = measureNodeHeight(context, c, w, density)
271
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
272
+ drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
273
+ curLeft += w + spacing
274
+ }
275
+ } else {
276
+ var curLeft = left + pad
277
+ for (i in 0 until children.length()) {
278
+ val c = children.optJSONObject(i) ?: continue
279
+ if (c.optString("type") == "Spacer") {
280
+ val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
281
+ curLeft += gapW
282
+ continue
283
+ }
284
+ val remainingW = (left + availWidth - pad - curLeft).coerceAtLeast(20f)
285
+ val w = if (i == children.length() - 1 && c.optString("type") in listOf("VStack", "Progress", "Text")) {
286
+ remainingW
287
+ } else {
288
+ min(remainingW, measureNodeWidth(context, c, innerW, density))
289
+ }
290
+ val childH = measureNodeHeight(context, c, w, density)
291
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
292
+ drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
293
+ curLeft += w + spacing
294
+ }
295
+ }
296
+ }
297
+ rowH
298
+ }
299
+
300
+ "Text" -> {
301
+ val text = props?.optString("text", "") ?: ""
302
+ val fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density
303
+ val weight = props?.optString("fontWeight", "regular") ?: "regular"
304
+ val colorHex = props?.optString("color", "#0F172A") ?: "#0F172A"
305
+ val toggleKey = props?.optString("strikethroughWhenToggled", "") ?: ""
306
+ val isStruck = toggleKey.isNotEmpty() && DynamicWidgetStateStore.isToggled(context, toggleKey)
307
+
308
+ paint.color = if (isStruck) Color.parseColor("#94A3B8") else parseHexColor(colorHex, Color.BLACK)
309
+ paint.textSize = fontSize
310
+ paint.isStrikeThruText = isStruck
311
+ paint.typeface = when (weight) {
312
+ "bold", "heavy" -> Typeface.create("sans-serif", Typeface.BOLD)
313
+ "semibold", "medium" -> Typeface.create("sans-serif-medium", Typeface.NORMAL)
314
+ else -> Typeface.create("sans-serif", Typeface.NORMAL)
315
+ }
316
+
317
+ val display = ellipsizeText(text, paint, availWidth)
318
+ val fm = paint.fontMetrics
319
+ val textH = fm.descent - fm.ascent
320
+ canvas.drawText(display, left, top - fm.ascent, paint)
321
+ textH + 1.5f * density
322
+ }
323
+
324
+ "Timer" -> {
325
+ val formatted = formatTimerString(props)
326
+ val fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density
327
+ val colorHex = props?.optString("color", "#2563EB") ?: "#2563EB"
328
+
329
+ paint.color = parseHexColor(colorHex, Color.BLUE)
330
+ paint.textSize = fontSize
331
+ paint.typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD)
332
+ val fm = paint.fontMetrics
333
+ canvas.drawText(formatted, left, top - fm.ascent, paint)
334
+ (fm.descent - fm.ascent) + 1.5f * density
335
+ }
336
+
337
+ "Progress" -> {
338
+ val style = props?.optString("style", "bar") ?: "bar"
339
+ val value = (props?.optDouble("value", 0.75) ?: 0.75).toFloat().coerceIn(0f, 1f)
340
+ val tintHex = props?.optString("tintColor", "#2563EB") ?: "#2563EB"
341
+ val trackHex = props?.optString("trackColor", "#CBD5E1") ?: "#CBD5E1"
342
+ val pctColorHex = props?.optString("percentageColor", "")?.ifEmpty { tintHex } ?: tintHex
343
+ val showPct = props?.optBoolean("showPercentage", false) ?: false
344
+ val tintColor = parseHexColor(tintHex, Color.BLUE)
345
+ val trackColor = parseHexColor(trackHex, Color.LTGRAY)
346
+ val pctColor = parseHexColor(pctColorHex, tintColor)
347
+
348
+ when (style) {
349
+ "circular" -> {
350
+ val size = ((props?.optDouble("height", 52.0) ?: 52.0).toFloat() * density)
351
+ val strokeW = (size * 0.12f).coerceAtLeast(3f * density)
352
+ val arcRect = RectF(
353
+ left + strokeW,
354
+ top + strokeW,
355
+ left + size - strokeW,
356
+ top + size - strokeW
357
+ )
358
+ paint.style = Paint.Style.STROKE
359
+ paint.strokeWidth = strokeW
360
+ paint.strokeCap = Paint.Cap.ROUND
361
+ paint.color = trackColor
362
+ canvas.drawOval(arcRect, paint)
363
+
364
+ paint.color = tintColor
365
+ canvas.drawArc(arcRect, -90f, 360f * value, false, paint)
366
+
367
+ if (showPct) {
368
+ paint.style = Paint.Style.FILL
369
+ paint.color = pctColor
370
+ paint.textSize = size * 0.24f
371
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
372
+ paint.textAlign = Paint.Align.CENTER
373
+ val fm = paint.fontMetrics
374
+ val centerY = top + size / 2f - (fm.ascent + fm.descent) / 2f
375
+ canvas.drawText("${(value * 100).roundToInt()}%", left + size / 2f, centerY, paint)
376
+ }
377
+ size + 2f * density
378
+ }
379
+ "segmented" -> {
380
+ val segments = (props?.optInt("segments", 4) ?: 4).coerceAtLeast(1)
381
+ val currentSeg = if (props?.has("currentSegment") == true) {
382
+ props.optInt("currentSegment", 1)
383
+ } else {
384
+ (value * segments).roundToInt()
385
+ }
386
+ val h = ((props?.optDouble("height", 8.0) ?: 8.0).toFloat() * density)
387
+ val gap = ((props?.optDouble("segmentSpacing", 5.0) ?: 5.0).toFloat() * density)
388
+
389
+ val pctStr = "${(value * 100).roundToInt()}%"
390
+ paint.textSize = 11f * density
391
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
392
+ val pctReserve = if (showPct) paint.measureText(pctStr) + 8f * density else 0f
393
+
394
+ val barTotalW = (availWidth - pctReserve).coerceAtLeast(40f)
395
+ val segW = (barTotalW - gap * (segments - 1)) / segments
396
+ val barTop = top + 2f * density
397
+
398
+ for (i in 0 until segments) {
399
+ val segLeft = left + i * (segW + gap)
400
+ paint.style = Paint.Style.FILL
401
+ paint.color = if (i + 1 <= currentSeg) tintColor else trackColor
402
+ canvas.drawRoundRect(
403
+ RectF(segLeft, barTop, segLeft + segW, barTop + h),
404
+ h / 2f,
405
+ h / 2f,
406
+ paint
407
+ )
408
+ }
409
+ if (showPct) {
410
+ paint.color = pctColor
411
+ paint.textAlign = Paint.Align.RIGHT
412
+ val fm = paint.fontMetrics
413
+ val centerY = barTop + h / 2f - (fm.ascent + fm.descent) / 2f
414
+ canvas.drawText(pctStr, left + availWidth, centerY, paint)
415
+ }
416
+ max(h + 4f * density, if (showPct) 13f * density else 0f)
417
+ }
418
+ else -> {
419
+ val h = ((props?.optDouble("height", 8.0) ?: 8.0).toFloat() * density)
420
+ val pctStr = "${(value * 100).roundToInt()}%"
421
+ paint.textSize = 11f * density
422
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
423
+ val pctReserve = if (showPct) paint.measureText(pctStr) + 8f * density else 0f
424
+
425
+ val barW = (availWidth - pctReserve).coerceAtLeast(40f)
426
+ val barTop = top + 2f * density
427
+ paint.style = Paint.Style.FILL
428
+ paint.color = trackColor
429
+ canvas.drawRoundRect(
430
+ RectF(left, barTop, left + barW, barTop + h),
431
+ h / 2f,
432
+ h / 2f,
433
+ paint
434
+ )
435
+ paint.color = tintColor
436
+ canvas.drawRoundRect(
437
+ RectF(left, barTop, left + barW * value, barTop + h),
438
+ h / 2f,
439
+ h / 2f,
440
+ paint
441
+ )
442
+ if (showPct) {
443
+ paint.color = pctColor
444
+ paint.textAlign = Paint.Align.RIGHT
445
+ val fm = paint.fontMetrics
446
+ val centerY = barTop + h / 2f - (fm.ascent + fm.descent) / 2f
447
+ canvas.drawText(pctStr, left + availWidth, centerY, paint)
448
+ }
449
+ max(h + 4f * density, if (showPct) 13f * density else 0f)
450
+ }
451
+ }
452
+ }
453
+
454
+ "Image" -> {
455
+ val w = ((props?.optDouble("width", 32.0) ?: 32.0).toFloat() * density)
456
+ val h = ((props?.optDouble("height", 32.0) ?: 32.0).toFloat() * density)
457
+ val rawRadius = props?.optDouble("cornerRadius", -1.0)?.takeIf { it >= 0 }
458
+ ?: props?.optDouble("borderRadius", 8.0) ?: 8.0
459
+ val radius = min(w / 2f, (rawRadius.toFloat() * density))
460
+ val borderW = ((props?.optDouble("borderWidth", 0.0) ?: 0.0).toFloat() * density)
461
+ val borderHex = props?.optString("borderColor", "#2563EB") ?: "#2563EB"
462
+ val bgHex = props?.optString("backgroundColor", "") ?: ""
463
+ val tintHex = props?.optString("tintColor", "#2563EB") ?: "#2563EB"
464
+ val rect = RectF(left, top, left + w, top + h)
465
+
466
+ val bmp = DynamicWidgetImageCache.resolveNodeBitmap(context, props)
467
+ if (bmp != null) {
468
+ val shader = BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
469
+ val matrix = Matrix()
470
+ val scale = max(w / bmp.width.toFloat(), h / bmp.height.toFloat())
471
+ matrix.setScale(scale, scale)
472
+ matrix.postTranslate(left + (w - bmp.width * scale) / 2f, top + (h - bmp.height * scale) / 2f)
473
+ shader.setLocalMatrix(matrix)
474
+
475
+ paint.shader = shader
476
+ paint.style = Paint.Style.FILL
477
+ canvas.drawRoundRect(rect, radius, radius, paint)
478
+ paint.shader = null
479
+ } else {
480
+ paint.style = Paint.Style.FILL
481
+ paint.color = if (bgHex.isNotEmpty()) parseHexColor(bgHex, Color.LTGRAY) else Color.parseColor("#DBEAFE")
482
+ canvas.drawRoundRect(rect, radius, radius, paint)
483
+
484
+ paint.color = parseHexColor(tintHex, Color.BLUE)
485
+ paint.textSize = min(w, h) * 0.48f
486
+ paint.textAlign = Paint.Align.CENTER
487
+ val symbol = props?.optString("systemName", "") ?: ""
488
+ val glyph = when {
489
+ symbol.contains("person") -> "👤"
490
+ symbol.contains("sparkle") -> "✨"
491
+ symbol.contains("check") -> "✓"
492
+ symbol.contains("bell") -> "🔔"
493
+ symbol.contains("heart") -> "💜"
494
+ symbol.contains("star") -> "★"
495
+ else -> "🖼️"
496
+ }
497
+ val fm = paint.fontMetrics
498
+ val cy = top + h / 2f - (fm.ascent + fm.descent) / 2f
499
+ canvas.drawText(glyph, left + w / 2f, cy, paint)
500
+ }
501
+
502
+ if (borderW > 0f) {
503
+ paint.style = Paint.Style.STROKE
504
+ paint.strokeWidth = borderW
505
+ paint.color = parseHexColor(borderHex, Color.BLUE)
506
+ canvas.drawRoundRect(rect, radius, radius, paint)
507
+ }
508
+ h + 1.5f * density
509
+ }
510
+
511
+ "Button" -> {
512
+ val rawTitle = props?.optString("title", "Button") ?: "Button"
513
+ val actionId = props?.optString("actionId", "") ?: ""
514
+ val deepLink = props?.optString("deepLink", "")?.ifEmpty { null }
515
+ val isToggled = if (actionId.startsWith("toggle:")) {
516
+ DynamicWidgetStateStore.isToggled(context, actionId.removePrefix("toggle:"))
517
+ } else false
518
+
519
+ val displayTitle = if (isToggled) {
520
+ props?.optString("checkedTitle", rawTitle)?.ifEmpty { rawTitle } ?: rawTitle
521
+ } else rawTitle
522
+
523
+ val bgHex = if (isToggled) {
524
+ props?.optString("checkedBackgroundColor", "")
525
+ ?.ifEmpty { props.optString("activeBackgroundColor", "") }
526
+ ?.ifEmpty { props.optString("backgroundColor", "#2563EB") }
527
+ ?: "#DCFCE7"
528
+ } else {
529
+ props?.optString("backgroundColor", "#2563EB") ?: "#2563EB"
530
+ }
531
+
532
+ val textHex = if (isToggled) {
533
+ props?.optString("checkedTextColor", "")
534
+ ?.ifEmpty { props.optString("activeColor", "") }
535
+ ?.ifEmpty { props.optString("textColor", "") }
536
+ ?.ifEmpty { props.optString("color", "#FFFFFF") }
537
+ ?: "#15803D"
538
+ } else {
539
+ props?.optString("textColor", "")
540
+ ?.ifEmpty { props.optString("color", "#FFFFFF") }
541
+ ?: "#FFFFFF"
542
+ }
543
+
544
+ val fontSize = ((props?.optDouble("fontSize", 11.0) ?: 11.0).toFloat() * density)
545
+ val padH = ((props?.optDouble("paddingHorizontal", 10.0) ?: 10.0).toFloat() * density)
546
+ val padV = ((props?.optDouble("paddingVertical", 5.5) ?: 5.5).toFloat() * density)
547
+ val radius = ((props?.optDouble("borderRadius", 8.0) ?: 8.0).toFloat() * density)
548
+ val fullWidth = props?.optBoolean("fullWidth", false) ?: false
549
+
550
+ paint.textSize = fontSize
551
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
552
+ val textW = paint.measureText(displayTitle)
553
+ val btnW = if (fullWidth) availWidth else min(availWidth, textW + padH * 2)
554
+ val btnH = fontSize + padV * 2
555
+
556
+ paint.style = Paint.Style.FILL
557
+ paint.color = parseHexColor(bgHex, Color.BLUE)
558
+ val rect = RectF(left, top, left + btnW, top + btnH)
559
+ canvas.drawRoundRect(rect, radius, radius, paint)
560
+
561
+ paint.color = parseHexColor(textHex, Color.WHITE)
562
+ val fm = paint.fontMetrics
563
+ val textY = top + btnH / 2f - (fm.ascent + fm.descent) / 2f
564
+ val display = ellipsizeText(displayTitle, paint, (btnW - padH * 1.5f).coerceAtLeast(20f))
565
+ if (fullWidth && !actionId.startsWith("toggle:")) {
566
+ paint.textAlign = Paint.Align.CENTER
567
+ canvas.drawText(display, left + btnW / 2f, textY, paint)
568
+ } else {
569
+ paint.textAlign = Paint.Align.LEFT
570
+ canvas.drawText(display, left + padH, textY, paint)
571
+ }
572
+
573
+ if (actionId.isNotEmpty() || !deepLink.isNullOrEmpty()) {
574
+ val zone = when {
575
+ top < totalHeight * 0.25f -> "top"
576
+ top > totalHeight * 0.76f -> "bot"
577
+ else -> "mid"
578
+ }
579
+ bindings.add(ButtonActionBinding(actionId = actionId, deepLink = deepLink, zone = zone))
580
+ }
581
+ btnH + 1.5f * density
582
+ }
583
+
584
+ "ZStack" -> {
585
+ val pad = (props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density
586
+ val bgHex = props?.optString("backgroundColor", "") ?: ""
587
+ val radius = (props?.optDouble("borderRadius", 10.0) ?: 10.0).toFloat() * density
588
+ val boxH = measureNodeHeight(context, node, availWidth, density)
589
+
590
+ if (!isRoot && bgHex.isNotEmpty()) {
591
+ paint.style = Paint.Style.FILL
592
+ paint.color = parseHexColor(bgHex, Color.TRANSPARENT)
593
+ canvas.drawRoundRect(RectF(left, top, left + availWidth, top + boxH), radius, radius, paint)
594
+ }
595
+
596
+ val children = node.optJSONArray("children")
597
+ if (children != null) {
598
+ val innerW = (availWidth - pad * 2).coerceAtLeast(10f)
599
+ val innerH = (boxH - pad * 2).coerceAtLeast(10f)
600
+ for (i in 0 until children.length()) {
601
+ val child = children.optJSONObject(i) ?: continue
602
+ val childH = measureNodeHeight(context, child, innerW, density)
603
+ drawNode(context, canvas, child, left + pad, top + pad, innerW, childH.coerceAtMost(innerH), totalHeight, density, false, bindings)
604
+ }
605
+ }
606
+ boxH
607
+ }
608
+
609
+ "Grid" -> {
610
+ val pad = (props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density
611
+ val cols = (props?.optInt("columns", 2) ?: 2).coerceAtLeast(1)
612
+ val colSpacing = ((props?.optDouble("columnSpacing", props.optDouble("spacing", 8.0)) ?: 8.0).toFloat() * density)
613
+ val rowSpacing = ((props?.optDouble("rowSpacing", props.optDouble("spacing", 8.0)) ?: 8.0).toFloat() * density)
614
+ val bgHex = props?.optString("backgroundColor", "") ?: ""
615
+ val radius = (props?.optDouble("borderRadius", 10.0) ?: 10.0).toFloat() * density
616
+ val gridH = measureNodeHeight(context, node, availWidth, density)
617
+
618
+ if (!isRoot && bgHex.isNotEmpty()) {
619
+ paint.style = Paint.Style.FILL
620
+ paint.color = parseHexColor(bgHex, Color.TRANSPARENT)
621
+ canvas.drawRoundRect(RectF(left, top, left + availWidth, top + gridH), radius, radius, paint)
622
+ }
623
+
624
+ val children = node.optJSONArray("children")
625
+ var curY = top + pad
626
+ if (children != null && children.length() > 0) {
627
+ val innerW = (availWidth - pad * 2).coerceAtLeast(20f)
628
+ val cellW = ((innerW - colSpacing * (cols - 1)) / cols).coerceAtLeast(10f)
629
+ val rows = (children.length() + cols - 1) / cols
630
+
631
+ for (r in 0 until rows) {
632
+ var rowMaxH = 0f
633
+ for (c in 0 until cols) {
634
+ val idx = r * cols + c
635
+ if (idx >= children.length()) break
636
+ val child = children.optJSONObject(idx) ?: continue
637
+ val cellLeft = left + pad + c * (cellW + colSpacing)
638
+ val childH = measureNodeHeight(context, child, cellW, density)
639
+ drawNode(context, canvas, child, cellLeft, curY, cellW, childH, totalHeight, density, false, bindings)
640
+ rowMaxH = max(rowMaxH, childH)
641
+ }
642
+ curY += rowMaxH
643
+ if (r < rows - 1) curY += rowSpacing
644
+ }
645
+ }
646
+ gridH
647
+ }
648
+
649
+ "Divider" -> {
650
+ val colorHex = props?.optString("color", "#CBD5E1") ?: "#CBD5E1"
651
+ val thickness = ((props?.optDouble("thickness", 1.0) ?: 1.0).toFloat() * density).coerceAtLeast(1f)
652
+ val marginV = ((props?.optDouble("marginVertical", 3.0) ?: 3.0).toFloat() * density)
653
+ val marginH = ((props?.optDouble("marginHorizontal", 0.0) ?: 0.0).toFloat() * density)
654
+ paint.style = Paint.Style.FILL
655
+ paint.color = parseHexColor(colorHex, Color.LTGRAY)
656
+ val y = top + marginV
657
+ canvas.drawRect(left + marginH, y, left + availWidth - marginH, y + thickness, paint)
658
+ thickness + marginV * 2
659
+ }
660
+
661
+ "Gauge" -> {
662
+ val value = (props?.optDouble("value", 0.75) ?: 0.75).toFloat().coerceIn(0f, 1f)
663
+ val size = ((props?.optDouble("size", 64.0) ?: 64.0).toFloat() * density)
664
+ val strokeW = ((props?.optDouble("strokeWidth", (size / density * 0.11).coerceAtLeast(4.0)) ?: 6.0).toFloat() * density)
665
+ val tintHex = props?.optString("tintColor", "#2563EB") ?: "#2563EB"
666
+ val trackHex = props?.optString("trackColor", "#CBD5E1") ?: "#CBD5E1"
667
+ val textHex = props?.optString("textColor", "")?.ifEmpty { tintHex } ?: tintHex
668
+ val labelHex = props?.optString("labelColor", "#64748B") ?: "#64748B"
669
+ val valueText = props?.optString("valueText", "${(value * 100).roundToInt()}%") ?: "${(value * 100).roundToInt()}%"
670
+ val label = props?.optString("label", "") ?: ""
671
+
672
+ val arcRect = RectF(
673
+ left + strokeW,
674
+ top + strokeW,
675
+ left + size - strokeW,
676
+ top + size - strokeW
677
+ )
678
+ paint.style = Paint.Style.STROKE
679
+ paint.strokeWidth = strokeW
680
+ paint.strokeCap = Paint.Cap.ROUND
681
+ paint.color = parseHexColor(trackHex, Color.LTGRAY)
682
+ canvas.drawArc(arcRect, 146f, 248f, false, paint)
683
+
684
+ paint.color = parseHexColor(tintHex, Color.BLUE)
685
+ canvas.drawArc(arcRect, 146f, 248f * value, false, paint)
686
+
687
+ paint.style = Paint.Style.FILL
688
+ paint.color = parseHexColor(textHex, Color.BLACK)
689
+ paint.textSize = size * 0.23f
690
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
691
+ paint.textAlign = Paint.Align.CENTER
692
+ val fm = paint.fontMetrics
693
+ val centerY = top + size * 0.50f - (fm.ascent + fm.descent) / 2f
694
+ canvas.drawText(valueText, left + size / 2f, centerY, paint)
695
+
696
+ if (label.isNotEmpty()) {
697
+ paint.color = parseHexColor(labelHex, Color.GRAY)
698
+ paint.textSize = size * 0.15f
699
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
700
+ canvas.drawText(label, left + size / 2f, top + size * 0.84f, paint)
701
+ }
702
+ size + 2f * density
703
+ }
704
+
705
+ "Chart" -> {
706
+ val style = props?.optString("style", "bar") ?: "bar"
707
+ val valuesArr = props?.optJSONArray("values")
708
+ val labelsArr = props?.optJSONArray("labels")
709
+ val chartH = ((props?.optDouble("height", 52.0) ?: 52.0).toFloat() * density)
710
+ val tintHex = props?.optString("tintColor", "#2563EB") ?: "#2563EB"
711
+ val secondaryHex = props?.optString("secondaryColor", "") ?: ""
712
+ val labelHex = props?.optString("labelColor", "#64748B") ?: "#64748B"
713
+ val tintColor = parseHexColor(tintHex, Color.BLUE)
714
+ val secondaryColor = if (secondaryHex.isNotEmpty()) {
715
+ parseHexColor(secondaryHex, Color.LTGRAY)
716
+ } else {
717
+ Color.argb(65, Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor))
718
+ }
719
+
720
+ val values = mutableListOf<Float>()
721
+ if (valuesArr != null) {
722
+ for (i in 0 until valuesArr.length()) {
723
+ values.add(valuesArr.optDouble(i, 0.0).toFloat())
724
+ }
725
+ }
726
+ val maxVal = (values.maxOrNull() ?: 1f).coerceAtLeast(0.001f)
727
+
728
+ if (values.isNotEmpty()) {
729
+ if (style == "line" && values.size >= 2) {
730
+ val stepX = availWidth / (values.size - 1).coerceAtLeast(1)
731
+ val linePath = Path()
732
+ val fillPath = Path()
733
+ fillPath.moveTo(left, top + chartH)
734
+
735
+ for (i in values.indices) {
736
+ val px = left + i * stepX
737
+ val py = top + chartH - (values[i].coerceAtLeast(0f) / maxVal) * (chartH - 6f * density) - 3f * density
738
+ if (i == 0) linePath.moveTo(px, py) else linePath.lineTo(px, py)
739
+ fillPath.lineTo(px, py)
740
+ }
741
+ fillPath.lineTo(left + availWidth, top + chartH)
742
+ fillPath.close()
743
+
744
+ paint.style = Paint.Style.FILL
745
+ paint.color = secondaryColor
746
+ canvas.drawPath(fillPath, paint)
747
+
748
+ paint.style = Paint.Style.STROKE
749
+ paint.strokeWidth = 2.2f * density
750
+ paint.strokeCap = Paint.Cap.ROUND
751
+ paint.color = tintColor
752
+ canvas.drawPath(linePath, paint)
753
+ } else {
754
+ val gap = 6f * density
755
+ val barW = ((availWidth - gap * (values.size - 1)) / values.size).coerceAtLeast(4f)
756
+ paint.style = Paint.Style.FILL
757
+ for (i in values.indices) {
758
+ val ratio = (values[i] / maxVal).coerceIn(0.08f, 1f)
759
+ val bH = chartH * ratio
760
+ val bLeft = left + i * (barW + gap)
761
+ val bTop = top + chartH - bH
762
+ paint.color = if (i == values.size - 1) tintColor else secondaryColor
763
+ canvas.drawRoundRect(
764
+ RectF(bLeft, bTop, bLeft + barW, top + chartH),
765
+ 4f * density,
766
+ 4f * density,
767
+ paint
768
+ )
769
+ }
770
+ }
771
+ }
772
+
773
+ var labelExtraH = 0f
774
+ if (labelsArr != null && labelsArr.length() > 0 && values.isNotEmpty()) {
775
+ val count = min(labelsArr.length(), values.size)
776
+ val colW = availWidth / values.size
777
+ paint.style = Paint.Style.FILL
778
+ paint.color = parseHexColor(labelHex, Color.GRAY)
779
+ paint.textSize = 9f * density
780
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
781
+ paint.textAlign = Paint.Align.CENTER
782
+ val labelY = top + chartH + 11f * density
783
+ for (i in 0 until count) {
784
+ val lbl = labelsArr.optString(i, "")
785
+ canvas.drawText(lbl, left + colW * i + colW / 2f, labelY, paint)
786
+ }
787
+ labelExtraH = 14f * density
788
+ }
789
+ chartH + labelExtraH + 2f * density
790
+ }
791
+
792
+ "Toggle" -> {
793
+ val rawActionId = props?.optString("actionId", "toggle:default") ?: "toggle:default"
794
+ val normalizedActionId = if (rawActionId.startsWith("toggle:")) rawActionId else "toggle:$rawActionId"
795
+ val key = normalizedActionId.removePrefix("toggle:")
796
+ val isChecked = DynamicWidgetStateStore.isToggled(context, key) || (props?.optBoolean("checked", false) == true)
797
+ val label = props?.optString("label", "") ?: ""
798
+ val style = props?.optString("style", "checkbox") ?: "checkbox"
799
+ val tintHex = props?.optString("tintColor", "#10B981") ?: "#10B981"
800
+ val textHex = props?.optString("textColor", "#0F172A") ?: "#0F172A"
801
+ val checkedTextHex = props?.optString("checkedTextColor", "#64748B") ?: "#64748B"
802
+ val fontSize = ((props?.optDouble("fontSize", 12.0) ?: 12.0).toFloat() * density)
803
+ val strike = props?.optBoolean("strikethroughOnCheck", true) ?: true
804
+ val rowH = fontSize * 1.55f
805
+
806
+ val tintColor = parseHexColor(tintHex, Color.GREEN)
807
+ var textStartLeft = left
808
+
809
+ if (style == "switch") {
810
+ val swW = 30f * density
811
+ val swH = 16f * density
812
+ val swTop = top + (rowH - swH) / 2f
813
+ paint.style = Paint.Style.FILL
814
+ paint.color = if (isChecked) tintColor else Color.parseColor("#CBD5E1")
815
+ canvas.drawRoundRect(RectF(left, swTop, left + swW, swTop + swH), swH / 2f, swH / 2f, paint)
816
+
817
+ paint.color = Color.WHITE
818
+ val knobR = 6f * density
819
+ val knobCx = if (isChecked) left + swW - knobR - 2f * density else left + knobR + 2f * density
820
+ canvas.drawCircle(knobCx, swTop + swH / 2f, knobR, paint)
821
+ textStartLeft = left + swW + 8f * density
822
+ } else {
823
+ val boxSize = 15f * density
824
+ val boxTop = top + (rowH - boxSize) / 2f
825
+ val boxRect = RectF(left, boxTop, left + boxSize, boxTop + boxSize)
826
+ if (isChecked) {
827
+ paint.style = Paint.Style.FILL
828
+ paint.color = tintColor
829
+ canvas.drawRoundRect(boxRect, boxSize / 2f, boxSize / 2f, paint)
830
+ paint.color = Color.WHITE
831
+ paint.textSize = 10f * density
832
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
833
+ paint.textAlign = Paint.Align.CENTER
834
+ canvas.drawText("✓", left + boxSize / 2f, boxTop + boxSize * 0.74f, paint)
835
+ } else {
836
+ paint.style = Paint.Style.STROKE
837
+ paint.strokeWidth = 1.6f * density
838
+ paint.color = Color.parseColor("#94A3B8")
839
+ canvas.drawRoundRect(boxRect, boxSize / 2f, boxSize / 2f, paint)
840
+ }
841
+ textStartLeft = left + boxSize + 7f * density
842
+ }
843
+
844
+ paint.style = Paint.Style.FILL
845
+ paint.textAlign = Paint.Align.LEFT
846
+ paint.textSize = fontSize
847
+ paint.typeface = if (isChecked) Typeface.create("sans-serif", Typeface.NORMAL) else Typeface.create("sans-serif", Typeface.BOLD)
848
+ paint.isStrikeThruText = isChecked && strike
849
+ paint.color = if (isChecked) parseHexColor(checkedTextHex, Color.GRAY) else parseHexColor(textHex, Color.BLACK)
850
+ val fm = paint.fontMetrics
851
+ val ty = top + rowH / 2f - (fm.ascent + fm.descent) / 2f
852
+ val display = ellipsizeText(label, paint, (left + availWidth - textStartLeft).coerceAtLeast(20f))
853
+ canvas.drawText(display, textStartLeft, ty, paint)
854
+ paint.isStrikeThruText = false
855
+
856
+ val zone = when {
857
+ top < totalHeight * 0.25f -> "top"
858
+ top > totalHeight * 0.76f -> "bot"
859
+ else -> "mid"
860
+ }
861
+ bindings.add(ButtonActionBinding(actionId = normalizedActionId, deepLink = null, zone = zone))
862
+ rowH + 2f * density
863
+ }
864
+
865
+ "Badge" -> {
866
+ val text = props?.optString("text", "") ?: ""
867
+ val bgHex = props?.optString("backgroundColor", "#DBEAFE") ?: "#DBEAFE"
868
+ val textHex = props?.optString("textColor", "#1D4ED8") ?: "#1D4ED8"
869
+ val borderHex = props?.optString("borderColor", "") ?: ""
870
+ val borderW = ((props?.optDouble("borderWidth", if (borderHex.isNotEmpty()) 1.0 else 0.0) ?: 0.0).toFloat() * density)
871
+ val fontSize = ((props?.optDouble("fontSize", 10.0) ?: 10.0).toFloat() * density)
872
+ val padH = ((props?.optDouble("paddingHorizontal", 8.0) ?: 8.0).toFloat() * density)
873
+ val padV = ((props?.optDouble("paddingVertical", 3.5) ?: 3.5).toFloat() * density)
874
+ val radius = ((props?.optDouble("borderRadius", 999.0) ?: 999.0).toFloat() * density)
875
+
876
+ paint.textSize = fontSize
877
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
878
+ val textW = paint.measureText(text)
879
+ val badgeW = min(availWidth, textW + padH * 2)
880
+ val badgeH = fontSize + padV * 2
881
+ val r = min(badgeH / 2f, radius)
882
+ val rect = RectF(left, top, left + badgeW, top + badgeH)
883
+
884
+ paint.style = Paint.Style.FILL
885
+ paint.color = parseHexColor(bgHex, Color.LTGRAY)
886
+ canvas.drawRoundRect(rect, r, r, paint)
887
+
888
+ if (borderW > 0f && borderHex.isNotEmpty()) {
889
+ paint.style = Paint.Style.STROKE
890
+ paint.strokeWidth = borderW
891
+ paint.color = parseHexColor(borderHex, Color.BLUE)
892
+ canvas.drawRoundRect(rect, r, r, paint)
893
+ }
894
+
895
+ paint.style = Paint.Style.FILL
896
+ paint.color = parseHexColor(textHex, Color.BLUE)
897
+ paint.textAlign = Paint.Align.CENTER
898
+ val fm = paint.fontMetrics
899
+ val ty = top + badgeH / 2f - (fm.ascent + fm.descent) / 2f
900
+ canvas.drawText(ellipsizeText(text, paint, badgeW - padH), left + badgeW / 2f, ty, paint)
901
+ badgeH + 1.5f * density
902
+ }
903
+
904
+ "Link" -> {
905
+ val actionId = props?.optString("actionId", "") ?: ""
906
+ val deepLink = props?.optString("deepLink", "")?.ifEmpty { null }
907
+ val asVStack = JSONObject(node.toString()).apply { put("type", "VStack") }
908
+ val usedH = drawNode(context, canvas, asVStack, left, top, availWidth, availHeight, totalHeight, density, false, bindings)
909
+ if (actionId.isNotEmpty() || !deepLink.isNullOrEmpty()) {
910
+ val zone = when {
911
+ top < totalHeight * 0.25f -> "top"
912
+ top > totalHeight * 0.76f -> "bot"
913
+ else -> "mid"
914
+ }
915
+ bindings.add(ButtonActionBinding(actionId = actionId, deepLink = deepLink, zone = zone))
916
+ }
917
+ usedH
918
+ }
919
+
920
+ "Equalizer" -> {
921
+ val isPlaying = props?.optBoolean("isPlaying", true) ?: true
922
+ val barCount = (props?.optInt("barCount", 4) ?: 4).coerceIn(2, 12)
923
+ val tintHex = props?.optString("tintColor", "#10B981") ?: "#10B981"
924
+ val maxHeight = ((props?.optDouble("height", 16.0) ?: 16.0).toFloat() * density)
925
+ val barW = ((props?.optDouble("barWidth", 3.0) ?: 3.0).toFloat() * density).coerceAtLeast(2f)
926
+ val spacing = ((props?.optDouble("spacing", 2.0) ?: 2.0).toFloat() * density).coerceAtLeast(1f)
927
+ val defaultRatios = floatArrayOf(0.45f, 0.9f, 0.6f, 0.8f, 0.35f, 0.95f, 0.5f)
928
+
929
+ paint.style = Paint.Style.FILL
930
+ paint.color = parseHexColor(tintHex, Color.parseColor("#10B981"))
931
+ if (!isPlaying) paint.alpha = 140
932
+
933
+ val cornerRadius = barW / 2f
934
+ val startX = left
935
+
936
+ for (i in 0 until barCount) {
937
+ val ratio = if (isPlaying) defaultRatios[i % defaultRatios.size] else 0.25f
938
+ val barH = max(barW, maxHeight * ratio)
939
+ val bx = startX + i * (barW + spacing)
940
+ val by = top + (maxHeight - barH)
941
+ canvas.drawRoundRect(RectF(bx, by, bx + barW, top + maxHeight), cornerRadius, cornerRadius, paint)
942
+ }
943
+ maxHeight + 1.5f * density
944
+ }
945
+
946
+ "Custom" -> {
947
+ val customComp = DynamicNodeRegistry.resolve(node)
948
+ if (customComp != null) {
949
+ val h = customComp.heightDpProvider(props) * density
950
+ val rect = RectF(left, top, left + availWidth, top + h)
951
+ customComp.drawer.draw(context, canvas, rect, props, density)
952
+ h + 2f * density
953
+ } else {
954
+ val children = node.optJSONArray("children")
955
+ if (children != null && children.length() > 0) {
956
+ val asVStack = JSONObject(node.toString()).apply { put("type", "VStack") }
957
+ drawNode(context, canvas, asVStack, left, top, availWidth, availHeight, totalHeight, density, false, bindings)
958
+ } else {
959
+ val title = props?.optString("title", props.optString("componentName", "CustomView")) ?: "CustomView"
960
+ val h = 28f * density
961
+ paint.style = Paint.Style.FILL
962
+ paint.color = parseHexColor(props?.optString("backgroundColor", "#F1F5F9") ?: "#F1F5F9", Color.LTGRAY)
963
+ canvas.drawRoundRect(RectF(left, top, left + availWidth, top + h), 8f * density, 8f * density, paint)
964
+ paint.color = parseHexColor(props?.optString("textColor", "#0F172A") ?: "#0F172A", Color.BLACK)
965
+ paint.textSize = 11f * density
966
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
967
+ paint.textAlign = Paint.Align.LEFT
968
+ canvas.drawText(title, left + 10f * density, top + h * 0.65f, paint)
969
+ h + 2f * density
970
+ }
971
+ }
972
+ }
973
+
974
+ else -> {
975
+ val customComp = DynamicNodeRegistry.resolve(node)
976
+ if (customComp != null) {
977
+ val h = customComp.heightDpProvider(props) * density
978
+ val rect = RectF(left, top, left + availWidth, top + h)
979
+ customComp.drawer.draw(context, canvas, rect, props, density)
980
+ h + 2f * density
981
+ } else 0f
982
+ }
983
+ }
984
+ }
985
+
986
+ private fun formatTimerString(props: JSONObject?): String {
987
+ val targetTs = props?.optDouble("targetTimestamp", 0.0) ?: 0.0
988
+ val showsHours = props?.optBoolean("showsHours", true) ?: true
989
+ val diffSec = abs(targetTs - System.currentTimeMillis() / 1000.0).toLong()
990
+ val hrs = diffSec / 3600
991
+ val mins = (diffSec % 3600) / 60
992
+ val secs = diffSec % 60
993
+ return if (showsHours && hrs > 0) {
994
+ String.format(Locale.US, "%d:%02d:%02d", hrs, mins, secs)
995
+ } else {
996
+ String.format(Locale.US, "%02d:%02d", mins, secs)
997
+ }
998
+ }
999
+
1000
+ private fun measureNodeHeight(context: Context, node: JSONObject, availWidth: Float, density: Float): Float {
1001
+ val type = node.optString("type", "")
1002
+ val props = node.optJSONObject("props")
1003
+ return when (type) {
1004
+ "VStack" -> {
1005
+ val pad = ((props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density)
1006
+ val spacing = ((props?.optDouble("spacing", 6.0) ?: 6.0).toFloat() * density)
1007
+ val children = node.optJSONArray("children")
1008
+ var h = pad * 2
1009
+ if (children != null) {
1010
+ val count = children.length()
1011
+ for (i in 0 until count) {
1012
+ val c = children.optJSONObject(i) ?: continue
1013
+ h += measureNodeHeight(context, c, (availWidth - pad * 2).coerceAtLeast(10f), density)
1014
+ if (i < count - 1) h += spacing
1015
+ }
1016
+ }
1017
+ h
1018
+ }
1019
+ "HStack" -> {
1020
+ val pad = ((props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density)
1021
+ val children = node.optJSONArray("children")
1022
+ var maxChildH = 16f * density
1023
+ if (children != null) {
1024
+ for (i in 0 until children.length()) {
1025
+ val c = children.optJSONObject(i) ?: continue
1026
+ if (c.optString("type") == "Spacer") continue
1027
+ maxChildH = max(maxChildH, measureNodeHeight(context, c, availWidth, density))
1028
+ }
1029
+ }
1030
+ maxChildH + pad * 2
1031
+ }
1032
+ "Text", "Timer" -> {
1033
+ val fs = ((props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density)
1034
+ fs * 1.45f + 2.5f * density
1035
+ }
1036
+ "Progress" -> {
1037
+ val style = props?.optString("style", "bar") ?: "bar"
1038
+ val showPct = props?.optBoolean("showPercentage", false) ?: false
1039
+ if (style == "circular") {
1040
+ ((props?.optDouble("height", 52.0) ?: 52.0).toFloat() * density) + 2f * density
1041
+ } else {
1042
+ val barH = ((props?.optDouble("height", 8.0) ?: 8.0).toFloat() * density) + 4f * density
1043
+ max(barH, if (showPct) 13f * density else 0f)
1044
+ }
1045
+ }
1046
+ "Image" -> ((props?.optDouble("height", 32.0) ?: 32.0).toFloat() * density) + 1.5f * density
1047
+ "Button" -> {
1048
+ val fs = ((props?.optDouble("fontSize", 11.0) ?: 11.0).toFloat() * density)
1049
+ val padV = ((props?.optDouble("paddingVertical", 5.5) ?: 5.5).toFloat() * density)
1050
+ fs + padV * 2 + 1.5f * density
1051
+ }
1052
+ "ZStack" -> {
1053
+ val pad = ((props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density)
1054
+ val children = node.optJSONArray("children")
1055
+ var maxH = 20f * density
1056
+ if (children != null) {
1057
+ for (i in 0 until children.length()) {
1058
+ val c = children.optJSONObject(i) ?: continue
1059
+ maxH = max(maxH, measureNodeHeight(context, c, (availWidth - pad * 2).coerceAtLeast(10f), density))
1060
+ }
1061
+ }
1062
+ maxH + pad * 2
1063
+ }
1064
+ "Grid" -> {
1065
+ val pad = ((props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density)
1066
+ val cols = (props?.optInt("columns", 2) ?: 2).coerceAtLeast(1)
1067
+ val colSpacing = ((props?.optDouble("columnSpacing", props.optDouble("spacing", 8.0)) ?: 8.0).toFloat() * density)
1068
+ val rowSpacing = ((props?.optDouble("rowSpacing", props.optDouble("spacing", 8.0)) ?: 8.0).toFloat() * density)
1069
+ val children = node.optJSONArray("children")
1070
+ var totalH = pad * 2
1071
+ if (children != null && children.length() > 0) {
1072
+ val innerW = (availWidth - pad * 2).coerceAtLeast(20f)
1073
+ val cellW = ((innerW - colSpacing * (cols - 1)) / cols).coerceAtLeast(10f)
1074
+ val rows = (children.length() + cols - 1) / cols
1075
+ for (r in 0 until rows) {
1076
+ var rowMax = 0f
1077
+ for (c in 0 until cols) {
1078
+ val idx = r * cols + c
1079
+ if (idx >= children.length()) break
1080
+ val child = children.optJSONObject(idx) ?: continue
1081
+ rowMax = max(rowMax, measureNodeHeight(context, child, cellW, density))
1082
+ }
1083
+ totalH += rowMax
1084
+ if (r < rows - 1) totalH += rowSpacing
1085
+ }
1086
+ }
1087
+ totalH
1088
+ }
1089
+ "Divider" -> {
1090
+ val thickness = ((props?.optDouble("thickness", 1.0) ?: 1.0).toFloat() * density).coerceAtLeast(1f)
1091
+ val marginV = ((props?.optDouble("marginVertical", 3.0) ?: 3.0).toFloat() * density)
1092
+ thickness + marginV * 2
1093
+ }
1094
+ "Gauge" -> ((props?.optDouble("size", 64.0) ?: 64.0).toFloat() * density) + 2f * density
1095
+ "Chart" -> {
1096
+ val h = ((props?.optDouble("height", 52.0) ?: 52.0).toFloat() * density)
1097
+ val hasLabels = (props?.optJSONArray("labels")?.length() ?: 0) > 0
1098
+ h + (if (hasLabels) 14f * density else 0f) + 2f * density
1099
+ }
1100
+ "Toggle" -> {
1101
+ val fs = ((props?.optDouble("fontSize", 12.0) ?: 12.0).toFloat() * density)
1102
+ fs * 1.55f + 2f * density
1103
+ }
1104
+ "Badge" -> {
1105
+ val fs = ((props?.optDouble("fontSize", 10.0) ?: 10.0).toFloat() * density)
1106
+ val padV = ((props?.optDouble("paddingVertical", 3.5) ?: 3.5).toFloat() * density)
1107
+ fs + padV * 2 + 1.5f * density
1108
+ }
1109
+ "Link" -> {
1110
+ val asVStack = JSONObject(node.toString()).apply { put("type", "VStack") }
1111
+ measureNodeHeight(context, asVStack, availWidth, density)
1112
+ }
1113
+ "Equalizer" -> ((props?.optDouble("height", 16.0) ?: 16.0).toFloat() * density) + 1.5f * density
1114
+
1115
+ "Custom" -> {
1116
+ val customComp = DynamicNodeRegistry.resolve(node)
1117
+ if (customComp != null) {
1118
+ customComp.heightDpProvider(props) * density + 2f * density
1119
+ } else 30f * density
1120
+ }
1121
+ "Spacer" -> ((props?.optDouble("minLength", 4.0) ?: 4.0).toFloat() * density)
1122
+ else -> 0f
1123
+ }
1124
+ }
1125
+
1126
+ private fun measureNodeWidth(context: Context, node: JSONObject, availWidth: Float, density: Float): Float {
1127
+ val type = node.optString("type", "")
1128
+ val props = node.optJSONObject("props")
1129
+ val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
1130
+ return when (type) {
1131
+ "Text" -> {
1132
+ val text = props?.optString("text", "") ?: ""
1133
+ val fs = ((props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density)
1134
+ paint.textSize = fs
1135
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
1136
+ min(availWidth, paint.measureText(text) + 2f * density)
1137
+ }
1138
+ "Timer" -> {
1139
+ val formatted = formatTimerString(props)
1140
+ val fs = ((props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density)
1141
+ paint.textSize = fs
1142
+ paint.typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD)
1143
+ paint.measureText(formatted) + 2f * density
1144
+ }
1145
+ "Image" -> ((props?.optDouble("width", 32.0) ?: 32.0).toFloat() * density)
1146
+ "Progress" -> {
1147
+ val style = props?.optString("style", "bar") ?: "bar"
1148
+ if (style == "circular") {
1149
+ ((props?.optDouble("height", 52.0) ?: 52.0).toFloat() * density)
1150
+ } else availWidth * 0.55f
1151
+ }
1152
+ "Button" -> {
1153
+ val title = props?.optString("title", "Button") ?: "Button"
1154
+ val fs = ((props?.optDouble("fontSize", 11.0) ?: 11.0).toFloat() * density)
1155
+ val padH = ((props?.optDouble("paddingHorizontal", 10.0) ?: 10.0).toFloat() * density)
1156
+ paint.textSize = fs
1157
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
1158
+ paint.measureText(title) + padH * 2
1159
+ }
1160
+ "Badge" -> {
1161
+ val text = props?.optString("text", "") ?: ""
1162
+ val fs = ((props?.optDouble("fontSize", 10.0) ?: 10.0).toFloat() * density)
1163
+ val padH = ((props?.optDouble("paddingHorizontal", 8.0) ?: 8.0).toFloat() * density)
1164
+ paint.textSize = fs
1165
+ paint.typeface = Typeface.create("sans-serif", Typeface.BOLD)
1166
+ paint.measureText(text) + padH * 2
1167
+ }
1168
+ "Gauge" -> ((props?.optDouble("size", 64.0) ?: 64.0).toFloat() * density)
1169
+ "Equalizer" -> {
1170
+ val barCount = (props?.optInt("barCount", 4) ?: 4).coerceIn(2, 12)
1171
+ val barW = ((props?.optDouble("barWidth", 3.0) ?: 3.0).toFloat() * density).coerceAtLeast(2f)
1172
+ val spacing = ((props?.optDouble("spacing", 2.0) ?: 2.0).toFloat() * density).coerceAtLeast(1f)
1173
+ barCount * barW + (barCount - 1) * spacing
1174
+ }
1175
+ "Spacer" -> ((props?.optDouble("minLength", 4.0) ?: 4.0).toFloat() * density)
1176
+ "VStack" -> {
1177
+ val pad = ((props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density)
1178
+ val children = node.optJSONArray("children")
1179
+ var maxW = 40f * density
1180
+ if (children != null) {
1181
+ for (i in 0 until children.length()) {
1182
+ val c = children.optJSONObject(i) ?: continue
1183
+ maxW = max(maxW, measureNodeWidth(context, c, availWidth - pad * 2, density))
1184
+ }
1185
+ }
1186
+ min(availWidth * 0.78f, maxW + pad * 2)
1187
+ }
1188
+ "HStack" -> {
1189
+ val pad = ((props?.optDouble("padding", 0.0) ?: 0.0).toFloat() * density)
1190
+ val spacing = ((props?.optDouble("spacing", 6.0) ?: 6.0).toFloat() * density)
1191
+ val children = node.optJSONArray("children")
1192
+ var totalW = pad * 2
1193
+ if (children != null) {
1194
+ for (i in 0 until children.length()) {
1195
+ val c = children.optJSONObject(i) ?: continue
1196
+ totalW += measureNodeWidth(context, c, availWidth, density)
1197
+ if (i < children.length() - 1) totalW += spacing
1198
+ }
1199
+ }
1200
+ min(availWidth, totalW)
1201
+ }
1202
+ else -> availWidth * 0.5f
1203
+ }
1204
+ }
1205
+
1206
+ private fun ellipsizeText(text: String, paint: Paint, maxWidth: Float): String {
1207
+ if (paint.measureText(text) <= maxWidth) return text
1208
+ var end = text.length
1209
+ while (end > 1 && paint.measureText(text.substring(0, end) + "…") > maxWidth) {
1210
+ end--
1211
+ }
1212
+ return text.substring(0, end) + "…"
1213
+ }
1214
+
1215
+ private fun parseHexColor(hex: String, fallback: Int): Int {
1216
+ return try {
1217
+ Color.parseColor(hex)
1218
+ } catch (_: Exception) {
1219
+ fallback
1220
+ }
1221
+ }
1222
+ }
1223
+