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
@@ -1,3 +1,4 @@
1
+ import ActivityKit
1
2
  import AppIntents
2
3
  import WidgetKit
3
4
 
@@ -22,9 +23,26 @@ public struct DynamicWidgetActionIntent: AppIntent {
22
23
  public func perform() async throws -> some IntentResult {
23
24
  if actionId == "refresh" || actionId.hasPrefix("refresh:") {
24
25
  let key = actionId.hasPrefix("refresh:") ? String(actionId.dropFirst(8)) : "main"
25
- await DynamicWidgetStateStore.performRemoteSync(widgetKey: key)
26
+ await DynamicWidgetStateStore.performRemoteSync(widgetKey: key, forceRefresh: true)
26
27
  } else {
27
- DynamicWidgetStateStore.handleAction(actionId)
28
+ let updatedState = DynamicWidgetStateStore.handleAction(actionId)
29
+ if DynamicWidgetStateStore.isCounterAction(actionId),
30
+ let newCounter = updatedState.remoteVariables?["counter"] {
31
+ for activity in Activity<DynamicActivityAttributes>.activities {
32
+ let oldJson = activity.content.state.payloadJson
33
+ let updatedJson = DynamicWidgetStateStore.applyCounterReplacement(in: oldJson, counterValue: newCounter)
34
+ if updatedJson != oldJson {
35
+ let nextContentState = DynamicActivityAttributes.ContentState(payloadJson: updatedJson)
36
+ await activity.update(
37
+ ActivityContent(
38
+ state: nextContentState,
39
+ staleDate: activity.content.staleDate,
40
+ relevanceScore: activity.content.relevanceScore
41
+ )
42
+ )
43
+ }
44
+ }
45
+ }
28
46
  }
29
47
 
30
48
  // Thông báo cho Main App nếu đang chạy ngầm hoặc nền
@@ -32,6 +50,43 @@ public struct DynamicWidgetActionIntent: AppIntent {
32
50
  CFNotificationCenterPostNotification(center, CFNotificationName("rn_live_widgets_action_clicked" as CFString), nil, nil, true)
33
51
 
34
52
  WidgetCenter.shared.reloadAllTimelines()
53
+ if #available(iOS 18.0, *) {
54
+ ControlCenter.shared.reloadAllControls()
55
+ }
56
+ return .result()
57
+ }
58
+ }
59
+
60
+ @available(iOS 18.0, *)
61
+ public struct DynamicControlToggleIntent: SetValueIntent {
62
+ public static var title: LocalizedStringResource = "Toggle Control Widget"
63
+ public static var description = IntentDescription("Toggle interactive Control Center widget state.")
64
+
65
+ @Parameter(title: "Action ID")
66
+ public var actionId: String
67
+
68
+ @Parameter(title: "Value")
69
+ public var value: Bool
70
+
71
+ public init() {
72
+ self.actionId = ""
73
+ self.value = false
74
+ }
75
+
76
+ public init(actionId: String, value: Bool = false) {
77
+ self.actionId = actionId
78
+ self.value = value
79
+ }
80
+
81
+ public func perform() async throws -> some IntentResult {
82
+ let key = actionId.hasPrefix("toggle:") ? String(actionId.dropFirst(7)) : actionId
83
+ DynamicWidgetStateStore.setToggle(key, value: value)
84
+
85
+ let center = CFNotificationCenterGetDarwinNotifyCenter()
86
+ CFNotificationCenterPostNotification(center, CFNotificationName("rn_live_widgets_action_clicked" as CFString), nil, nil, true)
87
+
88
+ WidgetCenter.shared.reloadAllTimelines()
89
+ ControlCenter.shared.reloadAllControls()
35
90
  return .result()
36
91
  }
37
92
  }
@@ -3,11 +3,13 @@ import Foundation
3
3
  // MARK: - Declarative Layout Models (Shared between Live Activity & Home Screen Widgets)
4
4
 
5
5
  public struct LayoutNodeDTO: Codable {
6
+ public let id: String?
6
7
  public let type: String
7
8
  public let props: [String: AnyCodable]?
8
9
  public let children: [LayoutNodeDTO]?
9
10
 
10
- public init(type: String, props: [String: AnyCodable]? = nil, children: [LayoutNodeDTO]? = nil) {
11
+ public init(id: String? = nil, type: String, props: [String: AnyCodable]? = nil, children: [LayoutNodeDTO]? = nil) {
12
+ self.id = id
11
13
  self.type = type
12
14
  self.props = props
13
15
  self.children = children
@@ -19,17 +21,47 @@ public struct DynamicPayloadDTO: Codable {
19
21
  public let lockScreen: LayoutNodeDTO?
20
22
  public let dynamicIsland: DynamicIslandDTO?
21
23
  public let staleTimestamp: Double?
24
+ public let staleLockScreen: LayoutNodeDTO?
25
+ public let staleDynamicIsland: DynamicIslandDTO?
26
+ public let expiredLockScreen: LayoutNodeDTO?
27
+ public let relevanceScore: Double?
28
+ public let scheduledEndTimestamp: Double?
29
+ public let autoEndAfterSeconds: Double?
30
+ public let expiredDismissalPolicy: String?
31
+ public let alertTitle: String?
32
+ public let alertBody: String?
33
+ public let alertSound: String?
22
34
 
23
35
  public init(
24
36
  activityId: String? = nil,
25
37
  lockScreen: LayoutNodeDTO? = nil,
26
38
  dynamicIsland: DynamicIslandDTO? = nil,
27
- staleTimestamp: Double? = nil
39
+ staleTimestamp: Double? = nil,
40
+ staleLockScreen: LayoutNodeDTO? = nil,
41
+ staleDynamicIsland: DynamicIslandDTO? = nil,
42
+ expiredLockScreen: LayoutNodeDTO? = nil,
43
+ relevanceScore: Double? = nil,
44
+ scheduledEndTimestamp: Double? = nil,
45
+ autoEndAfterSeconds: Double? = nil,
46
+ expiredDismissalPolicy: String? = nil,
47
+ alertTitle: String? = nil,
48
+ alertBody: String? = nil,
49
+ alertSound: String? = nil
28
50
  ) {
29
51
  self.activityId = activityId
30
52
  self.lockScreen = lockScreen
31
53
  self.dynamicIsland = dynamicIsland
32
54
  self.staleTimestamp = staleTimestamp
55
+ self.staleLockScreen = staleLockScreen
56
+ self.staleDynamicIsland = staleDynamicIsland
57
+ self.expiredLockScreen = expiredLockScreen
58
+ self.relevanceScore = relevanceScore
59
+ self.scheduledEndTimestamp = scheduledEndTimestamp
60
+ self.autoEndAfterSeconds = autoEndAfterSeconds
61
+ self.expiredDismissalPolicy = expiredDismissalPolicy
62
+ self.alertTitle = alertTitle
63
+ self.alertBody = alertBody
64
+ self.alertSound = alertSound
33
65
  }
34
66
  }
35
67
 
@@ -77,15 +109,22 @@ public struct WidgetSinglePageDTO: Codable {
77
109
  public let small: LayoutNodeDTO?
78
110
  public let medium: LayoutNodeDTO?
79
111
  public let large: LayoutNodeDTO?
112
+ public let extraLarge: LayoutNodeDTO?
113
+ public let standBy: LayoutNodeDTO?
80
114
  public let accessoryRectangular: LayoutNodeDTO?
81
115
  public let accessoryCircular: LayoutNodeDTO?
82
116
  public let accessoryInline: LayoutNodeDTO?
83
117
  }
84
118
 
85
119
  public struct WidgetMultiPagePayloadDTO: Codable {
120
+ public let root: LayoutNodeDTO?
121
+ public let version: String?
122
+ public let semanticSummary: String?
86
123
  public let small: LayoutNodeDTO?
87
124
  public let medium: LayoutNodeDTO?
88
125
  public let large: LayoutNodeDTO?
126
+ public let extraLarge: LayoutNodeDTO?
127
+ public let standBy: LayoutNodeDTO?
89
128
  public let accessoryRectangular: LayoutNodeDTO?
90
129
  public let accessoryCircular: LayoutNodeDTO?
91
130
  public let accessoryInline: LayoutNodeDTO?
@@ -96,6 +135,19 @@ public struct WidgetMultiPagePayloadDTO: Codable {
96
135
  public let containerBackground: WidgetContainerBackgroundDTO?
97
136
  }
98
137
 
138
+ public struct ControlWidgetConfigDTO: Codable {
139
+ public let type: String?
140
+ public let title: String
141
+ public let subtitle: String?
142
+ public let checkedTitle: String?
143
+ public let checkedSubtitle: String?
144
+ public let systemName: String?
145
+ public let tintColor: String?
146
+ public let checked: Bool?
147
+ public let actionId: String
148
+ public let deepLink: String?
149
+ }
150
+
99
151
  public struct WidgetContainerBackgroundDTO: Codable {
100
152
  public let backgroundColor: AnyCodable?
101
153
  public let gradientColors: [String]?
@@ -114,10 +166,46 @@ public struct WidgetTimelineConfigDTO: Codable {
114
166
  public let afterDate: Double?
115
167
  }
116
168
 
117
- public struct WidgetRemoteConfigDTO: Codable {
169
+ public struct WidgetDataSchemaDTO: Codable {
170
+ public let requiredKeys: [String]?
171
+ public let types: [String: String]?
172
+ public let defaults: [String: AnyCodable]?
173
+ }
174
+
175
+ public struct WidgetAuthRefreshConfigDTO: Codable {
176
+ public let url: String
177
+ public let method: String?
178
+ public let headers: [String: String]?
179
+ public let body: AnyCodable?
180
+ public let tokenPath: String?
181
+ public let headerName: String?
182
+ public let headerPrefix: String?
183
+ }
184
+
185
+ public struct WidgetRemoteSourceConfigDTO: Codable {
118
186
  public let url: String
119
187
  public let method: String?
120
188
  public let headers: [String: String]?
189
+ public let body: AnyCodable?
190
+ public let timeoutMs: Int?
191
+ public let enableETag: Bool?
192
+ }
193
+
194
+ public struct WidgetRemoteConfigDTO: Codable {
195
+ public let url: String?
196
+ public let method: String?
197
+ public let headers: [String: String]?
198
+ public let body: AnyCodable?
199
+ public let timeoutMs: Int?
200
+ public let cacheTtlSeconds: Int?
201
+ public let enableETag: Bool?
202
+ public let maxRetries: Int?
203
+ public let retryDelayMs: Int?
204
+ public let sources: [String: WidgetRemoteSourceConfigDTO]?
205
+ public let mapping: [String: String]?
206
+ public let schema: WidgetDataSchemaDTO?
207
+ public let authRefresh: WidgetAuthRefreshConfigDTO?
208
+ public let errorFallback: LayoutNodeDTO?
121
209
  public let refreshIntervalMinutes: Int?
122
210
  public let intervalMinutes: Int?
123
211
 
@@ -131,26 +219,47 @@ public struct InteractiveWidgetState: Codable {
131
219
  public var pageOrder: [String]?
132
220
  public var toggledItems: [String: Bool]
133
221
  public var lastActionId: String?
222
+ public var actionUpdatedAt: Double?
134
223
  public var lastSyncTime: String?
135
224
  public var remoteVariables: [String: String]?
136
225
  public var updatedAt: Double
226
+ public var etags: [String: String]?
227
+ public var lastModified: [String: String]?
228
+ public var syncStatus: String?
229
+ public var lastSyncError: String?
230
+ public var lastHttpStatus: Int?
231
+ public var accessToken: String?
137
232
 
138
233
  public init(
139
234
  activePage: String? = nil,
140
235
  pageOrder: [String]? = nil,
141
236
  toggledItems: [String: Bool] = [:],
142
237
  lastActionId: String? = nil,
238
+ actionUpdatedAt: Double? = nil,
143
239
  lastSyncTime: String? = nil,
144
240
  remoteVariables: [String: String]? = nil,
145
- updatedAt: Double = Date().timeIntervalSince1970
241
+ updatedAt: Double = Date().timeIntervalSince1970,
242
+ etags: [String: String]? = nil,
243
+ lastModified: [String: String]? = nil,
244
+ syncStatus: String? = nil,
245
+ lastSyncError: String? = nil,
246
+ lastHttpStatus: Int? = nil,
247
+ accessToken: String? = nil
146
248
  ) {
147
249
  self.activePage = activePage
148
250
  self.pageOrder = pageOrder
149
251
  self.toggledItems = toggledItems
150
252
  self.lastActionId = lastActionId
253
+ self.actionUpdatedAt = actionUpdatedAt
151
254
  self.lastSyncTime = lastSyncTime
152
255
  self.remoteVariables = remoteVariables
153
256
  self.updatedAt = updatedAt
257
+ self.etags = etags
258
+ self.lastModified = lastModified
259
+ self.syncStatus = syncStatus
260
+ self.lastSyncError = lastSyncError
261
+ self.lastHttpStatus = lastHttpStatus
262
+ self.accessToken = accessToken
154
263
  }
155
264
  }
156
265
 
@@ -223,7 +332,13 @@ public struct AnyCodable: Codable {
223
332
  }
224
333
 
225
334
  public var stringValue: String? { value as? String }
226
- public var doubleValue: Double? { value as? Double }
335
+ public var doubleValue: Double? {
336
+ if let d = value as? Double { return d }
337
+ if let i = value as? Int { return Double(i) }
338
+ if let n = value as? NSNumber { return n.doubleValue }
339
+ if let s = value as? String { return Double(s.trimmingCharacters(in: .whitespaces)) }
340
+ return nil
341
+ }
227
342
  public var boolValue: Bool? { value as? Bool }
228
343
  public var arrayValue: [Any]? { value as? [Any] }
229
344
  public var doubleArrayValue: [Double]? {
@@ -19,6 +19,7 @@ public struct DynamicNodeView: View {
19
19
  node.props?["privacySensitive"]?.boolValue == true,
20
20
  isRedacted: node.props?["redacted"]?.boolValue == true
21
21
  )
22
+ .modifier(DynamicNodeAccessibilityModifier(props: node.props))
22
23
  }
23
24
 
24
25
  @ViewBuilder
@@ -107,17 +108,53 @@ public struct DynamicNodeView: View {
107
108
  let fontWeightStr = node.props?["fontWeight"]?.stringValue ?? "regular"
108
109
  let numberOfLines = node.props?["numberOfLines"]?.doubleValue.map { Int($0) }
109
110
  let numericAnimation = node.props?["numericAnimation"]?.boolValue ?? false
111
+ let fontDesignStr = node.props?["fontDesign"]?.stringValue ?? "default"
112
+ let design: Font.Design = fontDesignStr == "monospaced" ? .monospaced : (fontDesignStr == "rounded" ? .rounded : (fontDesignStr == "serif" ? .serif : .default))
113
+
114
+ let font: Font = {
115
+ if let typo = node.props?["typography"]?.stringValue {
116
+ switch typo {
117
+ case "largeTitle": return .system(.largeTitle, design: design)
118
+ case "title1", "title": return .system(.title, design: design)
119
+ case "title2": return .system(.title2, design: design)
120
+ case "title3": return .system(.title3, design: design)
121
+ case "headline": return .system(.headline, design: design)
122
+ case "subheadline": return .system(.subheadline, design: design)
123
+ case "body": return .system(.body, design: design)
124
+ case "callout": return .system(.callout, design: design)
125
+ case "footnote": return .system(.footnote, design: design)
126
+ case "caption1", "caption": return .system(.caption, design: design)
127
+ case "caption2": return .system(.caption2, design: design)
128
+ default: return .system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr), design: design)
129
+ }
130
+ } else {
131
+ return .system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr), design: design)
132
+ }
133
+ }()
110
134
 
111
135
  let textNode = Text(text)
112
- .font(.system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr)))
136
+ .font(font)
113
137
  .foregroundColor(color)
114
138
  .lineLimit(numberOfLines)
115
139
  .minimumScaleFactor(0.85)
116
140
 
141
+ let shadowColor = Color.resolve(from: node.props?["shadowColor"], colorScheme: colorScheme)
142
+ let shadowRad = node.props?["shadowRadius"]?.doubleValue.map { CGFloat($0) } ?? (shadowColor != nil ? 2 : 0)
143
+ let shadowOffsetDict = node.props?["shadowOffset"]?.value as? [String: Any]
144
+ let shadowX = (shadowOffsetDict?["x"] as? Double).map { CGFloat($0) } ?? 0
145
+ let shadowY = (shadowOffsetDict?["y"] as? Double).map { CGFloat($0) } ?? 1
146
+
147
+ let isAccentable = node.props?["widgetAccentable"]?.boolValue == true
148
+
117
149
  if #available(iOS 16.0, *), numericAnimation {
118
- textNode.contentTransition(.numericText())
150
+ textNode
151
+ .contentTransition(.numericText())
152
+ .shadow(color: shadowColor ?? .clear, radius: shadowRad, x: shadowX, y: shadowY)
153
+ .applyWidgetAccentable(isAccentable)
119
154
  } else {
120
155
  textNode
156
+ .shadow(color: shadowColor ?? .clear, radius: shadowRad, x: shadowX, y: shadowY)
157
+ .applyWidgetAccentable(isAccentable)
121
158
  }
122
159
  }
123
160
 
@@ -161,14 +198,18 @@ public struct DynamicNodeView: View {
161
198
  @ViewBuilder
162
199
  private func renderProgress() -> some View {
163
200
  let style = node.props?["style"]?.stringValue ?? "bar"
201
+ let isAccentable = node.props?["widgetAccentable"]?.boolValue == true
164
202
 
165
- if style == "segmented" {
166
- renderSegmentedProgress()
167
- } else if style == "circular" {
168
- renderCircularProgress()
169
- } else {
170
- renderBarProgress()
203
+ Group {
204
+ if style == "segmented" {
205
+ renderSegmentedProgress()
206
+ } else if style == "circular" {
207
+ renderCircularProgress()
208
+ } else {
209
+ renderBarProgress()
210
+ }
171
211
  }
212
+ .applyWidgetAccentable(isAccentable)
172
213
  }
173
214
 
174
215
  @ViewBuilder
@@ -595,6 +636,7 @@ public struct DynamicNodeView: View {
595
636
  .offset(y: size * 0.04)
596
637
  }
597
638
  .frame(width: size, height: size)
639
+ .applyWidgetAccentable(node.props?["widgetAccentable"]?.boolValue == true)
598
640
  }
599
641
 
600
642
  @ViewBuilder
@@ -878,7 +920,56 @@ public struct DynamicNodeView: View {
878
920
  }
879
921
  }
880
922
 
881
- // Container padding, background modifier
923
+ // ♿ Accessibility Semantics & Fallback Modifier (Phase 1)
924
+ struct DynamicNodeAccessibilityModifier: ViewModifier {
925
+ let props: [String: AnyCodable]?
926
+
927
+ func body(content: Content) -> some View {
928
+ buildAccessibilityView(content: content)
929
+ }
930
+
931
+ private func buildAccessibilityView(content: Content) -> AnyView {
932
+ let accessible = props?["accessible"]?.boolValue
933
+ let label = props?["accessibilityLabel"]?.stringValue
934
+ let hint = props?["accessibilityHint"]?.stringValue
935
+ let value = props?["accessibilityValue"]?.stringValue
936
+ let role = props?["accessibilityRole"]?.stringValue
937
+
938
+ if accessible == false {
939
+ return AnyView(content.accessibilityHidden(true))
940
+ }
941
+
942
+ if label == nil && hint == nil && value == nil && role == nil {
943
+ return AnyView(content)
944
+ }
945
+
946
+ var view = AnyView(content)
947
+ if let label = label {
948
+ view = AnyView(view.accessibilityLabel(Text(label)))
949
+ }
950
+ if let hint = hint {
951
+ view = AnyView(view.accessibilityHint(Text(hint)))
952
+ }
953
+ if let value = value {
954
+ view = AnyView(view.accessibilityValue(Text(value)))
955
+ }
956
+ if let role = role {
957
+ switch role {
958
+ case "header":
959
+ view = AnyView(view.accessibilityAddTraits(.isHeader))
960
+ case "button":
961
+ view = AnyView(view.accessibilityAddTraits(.isButton))
962
+ case "image":
963
+ view = AnyView(view.accessibilityAddTraits(.isImage))
964
+ default:
965
+ break
966
+ }
967
+ }
968
+ return view
969
+ }
970
+ }
971
+
972
+ // Container padding, background, gradient, border and shadow modifier
882
973
  struct ContainerStyleModifier: ViewModifier {
883
974
  @Environment(\.widgetFamily) var family
884
975
  @Environment(\.colorScheme) var colorScheme
@@ -891,22 +982,95 @@ struct ContainerStyleModifier: ViewModifier {
891
982
  return false
892
983
  }
893
984
 
985
+ @ViewBuilder
986
+ private func backgroundView(cornerRadius: CGFloat) -> some View {
987
+ if isAccessory {
988
+ EmptyView()
989
+ } else if let gradDict = props?["gradient"]?.value as? [String: Any],
990
+ let colorList = gradDict["colors"] as? [Any] {
991
+ let colors: [Color] = colorList.compactMap {
992
+ if let s = $0 as? String { return Color(hex: s) }
993
+ if let d = $0 as? [String: Any] {
994
+ let key = (colorScheme == .dark) ? "dark" : "light"
995
+ let hex = (d[key] as? String) ?? (d["light"] as? String) ?? (d["dark"] as? String) ?? (d["tinted"] as? String)
996
+ return hex.flatMap { Color(hex: $0) }
997
+ }
998
+ return nil
999
+ }
1000
+ if colors.count >= 2 {
1001
+ let startX = (gradDict["startPoint"] as? [String: Any])?["x"] as? Double ?? 0.0
1002
+ let startY = (gradDict["startPoint"] as? [String: Any])?["y"] as? Double ?? 0.0
1003
+ let endX = (gradDict["endPoint"] as? [String: Any])?["x"] as? Double ?? 1.0
1004
+ let endY = (gradDict["endPoint"] as? [String: Any])?["y"] as? Double ?? 1.0
1005
+ RoundedRectangle(cornerRadius: cornerRadius)
1006
+ .fill(LinearGradient(
1007
+ colors: colors,
1008
+ startPoint: UnitPoint(x: startX, y: startY),
1009
+ endPoint: UnitPoint(x: endX, y: endY)
1010
+ ))
1011
+ } else if let firstColor = colors.first {
1012
+ RoundedRectangle(cornerRadius: cornerRadius).fill(firstColor)
1013
+ } else if let color = Color.resolve(from: props?["backgroundColor"], colorScheme: colorScheme) {
1014
+ RoundedRectangle(cornerRadius: cornerRadius).fill(color)
1015
+ } else {
1016
+ EmptyView()
1017
+ }
1018
+ } else if let color = Color.resolve(from: props?["backgroundColor"], colorScheme: colorScheme) {
1019
+ RoundedRectangle(cornerRadius: cornerRadius).fill(color)
1020
+ } else {
1021
+ EmptyView()
1022
+ }
1023
+ }
1024
+
1025
+ @ViewBuilder
1026
+ private func borderOverlay(cornerRadius: CGFloat) -> some View {
1027
+ let borderWidth = props?["borderWidth"]?.doubleValue.map { CGFloat($0) } ?? 0
1028
+ if borderWidth > 0, let borderColor = Color.resolve(from: props?["borderColor"], colorScheme: colorScheme) {
1029
+ let borderStyle = props?["borderStyle"]?.stringValue ?? "solid"
1030
+ let strokeStyle: StrokeStyle = {
1031
+ if borderStyle == "dashed" {
1032
+ return StrokeStyle(lineWidth: borderWidth, dash: [6, 4])
1033
+ } else if borderStyle == "dotted" {
1034
+ return StrokeStyle(lineWidth: borderWidth, dash: [2, 3])
1035
+ } else {
1036
+ return StrokeStyle(lineWidth: borderWidth)
1037
+ }
1038
+ }()
1039
+ RoundedRectangle(cornerRadius: cornerRadius)
1040
+ .strokeBorder(borderColor, style: strokeStyle)
1041
+ } else {
1042
+ EmptyView()
1043
+ }
1044
+ }
1045
+
894
1046
  func body(content: Content) -> some View {
895
1047
  let rawPad = props?["padding"]?.doubleValue.map { CGFloat($0) } ?? 0
896
1048
  let pad = isAccessory ? min(rawPad, 2) : rawPad
897
- // Trên Lock Screen widgets, nền luôn trong suốt để hiển thị tự nhiên trên hình nền điện thoại
898
- let bgColor = isAccessory ? nil : Color.resolve(from: props?["backgroundColor"], colorScheme: colorScheme)
899
- let cornerRadius = props?["borderRadius"]?.doubleValue.map { CGFloat($0) } ?? 0
1049
+ let cornerRadius = props?["borderRadius"]?.doubleValue.map { CGFloat($0) } ?? (props?["cornerRadius"]?.doubleValue.map { CGFloat($0) } ?? 0)
1050
+
1051
+ let isRTL = (props?["isRTL"]?.boolValue == true) || (props?["direction"]?.stringValue == "rtl")
1052
+ let shadowColor = Color.resolve(from: props?["shadowColor"], colorScheme: colorScheme)
1053
+ let shadowRadius = props?["shadowRadius"]?.doubleValue.map { CGFloat($0) } ?? (shadowColor != nil ? 4 : 0)
1054
+ let shadowOpacity = props?["shadowOpacity"]?.doubleValue ?? 0.35
1055
+ let offsetDict = props?["shadowOffset"]?.value as? [String: Any]
1056
+ let shadowX = (offsetDict?["x"] as? Double).map { CGFloat($0) } ?? 0
1057
+ let shadowY = (offsetDict?["y"] as? Double).map { CGFloat($0) } ?? 2
900
1058
 
901
- content
1059
+ let styledContent = content
902
1060
  .padding(pad)
903
- .background(
904
- Group {
905
- if let color = bgColor {
906
- RoundedRectangle(cornerRadius: cornerRadius).fill(color)
907
- }
908
- }
909
- )
1061
+ .background(backgroundView(cornerRadius: cornerRadius))
1062
+ .overlay(borderOverlay(cornerRadius: cornerRadius))
1063
+
1064
+ let withShadow = Group {
1065
+ if let sColor = shadowColor, shadowRadius > 0 {
1066
+ styledContent.shadow(color: sColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
1067
+ } else {
1068
+ styledContent
1069
+ }
1070
+ }
1071
+
1072
+ return withShadow
1073
+ .environment(\.layoutDirection, isRTL ? .rightToLeft : .leftToRight)
910
1074
  }
911
1075
  }
912
1076