react-native-live-widgets 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +129 -50
  3. package/android/src/main/AndroidManifest.xml +12 -0
  4. package/android/src/main/assets/fonts/MaterialIcons-Regular.ttf +0 -0
  5. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +61 -1
  6. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +153 -17
  7. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +1 -1
  8. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +529 -59
  9. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +53 -0
  10. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +684 -83
  11. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +129 -57
  12. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicQuickSettingsTileService.kt +120 -0
  13. package/android/src/main/res/layout/rn_live_widget_hit_zone.xml +16 -0
  14. package/android/src/main/res/layout/rn_live_widget_host.xml +16 -87
  15. package/app.plugin.js +36 -0
  16. package/assets/MaterialIcons.json +2236 -0
  17. package/cli/codegen.js +789 -0
  18. package/cli/setup.js +323 -63
  19. package/docs/EXPO-GUIDE-EN.md +21 -3
  20. package/docs/EXPO-GUIDE-VI.md +21 -3
  21. package/docs/GUIDE-EN.md +444 -21
  22. package/docs/GUIDE-VI.md +473 -22
  23. package/docs/index.html +2263 -265
  24. package/docs/llms-full.txt +41 -0
  25. package/docs/llms.txt +24 -7
  26. package/ios/Bridge/LiveActivityModule.swift +333 -44
  27. package/ios/Intents/DynamicWidgetActionIntent.swift +57 -2
  28. package/ios/Models/DynamicLayoutModels.swift +120 -5
  29. package/ios/Renderer/DynamicNodeView.swift +184 -20
  30. package/ios/Storage/DynamicWidgetStateStore.swift +649 -38
  31. package/ios/Widgets/DynamicControlWidget.swift +63 -0
  32. package/ios/Widgets/DynamicHomeScreenWidget.swift +104 -26
  33. package/ios/Widgets/DynamicLiveActivityWidget.swift +25 -4
  34. package/lib/builder.d.ts +50 -7
  35. package/lib/builder.d.ts.map +1 -1
  36. package/lib/builder.js +391 -29
  37. package/lib/builder.js.map +1 -1
  38. package/lib/dispatcher.js +16 -10
  39. package/lib/dispatcher.js.map +1 -1
  40. package/lib/hooks.d.ts +13 -9
  41. package/lib/hooks.d.ts.map +1 -1
  42. package/lib/hooks.js +105 -42
  43. package/lib/hooks.js.map +1 -1
  44. package/lib/index.d.ts +96 -8
  45. package/lib/index.d.ts.map +1 -1
  46. package/lib/index.js +415 -97
  47. package/lib/index.js.map +1 -1
  48. package/lib/ir.d.ts +15 -0
  49. package/lib/ir.d.ts.map +1 -0
  50. package/lib/ir.js +386 -0
  51. package/lib/ir.js.map +1 -0
  52. package/lib/jsx.d.ts +19 -7
  53. package/lib/jsx.d.ts.map +1 -1
  54. package/lib/jsx.js +179 -95
  55. package/lib/jsx.js.map +1 -1
  56. package/lib/material-icons.generated.d.ts +8 -0
  57. package/lib/material-icons.generated.d.ts.map +1 -0
  58. package/lib/material-icons.generated.js +9 -0
  59. package/lib/material-icons.generated.js.map +1 -0
  60. package/lib/preview.d.ts +11 -27
  61. package/lib/preview.d.ts.map +1 -1
  62. package/lib/preview.js +456 -119
  63. package/lib/preview.js.map +1 -1
  64. package/lib/sf-symbols.generated.d.ts +8 -0
  65. package/lib/sf-symbols.generated.d.ts.map +1 -0
  66. package/lib/sf-symbols.generated.js +9 -0
  67. package/lib/sf-symbols.generated.js.map +1 -0
  68. package/lib/types.d.ts +506 -30
  69. package/lib/types.d.ts.map +1 -1
  70. package/lib/types.js +2 -1
  71. package/lib/types.js.map +1 -1
  72. package/package.json +7 -2
  73. package/scripts/build.js +98 -0
  74. package/scripts/generate-material-icons.js +93 -0
  75. package/scripts/generate-sf-symbols.js +95 -0
  76. package/scripts/release.js +115 -0
  77. package/scripts/test-ir-semantic.js +314 -0
  78. package/src/builder.ts +439 -26
  79. package/src/hooks.ts +88 -12
  80. package/src/index.ts +385 -57
  81. package/src/ir.ts +436 -0
  82. package/src/jsx.tsx +99 -21
  83. package/src/material-icons.generated.ts +3844 -0
  84. package/src/preview.tsx +533 -55
  85. package/src/react-native-env.d.ts +1 -0
  86. package/src/sf-symbols.generated.ts +9533 -0
  87. package/src/types.ts +601 -31
@@ -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,69 @@ 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
49
+ private var materialTypeface: Typeface? = null
50
+
51
+ private fun getMaterialTypeface(context: Context): Typeface? {
52
+ if (materialTypeface == null) {
53
+ try {
54
+ materialTypeface = Typeface.createFromAsset(context.assets, "fonts/MaterialIcons-Regular.ttf")
55
+ } catch (_: Exception) {
56
+ }
57
+ }
58
+ return materialTypeface
59
+ }
60
+
61
+ private fun normalizeMaterialName(symbol: String): String {
62
+ if (symbol.isEmpty()) return ""
63
+ val s = symbol.lowercase().replace('.', '_').replace('-', '_')
64
+ return when {
65
+ s == "bolt_horizontal_fill" || s == "bolt_fill" -> "bolt"
66
+ s == "chart_line_uptrend_xyaxis" -> "trending_up"
67
+ s == "chart_line_downtrend_xyaxis" -> "trending_down"
68
+ s == "person_crop_circle_badge_checkmark" -> "verified_user"
69
+ s == "checkmark_circle_fill" || s == "checkmark_circle" -> "check_circle"
70
+ s == "xmark_circle_fill" || s == "xmark_circle" -> "cancel"
71
+ s == "bell_fill" || s == "bell" -> "notifications"
72
+ s == "clock_fill" || s == "clock" -> "schedule"
73
+ s == "star_fill" -> "star"
74
+ s == "heart_fill" -> "favorite"
75
+ s == "lock_fill" -> "lock"
76
+ s == "shield_fill" -> "security"
77
+ s == "gearshape_fill" || s == "gearshape" -> "settings"
78
+ s == "arrow_clockwise" -> "sync"
79
+ s == "cart_fill" -> "shopping_cart"
80
+ s == "envelope_fill" -> "mail"
81
+ s == "trash_fill" -> "delete"
82
+ else -> s.removeSuffix("_fill")
83
+ }
84
+ }
31
85
 
32
86
  private fun isFlexibleSpacer(node: JSONObject?): Boolean {
33
87
  if (node == null || node.optString("type") != "Spacer") return false
@@ -35,6 +89,139 @@ object DynamicWidgetCanvasRenderer {
35
89
  return !props.has("minLength")
36
90
  }
37
91
 
92
+ /**
93
+ * ♿ Trích xuất bản tóm tắt Trợ năng (Accessibility Summary) từ cây JSON DSL.
94
+ * Cho phép TalkBack của Android đọc đầy đủ các nhãn, con số, trạng thái và nút bấm
95
+ * ngay cả khi toàn bộ giao diện đang được render bằng Canvas 2D Bitmap.
96
+ */
97
+ fun extractAccessibilitySummary(node: JSONObject?): String {
98
+ if (node == null) return ""
99
+ val parts = mutableListOf<String>()
100
+ traverseAccessibility(node, parts)
101
+ return parts.distinct().joinToString(". ")
102
+ }
103
+
104
+ private fun traverseAccessibility(node: JSONObject, parts: MutableList<String>) {
105
+ val props = node.optJSONObject("props")
106
+ val accessible = props?.optBoolean("accessible", true) ?: true
107
+ if (!accessible) return
108
+
109
+ val customLabel = props?.optString("accessibilityLabel", "")?.trim() ?: ""
110
+ val customValue = props?.optString("accessibilityValue", "")?.trim() ?: ""
111
+ val customHint = props?.optString("accessibilityHint", "")?.trim() ?: ""
112
+
113
+ val type = node.optString("type")
114
+ when (type) {
115
+ "Text" -> {
116
+ val text = props?.optString("text", "")?.trim() ?: ""
117
+ val content = when {
118
+ customLabel.isNotEmpty() -> customLabel
119
+ text.isNotEmpty() -> text
120
+ else -> ""
121
+ }
122
+ if (content.isNotEmpty()) parts.add(content)
123
+ }
124
+ "Button" -> {
125
+ val title = props?.optString("title", "")?.trim() ?: ""
126
+ val btnName = if (customLabel.isNotEmpty()) customLabel else title
127
+ val fullBtn = if (customHint.isNotEmpty()) "Nút $btnName, $customHint" else "Nút $btnName"
128
+ if (btnName.isNotEmpty()) parts.add(fullBtn)
129
+ }
130
+ "Progress" -> {
131
+ val label = if (customLabel.isNotEmpty()) customLabel else "Tiến độ"
132
+ val value = when {
133
+ customValue.isNotEmpty() -> customValue
134
+ props != null && props.has("value") -> "${(props.optDouble("value", 0.0) * 100).toInt()}%"
135
+ else -> ""
136
+ }
137
+ parts.add(if (value.isNotEmpty()) "$label $value" else label)
138
+ }
139
+ "Gauge" -> {
140
+ val label = if (customLabel.isNotEmpty()) customLabel else (props?.optString("label", "")?.trim() ?: "Chỉ số")
141
+ val valText = props?.optString("valueText", "")?.trim() ?: ""
142
+ val value = when {
143
+ customValue.isNotEmpty() -> customValue
144
+ valText.isNotEmpty() -> valText
145
+ props != null && props.has("value") -> "${(props.optDouble("value", 0.0) * 100).toInt()}%"
146
+ else -> ""
147
+ }
148
+ parts.add(if (value.isNotEmpty()) "$label: $value" else label)
149
+ }
150
+ "Timer" -> {
151
+ val label = if (customLabel.isNotEmpty()) customLabel else "Đồng hồ"
152
+ parts.add(label)
153
+ }
154
+ "Badge" -> {
155
+ val text = props?.optString("text", "")?.trim() ?: ""
156
+ val label = if (customLabel.isNotEmpty()) customLabel else text
157
+ if (label.isNotEmpty()) parts.add(label)
158
+ }
159
+ "Toggle" -> {
160
+ val label = props?.optString("label", "")?.trim() ?: ""
161
+ val isChecked = props?.optBoolean("isChecked", false) ?: false
162
+ val desc = if (customLabel.isNotEmpty()) customLabel else label
163
+ if (desc.isNotEmpty()) {
164
+ parts.add("$desc: " + if (isChecked) "Đã chọn" else "Chưa chọn")
165
+ }
166
+ }
167
+ "Image" -> {
168
+ if (customLabel.isNotEmpty()) {
169
+ parts.add("Ảnh $customLabel")
170
+ }
171
+ }
172
+ else -> {
173
+ if (customLabel.isNotEmpty()) {
174
+ parts.add(customLabel)
175
+ }
176
+ }
177
+ }
178
+
179
+ val children = node.optJSONArray("children")
180
+ if (children != null) {
181
+ for (i in 0 until children.length()) {
182
+ val child = children.optJSONObject(i)
183
+ if (child != null) {
184
+ traverseAccessibility(child, parts)
185
+ }
186
+ }
187
+ }
188
+ }
189
+
190
+ /**
191
+ * ⚡ [Phase 5 Optimization] Kết xuất Sub-Bitmap cục bộ chỉ cho thành phần đồ họa cụ thể
192
+ * (Gauge, Equalizer, Circular Progress) thay vì vẽ cả một Bitmap khổng lồ toàn màn hình.
193
+ * Tiết kiệm >90% dung lượng RAM và tránh lỗi TransactionTooLargeException của Android IPC Binder.
194
+ */
195
+ fun renderSubNodeBitmap(
196
+ context: Context,
197
+ node: JSONObject,
198
+ widthPx: Int,
199
+ heightPx: Int,
200
+ density: Float
201
+ ): Bitmap {
202
+ val bmp = Bitmap.createBitmap(
203
+ widthPx.coerceAtLeast(1),
204
+ heightPx.coerceAtLeast(1),
205
+ Bitmap.Config.ARGB_8888
206
+ )
207
+ val canvas = Canvas(bmp)
208
+ val bindings = mutableListOf<ButtonActionBinding>()
209
+ drawNode(
210
+ context = context,
211
+ canvas = canvas,
212
+ node = node,
213
+ left = 0f,
214
+ top = 0f,
215
+ availWidth = widthPx.toFloat(),
216
+ availHeight = heightPx.toFloat(),
217
+ totalHeight = heightPx.toFloat(),
218
+ density = density,
219
+ isRoot = true,
220
+ bindings = bindings
221
+ )
222
+ return bmp
223
+ }
224
+
38
225
  fun renderWidgetBitmap(
39
226
  context: Context,
40
227
  rootNode: JSONObject?,
@@ -44,6 +231,7 @@ object DynamicWidgetCanvasRenderer {
44
231
  family: String,
45
232
  bindings: MutableList<ButtonActionBinding>
46
233
  ): Bitmap {
234
+ activeTimerInfo = null
47
235
  val bmp = Bitmap.createBitmap(widthPx, heightPx, Bitmap.Config.ARGB_8888)
48
236
  val canvas = Canvas(bmp)
49
237
  val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
@@ -71,7 +259,7 @@ object DynamicWidgetCanvasRenderer {
71
259
 
72
260
  val naturalH = measureNodeHeight(context, rootNode, widthPx.toFloat(), baseDensity)
73
261
  val fitScale = if (naturalH > heightPx * 0.96f && naturalH > 0f) {
74
- ((heightPx * 0.95f) / naturalH).coerceIn(0.62f, 1.0f)
262
+ ((heightPx * 0.95f) / naturalH).coerceIn(0.55f, 1.0f)
75
263
  } else {
76
264
  1.0f
77
265
  }
@@ -141,17 +329,41 @@ object DynamicWidgetCanvasRenderer {
141
329
  val measuredH = measureNodeHeight(context, node, availWidth, density)
142
330
  val boxH = if (isRoot) max(availHeight, measuredH) else (measuredH + 2f * density)
143
331
 
144
- if (!isRoot && bgHex.isNotEmpty()) {
332
+ if (!isRoot && (bgHex.isNotEmpty() || props?.has("gradient") == true)) {
145
333
  val rect = RectF(left, top, left + availWidth, top + boxH)
334
+ val gradShader = createLinearGradient(props?.optJSONObject("gradient"), rect)
146
335
  paint.style = Paint.Style.FILL
147
- paint.color = parseHexColor(bgHex, Color.TRANSPARENT)
336
+ if (gradShader != null) {
337
+ paint.shader = gradShader
338
+ } else {
339
+ paint.color = parseDynamicColor(props?.opt("backgroundColor") ?: bgHex, Color.TRANSPARENT)
340
+ }
341
+
342
+ val shadowColor = props?.optString("shadowColor", "")
343
+ val shadowRad = (props?.optDouble("shadowRadius", 0.0) ?: 0.0).toFloat() * density
344
+ if (!shadowColor.isNullOrEmpty() && shadowRad > 0f) {
345
+ val sOff = props?.optJSONObject("shadowOffset")
346
+ val sx = (sOff?.optDouble("x", 0.0) ?: 0.0).toFloat() * density
347
+ val sy = (sOff?.optDouble("y", 2.0) ?: 2.0).toFloat() * density
348
+ paint.setShadowLayer(shadowRad, sx, sy, parseHexColor(shadowColor, Color.BLACK))
349
+ }
350
+
148
351
  canvas.drawRoundRect(rect, radius, radius, paint)
352
+ paint.clearShadowLayer()
353
+ paint.shader = null
149
354
 
150
355
  if (borderW > 0f && borderHex.isNotEmpty()) {
151
356
  paint.style = Paint.Style.STROKE
152
357
  paint.strokeWidth = borderW
153
358
  paint.color = parseHexColor(borderHex, Color.LTGRAY)
359
+ val borderStyle = props?.optString("borderStyle", "solid")
360
+ if (borderStyle == "dashed") {
361
+ paint.pathEffect = android.graphics.DashPathEffect(floatArrayOf(12f * density, 8f * density), 0f)
362
+ } else if (borderStyle == "dotted") {
363
+ paint.pathEffect = android.graphics.DashPathEffect(floatArrayOf(4f * density, 6f * density), 0f)
364
+ }
154
365
  canvas.drawRoundRect(rect, radius, radius, paint)
366
+ paint.pathEffect = null
155
367
  }
156
368
  }
157
369
 
@@ -276,24 +488,83 @@ object DynamicWidgetCanvasRenderer {
276
488
  curLeft += w + spacing
277
489
  }
278
490
  } else {
279
- var curLeft = left + pad
491
+ var fullWidthCount = 0
280
492
  for (i in 0 until children.length()) {
281
493
  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
494
+ if (c.optJSONObject("props")?.optBoolean("fullWidth", false) == true) {
495
+ fullWidthCount++
286
496
  }
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))
497
+ }
498
+
499
+ val justify = props?.optString("justifyContent", "") ?: ""
500
+ if (fullWidthCount > 0) {
501
+ var fixedW = 0f
502
+ for (i in 0 until children.length()) {
503
+ val c = children.optJSONObject(i) ?: continue
504
+ if (c.optString("type") == "Spacer") {
505
+ fixedW += (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
506
+ } else if (c.optJSONObject("props")?.optBoolean("fullWidth", false) != true) {
507
+ fixedW += measureNodeWidth(context, c, innerW, density)
508
+ }
509
+ }
510
+ val totalSpacing = spacing * (children.length() - 1).coerceAtLeast(0)
511
+ val fullWidthW = ((innerW - fixedW - totalSpacing) / fullWidthCount).coerceAtLeast(30f)
512
+
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 + spacing
519
+ continue
520
+ }
521
+ val isFull = c.optJSONObject("props")?.optBoolean("fullWidth", false) == true
522
+ val w = if (isFull) fullWidthW else measureNodeWidth(context, c, innerW, density)
523
+ val childH = measureNodeHeight(context, c, w, density)
524
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
525
+ drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
526
+ curLeft += w + spacing
527
+ }
528
+ } else if (justify in listOf("spaceBetween", "spaceAround") && children.length() == 2) {
529
+ val c0 = children.optJSONObject(0)
530
+ val c1 = children.optJSONObject(1)
531
+ if (c0 != null && c1 != null) {
532
+ val w1 = min(innerW * 0.72f, measureNodeWidth(context, c1, innerW, density))
533
+ val h1 = measureNodeHeight(context, c1, w1, density)
534
+ val top1 = top + pad + ((innerH - h1) / 2f).coerceAtLeast(0f)
535
+ val rightLeft = left + availWidth - pad - w1
536
+ drawNode(context, canvas, c1, rightLeft, top1, w1, h1, totalHeight, density, false, bindings)
537
+
538
+ val maxLeftAreaW = (rightLeft - spacing - (left + pad)).coerceAtLeast(30f)
539
+ val w0 = if (c0.optString("type") == "VStack") {
540
+ maxLeftAreaW
541
+ } else {
542
+ min(maxLeftAreaW, measureNodeWidth(context, c0, maxLeftAreaW, density))
543
+ }
544
+ val h0 = measureNodeHeight(context, c0, w0, density)
545
+ val top0 = top + pad + ((innerH - h0) / 2f).coerceAtLeast(0f)
546
+ drawNode(context, canvas, c0, left + pad, top0, w0, h0, totalHeight, density, false, bindings)
547
+ }
548
+ } else {
549
+ var curLeft = left + pad
550
+ for (i in 0 until children.length()) {
551
+ val c = children.optJSONObject(i) ?: continue
552
+ if (c.optString("type") == "Spacer") {
553
+ val gapW = (c.optJSONObject("props")?.optDouble("minLength", 6.0) ?: 6.0).toFloat() * density
554
+ curLeft += gapW
555
+ continue
556
+ }
557
+ val remainingW = (left + availWidth - pad - curLeft).coerceAtLeast(20f)
558
+ val w = if (i == children.length() - 1 && c.optString("type") in listOf("VStack", "Progress", "Text")) {
559
+ remainingW
560
+ } else {
561
+ min(remainingW, measureNodeWidth(context, c, innerW, density))
562
+ }
563
+ val childH = measureNodeHeight(context, c, w, density)
564
+ val childTop = top + pad + ((innerH - childH) / 2f).coerceAtLeast(0f)
565
+ drawNode(context, canvas, c, curLeft, childTop, w, childH, totalHeight, density, false, bindings)
566
+ curLeft += w + spacing
292
567
  }
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
568
  }
298
569
  }
299
570
  }
@@ -302,39 +573,111 @@ object DynamicWidgetCanvasRenderer {
302
573
 
303
574
  "Text" -> {
304
575
  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"
576
+ val fontScale = context.resources.configuration.fontScale
577
+ val dynamicType = props?.optBoolean("dynamicType", true) ?: true
578
+ val effectiveScale = if (dynamicType) fontScale else 1.0f
579
+
580
+ var fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density * effectiveScale
581
+ var weight = props?.optString("fontWeight", "regular") ?: "regular"
582
+
583
+ val typo = props?.optString("typography", "")
584
+ if (!typo.isNullOrEmpty()) {
585
+ when (typo) {
586
+ "largeTitle" -> { fontSize = 34f * density * effectiveScale; weight = "bold" }
587
+ "title1", "title" -> { fontSize = 28f * density * effectiveScale; weight = "bold" }
588
+ "title2" -> { fontSize = 22f * density * effectiveScale; weight = "bold" }
589
+ "title3" -> { fontSize = 20f * density * effectiveScale; weight = "semibold" }
590
+ "headline" -> { fontSize = 17f * density * effectiveScale; weight = "semibold" }
591
+ "subheadline" -> { fontSize = 15f * density * effectiveScale }
592
+ "body" -> { fontSize = 17f * density * effectiveScale }
593
+ "callout" -> { fontSize = 16f * density * effectiveScale }
594
+ "footnote" -> { fontSize = 13f * density * effectiveScale }
595
+ "caption1", "caption" -> { fontSize = 12f * density * effectiveScale }
596
+ "caption2" -> { fontSize = 11f * density * effectiveScale }
597
+ }
598
+ }
599
+
307
600
  val colorHex = props?.optString("color", "#0F172A") ?: "#0F172A"
308
601
  val toggleKey = props?.optString("strikethroughWhenToggled", "") ?: ""
309
602
  val isStruck = toggleKey.isNotEmpty() && DynamicWidgetStateStore.isToggled(context, toggleKey)
310
603
 
311
- paint.color = if (isStruck) Color.parseColor("#94A3B8") else parseHexColor(colorHex, Color.BLACK)
604
+ paint.color = if (isStruck) Color.parseColor("#94A3B8") else parseDynamicColor(props?.opt("color") ?: colorHex, Color.BLACK)
312
605
  paint.textSize = fontSize
313
606
  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)
607
+
608
+ val fontDesign = props?.optString("fontDesign", "default")
609
+ if (fontDesign == "monospaced") {
610
+ paint.typeface = Typeface.create(Typeface.MONOSPACE, if (weight == "bold" || weight == "heavy") Typeface.BOLD else Typeface.NORMAL)
611
+ } else {
612
+ paint.typeface = when (weight) {
613
+ "bold", "heavy" -> Typeface.create("sans-serif", Typeface.BOLD)
614
+ "semibold", "medium" -> Typeface.create("sans-serif-medium", Typeface.NORMAL)
615
+ else -> Typeface.create("sans-serif", Typeface.NORMAL)
616
+ }
617
+ }
618
+
619
+ val shadowColor = props?.optString("shadowColor", "")
620
+ val shadowRad = (props?.optDouble("shadowRadius", 0.0) ?: 0.0).toFloat() * density
621
+ if (!shadowColor.isNullOrEmpty() && shadowRad > 0f) {
622
+ val sOff = props?.optJSONObject("shadowOffset")
623
+ val sx = (sOff?.optDouble("x", 0.0) ?: 0.0).toFloat() * density
624
+ val sy = (sOff?.optDouble("y", 1.0) ?: 1.0).toFloat() * density
625
+ paint.setShadowLayer(shadowRad, sx, sy, parseHexColor(shadowColor, Color.BLACK))
318
626
  }
319
627
 
320
628
  val display = ellipsizeText(text, paint, availWidth)
321
629
  val fm = paint.fontMetrics
322
630
  val textH = fm.descent - fm.ascent
323
- canvas.drawText(display, left, top - fm.ascent, paint)
631
+ if (props?.optBoolean("redacted", false) == true) {
632
+ val redW = min(availWidth, paint.measureText(display).coerceAtLeast(24f * density))
633
+ val c = paint.color
634
+ paint.style = Paint.Style.FILL
635
+ paint.color = Color.argb(95, Color.red(c), Color.green(c), Color.blue(c))
636
+ val rr = 4f * density
637
+ canvas.drawRoundRect(
638
+ RectF(left, top + textH * 0.14f, left + redW, top + textH * 0.86f),
639
+ rr,
640
+ rr,
641
+ paint
642
+ )
643
+ } else {
644
+ canvas.drawText(display, left, top - fm.ascent, paint)
645
+ }
646
+ paint.clearShadowLayer()
324
647
  textH + 1.5f * density
325
648
  }
326
649
 
327
650
  "Timer" -> {
328
- val formatted = formatTimerString(props)
651
+ val targetTs = props?.optDouble("targetTimestamp", 0.0) ?: 0.0
329
652
  val fontSize = (props?.optDouble("fontSize", 13.0) ?: 13.0).toFloat() * density
330
653
  val colorHex = props?.optString("color", "#2563EB") ?: "#2563EB"
654
+ val tintColor = parseHexColor(colorHex, Color.BLUE)
331
655
 
332
- paint.color = parseHexColor(colorHex, Color.BLUE)
656
+ paint.color = tintColor
333
657
  paint.textSize = fontSize
334
658
  paint.typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD)
335
659
  val fm = paint.fontMetrics
336
- canvas.drawText(formatted, left, top - fm.ascent, paint)
337
- (fm.descent - fm.ascent) + 1.5f * density
660
+ val formatted = formatTimerString(props)
661
+ val textH = (fm.descent - fm.ascent) + 1.5f * density
662
+
663
+ if (targetTs > 0.0) {
664
+ val targetEpochMillis = (targetTs * 1000.0).toLong()
665
+ val remainingMillis = targetEpochMillis - System.currentTimeMillis()
666
+ val baseMillis = SystemClock.elapsedRealtime() + remainingMillis
667
+ val isCountdown = props?.optString("mode", "countdown") != "timer"
668
+
669
+ activeTimerInfo = WidgetTimerInfo(
670
+ baseMillis = baseMillis,
671
+ isCountDown = isCountdown,
672
+ leftPx = left.roundToInt(),
673
+ topPx = top.roundToInt(),
674
+ fontSizePx = fontSize,
675
+ color = tintColor
676
+ )
677
+ } else {
678
+ canvas.drawText(formatted, left, top - fm.ascent, paint)
679
+ }
680
+ textH
338
681
  }
339
682
 
340
683
  "Progress" -> {
@@ -485,21 +828,58 @@ object DynamicWidgetCanvasRenderer {
485
828
  canvas.drawRoundRect(rect, radius, radius, paint)
486
829
 
487
830
  paint.color = parseHexColor(tintHex, Color.BLUE)
488
- paint.textSize = min(w, h) * 0.48f
489
831
  paint.textAlign = Paint.Align.CENTER
490
- val symbol = props?.optString("systemName", "") ?: ""
491
- val glyph = when {
492
- symbol.contains("person") -> "👤"
493
- symbol.contains("sparkle") -> "✨"
494
- symbol.contains("check") -> "✓"
495
- symbol.contains("bell") -> "🔔"
496
- symbol.contains("heart") -> "💜"
497
- symbol.contains("star") -> "★"
498
- else -> "🖼️"
832
+
833
+ val symbol = (props?.optString("androidSystemName", "")
834
+ ?.ifEmpty { props.optString("systemNameAndroid", "") }
835
+ ?.ifEmpty { props.optString("materialName", "") }
836
+ ?.ifEmpty { props.optString("systemName", "") } ?: "").lowercase()
837
+
838
+ val matName = normalizeMaterialName(symbol)
839
+ val matTypeface = getMaterialTypeface(context)
840
+
841
+ if (matTypeface != null && matName.isNotEmpty()) {
842
+ paint.typeface = matTypeface
843
+ paint.textSize = min(w, h) * 0.58f
844
+ val fm = paint.fontMetrics
845
+ val cy = top + h / 2f - (fm.ascent + fm.descent) / 2f
846
+ // Tận dụng OpenType Ligatures của Google: Font tự ghép chuỗi ký tự thành icon vector!
847
+ canvas.drawText(matName, left + w / 2f, cy, paint)
848
+ paint.typeface = null
849
+ } else {
850
+ paint.textSize = min(w, h) * 0.48f
851
+ val glyph = when {
852
+ symbol.contains("bolt") -> "⚡"
853
+ symbol.contains("chart") -> "📈"
854
+ symbol.contains("clock") || symbol.contains("timer") || symbol.contains("stopwatch") -> "⏱️"
855
+ symbol.contains("calendar") -> "📅"
856
+ symbol.contains("check") -> "✓"
857
+ symbol.contains("xmark") -> "✕"
858
+ symbol.contains("person") -> "👤"
859
+ symbol.contains("bell") -> "🔔"
860
+ symbol.contains("flame") -> "🔥"
861
+ symbol.contains("star") -> "★"
862
+ symbol.contains("heart") -> "💜"
863
+ symbol.contains("sparkle") -> "✨"
864
+ symbol.contains("trophy") || symbol.contains("medal") -> "🏆"
865
+ symbol.contains("target") -> "🎯"
866
+ symbol.contains("shield") || symbol.contains("lock") -> "🔒"
867
+ symbol.contains("plus") -> "➕"
868
+ symbol.contains("minus") -> "➖"
869
+ symbol.contains("play") -> "▶️"
870
+ symbol.contains("pause") -> "⏸️"
871
+ symbol.contains("wifi") -> "📶"
872
+ symbol.contains("battery") -> "🔋"
873
+ symbol.contains("cart") || symbol.contains("bag") || symbol.contains("dollar") -> "💳"
874
+ symbol.contains("map") || symbol.contains("location") -> "📍"
875
+ symbol.contains("phone") -> "📞"
876
+ symbol.contains("message") || symbol.contains("envelope") || symbol.contains("mail") -> "✉️"
877
+ else -> "🖼️"
878
+ }
879
+ val fm = paint.fontMetrics
880
+ val cy = top + h / 2f - (fm.ascent + fm.descent) / 2f
881
+ canvas.drawText(glyph, left + w / 2f, cy, paint)
499
882
  }
500
- val fm = paint.fontMetrics
501
- val cy = top + h / 2f - (fm.ascent + fm.descent) / 2f
502
- canvas.drawText(glyph, left + w / 2f, cy, paint)
503
883
  }
504
884
 
505
885
  if (borderW > 0f) {
@@ -574,14 +954,20 @@ object DynamicWidgetCanvasRenderer {
574
954
  }
575
955
 
576
956
  if (actionId.isNotEmpty() || !deepLink.isNullOrEmpty()) {
957
+ val canvasW = canvas.width.toFloat().coerceAtLeast(1f)
958
+ val canvasH = canvas.height.toFloat().coerceAtLeast(1f)
577
959
  val zone = when {
578
- top < totalHeight * 0.25f -> "top"
579
- top > totalHeight * 0.76f -> "bot"
960
+ top < canvasH * 0.17f -> "top"
961
+ top > canvasH * 0.80f -> "bot"
580
962
  else -> "mid"
581
963
  }
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)
964
+ val lF = (left / canvasW).coerceIn(0f, 1f)
965
+ val rF = ((left + btnW) / canvasW).coerceIn(0f, 1f)
966
+ val tF = (top / canvasH).coerceIn(0f, 1f)
967
+ val bF = ((top + btnH) / canvasH).coerceIn(0f, 1f)
968
+ val xF = ((lF + rF) / 2f).coerceIn(0f, 1f)
969
+ val yF = ((tF + bF) / 2f).coerceIn(0f, 1f)
970
+ val wF = (rF - lF).coerceIn(0.05f, 1f)
585
971
  bindings.add(
586
972
  ButtonActionBinding(
587
973
  actionId = actionId,
@@ -589,7 +975,14 @@ object DynamicWidgetCanvasRenderer {
589
975
  zone = zone,
590
976
  xFraction = xF,
591
977
  yFraction = yF,
592
- widthFraction = if (fullWidth) 1.0f else wF
978
+ widthFraction = if (fullWidth) 1.0f else wF,
979
+ title = displayTitle,
980
+ accessibilityLabel = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() },
981
+ accessibilityHint = props?.optString("accessibilityHint", "")?.takeIf { it.isNotEmpty() },
982
+ leftFraction = lF,
983
+ rightFraction = rF,
984
+ topFraction = tF,
985
+ bottomFraction = bF
593
986
  )
594
987
  )
595
988
  }
@@ -808,7 +1201,8 @@ object DynamicWidgetCanvasRenderer {
808
1201
  val rawActionId = props?.optString("actionId", "toggle:default") ?: "toggle:default"
809
1202
  val normalizedActionId = if (rawActionId.startsWith("toggle:")) rawActionId else "toggle:$rawActionId"
810
1203
  val key = normalizedActionId.removePrefix("toggle:")
811
- val isChecked = DynamicWidgetStateStore.isToggled(context, key) || (props?.optBoolean("checked", false) == true)
1204
+ val defaultChecked = (props?.optBoolean("checked", false) == true) || (props?.optBoolean("isChecked", false) == true)
1205
+ val isChecked = DynamicWidgetStateStore.isToggled(context, key, defaultChecked)
812
1206
  val label = props?.optString("label", "") ?: ""
813
1207
  val style = props?.optString("style", "checkbox") ?: "checkbox"
814
1208
  val tintHex = props?.optString("tintColor", "#10B981") ?: "#10B981"
@@ -868,12 +1262,33 @@ object DynamicWidgetCanvasRenderer {
868
1262
  canvas.drawText(display, textStartLeft, ty, paint)
869
1263
  paint.isStrikeThruText = false
870
1264
 
1265
+ val canvasW = canvas.width.toFloat().coerceAtLeast(1f)
1266
+ val canvasH = canvas.height.toFloat().coerceAtLeast(1f)
871
1267
  val zone = when {
872
- top < totalHeight * 0.25f -> "top"
873
- top > totalHeight * 0.76f -> "bot"
1268
+ top < canvasH * 0.17f -> "top"
1269
+ top > canvasH * 0.80f -> "bot"
874
1270
  else -> "mid"
875
1271
  }
876
- bindings.add(ButtonActionBinding(actionId = normalizedActionId, deepLink = null, zone = zone))
1272
+ val tF = (top / canvasH).coerceIn(0f, 1f)
1273
+ val bF = ((top + rowH) / canvasH).coerceIn(0f, 1f)
1274
+ val yF = ((tF + bF) / 2f).coerceIn(0f, 1f)
1275
+ bindings.add(
1276
+ ButtonActionBinding(
1277
+ actionId = normalizedActionId,
1278
+ deepLink = null,
1279
+ zone = zone,
1280
+ xFraction = 0.5f,
1281
+ yFraction = yF,
1282
+ widthFraction = 1.0f,
1283
+ title = label,
1284
+ accessibilityLabel = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() } ?: label,
1285
+ accessibilityHint = if (isChecked) "Đã chọn, bấm để bỏ chọn" else "Chưa chọn, bấm để chọn",
1286
+ leftFraction = (left / canvasW).coerceIn(0f, 1f),
1287
+ rightFraction = ((left + availWidth) / canvasW).coerceIn(0f, 1f),
1288
+ topFraction = tF,
1289
+ bottomFraction = bF
1290
+ )
1291
+ )
877
1292
  rowH + 2f * density
878
1293
  }
879
1294
 
@@ -922,13 +1337,19 @@ object DynamicWidgetCanvasRenderer {
922
1337
  val asVStack = JSONObject(node.toString()).apply { put("type", "VStack") }
923
1338
  val usedH = drawNode(context, canvas, asVStack, left, top, availWidth, availHeight, totalHeight, density, false, bindings)
924
1339
  if (actionId.isNotEmpty() || !deepLink.isNullOrEmpty()) {
1340
+ val canvasW = canvas.width.toFloat().coerceAtLeast(1f)
1341
+ val canvasH = canvas.height.toFloat().coerceAtLeast(1f)
925
1342
  val zone = when {
926
- top < totalHeight * 0.25f -> "top"
927
- top > totalHeight * 0.76f -> "bot"
1343
+ top < canvasH * 0.17f -> "top"
1344
+ top > canvasH * 0.80f -> "bot"
928
1345
  else -> "mid"
929
1346
  }
930
- val xF = ((left + availWidth / 2f) / availWidth).coerceIn(0f, 1f)
931
- val yF = ((top + usedH / 2f) / totalHeight).coerceIn(0f, 1f)
1347
+ val lF = (left / canvasW).coerceIn(0f, 1f)
1348
+ val rF = ((left + availWidth) / canvasW).coerceIn(0f, 1f)
1349
+ val tF = (top / canvasH).coerceIn(0f, 1f)
1350
+ val bF = ((top + usedH) / canvasH).coerceIn(0f, 1f)
1351
+ val xF = ((lF + rF) / 2f).coerceIn(0f, 1f)
1352
+ val yF = ((tF + bF) / 2f).coerceIn(0f, 1f)
932
1353
  bindings.add(
933
1354
  ButtonActionBinding(
934
1355
  actionId = actionId,
@@ -936,7 +1357,14 @@ object DynamicWidgetCanvasRenderer {
936
1357
  zone = zone,
937
1358
  xFraction = xF,
938
1359
  yFraction = yF,
939
- widthFraction = 1.0f
1360
+ widthFraction = (rF - lF).coerceIn(0.05f, 1f),
1361
+ title = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() } ?: "Liên kết",
1362
+ accessibilityLabel = props?.optString("accessibilityLabel", "")?.takeIf { it.isNotEmpty() },
1363
+ accessibilityHint = props?.optString("accessibilityHint", "")?.takeIf { it.isNotEmpty() },
1364
+ leftFraction = lF,
1365
+ rightFraction = rF,
1366
+ topFraction = tF,
1367
+ bottomFraction = bF
940
1368
  )
941
1369
  )
942
1370
  }
@@ -1239,11 +1667,53 @@ object DynamicWidgetCanvasRenderer {
1239
1667
  }
1240
1668
 
1241
1669
  private fun parseHexColor(hex: String, fallback: Int): Int {
1670
+ val trimmed = hex.trim()
1671
+ if (trimmed.isEmpty()) return fallback
1672
+ if (trimmed.startsWith("{")) {
1673
+ return try {
1674
+ parseDynamicColor(JSONObject(trimmed), fallback)
1675
+ } catch (_: Exception) {
1676
+ fallback
1677
+ }
1678
+ }
1242
1679
  return try {
1243
- Color.parseColor(hex)
1680
+ Color.parseColor(trimmed)
1244
1681
  } catch (_: Exception) {
1245
1682
  fallback
1246
1683
  }
1247
1684
  }
1685
+
1686
+ private fun parseDynamicColor(raw: Any?, fallback: Int): Int {
1687
+ if (raw == null) return fallback
1688
+ if (raw is String) return parseHexColor(raw, fallback)
1689
+ if (raw is JSONObject) {
1690
+ val hex = raw.optString("dark", "")
1691
+ .ifEmpty { raw.optString("light", "") }
1692
+ .ifEmpty { raw.optString("tinted", "") }
1693
+ if (hex.isNotEmpty()) return parseHexColor(hex, fallback)
1694
+ }
1695
+ return fallback
1696
+ }
1697
+
1698
+ private fun createLinearGradient(
1699
+ gradObj: JSONObject?,
1700
+ rect: RectF
1701
+ ): Shader? {
1702
+ if (gradObj == null) return null
1703
+ val colorsArr = gradObj.optJSONArray("colors") ?: return null
1704
+ if (colorsArr.length() < 2) return null
1705
+ val colors = IntArray(colorsArr.length())
1706
+ for (i in 0 until colorsArr.length()) {
1707
+ val item = colorsArr.opt(i)
1708
+ colors[i] = parseDynamicColor(item, Color.TRANSPARENT)
1709
+ }
1710
+ val startPt = gradObj.optJSONObject("startPoint")
1711
+ val endPt = gradObj.optJSONObject("endPoint")
1712
+ val x0 = rect.left + rect.width() * (startPt?.optDouble("x", 0.0)?.toFloat() ?: 0f)
1713
+ val y0 = rect.top + rect.height() * (startPt?.optDouble("y", 0.0)?.toFloat() ?: 0f)
1714
+ val x1 = rect.left + rect.width() * (endPt?.optDouble("x", 1.0)?.toFloat() ?: 1f)
1715
+ val y1 = rect.top + rect.height() * (endPt?.optDouble("y", 1.0)?.toFloat() ?: 1f)
1716
+ return android.graphics.LinearGradient(x0, y0, x1, y1, colors, null, Shader.TileMode.CLAMP)
1717
+ }
1248
1718
  }
1249
1719