react-native-live-widgets 1.1.1 → 1.2.1

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 (58) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +113 -10
  3. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +5 -1
  4. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +443 -46
  5. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +86 -24
  6. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +126 -56
  7. package/android/src/main/res/layout/rn_live_widget_hit_zone.xml +16 -0
  8. package/android/src/main/res/layout/rn_live_widget_host.xml +16 -87
  9. package/docs/GUIDE-EN.md +332 -7
  10. package/docs/GUIDE-VI.md +357 -7
  11. package/docs/index.html +1143 -227
  12. package/docs/llms-full.txt +46 -6
  13. package/docs/llms.txt +37 -7
  14. package/ios/Models/DynamicLayoutModels.swift +11 -1
  15. package/ios/Renderer/DynamicNodeView.swift +169 -14
  16. package/ios/Widgets/DynamicHomeScreenWidget.swift +7 -3
  17. package/lib/builder.d.ts.map +1 -1
  18. package/lib/builder.js +92 -28
  19. package/lib/builder.js.map +1 -1
  20. package/lib/dispatcher.d.ts +7 -0
  21. package/lib/dispatcher.d.ts.map +1 -1
  22. package/lib/dispatcher.js +23 -6
  23. package/lib/dispatcher.js.map +1 -1
  24. package/lib/hooks.d.ts +66 -0
  25. package/lib/hooks.d.ts.map +1 -0
  26. package/lib/hooks.js +171 -0
  27. package/lib/hooks.js.map +1 -0
  28. package/lib/index.d.ts +48 -8
  29. package/lib/index.d.ts.map +1 -1
  30. package/lib/index.js +216 -35
  31. package/lib/index.js.map +1 -1
  32. package/lib/ir.d.ts +15 -0
  33. package/lib/ir.d.ts.map +1 -0
  34. package/lib/ir.js +379 -0
  35. package/lib/ir.js.map +1 -0
  36. package/lib/jsx.d.ts +87 -0
  37. package/lib/jsx.d.ts.map +1 -0
  38. package/lib/jsx.js +243 -0
  39. package/lib/jsx.js.map +1 -0
  40. package/lib/preview.d.ts +31 -0
  41. package/lib/preview.d.ts.map +1 -0
  42. package/lib/preview.js +735 -0
  43. package/lib/preview.js.map +1 -0
  44. package/lib/types.d.ts +212 -12
  45. package/lib/types.d.ts.map +1 -1
  46. package/lib/types.js +2 -1
  47. package/lib/types.js.map +1 -1
  48. package/package.json +3 -1
  49. package/src/builder.ts +86 -21
  50. package/src/dispatcher.ts +17 -0
  51. package/src/hooks.ts +223 -0
  52. package/src/index.ts +202 -13
  53. package/src/ir.ts +430 -0
  54. package/src/jsx.tsx +326 -0
  55. package/src/preview.tsx +1043 -0
  56. package/src/react-ambient.d.ts +10 -0
  57. package/src/react-native-env.d.ts +44 -0
  58. package/src/types.ts +261 -13
@@ -11,6 +11,7 @@ import android.graphics.Path
11
11
  import android.graphics.RectF
12
12
  import android.graphics.Shader
13
13
  import android.graphics.Typeface
14
+ import android.os.SystemClock
14
15
  import org.json.JSONObject
15
16
  import java.util.Locale
16
17
  import kotlin.math.abs
@@ -18,16 +19,33 @@ import kotlin.math.max
18
19
  import kotlin.math.min
19
20
  import kotlin.math.roundToInt
20
21
 
22
+ data class WidgetTimerInfo(
23
+ val baseMillis: Long,
24
+ val isCountDown: Boolean,
25
+ val leftPx: Int,
26
+ val topPx: Int,
27
+ val fontSizePx: Float,
28
+ val color: Int
29
+ )
30
+
21
31
  data class ButtonActionBinding(
22
32
  val actionId: String,
23
33
  val deepLink: String?,
24
34
  val zone: String = "mid", // "top", "mid", "bot"
25
35
  val xFraction: Float = 0.5f,
26
36
  val yFraction: Float = 0.5f,
27
- val widthFraction: Float = 1.0f
37
+ val widthFraction: Float = 1.0f,
38
+ val title: String = "",
39
+ val accessibilityLabel: String? = null,
40
+ val accessibilityHint: String? = null,
41
+ val leftFraction: Float = 0f,
42
+ val rightFraction: Float = 1f,
43
+ val topFraction: Float = 0f,
44
+ val bottomFraction: Float = 1f
28
45
  )
29
46
 
30
47
  object DynamicWidgetCanvasRenderer {
48
+ var activeTimerInfo: WidgetTimerInfo? = null
31
49
 
32
50
  private fun isFlexibleSpacer(node: JSONObject?): Boolean {
33
51
  if (node == null || node.optString("type") != "Spacer") return false
@@ -35,6 +53,139 @@ object DynamicWidgetCanvasRenderer {
35
53
  return !props.has("minLength")
36
54
  }
37
55
 
56
+ /**
57
+ * ♿ Trích xuất bản tóm tắt Trợ năng (Accessibility Summary) từ cây JSON DSL.
58
+ * Cho phép TalkBack của Android đọc đầy đủ các nhãn, con số, trạng thái và nút bấm
59
+ * ngay cả khi toàn bộ giao diện đang được render bằng Canvas 2D Bitmap.
60
+ */
61
+ fun extractAccessibilitySummary(node: JSONObject?): String {
62
+ if (node == null) return ""
63
+ val parts = mutableListOf<String>()
64
+ traverseAccessibility(node, parts)
65
+ return parts.distinct().joinToString(". ")
66
+ }
67
+
68
+ private fun traverseAccessibility(node: JSONObject, parts: MutableList<String>) {
69
+ val props = node.optJSONObject("props")
70
+ val accessible = props?.optBoolean("accessible", true) ?: true
71
+ if (!accessible) return
72
+
73
+ val customLabel = props?.optString("accessibilityLabel", "")?.trim() ?: ""
74
+ val customValue = props?.optString("accessibilityValue", "")?.trim() ?: ""
75
+ val customHint = props?.optString("accessibilityHint", "")?.trim() ?: ""
76
+
77
+ val type = node.optString("type")
78
+ when (type) {
79
+ "Text" -> {
80
+ val text = props?.optString("text", "")?.trim() ?: ""
81
+ val content = when {
82
+ customLabel.isNotEmpty() -> customLabel
83
+ text.isNotEmpty() -> text
84
+ else -> ""
85
+ }
86
+ if (content.isNotEmpty()) parts.add(content)
87
+ }
88
+ "Button" -> {
89
+ val title = props?.optString("title", "")?.trim() ?: ""
90
+ val btnName = if (customLabel.isNotEmpty()) customLabel else title
91
+ val fullBtn = if (customHint.isNotEmpty()) "Nút $btnName, $customHint" else "Nút $btnName"
92
+ if (btnName.isNotEmpty()) parts.add(fullBtn)
93
+ }
94
+ "Progress" -> {
95
+ val label = if (customLabel.isNotEmpty()) customLabel else "Tiến độ"
96
+ val value = when {
97
+ customValue.isNotEmpty() -> customValue
98
+ props != null && props.has("value") -> "${(props.optDouble("value", 0.0) * 100).toInt()}%"
99
+ else -> ""
100
+ }
101
+ parts.add(if (value.isNotEmpty()) "$label $value" else label)
102
+ }
103
+ "Gauge" -> {
104
+ val label = if (customLabel.isNotEmpty()) customLabel else (props?.optString("label", "")?.trim() ?: "Chỉ số")
105
+ val valText = props?.optString("valueText", "")?.trim() ?: ""
106
+ val value = when {
107
+ customValue.isNotEmpty() -> customValue
108
+ valText.isNotEmpty() -> valText
109
+ props != null && props.has("value") -> "${(props.optDouble("value", 0.0) * 100).toInt()}%"
110
+ else -> ""
111
+ }
112
+ parts.add(if (value.isNotEmpty()) "$label: $value" else label)
113
+ }
114
+ "Timer" -> {
115
+ val label = if (customLabel.isNotEmpty()) customLabel else "Đồng hồ"
116
+ parts.add(label)
117
+ }
118
+ "Badge" -> {
119
+ val text = props?.optString("text", "")?.trim() ?: ""
120
+ val label = if (customLabel.isNotEmpty()) customLabel else text
121
+ if (label.isNotEmpty()) parts.add(label)
122
+ }
123
+ "Toggle" -> {
124
+ val label = props?.optString("label", "")?.trim() ?: ""
125
+ val isChecked = props?.optBoolean("isChecked", false) ?: false
126
+ val desc = if (customLabel.isNotEmpty()) customLabel else label
127
+ if (desc.isNotEmpty()) {
128
+ parts.add("$desc: " + if (isChecked) "Đã chọn" else "Chưa chọn")
129
+ }
130
+ }
131
+ "Image" -> {
132
+ if (customLabel.isNotEmpty()) {
133
+ parts.add("Ảnh $customLabel")
134
+ }
135
+ }
136
+ else -> {
137
+ if (customLabel.isNotEmpty()) {
138
+ parts.add(customLabel)
139
+ }
140
+ }
141
+ }
142
+
143
+ val children = node.optJSONArray("children")
144
+ if (children != null) {
145
+ for (i in 0 until children.length()) {
146
+ val child = children.optJSONObject(i)
147
+ if (child != null) {
148
+ traverseAccessibility(child, parts)
149
+ }
150
+ }
151
+ }
152
+ }
153
+
154
+ /**
155
+ * ⚡ [Phase 5 Optimization] Kết xuất Sub-Bitmap cục bộ chỉ cho thành phần đồ họa cụ thể
156
+ * (Gauge, Equalizer, Circular Progress) thay vì vẽ cả một Bitmap khổng lồ toàn màn hình.
157
+ * Tiết kiệm >90% dung lượng RAM và tránh lỗi TransactionTooLargeException của Android IPC Binder.
158
+ */
159
+ fun renderSubNodeBitmap(
160
+ context: Context,
161
+ node: JSONObject,
162
+ widthPx: Int,
163
+ heightPx: Int,
164
+ density: Float
165
+ ): Bitmap {
166
+ val bmp = Bitmap.createBitmap(
167
+ widthPx.coerceAtLeast(1),
168
+ heightPx.coerceAtLeast(1),
169
+ Bitmap.Config.ARGB_8888
170
+ )
171
+ val canvas = Canvas(bmp)
172
+ val bindings = mutableListOf<ButtonActionBinding>()
173
+ drawNode(
174
+ context = context,
175
+ canvas = canvas,
176
+ node = node,
177
+ left = 0f,
178
+ top = 0f,
179
+ availWidth = widthPx.toFloat(),
180
+ availHeight = heightPx.toFloat(),
181
+ totalHeight = heightPx.toFloat(),
182
+ density = density,
183
+ isRoot = true,
184
+ bindings = bindings
185
+ )
186
+ return bmp
187
+ }
188
+
38
189
  fun renderWidgetBitmap(
39
190
  context: Context,
40
191
  rootNode: JSONObject?,
@@ -44,6 +195,7 @@ object DynamicWidgetCanvasRenderer {
44
195
  family: String,
45
196
  bindings: MutableList<ButtonActionBinding>
46
197
  ): Bitmap {
198
+ activeTimerInfo = null
47
199
  val bmp = Bitmap.createBitmap(widthPx, heightPx, Bitmap.Config.ARGB_8888)
48
200
  val canvas = Canvas(bmp)
49
201
  val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
@@ -71,7 +223,7 @@ object DynamicWidgetCanvasRenderer {
71
223
 
72
224
  val naturalH = measureNodeHeight(context, rootNode, widthPx.toFloat(), baseDensity)
73
225
  val fitScale = if (naturalH > heightPx * 0.96f && naturalH > 0f) {
74
- ((heightPx * 0.95f) / naturalH).coerceIn(0.62f, 1.0f)
226
+ ((heightPx * 0.95f) / naturalH).coerceIn(0.55f, 1.0f)
75
227
  } else {
76
228
  1.0f
77
229
  }
@@ -141,17 +293,41 @@ object DynamicWidgetCanvasRenderer {
141
293
  val measuredH = measureNodeHeight(context, node, availWidth, density)
142
294
  val boxH = if (isRoot) max(availHeight, measuredH) else (measuredH + 2f * density)
143
295
 
144
- if (!isRoot && bgHex.isNotEmpty()) {
296
+ if (!isRoot && (bgHex.isNotEmpty() || props?.has("gradient") == true)) {
145
297
  val rect = RectF(left, top, left + availWidth, top + boxH)
298
+ val gradShader = createLinearGradient(props?.optJSONObject("gradient"), rect)
146
299
  paint.style = Paint.Style.FILL
147
- paint.color = parseHexColor(bgHex, Color.TRANSPARENT)
300
+ if (gradShader != null) {
301
+ paint.shader = gradShader
302
+ } else {
303
+ paint.color = parseDynamicColor(props?.opt("backgroundColor") ?: bgHex, Color.TRANSPARENT)
304
+ }
305
+
306
+ val shadowColor = props?.optString("shadowColor", "")
307
+ val shadowRad = (props?.optDouble("shadowRadius", 0.0) ?: 0.0).toFloat() * density
308
+ if (!shadowColor.isNullOrEmpty() && shadowRad > 0f) {
309
+ val sOff = props?.optJSONObject("shadowOffset")
310
+ val sx = (sOff?.optDouble("x", 0.0) ?: 0.0).toFloat() * density
311
+ val sy = (sOff?.optDouble("y", 2.0) ?: 2.0).toFloat() * density
312
+ paint.setShadowLayer(shadowRad, sx, sy, parseHexColor(shadowColor, Color.BLACK))
313
+ }
314
+
148
315
  canvas.drawRoundRect(rect, radius, radius, paint)
316
+ paint.clearShadowLayer()
317
+ paint.shader = null
149
318
 
150
319
  if (borderW > 0f && borderHex.isNotEmpty()) {
151
320
  paint.style = Paint.Style.STROKE
152
321
  paint.strokeWidth = borderW
153
322
  paint.color = parseHexColor(borderHex, Color.LTGRAY)
323
+ val borderStyle = props?.optString("borderStyle", "solid")
324
+ if (borderStyle == "dashed") {
325
+ paint.pathEffect = android.graphics.DashPathEffect(floatArrayOf(12f * density, 8f * density), 0f)
326
+ } else if (borderStyle == "dotted") {
327
+ paint.pathEffect = android.graphics.DashPathEffect(floatArrayOf(4f * density, 6f * density), 0f)
328
+ }
154
329
  canvas.drawRoundRect(rect, radius, radius, paint)
330
+ paint.pathEffect = null
155
331
  }
156
332
  }
157
333
 
@@ -276,24 +452,83 @@ object DynamicWidgetCanvasRenderer {
276
452
  curLeft += w + spacing
277
453
  }
278
454
  } else {
279
- var curLeft = left + pad
455
+ var fullWidthCount = 0
280
456
  for (i in 0 until children.length()) {
281
457
  val c = children.optJSONObject(i) ?: continue
282
- if (c.optString("type") == "Spacer") {
283
- val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
284
- curLeft += gapW
285
- continue
458
+ if (c.optJSONObject("props")?.optBoolean("fullWidth", false) == true) {
459
+ fullWidthCount++
286
460
  }
287
- val remainingW = (left + availWidth - pad - curLeft).coerceAtLeast(20f)
288
- val w = if (i == children.length() - 1 && c.optString("type") in listOf("VStack", "Progress", "Text")) {
289
- remainingW
290
- } else {
291
- min(remainingW, measureNodeWidth(context, c, innerW, density))
461
+ }
462
+
463
+ val justify = props?.optString("justifyContent", "") ?: ""
464
+ if (fullWidthCount > 0) {
465
+ var fixedW = 0f
466
+ for (i in 0 until children.length()) {
467
+ val c = children.optJSONObject(i) ?: continue
468
+ if (c.optString("type") == "Spacer") {
469
+ fixedW += (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
470
+ } else if (c.optJSONObject("props")?.optBoolean("fullWidth", false) != true) {
471
+ fixedW += measureNodeWidth(context, c, innerW, density)
472
+ }
473
+ }
474
+ val totalSpacing = spacing * (children.length() - 1).coerceAtLeast(0)
475
+ val fullWidthW = ((innerW - fixedW - totalSpacing) / fullWidthCount).coerceAtLeast(30f)
476
+
477
+ var curLeft = left + pad
478
+ for (i in 0 until children.length()) {
479
+ val c = children.optJSONObject(i) ?: continue
480
+ if (c.optString("type") == "Spacer") {
481
+ val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
482
+ curLeft += gapW + spacing
483
+ continue
484
+ }
485
+ val isFull = c.optJSONObject("props")?.optBoolean("fullWidth", false) == true
486
+ val w = if (isFull) fullWidthW else measureNodeWidth(context, c, innerW, density)
487
+ val childH = measureNodeHeight(context, c, w, density)
488
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
489
+ drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
490
+ curLeft += w + spacing
491
+ }
492
+ } else if (justify in listOf("spaceBetween", "spaceAround") && children.length() == 2) {
493
+ val c0 = children.optJSONObject(0)
494
+ val c1 = children.optJSONObject(1)
495
+ if (c0 != null && c1 != null) {
496
+ val w1 = min(innerW * 0.72f, measureNodeWidth(context, c1, innerW, density))
497
+ val h1 = measureNodeHeight(context, c1, w1, density)
498
+ val top1 = top + pad + ((innerH - h1) / 2f).coerceAtLeast(0f)
499
+ val rightLeft = left + availWidth - pad - w1
500
+ drawNode(context, canvas, c1, rightLeft, top1, w1, h1, totalHeight, density, false, bindings)
501
+
502
+ val maxLeftAreaW = (rightLeft - spacing - (left + pad)).coerceAtLeast(30f)
503
+ val w0 = if (c0.optString("type") == "VStack") {
504
+ maxLeftAreaW
505
+ } else {
506
+ min(maxLeftAreaW, measureNodeWidth(context, c0, maxLeftAreaW, density))
507
+ }
508
+ val h0 = measureNodeHeight(context, c0, w0, density)
509
+ val top0 = top + pad + ((innerH - h0) / 2f).coerceAtLeast(0f)
510
+ drawNode(context, canvas, c0, left + pad, top0, w0, h0, totalHeight, density, false, bindings)
511
+ }
512
+ } else {
513
+ var curLeft = left + pad
514
+ for (i in 0 until children.length()) {
515
+ val c = children.optJSONObject(i) ?: continue
516
+ if (c.optString("type") == "Spacer") {
517
+ val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
518
+ curLeft += gapW
519
+ continue
520
+ }
521
+ val remainingW = (left + availWidth - pad - curLeft).coerceAtLeast(20f)
522
+ val w = if (i == children.length() - 1 && c.optString("type") in listOf("VStack", "Progress", "Text")) {
523
+ remainingW
524
+ } else {
525
+ min(remainingW, measureNodeWidth(context, c, innerW, density))
526
+ }
527
+ val childH = measureNodeHeight(context, c, w, density)
528
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
529
+ drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
530
+ curLeft += w + spacing
292
531
  }
293
- val childH = measureNodeHeight(context, c, w, density)
294
- val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
295
- drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
296
- curLeft += w + spacing
297
532
  }
298
533
  }
299
534
  }
@@ -302,39 +537,111 @@ object DynamicWidgetCanvasRenderer {
302
537
 
303
538
  "Text" -> {
304
539
  val text = props?.optString("text", "") ?: ""
305
- val fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density
306
- val weight = props?.optString("fontWeight", "regular") ?: "regular"
540
+ val fontScale = context.resources.configuration.fontScale
541
+ val dynamicType = props?.optBoolean("dynamicType", true) ?: true
542
+ val effectiveScale = if (dynamicType) fontScale else 1.0f
543
+
544
+ var fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density * effectiveScale
545
+ var weight = props?.optString("fontWeight", "regular") ?: "regular"
546
+
547
+ val typo = props?.optString("typography", "")
548
+ if (!typo.isNullOrEmpty()) {
549
+ when (typo) {
550
+ "largeTitle" -> { fontSize = 34f * density * effectiveScale; weight = "bold" }
551
+ "title1", "title" -> { fontSize = 28f * density * effectiveScale; weight = "bold" }
552
+ "title2" -> { fontSize = 22f * density * effectiveScale; weight = "bold" }
553
+ "title3" -> { fontSize = 20f * density * effectiveScale; weight = "semibold" }
554
+ "headline" -> { fontSize = 17f * density * effectiveScale; weight = "semibold" }
555
+ "subheadline" -> { fontSize = 15f * density * effectiveScale }
556
+ "body" -> { fontSize = 17f * density * effectiveScale }
557
+ "callout" -> { fontSize = 16f * density * effectiveScale }
558
+ "footnote" -> { fontSize = 13f * density * effectiveScale }
559
+ "caption1", "caption" -> { fontSize = 12f * density * effectiveScale }
560
+ "caption2" -> { fontSize = 11f * density * effectiveScale }
561
+ }
562
+ }
563
+
307
564
  val colorHex = props?.optString("color", "#0F172A") ?: "#0F172A"
308
565
  val toggleKey = props?.optString("strikethroughWhenToggled", "") ?: ""
309
566
  val isStruck = toggleKey.isNotEmpty() && DynamicWidgetStateStore.isToggled(context, toggleKey)
310
567
 
311
- paint.color = if (isStruck) Color.parseColor("#94A3B8") else parseHexColor(colorHex, Color.BLACK)
568
+ paint.color = if (isStruck) Color.parseColor("#94A3B8") else parseDynamicColor(props?.opt("color") ?: colorHex, Color.BLACK)
312
569
  paint.textSize = fontSize
313
570
  paint.isStrikeThruText = isStruck
314
- paint.typeface = when (weight) {
315
- "bold", "heavy" -> Typeface.create("sans-serif", Typeface.BOLD)
316
- "semibold", "medium" -> Typeface.create("sans-serif-medium", Typeface.NORMAL)
317
- else -> Typeface.create("sans-serif", Typeface.NORMAL)
571
+
572
+ val fontDesign = props?.optString("fontDesign", "default")
573
+ if (fontDesign == "monospaced") {
574
+ paint.typeface = Typeface.create(Typeface.MONOSPACE, if (weight == "bold" || weight == "heavy") Typeface.BOLD else Typeface.NORMAL)
575
+ } else {
576
+ paint.typeface = when (weight) {
577
+ "bold", "heavy" -> Typeface.create("sans-serif", Typeface.BOLD)
578
+ "semibold", "medium" -> Typeface.create("sans-serif-medium", Typeface.NORMAL)
579
+ else -> Typeface.create("sans-serif", Typeface.NORMAL)
580
+ }
581
+ }
582
+
583
+ val shadowColor = props?.optString("shadowColor", "")
584
+ val shadowRad = (props?.optDouble("shadowRadius", 0.0) ?: 0.0).toFloat() * density
585
+ if (!shadowColor.isNullOrEmpty() && shadowRad > 0f) {
586
+ val sOff = props?.optJSONObject("shadowOffset")
587
+ val sx = (sOff?.optDouble("x", 0.0) ?: 0.0).toFloat() * density
588
+ val sy = (sOff?.optDouble("y", 1.0) ?: 1.0).toFloat() * density
589
+ paint.setShadowLayer(shadowRad, sx, sy, parseHexColor(shadowColor, Color.BLACK))
318
590
  }
319
591
 
320
592
  val display = ellipsizeText(text, paint, availWidth)
321
593
  val fm = paint.fontMetrics
322
594
  val textH = fm.descent - fm.ascent
323
- canvas.drawText(display, left, top - fm.ascent, paint)
595
+ if (props?.optBoolean("redacted", false) == true) {
596
+ val redW = min(availWidth, paint.measureText(display).coerceAtLeast(24f * density))
597
+ val c = paint.color
598
+ paint.style = Paint.Style.FILL
599
+ paint.color = Color.argb(95, Color.red(c), Color.green(c), Color.blue(c))
600
+ val rr = 4f * density
601
+ canvas.drawRoundRect(
602
+ RectF(left, top + textH * 0.14f, left + redW, top + textH * 0.86f),
603
+ rr,
604
+ rr,
605
+ paint
606
+ )
607
+ } else {
608
+ canvas.drawText(display, left, top - fm.ascent, paint)
609
+ }
610
+ paint.clearShadowLayer()
324
611
  textH + 1.5f * density
325
612
  }
326
613
 
327
614
  "Timer" -> {
328
- val formatted = formatTimerString(props)
615
+ val targetTs = props?.optDouble("targetTimestamp", 0.0) ?: 0.0
329
616
  val fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density
330
617
  val colorHex = props?.optString("color", "#2563EB") ?: "#2563EB"
618
+ val tintColor = parseHexColor(colorHex, Color.BLUE)
331
619
 
332
- paint.color = parseHexColor(colorHex, Color.BLUE)
620
+ paint.color = tintColor
333
621
  paint.textSize = fontSize
334
622
  paint.typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD)
335
623
  val fm = paint.fontMetrics
336
- canvas.drawText(formatted, left, top - fm.ascent, paint)
337
- (fm.descent - fm.ascent) + 1.5f * density
624
+ val formatted = formatTimerString(props)
625
+ val textH = (fm.descent - fm.ascent) + 1.5f * density
626
+
627
+ if (targetTs > 0.0) {
628
+ val targetEpochMillis = (targetTs * 1000.0).toLong()
629
+ val remainingMillis = targetEpochMillis - System.currentTimeMillis()
630
+ val baseMillis = SystemClock.elapsedRealtime() + remainingMillis
631
+ val isCountdown = props?.optString("mode", "countdown") != "timer"
632
+
633
+ activeTimerInfo = WidgetTimerInfo(
634
+ baseMillis = baseMillis,
635
+ isCountDown = isCountdown,
636
+ leftPx = left.roundToInt(),
637
+ topPx = top.roundToInt(),
638
+ fontSizePx = fontSize,
639
+ color = tintColor
640
+ )
641
+ } else {
642
+ canvas.drawText(formatted, left, top - fm.ascent, paint)
643
+ }
644
+ textH
338
645
  }
339
646
 
340
647
  "Progress" -> {
@@ -574,14 +881,20 @@ object DynamicWidgetCanvasRenderer {
574
881
  }
575
882
 
576
883
  if (actionId.isNotEmpty() || !deepLink.isNullOrEmpty()) {
884
+ val canvasW = canvas.width.toFloat().coerceAtLeast(1f)
885
+ val canvasH = canvas.height.toFloat().coerceAtLeast(1f)
577
886
  val zone = when {
578
- top < totalHeight * 0.25f -> "top"
579
- top > totalHeight * 0.76f -> "bot"
887
+ top < canvasH * 0.17f -> "top"
888
+ top > canvasH * 0.80f -> "bot"
580
889
  else -> "mid"
581
890
  }
582
- val xF = ((left + btnW / 2f) / availWidth).coerceIn(0f, 1f)
583
- val yF = ((top + btnH / 2f) / totalHeight).coerceIn(0f, 1f)
584
- val wF = (btnW / availWidth).coerceIn(0.1f, 1f)
891
+ val lF = (left / canvasW).coerceIn(0f, 1f)
892
+ val rF = ((left + btnW) / canvasW).coerceIn(0f, 1f)
893
+ val tF = (top / canvasH).coerceIn(0f, 1f)
894
+ val bF = ((top + btnH) / canvasH).coerceIn(0f, 1f)
895
+ val xF = ((lF + rF) / 2f).coerceIn(0f, 1f)
896
+ val yF = ((tF + bF) / 2f).coerceIn(0f, 1f)
897
+ val wF = (rF - lF).coerceIn(0.05f, 1f)
585
898
  bindings.add(
586
899
  ButtonActionBinding(
587
900
  actionId = actionId,
@@ -589,7 +902,14 @@ object DynamicWidgetCanvasRenderer {
589
902
  zone = zone,
590
903
  xFraction = xF,
591
904
  yFraction = yF,
592
- widthFraction = if (fullWidth) 1.0f else wF
905
+ widthFraction = if (fullWidth) 1.0f else wF,
906
+ title = displayTitle,
907
+ accessibilityLabel = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() },
908
+ accessibilityHint = props?.optString("accessibilityHint", "")?.takeIf { it.isNotEmpty() },
909
+ leftFraction = lF,
910
+ rightFraction = rF,
911
+ topFraction = tF,
912
+ bottomFraction = bF
593
913
  )
594
914
  )
595
915
  }
@@ -808,7 +1128,8 @@ object DynamicWidgetCanvasRenderer {
808
1128
  val rawActionId = props?.optString("actionId", "toggle:default") ?: "toggle:default"
809
1129
  val normalizedActionId = if (rawActionId.startsWith("toggle:")) rawActionId else "toggle:$rawActionId"
810
1130
  val key = normalizedActionId.removePrefix("toggle:")
811
- val isChecked = DynamicWidgetStateStore.isToggled(context, key) || (props?.optBoolean("checked", false) == true)
1131
+ val defaultChecked = (props?.optBoolean("checked", false) == true) || (props?.optBoolean("isChecked", false) == true)
1132
+ val isChecked = DynamicWidgetStateStore.isToggled(context, key, defaultChecked)
812
1133
  val label = props?.optString("label", "") ?: ""
813
1134
  val style = props?.optString("style", "checkbox") ?: "checkbox"
814
1135
  val tintHex = props?.optString("tintColor", "#10B981") ?: "#10B981"
@@ -868,12 +1189,33 @@ object DynamicWidgetCanvasRenderer {
868
1189
  canvas.drawText(display, textStartLeft, ty, paint)
869
1190
  paint.isStrikeThruText = false
870
1191
 
1192
+ val canvasW = canvas.width.toFloat().coerceAtLeast(1f)
1193
+ val canvasH = canvas.height.toFloat().coerceAtLeast(1f)
871
1194
  val zone = when {
872
- top < totalHeight * 0.25f -> "top"
873
- top > totalHeight * 0.76f -> "bot"
1195
+ top < canvasH * 0.17f -> "top"
1196
+ top > canvasH * 0.80f -> "bot"
874
1197
  else -> "mid"
875
1198
  }
876
- bindings.add(ButtonActionBinding(actionId = normalizedActionId, deepLink = null, zone = zone))
1199
+ val tF = (top / canvasH).coerceIn(0f, 1f)
1200
+ val bF = ((top + rowH) / canvasH).coerceIn(0f, 1f)
1201
+ val yF = ((tF + bF) / 2f).coerceIn(0f, 1f)
1202
+ bindings.add(
1203
+ ButtonActionBinding(
1204
+ actionId = normalizedActionId,
1205
+ deepLink = null,
1206
+ zone = zone,
1207
+ xFraction = 0.5f,
1208
+ yFraction = yF,
1209
+ widthFraction = 1.0f,
1210
+ title = label,
1211
+ accessibilityLabel = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() } ?: label,
1212
+ accessibilityHint = if (isChecked) "Đã chọn, bấm để bỏ chọn" else "Chưa chọn, bấm để chọn",
1213
+ leftFraction = (left / canvasW).coerceIn(0f, 1f),
1214
+ rightFraction = ((left + availWidth) / canvasW).coerceIn(0f, 1f),
1215
+ topFraction = tF,
1216
+ bottomFraction = bF
1217
+ )
1218
+ )
877
1219
  rowH + 2f * density
878
1220
  }
879
1221
 
@@ -922,13 +1264,19 @@ object DynamicWidgetCanvasRenderer {
922
1264
  val asVStack = JSONObject(node.toString()).apply { put("type", "VStack") }
923
1265
  val usedH = drawNode(context, canvas, asVStack, left, top, availWidth, availHeight, totalHeight, density, false, bindings)
924
1266
  if (actionId.isNotEmpty() || !deepLink.isNullOrEmpty()) {
1267
+ val canvasW = canvas.width.toFloat().coerceAtLeast(1f)
1268
+ val canvasH = canvas.height.toFloat().coerceAtLeast(1f)
925
1269
  val zone = when {
926
- top < totalHeight * 0.25f -> "top"
927
- top > totalHeight * 0.76f -> "bot"
1270
+ top < canvasH * 0.17f -> "top"
1271
+ top > canvasH * 0.80f -> "bot"
928
1272
  else -> "mid"
929
1273
  }
930
- val xF = ((left + availWidth / 2f) / availWidth).coerceIn(0f, 1f)
931
- val yF = ((top + usedH / 2f) / totalHeight).coerceIn(0f, 1f)
1274
+ val lF = (left / canvasW).coerceIn(0f, 1f)
1275
+ val rF = ((left + availWidth) / canvasW).coerceIn(0f, 1f)
1276
+ val tF = (top / canvasH).coerceIn(0f, 1f)
1277
+ val bF = ((top + usedH) / canvasH).coerceIn(0f, 1f)
1278
+ val xF = ((lF + rF) / 2f).coerceIn(0f, 1f)
1279
+ val yF = ((tF + bF) / 2f).coerceIn(0f, 1f)
932
1280
  bindings.add(
933
1281
  ButtonActionBinding(
934
1282
  actionId = actionId,
@@ -936,7 +1284,14 @@ object DynamicWidgetCanvasRenderer {
936
1284
  zone = zone,
937
1285
  xFraction = xF,
938
1286
  yFraction = yF,
939
- widthFraction = 1.0f
1287
+ widthFraction = (rF - lF).coerceIn(0.05f, 1f),
1288
+ title = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() } ?: "Liên kết",
1289
+ accessibilityLabel = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() },
1290
+ accessibilityHint = props?.optString("accessibilityHint", "")?.takeIf { it.isNotEmpty() },
1291
+ leftFraction = lF,
1292
+ rightFraction = rF,
1293
+ topFraction = tF,
1294
+ bottomFraction = bF
940
1295
  )
941
1296
  )
942
1297
  }
@@ -1239,11 +1594,53 @@ object DynamicWidgetCanvasRenderer {
1239
1594
  }
1240
1595
 
1241
1596
  private fun parseHexColor(hex: String, fallback: Int): Int {
1597
+ val trimmed = hex.trim()
1598
+ if (trimmed.isEmpty()) return fallback
1599
+ if (trimmed.startsWith("{")) {
1600
+ return try {
1601
+ parseDynamicColor(JSONObject(trimmed), fallback)
1602
+ } catch (_: Exception) {
1603
+ fallback
1604
+ }
1605
+ }
1242
1606
  return try {
1243
- Color.parseColor(hex)
1607
+ Color.parseColor(trimmed)
1244
1608
  } catch (_: Exception) {
1245
1609
  fallback
1246
1610
  }
1247
1611
  }
1612
+
1613
+ private fun parseDynamicColor(raw: Any?, fallback: Int): Int {
1614
+ if (raw == null) return fallback
1615
+ if (raw is String) return parseHexColor(raw, fallback)
1616
+ if (raw is JSONObject) {
1617
+ val hex = raw.optString("dark", "")
1618
+ .ifEmpty { raw.optString("light", "") }
1619
+ .ifEmpty { raw.optString("tinted", "") }
1620
+ if (hex.isNotEmpty()) return parseHexColor(hex, fallback)
1621
+ }
1622
+ return fallback
1623
+ }
1624
+
1625
+ private fun createLinearGradient(
1626
+ gradObj: JSONObject?,
1627
+ rect: RectF
1628
+ ): Shader? {
1629
+ if (gradObj == null) return null
1630
+ val colorsArr = gradObj.optJSONArray("colors") ?: return null
1631
+ if (colorsArr.length() < 2) return null
1632
+ val colors = IntArray(colorsArr.length())
1633
+ for (i in 0 until colorsArr.length()) {
1634
+ val item = colorsArr.opt(i)
1635
+ colors[i] = parseDynamicColor(item, Color.TRANSPARENT)
1636
+ }
1637
+ val startPt = gradObj.optJSONObject("startPoint")
1638
+ val endPt = gradObj.optJSONObject("endPoint")
1639
+ val x0 = rect.left + rect.width() * (startPt?.optDouble("x", 0.0)?.toFloat() ?: 0f)
1640
+ val y0 = rect.top + rect.height() * (startPt?.optDouble("y", 0.0)?.toFloat() ?: 0f)
1641
+ val x1 = rect.left + rect.width() * (endPt?.optDouble("x", 1.0)?.toFloat() ?: 1f)
1642
+ val y1 = rect.top + rect.height() * (endPt?.optDouble("y", 1.0)?.toFloat() ?: 1f)
1643
+ return android.graphics.LinearGradient(x0, y0, x1, y1, colors, null, Shader.TileMode.CLAMP)
1644
+ }
1248
1645
  }
1249
1646