react-native-live-widgets 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +221 -2
- package/android/build.gradle +49 -0
- package/android/src/main/AndroidManifest.xml +38 -0
- package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +440 -0
- package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityPackage.kt +17 -0
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityHeadlessTaskService.kt +24 -0
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +410 -0
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +67 -0
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicNodeRegistry.kt +64 -0
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +1223 -0
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +120 -0
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +281 -0
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +229 -0
- package/android/src/main/res/layout/rn_live_widget_host.xml +113 -0
- package/android/src/main/res/xml/rn_live_widget_info.xml +14 -0
- package/assets/dashboard_features.png +0 -0
- package/assets/dynamic_island_expanded.png +0 -0
- package/assets/home_widget_interactive.png +0 -0
- package/assets/lock_screen_live_activity.png +0 -0
- package/cli/setup.js +94 -0
- package/docs/GUIDE-EN.md +804 -0
- package/docs/GUIDE-VI.md +802 -0
- package/docs/widgets.md +52 -0
- package/ios/Bridge/LiveActivityModule.m +105 -0
- package/ios/Bridge/LiveActivityModule.swift +957 -0
- package/ios/Intents/DynamicWidgetActionIntent.swift +38 -0
- package/ios/Models/DynamicActivityAttributes.swift +22 -0
- package/ios/Models/DynamicLayoutModels.swift +243 -0
- package/ios/Renderer/ColorHexExtension.swift +49 -0
- package/ios/Renderer/DynamicNodeRegistry.swift +54 -0
- package/ios/Renderer/DynamicNodeView.swift +1060 -0
- package/ios/Storage/DynamicWidgetImageCache.swift +137 -0
- package/ios/Storage/DynamicWidgetStateStore.swift +162 -0
- package/ios/Widgets/DynamicHomeScreenWidget.swift +435 -0
- package/ios/Widgets/DynamicLiveActivityWidget.swift +79 -0
- package/lib/builder.d.ts +60 -0
- package/lib/builder.d.ts.map +1 -0
- package/lib/builder.js +296 -0
- package/lib/builder.js.map +1 -0
- package/lib/dispatcher.d.ts +16 -0
- package/lib/dispatcher.d.ts.map +1 -0
- package/lib/dispatcher.js +23 -0
- package/lib/dispatcher.js.map +1 -0
- package/lib/index.d.ts +199 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +634 -0
- package/lib/index.js.map +1 -0
- package/lib/types.d.ts +489 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +10 -0
- package/lib/types.js.map +1 -0
- package/package.json +68 -4
- package/react-native-live-widgets.podspec +38 -0
- package/src/builder.ts +409 -0
- package/src/dispatcher.ts +25 -0
- package/src/index.ts +681 -0
- package/src/types.ts +595 -0
|
@@ -0,0 +1,1060 @@
|
|
|
1
|
+
import AppIntents
|
|
2
|
+
import SwiftUI
|
|
3
|
+
import UIKit
|
|
4
|
+
import WidgetKit
|
|
5
|
+
|
|
6
|
+
// MARK: - SwiftUI Recursive Dynamic Node View
|
|
7
|
+
|
|
8
|
+
public struct DynamicNodeView: View {
|
|
9
|
+
@Environment(\.colorScheme) var colorScheme
|
|
10
|
+
public let node: LayoutNodeDTO
|
|
11
|
+
|
|
12
|
+
public init(node: LayoutNodeDTO) {
|
|
13
|
+
self.node = node
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public var body: some View {
|
|
17
|
+
rawContent
|
|
18
|
+
.applyPrivacySensitive(
|
|
19
|
+
node.props?["privacySensitive"]?.boolValue == true,
|
|
20
|
+
isRedacted: node.props?["redacted"]?.boolValue == true
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@ViewBuilder
|
|
25
|
+
private var rawContent: some View {
|
|
26
|
+
switch node.type {
|
|
27
|
+
case "VStack":
|
|
28
|
+
renderVStack()
|
|
29
|
+
case "HStack":
|
|
30
|
+
renderHStack()
|
|
31
|
+
case "ZStack":
|
|
32
|
+
renderZStack()
|
|
33
|
+
case "Grid":
|
|
34
|
+
renderGrid()
|
|
35
|
+
case "Divider":
|
|
36
|
+
renderDivider()
|
|
37
|
+
case "Text":
|
|
38
|
+
renderText()
|
|
39
|
+
case "Timer":
|
|
40
|
+
renderTimer()
|
|
41
|
+
case "Progress":
|
|
42
|
+
renderProgress()
|
|
43
|
+
case "Gauge":
|
|
44
|
+
renderGauge()
|
|
45
|
+
case "Chart":
|
|
46
|
+
renderChart()
|
|
47
|
+
case "Image":
|
|
48
|
+
renderImage()
|
|
49
|
+
case "Button":
|
|
50
|
+
renderButton()
|
|
51
|
+
case "Toggle":
|
|
52
|
+
renderToggle()
|
|
53
|
+
case "Badge":
|
|
54
|
+
renderBadge()
|
|
55
|
+
case "Link":
|
|
56
|
+
renderLink()
|
|
57
|
+
case "Equalizer":
|
|
58
|
+
renderEqualizer()
|
|
59
|
+
case "Spacer":
|
|
60
|
+
if let minLength = node.props?["minLength"]?.doubleValue {
|
|
61
|
+
Spacer(minLength: CGFloat(minLength))
|
|
62
|
+
} else {
|
|
63
|
+
Spacer()
|
|
64
|
+
}
|
|
65
|
+
case "Custom":
|
|
66
|
+
renderCustom()
|
|
67
|
+
default:
|
|
68
|
+
if let customView = DynamicNodeRegistry.shared.resolve(node) {
|
|
69
|
+
customView
|
|
70
|
+
} else {
|
|
71
|
+
EmptyView()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@ViewBuilder
|
|
77
|
+
private func renderVStack() -> some View {
|
|
78
|
+
let spacing = node.props?["spacing"]?.doubleValue.map { CGFloat($0) }
|
|
79
|
+
VStack(alignment: .leading, spacing: spacing) {
|
|
80
|
+
if let children = node.children {
|
|
81
|
+
ForEach(0..<children.count, id: \.self) { index in
|
|
82
|
+
DynamicNodeView(node: children[index])
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@ViewBuilder
|
|
90
|
+
private func renderHStack() -> some View {
|
|
91
|
+
let spacing = node.props?["spacing"]?.doubleValue.map { CGFloat($0) }
|
|
92
|
+
HStack(alignment: .center, spacing: spacing) {
|
|
93
|
+
if let children = node.children {
|
|
94
|
+
ForEach(0..<children.count, id: \.self) { index in
|
|
95
|
+
DynamicNodeView(node: children[index])
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@ViewBuilder
|
|
103
|
+
private func renderText() -> some View {
|
|
104
|
+
let text = node.props?["text"]?.stringValue ?? ""
|
|
105
|
+
let color = Color.resolve(from: node.props?["color"], colorScheme: colorScheme) ?? .primary
|
|
106
|
+
let fontSize = node.props?["fontSize"]?.doubleValue ?? 14
|
|
107
|
+
let fontWeightStr = node.props?["fontWeight"]?.stringValue ?? "regular"
|
|
108
|
+
let numberOfLines = node.props?["numberOfLines"]?.doubleValue.map { Int($0) }
|
|
109
|
+
let numericAnimation = node.props?["numericAnimation"]?.boolValue ?? false
|
|
110
|
+
|
|
111
|
+
let textNode = Text(text)
|
|
112
|
+
.font(.system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr)))
|
|
113
|
+
.foregroundColor(color)
|
|
114
|
+
.lineLimit(numberOfLines)
|
|
115
|
+
.minimumScaleFactor(0.85)
|
|
116
|
+
|
|
117
|
+
if #available(iOS 16.0, *), numericAnimation {
|
|
118
|
+
textNode.contentTransition(.numericText())
|
|
119
|
+
} else {
|
|
120
|
+
textNode
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@ViewBuilder
|
|
125
|
+
private func renderTimer() -> some View {
|
|
126
|
+
let targetTimestamp = node.props?["targetTimestamp"]?.doubleValue ?? 0
|
|
127
|
+
let targetDate = Date(timeIntervalSince1970: targetTimestamp)
|
|
128
|
+
let color = Color.resolve(from: node.props?["color"], colorScheme: colorScheme) ?? .orange
|
|
129
|
+
let fontSize = node.props?["fontSize"]?.doubleValue ?? 14
|
|
130
|
+
let fontWeightStr = node.props?["fontWeight"]?.stringValue ?? "semibold"
|
|
131
|
+
let mode = node.props?["mode"]?.stringValue ?? "countdown"
|
|
132
|
+
let isCountdown = mode != "countup"
|
|
133
|
+
|
|
134
|
+
// Giới hạn frame width cho Text(timerInterval:) để không bị giãn tràn toàn bộ Dynamic Island
|
|
135
|
+
let remaining = abs(targetTimestamp - Date().timeIntervalSince1970)
|
|
136
|
+
let showsHours = remaining >= 3600
|
|
137
|
+
let compactWidth = CGFloat(fontSize) * (showsHours ? 4.4 : 3.2)
|
|
138
|
+
|
|
139
|
+
if #available(iOS 16.0, *) {
|
|
140
|
+
Text(
|
|
141
|
+
timerInterval: min(Date(), targetDate)...max(Date(), targetDate),
|
|
142
|
+
pauseTime: nil,
|
|
143
|
+
countsDown: isCountdown,
|
|
144
|
+
showsHours: showsHours
|
|
145
|
+
)
|
|
146
|
+
.font(.system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr)))
|
|
147
|
+
.foregroundColor(color)
|
|
148
|
+
.monospacedDigit()
|
|
149
|
+
.multilineTextAlignment(.trailing)
|
|
150
|
+
.frame(width: compactWidth, alignment: .trailing)
|
|
151
|
+
.minimumScaleFactor(0.8)
|
|
152
|
+
} else {
|
|
153
|
+
Text(targetDate, style: .timer)
|
|
154
|
+
.font(.system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr)))
|
|
155
|
+
.foregroundColor(color)
|
|
156
|
+
.multilineTextAlignment(.trailing)
|
|
157
|
+
.frame(width: compactWidth, alignment: .trailing)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@ViewBuilder
|
|
162
|
+
private func renderProgress() -> some View {
|
|
163
|
+
let style = node.props?["style"]?.stringValue ?? "bar"
|
|
164
|
+
|
|
165
|
+
if style == "segmented" {
|
|
166
|
+
renderSegmentedProgress()
|
|
167
|
+
} else if style == "circular" {
|
|
168
|
+
renderCircularProgress()
|
|
169
|
+
} else {
|
|
170
|
+
renderBarProgress()
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
@ViewBuilder
|
|
175
|
+
private func renderBarProgress() -> some View {
|
|
176
|
+
let countsDown = node.props?["countsDown"]?.boolValue ?? false
|
|
177
|
+
let height = node.props?["height"]?.doubleValue.map { CGFloat($0) } ?? 6
|
|
178
|
+
let cornerRadius = node.props?["borderRadius"]?.doubleValue.map { CGFloat($0) } ?? (height / 2)
|
|
179
|
+
let tint = resolveProgressTintColor()
|
|
180
|
+
|
|
181
|
+
VStack(alignment: .leading, spacing: 4) {
|
|
182
|
+
if let startTimestamp = node.props?["startTimestamp"]?.doubleValue,
|
|
183
|
+
let targetTimestamp = node.props?["targetTimestamp"]?.doubleValue,
|
|
184
|
+
targetTimestamp > startTimestamp {
|
|
185
|
+
let startDate = Date(timeIntervalSince1970: startTimestamp)
|
|
186
|
+
let targetDate = Date(timeIntervalSince1970: targetTimestamp)
|
|
187
|
+
|
|
188
|
+
if #available(iOS 16.0, *) {
|
|
189
|
+
ProgressView(timerInterval: startDate...targetDate, countsDown: countsDown)
|
|
190
|
+
.tint(tint)
|
|
191
|
+
.frame(height: height)
|
|
192
|
+
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
|
|
193
|
+
} else {
|
|
194
|
+
let elapsed = max(0.0, min(1.0, (Date().timeIntervalSince1970 - startTimestamp) / (targetTimestamp - startTimestamp)))
|
|
195
|
+
let progressVal = countsDown ? (1.0 - elapsed) : elapsed
|
|
196
|
+
ProgressView(value: progressVal)
|
|
197
|
+
.tint(tint)
|
|
198
|
+
.frame(height: height)
|
|
199
|
+
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
let value = node.props?["value"]?.doubleValue ?? 0.0
|
|
203
|
+
let clampedValue = max(0.0, min(1.0, value))
|
|
204
|
+
|
|
205
|
+
HStack {
|
|
206
|
+
ProgressView(value: clampedValue)
|
|
207
|
+
.tint(tint)
|
|
208
|
+
.frame(height: height)
|
|
209
|
+
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
|
|
210
|
+
|
|
211
|
+
if node.props?["showPercentage"]?.boolValue == true {
|
|
212
|
+
let pctColorHex = node.props?["percentageColor"]?.stringValue
|
|
213
|
+
Text("\(Int(clampedValue * 100))%")
|
|
214
|
+
.font(.system(size: 11, weight: .bold))
|
|
215
|
+
.foregroundColor(pctColorHex.flatMap { Color(hex: $0) } ?? .secondary)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
@ViewBuilder
|
|
223
|
+
private func renderSegmentedProgress() -> some View {
|
|
224
|
+
let segments = max(1, Int(node.props?["segments"]?.doubleValue ?? 4))
|
|
225
|
+
let value = max(0.0, min(1.0, node.props?["value"]?.doubleValue ?? 0.5))
|
|
226
|
+
let currentSegment: Int = {
|
|
227
|
+
if let explicit = node.props?["currentSegment"]?.doubleValue {
|
|
228
|
+
return Int(explicit)
|
|
229
|
+
}
|
|
230
|
+
return Int(round(value * Double(segments)))
|
|
231
|
+
}()
|
|
232
|
+
let height = node.props?["height"]?.doubleValue.map { CGFloat($0) } ?? 6
|
|
233
|
+
let spacing = node.props?["segmentSpacing"]?.doubleValue.map { CGFloat($0) } ?? 4
|
|
234
|
+
let tint = resolveProgressTintColor()
|
|
235
|
+
let trackHex = node.props?["trackColor"]?.stringValue
|
|
236
|
+
let trackColor = trackHex.flatMap { Color(hex: $0) } ?? Color.gray.opacity(0.3)
|
|
237
|
+
|
|
238
|
+
HStack(spacing: 6) {
|
|
239
|
+
HStack(spacing: spacing) {
|
|
240
|
+
ForEach(1...segments, id: \.self) { index in
|
|
241
|
+
RoundedRectangle(cornerRadius: height / 2)
|
|
242
|
+
.fill(index <= currentSegment ? tint : trackColor)
|
|
243
|
+
.frame(height: height)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if node.props?["showPercentage"]?.boolValue == true {
|
|
248
|
+
let pctColorHex = node.props?["percentageColor"]?.stringValue
|
|
249
|
+
Text("\(Int(round(Double(currentSegment) / Double(segments) * 100)))%")
|
|
250
|
+
.font(.system(size: 11, weight: .bold))
|
|
251
|
+
.foregroundColor(pctColorHex.flatMap { Color(hex: $0) } ?? tint)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@ViewBuilder
|
|
257
|
+
private func renderCircularProgress() -> some View {
|
|
258
|
+
let value = node.props?["value"]?.doubleValue ?? 0.0
|
|
259
|
+
let clampedValue = max(0.0, min(1.0, value))
|
|
260
|
+
let size = node.props?["height"]?.doubleValue.map { CGFloat($0) } ?? 44
|
|
261
|
+
let lineWidth = max(3, size * 0.11)
|
|
262
|
+
let tint = resolveProgressTintColor()
|
|
263
|
+
let trackHex = node.props?["trackColor"]?.stringValue
|
|
264
|
+
let trackColor = trackHex.flatMap { Color(hex: $0) } ?? Color.gray.opacity(0.25)
|
|
265
|
+
|
|
266
|
+
ZStack {
|
|
267
|
+
Circle()
|
|
268
|
+
.stroke(trackColor, lineWidth: lineWidth)
|
|
269
|
+
Circle()
|
|
270
|
+
.trim(from: 0.0, to: CGFloat(clampedValue))
|
|
271
|
+
.stroke(tint, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round))
|
|
272
|
+
.rotationEffect(.degrees(-90))
|
|
273
|
+
|
|
274
|
+
if node.props?["showPercentage"]?.boolValue == true {
|
|
275
|
+
let pctColorHex = node.props?["percentageColor"]?.stringValue
|
|
276
|
+
Text("\(Int(clampedValue * 100))%")
|
|
277
|
+
.font(.system(size: max(9, size * 0.25), weight: .bold))
|
|
278
|
+
.foregroundColor(pctColorHex.flatMap { Color(hex: $0) } ?? tint)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
.frame(width: size, height: size)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private func resolveProgressTintColor() -> Color {
|
|
285
|
+
let now = Date().timeIntervalSince1970
|
|
286
|
+
if let targetTimestamp = node.props?["targetTimestamp"]?.doubleValue,
|
|
287
|
+
let warningSecs = node.props?["warningSeconds"]?.doubleValue {
|
|
288
|
+
let secondsRemaining = targetTimestamp - now
|
|
289
|
+
if secondsRemaining <= warningSecs {
|
|
290
|
+
return Color.resolve(from: node.props?["warningTintColor"], colorScheme: colorScheme) ?? .orange
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return Color.resolve(from: node.props?["tintColor"], colorScheme: colorScheme) ?? .blue
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
@ViewBuilder
|
|
298
|
+
private func renderImage() -> some View {
|
|
299
|
+
let width = node.props?["width"]?.doubleValue.map { CGFloat($0) } ?? 28
|
|
300
|
+
let height = node.props?["height"]?.doubleValue.map { CGFloat($0) } ?? 28
|
|
301
|
+
let tintHex = node.props?["tintColor"]?.stringValue
|
|
302
|
+
let tint = tintHex.flatMap { Color(hex: $0) }
|
|
303
|
+
let bgHex = node.props?["backgroundColor"]?.stringValue
|
|
304
|
+
let bgColor = bgHex.flatMap { Color(hex: $0) }
|
|
305
|
+
let pad = node.props?["padding"]?.doubleValue.map { CGFloat($0) } ?? 0
|
|
306
|
+
let cornerRadius = node.props?["borderRadius"]?.doubleValue.map { CGFloat($0) } ?? 0
|
|
307
|
+
let borderWidth = node.props?["borderWidth"]?.doubleValue.map { CGFloat($0) } ?? 0
|
|
308
|
+
let borderColorHex = node.props?["borderColor"]?.stringValue
|
|
309
|
+
let borderColor = borderColorHex.flatMap { Color(hex: $0) } ?? .white
|
|
310
|
+
let contentModeStr = node.props?["contentMode"]?.stringValue ?? "fill"
|
|
311
|
+
let isFill = contentModeStr == "fill"
|
|
312
|
+
|
|
313
|
+
if let uiImage = resolveBitmapImage() {
|
|
314
|
+
Image(uiImage: uiImage)
|
|
315
|
+
.resizable()
|
|
316
|
+
.aspectRatio(contentMode: isFill ? .fill : .fit)
|
|
317
|
+
.frame(width: width, height: height)
|
|
318
|
+
.background(bgColor ?? Color.clear)
|
|
319
|
+
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
|
|
320
|
+
.overlay(
|
|
321
|
+
Group {
|
|
322
|
+
if borderWidth > 0 {
|
|
323
|
+
RoundedRectangle(cornerRadius: cornerRadius)
|
|
324
|
+
.stroke(borderColor, lineWidth: borderWidth)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
)
|
|
328
|
+
} else if let symbolName = node.props?["systemName"]?.stringValue
|
|
329
|
+
?? node.props?["fallbackSystemName"]?.stringValue {
|
|
330
|
+
Image(systemName: symbolName)
|
|
331
|
+
.resizable()
|
|
332
|
+
.aspectRatio(contentMode: .fit)
|
|
333
|
+
.padding(pad)
|
|
334
|
+
.frame(width: width, height: height)
|
|
335
|
+
.foregroundColor(tint ?? .primary)
|
|
336
|
+
.applySymbolEffect(node.props?["symbolEffect"]?.stringValue)
|
|
337
|
+
.applyWidgetAccentable(node.props?["widgetAccentable"]?.boolValue == true)
|
|
338
|
+
.background(
|
|
339
|
+
Group {
|
|
340
|
+
if let bgColor = bgColor {
|
|
341
|
+
RoundedRectangle(cornerRadius: cornerRadius).fill(bgColor)
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
)
|
|
345
|
+
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
|
|
346
|
+
.overlay(
|
|
347
|
+
Group {
|
|
348
|
+
if borderWidth > 0 {
|
|
349
|
+
RoundedRectangle(cornerRadius: cornerRadius)
|
|
350
|
+
.stroke(borderColor, lineWidth: borderWidth)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
)
|
|
354
|
+
} else {
|
|
355
|
+
RoundedRectangle(cornerRadius: cornerRadius)
|
|
356
|
+
.fill(bgColor ?? Color.gray.opacity(0.2))
|
|
357
|
+
.frame(width: width, height: height)
|
|
358
|
+
.overlay(
|
|
359
|
+
Image(systemName: "photo")
|
|
360
|
+
.font(.system(size: min(width, height) * 0.45))
|
|
361
|
+
.foregroundColor(tint ?? .secondary)
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private func resolveBitmapImage() -> UIImage? {
|
|
367
|
+
if let urlStr = node.props?["url"]?.stringValue,
|
|
368
|
+
let cached = DynamicWidgetImageCache.cachedImage(for: urlStr) {
|
|
369
|
+
return cached
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if let base64Str = node.props?["base64"]?.stringValue {
|
|
373
|
+
let cleanBase64: String = {
|
|
374
|
+
if let commaRange = base64Str.range(of: ",") {
|
|
375
|
+
return String(base64Str[commaRange.upperBound...])
|
|
376
|
+
}
|
|
377
|
+
return base64Str
|
|
378
|
+
}()
|
|
379
|
+
if let data = Data(base64Encoded: cleanBase64, options: .ignoreUnknownCharacters),
|
|
380
|
+
let img = UIImage(data: data) {
|
|
381
|
+
return img
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if let filePath = node.props?["filePath"]?.stringValue,
|
|
386
|
+
let img = UIImage(contentsOfFile: filePath) {
|
|
387
|
+
return img
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if let assetName = node.props?["assetName"]?.stringValue,
|
|
391
|
+
let img = UIImage(named: assetName) {
|
|
392
|
+
return img
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return nil
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
@ViewBuilder
|
|
399
|
+
private func renderButton() -> some View {
|
|
400
|
+
let rawTitle = node.props?["title"]?.stringValue ?? "Button"
|
|
401
|
+
let actionId = node.props?["actionId"]?.stringValue ?? ""
|
|
402
|
+
let deepLink = node.props?["deepLink"]?.stringValue
|
|
403
|
+
|
|
404
|
+
let isToggled: Bool = {
|
|
405
|
+
if actionId.hasPrefix("toggle:") {
|
|
406
|
+
let key = String(actionId.dropFirst(7))
|
|
407
|
+
return DynamicWidgetStateStore.isToggled(key)
|
|
408
|
+
}
|
|
409
|
+
return false
|
|
410
|
+
}()
|
|
411
|
+
|
|
412
|
+
let displayTitle: String = {
|
|
413
|
+
if isToggled, let checkedTitle = node.props?["checkedTitle"]?.stringValue {
|
|
414
|
+
return checkedTitle
|
|
415
|
+
}
|
|
416
|
+
return rawTitle
|
|
417
|
+
}()
|
|
418
|
+
|
|
419
|
+
let bgColor: Color = {
|
|
420
|
+
if isToggled, let checkedBg = Color.resolve(from: node.props?["checkedBackgroundColor"], colorScheme: colorScheme) {
|
|
421
|
+
return checkedBg
|
|
422
|
+
}
|
|
423
|
+
return Color.resolve(from: node.props?["backgroundColor"], colorScheme: colorScheme) ?? Color.blue
|
|
424
|
+
}()
|
|
425
|
+
|
|
426
|
+
let textColor: Color = {
|
|
427
|
+
if isToggled, let checkedText = Color.resolve(from: node.props?["checkedTextColor"], colorScheme: colorScheme) {
|
|
428
|
+
return checkedText
|
|
429
|
+
}
|
|
430
|
+
return Color.resolve(from: node.props?["textColor"], colorScheme: colorScheme) ?? .white
|
|
431
|
+
}()
|
|
432
|
+
|
|
433
|
+
let fontSize = node.props?["fontSize"]?.doubleValue ?? 12
|
|
434
|
+
let fontWeightStr = node.props?["fontWeight"]?.stringValue ?? "bold"
|
|
435
|
+
let cornerRadius = node.props?["borderRadius"]?.doubleValue.map { CGFloat($0) } ?? 8
|
|
436
|
+
let padH = node.props?["paddingHorizontal"]?.doubleValue.map { CGFloat($0) }
|
|
437
|
+
?? node.props?["padding"]?.doubleValue.map { CGFloat($0) } ?? 10
|
|
438
|
+
let padV = node.props?["paddingVertical"]?.doubleValue.map { CGFloat($0) }
|
|
439
|
+
?? node.props?["padding"]?.doubleValue.map { CGFloat($0) } ?? 6
|
|
440
|
+
let fullWidth = node.props?["fullWidth"]?.boolValue ?? false
|
|
441
|
+
|
|
442
|
+
let labelView = HStack(spacing: 4) {
|
|
443
|
+
Text(displayTitle)
|
|
444
|
+
.font(.system(size: CGFloat(fontSize), weight: parseFontWeight(fontWeightStr)))
|
|
445
|
+
.foregroundColor(textColor)
|
|
446
|
+
.lineLimit(1)
|
|
447
|
+
if fullWidth {
|
|
448
|
+
Spacer(minLength: 0)
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
.padding(.horizontal, padH)
|
|
452
|
+
.padding(.vertical, padV)
|
|
453
|
+
.frame(maxWidth: fullWidth ? .infinity : nil, alignment: .leading)
|
|
454
|
+
.background(
|
|
455
|
+
RoundedRectangle(cornerRadius: cornerRadius)
|
|
456
|
+
.fill(bgColor)
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
if let deepLink = deepLink, let url = URL(string: deepLink) {
|
|
460
|
+
Link(destination: url) {
|
|
461
|
+
labelView
|
|
462
|
+
}
|
|
463
|
+
} else if #available(iOS 17.0, *), !actionId.isEmpty {
|
|
464
|
+
Button(intent: DynamicWidgetActionIntent(actionId: actionId)) {
|
|
465
|
+
labelView
|
|
466
|
+
}
|
|
467
|
+
.buttonStyle(.plain)
|
|
468
|
+
} else {
|
|
469
|
+
labelView
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// MARK: - New Advanced Native Components (ZStack, Grid, Divider, Gauge, Chart, Toggle, Badge, Link, Custom)
|
|
474
|
+
|
|
475
|
+
@ViewBuilder
|
|
476
|
+
private func renderZStack() -> some View {
|
|
477
|
+
let zAlignStr = node.props?["zAlignment"]?.stringValue ?? "center"
|
|
478
|
+
let align: Alignment = {
|
|
479
|
+
switch zAlignStr {
|
|
480
|
+
case "topLeading": return .topLeading
|
|
481
|
+
case "top": return .top
|
|
482
|
+
case "topTrailing": return .topTrailing
|
|
483
|
+
case "leading": return .leading
|
|
484
|
+
case "trailing": return .trailing
|
|
485
|
+
case "bottomLeading": return .bottomLeading
|
|
486
|
+
case "bottom": return .bottom
|
|
487
|
+
case "bottomTrailing": return .bottomTrailing
|
|
488
|
+
default: return .center
|
|
489
|
+
}
|
|
490
|
+
}()
|
|
491
|
+
ZStack(alignment: align) {
|
|
492
|
+
if let children = node.children {
|
|
493
|
+
ForEach(0..<children.count, id: \.self) { index in
|
|
494
|
+
DynamicNodeView(node: children[index])
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
@ViewBuilder
|
|
502
|
+
private func renderGrid() -> some View {
|
|
503
|
+
let columnsCount = max(1, Int(node.props?["columns"]?.doubleValue ?? 2))
|
|
504
|
+
let colSpacing = CGFloat(node.props?["columnSpacing"]?.doubleValue ?? node.props?["spacing"]?.doubleValue ?? 8)
|
|
505
|
+
let rowSpacing = CGFloat(node.props?["rowSpacing"]?.doubleValue ?? node.props?["spacing"]?.doubleValue ?? 8)
|
|
506
|
+
let children = node.children ?? []
|
|
507
|
+
let rowsCount = (children.count + columnsCount - 1) / columnsCount
|
|
508
|
+
|
|
509
|
+
VStack(alignment: .leading, spacing: rowSpacing) {
|
|
510
|
+
ForEach(0..<rowsCount, id: \.self) { rowIdx in
|
|
511
|
+
HStack(alignment: .top, spacing: colSpacing) {
|
|
512
|
+
ForEach(0..<columnsCount, id: \.self) { colIdx in
|
|
513
|
+
let itemIdx = rowIdx * columnsCount + colIdx
|
|
514
|
+
if itemIdx < children.count {
|
|
515
|
+
DynamicNodeView(node: children[itemIdx])
|
|
516
|
+
.frame(maxWidth: .infinity, alignment: .leading)
|
|
517
|
+
} else {
|
|
518
|
+
Spacer(minLength: 0)
|
|
519
|
+
.frame(maxWidth: .infinity)
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
@ViewBuilder
|
|
529
|
+
private func renderDivider() -> some View {
|
|
530
|
+
let colorHex = node.props?["color"]?.stringValue
|
|
531
|
+
let color = colorHex.flatMap { Color(hex: $0) } ?? Color.gray.opacity(0.25)
|
|
532
|
+
let thickness = CGFloat(node.props?["thickness"]?.doubleValue ?? 1)
|
|
533
|
+
let direction = node.props?["direction"]?.stringValue ?? "horizontal"
|
|
534
|
+
let marginV = CGFloat(node.props?["marginVertical"]?.doubleValue ?? 2)
|
|
535
|
+
let marginH = CGFloat(node.props?["marginHorizontal"]?.doubleValue ?? 0)
|
|
536
|
+
|
|
537
|
+
if direction == "vertical" {
|
|
538
|
+
Rectangle()
|
|
539
|
+
.fill(color)
|
|
540
|
+
.frame(width: thickness)
|
|
541
|
+
.padding(.vertical, marginV)
|
|
542
|
+
.padding(.horizontal, marginH)
|
|
543
|
+
} else {
|
|
544
|
+
Rectangle()
|
|
545
|
+
.fill(color)
|
|
546
|
+
.frame(height: thickness)
|
|
547
|
+
.frame(maxWidth: .infinity)
|
|
548
|
+
.padding(.vertical, marginV)
|
|
549
|
+
.padding(.horizontal, marginH)
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
@ViewBuilder
|
|
554
|
+
private func renderGauge() -> some View {
|
|
555
|
+
let rawVal = node.props?["value"]?.doubleValue ?? 0.0
|
|
556
|
+
let clamped = max(0.0, min(1.0, rawVal))
|
|
557
|
+
let size = CGFloat(node.props?["size"]?.doubleValue ?? 64)
|
|
558
|
+
let strokeW = CGFloat(node.props?["strokeWidth"]?.doubleValue ?? max(4, size * 0.11))
|
|
559
|
+
let tintHex = node.props?["tintColor"]?.stringValue
|
|
560
|
+
let tint = tintHex.flatMap { Color(hex: $0) } ?? .blue
|
|
561
|
+
let trackHex = node.props?["trackColor"]?.stringValue
|
|
562
|
+
let track = trackHex.flatMap { Color(hex: $0) } ?? Color.gray.opacity(0.25)
|
|
563
|
+
let textHex = node.props?["textColor"]?.stringValue
|
|
564
|
+
let textColor = textHex.flatMap { Color(hex: $0) } ?? .primary
|
|
565
|
+
let labelHex = node.props?["labelColor"]?.stringValue
|
|
566
|
+
let labelColor = labelHex.flatMap { Color(hex: $0) } ?? .secondary
|
|
567
|
+
let valueText = node.props?["valueText"]?.stringValue ?? "\(Int(round(clamped * 100)))%"
|
|
568
|
+
let label = node.props?["label"]?.stringValue
|
|
569
|
+
|
|
570
|
+
ZStack {
|
|
571
|
+
// Cung đồng hồ 240 độ (từ 150 độ đến 390 độ -> tương đương trim 0.0 -> 0.667 xoay 150 độ)
|
|
572
|
+
Circle()
|
|
573
|
+
.trim(from: 0.0, to: 0.68)
|
|
574
|
+
.stroke(track, style: StrokeStyle(lineWidth: strokeW, lineCap: .round))
|
|
575
|
+
.rotationEffect(.degrees(146))
|
|
576
|
+
|
|
577
|
+
Circle()
|
|
578
|
+
.trim(from: 0.0, to: CGFloat(clamped) * 0.68)
|
|
579
|
+
.stroke(tint, style: StrokeStyle(lineWidth: strokeW, lineCap: .round))
|
|
580
|
+
.rotationEffect(.degrees(146))
|
|
581
|
+
|
|
582
|
+
VStack(spacing: 1) {
|
|
583
|
+
Text(valueText)
|
|
584
|
+
.font(.system(size: max(10, size * 0.24), weight: .bold))
|
|
585
|
+
.foregroundColor(textColor)
|
|
586
|
+
.lineLimit(1)
|
|
587
|
+
.minimumScaleFactor(0.7)
|
|
588
|
+
if let label = label, !label.isEmpty {
|
|
589
|
+
Text(label)
|
|
590
|
+
.font(.system(size: max(8, size * 0.16), weight: .semibold))
|
|
591
|
+
.foregroundColor(labelColor)
|
|
592
|
+
.lineLimit(1)
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
.offset(y: size * 0.04)
|
|
596
|
+
}
|
|
597
|
+
.frame(width: size, height: size)
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
@ViewBuilder
|
|
601
|
+
private func renderChart() -> some View {
|
|
602
|
+
let style = node.props?["style"]?.stringValue ?? "bar"
|
|
603
|
+
let values = node.props?["values"]?.doubleArrayValue ?? []
|
|
604
|
+
let labels = node.props?["labels"]?.stringArrayValue ?? []
|
|
605
|
+
let height = CGFloat(node.props?["height"]?.doubleValue ?? 56)
|
|
606
|
+
let tintHex = node.props?["tintColor"]?.stringValue
|
|
607
|
+
let tint = tintHex.flatMap { Color(hex: $0) } ?? .blue
|
|
608
|
+
let secondaryHex = node.props?["secondaryColor"]?.stringValue
|
|
609
|
+
let secondary = secondaryHex.flatMap { Color(hex: $0) } ?? tint.opacity(0.22)
|
|
610
|
+
let labelHex = node.props?["labelColor"]?.stringValue
|
|
611
|
+
let labelColor = labelHex.flatMap { Color(hex: $0) } ?? .secondary
|
|
612
|
+
let maxVal = max(values.max() ?? 1.0, 0.0001)
|
|
613
|
+
|
|
614
|
+
VStack(spacing: 4) {
|
|
615
|
+
if style == "line", values.count >= 2 {
|
|
616
|
+
GeometryReader { geo in
|
|
617
|
+
let w = geo.size.width
|
|
618
|
+
let h = geo.size.height
|
|
619
|
+
let stepX = w / CGFloat(max(1, values.count - 1))
|
|
620
|
+
|
|
621
|
+
ZStack {
|
|
622
|
+
// Vùng tô mờ phía dưới đường Sparkline
|
|
623
|
+
Path { path in
|
|
624
|
+
path.move(to: CGPoint(x: 0, y: h))
|
|
625
|
+
for (idx, val) in values.enumerated() {
|
|
626
|
+
let x = CGFloat(idx) * stepX
|
|
627
|
+
let y = h - CGFloat(max(0, val) / maxVal) * (h - 6) - 3
|
|
628
|
+
path.addLine(to: CGPoint(x: x, y: y))
|
|
629
|
+
}
|
|
630
|
+
path.addLine(to: CGPoint(x: w, y: h))
|
|
631
|
+
path.closeSubpath()
|
|
632
|
+
}
|
|
633
|
+
.fill(secondary)
|
|
634
|
+
|
|
635
|
+
// Đường kẻ chính
|
|
636
|
+
Path { path in
|
|
637
|
+
for (idx, val) in values.enumerated() {
|
|
638
|
+
let x = CGFloat(idx) * stepX
|
|
639
|
+
let y = h - CGFloat(max(0, val) / maxVal) * (h - 6) - 3
|
|
640
|
+
if idx == 0 {
|
|
641
|
+
path.move(to: CGPoint(x: x, y: y))
|
|
642
|
+
} else {
|
|
643
|
+
path.addLine(to: CGPoint(x: x, y: y))
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
.stroke(tint, style: StrokeStyle(lineWidth: 2.2, lineCap: .round, lineJoin: .round))
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
.frame(height: height)
|
|
651
|
+
} else {
|
|
652
|
+
// Biểu đồ cột (Bar Chart)
|
|
653
|
+
HStack(alignment: .bottom, spacing: 6) {
|
|
654
|
+
ForEach(0..<values.count, id: \.self) { idx in
|
|
655
|
+
let ratio = CGFloat(max(0.08, values[idx] / maxVal))
|
|
656
|
+
let isLast = idx == values.count - 1
|
|
657
|
+
VStack(spacing: 3) {
|
|
658
|
+
Spacer(minLength: 0)
|
|
659
|
+
RoundedRectangle(cornerRadius: 4)
|
|
660
|
+
.fill(isLast ? tint : secondary)
|
|
661
|
+
.frame(height: max(4, height * ratio))
|
|
662
|
+
}
|
|
663
|
+
.frame(maxWidth: .infinity)
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
.frame(height: height)
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if !labels.isEmpty {
|
|
670
|
+
HStack(spacing: 4) {
|
|
671
|
+
ForEach(0..<min(labels.count, max(1, values.count)), id: \.self) { idx in
|
|
672
|
+
Text(labels[idx])
|
|
673
|
+
.font(.system(size: 9, weight: .semibold))
|
|
674
|
+
.foregroundColor(labelColor)
|
|
675
|
+
.lineLimit(1)
|
|
676
|
+
.frame(maxWidth: .infinity)
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
@ViewBuilder
|
|
684
|
+
private func renderToggle() -> some View {
|
|
685
|
+
let rawActionId = node.props?["actionId"]?.stringValue ?? "toggle:default"
|
|
686
|
+
let normalizedActionId = rawActionId.hasPrefix("toggle:") ? rawActionId : "toggle:\(rawActionId)"
|
|
687
|
+
let itemKey = String(normalizedActionId.dropFirst(7))
|
|
688
|
+
let isChecked = DynamicWidgetStateStore.isToggled(itemKey) || (node.props?["checked"]?.boolValue == true && DynamicWidgetStateStore.loadState().toggledItems[itemKey] == nil)
|
|
689
|
+
let label = node.props?["label"]?.stringValue ?? ""
|
|
690
|
+
let subtitle = node.props?["subtitle"]?.stringValue
|
|
691
|
+
let style = node.props?["style"]?.stringValue ?? "checkbox"
|
|
692
|
+
let tintHex = node.props?["tintColor"]?.stringValue
|
|
693
|
+
let tint = tintHex.flatMap { Color(hex: $0) } ?? .green
|
|
694
|
+
let textHex = node.props?["textColor"]?.stringValue
|
|
695
|
+
let checkedTextHex = node.props?["checkedTextColor"]?.stringValue
|
|
696
|
+
let fontSize = CGFloat(node.props?["fontSize"]?.doubleValue ?? 12)
|
|
697
|
+
let strikethrough = node.props?["strikethroughOnCheck"]?.boolValue ?? true
|
|
698
|
+
|
|
699
|
+
let rowView = HStack(spacing: 8) {
|
|
700
|
+
if style == "switch" {
|
|
701
|
+
ZStack(alignment: isChecked ? .trailing : .leading) {
|
|
702
|
+
Capsule()
|
|
703
|
+
.fill(isChecked ? tint : Color.gray.opacity(0.35))
|
|
704
|
+
.frame(width: 32, height: 18)
|
|
705
|
+
Circle()
|
|
706
|
+
.fill(Color.white)
|
|
707
|
+
.frame(width: 14, height: 14)
|
|
708
|
+
.padding(.horizontal, 2)
|
|
709
|
+
}
|
|
710
|
+
} else {
|
|
711
|
+
Image(systemName: isChecked ? "checkmark.circle.fill" : "circle")
|
|
712
|
+
.font(.system(size: fontSize + 3, weight: .bold))
|
|
713
|
+
.foregroundColor(isChecked ? tint : Color.gray.opacity(0.6))
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
VStack(alignment: .leading, spacing: 1) {
|
|
717
|
+
Text(label)
|
|
718
|
+
.font(.system(size: fontSize, weight: isChecked ? .regular : .semibold))
|
|
719
|
+
.strikethrough(isChecked && strikethrough)
|
|
720
|
+
.foregroundColor(
|
|
721
|
+
isChecked
|
|
722
|
+
? (checkedTextHex.flatMap { Color(hex: $0) } ?? .secondary)
|
|
723
|
+
: (textHex.flatMap { Color(hex: $0) } ?? .primary)
|
|
724
|
+
)
|
|
725
|
+
.lineLimit(1)
|
|
726
|
+
if let subtitle = subtitle, !subtitle.isEmpty {
|
|
727
|
+
Text(subtitle)
|
|
728
|
+
.font(.system(size: max(9, fontSize - 2)))
|
|
729
|
+
.foregroundColor(.secondary)
|
|
730
|
+
.lineLimit(1)
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
Spacer(minLength: 0)
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
if #available(iOS 17.0, *) {
|
|
737
|
+
Button(intent: DynamicWidgetActionIntent(actionId: normalizedActionId)) {
|
|
738
|
+
rowView
|
|
739
|
+
}
|
|
740
|
+
.buttonStyle(.plain)
|
|
741
|
+
} else {
|
|
742
|
+
rowView
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
@ViewBuilder
|
|
747
|
+
private func renderBadge() -> some View {
|
|
748
|
+
let text = node.props?["text"]?.stringValue ?? ""
|
|
749
|
+
let systemName = node.props?["systemName"]?.stringValue
|
|
750
|
+
let bgHex = node.props?["backgroundColor"]?.stringValue
|
|
751
|
+
let bgColor = bgHex.flatMap { Color(hex: $0) } ?? Color.blue.opacity(0.15)
|
|
752
|
+
let textHex = node.props?["textColor"]?.stringValue
|
|
753
|
+
let textColor = textHex.flatMap { Color(hex: $0) } ?? .blue
|
|
754
|
+
let borderHex = node.props?["borderColor"]?.stringValue
|
|
755
|
+
let borderW = CGFloat(node.props?["borderWidth"]?.doubleValue ?? (borderHex != nil ? 1 : 0))
|
|
756
|
+
let fontSize = CGFloat(node.props?["fontSize"]?.doubleValue ?? 10)
|
|
757
|
+
let padH = CGFloat(node.props?["paddingHorizontal"]?.doubleValue ?? 8)
|
|
758
|
+
let padV = CGFloat(node.props?["paddingVertical"]?.doubleValue ?? 3)
|
|
759
|
+
let cornerRadius = CGFloat(node.props?["borderRadius"]?.doubleValue ?? 999)
|
|
760
|
+
|
|
761
|
+
HStack(spacing: 4) {
|
|
762
|
+
if let systemName = systemName, !systemName.isEmpty {
|
|
763
|
+
Image(systemName: systemName)
|
|
764
|
+
.font(.system(size: fontSize, weight: .bold))
|
|
765
|
+
.foregroundColor(textColor)
|
|
766
|
+
}
|
|
767
|
+
Text(text)
|
|
768
|
+
.font(.system(size: fontSize, weight: .bold))
|
|
769
|
+
.foregroundColor(textColor)
|
|
770
|
+
.lineLimit(1)
|
|
771
|
+
}
|
|
772
|
+
.padding(.horizontal, padH)
|
|
773
|
+
.padding(.vertical, padV)
|
|
774
|
+
.background(
|
|
775
|
+
RoundedRectangle(cornerRadius: cornerRadius)
|
|
776
|
+
.fill(bgColor)
|
|
777
|
+
)
|
|
778
|
+
.overlay(
|
|
779
|
+
Group {
|
|
780
|
+
if borderW > 0, let borderHex = borderHex, let bColor = Color(hex: borderHex) {
|
|
781
|
+
RoundedRectangle(cornerRadius: cornerRadius)
|
|
782
|
+
.stroke(bColor, lineWidth: borderW)
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
)
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
@ViewBuilder
|
|
789
|
+
private func renderLink() -> some View {
|
|
790
|
+
let deepLink = node.props?["deepLink"]?.stringValue
|
|
791
|
+
let actionId = node.props?["actionId"]?.stringValue ?? ""
|
|
792
|
+
let spacing = node.props?["spacing"]?.doubleValue.map { CGFloat($0) }
|
|
793
|
+
|
|
794
|
+
let innerContent = VStack(alignment: .leading, spacing: spacing) {
|
|
795
|
+
if let children = node.children {
|
|
796
|
+
ForEach(0..<children.count, id: \.self) { index in
|
|
797
|
+
DynamicNodeView(node: children[index])
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
802
|
+
|
|
803
|
+
if let deepLink = deepLink, let url = URL(string: deepLink) {
|
|
804
|
+
Link(destination: url) {
|
|
805
|
+
innerContent
|
|
806
|
+
}
|
|
807
|
+
} else if #available(iOS 17.0, *), !actionId.isEmpty {
|
|
808
|
+
Button(intent: DynamicWidgetActionIntent(actionId: actionId)) {
|
|
809
|
+
innerContent
|
|
810
|
+
}
|
|
811
|
+
.buttonStyle(.plain)
|
|
812
|
+
} else {
|
|
813
|
+
innerContent
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
@ViewBuilder
|
|
818
|
+
private func renderEqualizer() -> some View {
|
|
819
|
+
let isPlaying = node.props?["isPlaying"]?.boolValue ?? true
|
|
820
|
+
let barCount = max(2, min(12, Int(node.props?["barCount"]?.doubleValue ?? 4)))
|
|
821
|
+
let tintHex = node.props?["tintColor"]?.stringValue
|
|
822
|
+
let tint = tintHex.flatMap { Color(hex: $0) } ?? Color(hex: "#10B981") ?? .green
|
|
823
|
+
let maxHeight = CGFloat(node.props?["height"]?.doubleValue ?? 16)
|
|
824
|
+
let barWidth = CGFloat(node.props?["barWidth"]?.doubleValue ?? 3)
|
|
825
|
+
let spacing = CGFloat(node.props?["spacing"]?.doubleValue ?? 2)
|
|
826
|
+
|
|
827
|
+
let phase = node.props?["phase"]?.doubleValue
|
|
828
|
+
let levels: [Double]? = node.props?["levels"]?.doubleArrayValue
|
|
829
|
+
|
|
830
|
+
PhaseEqualizerView(
|
|
831
|
+
isPlaying: isPlaying,
|
|
832
|
+
phase: Int(phase ?? 0),
|
|
833
|
+
levels: levels,
|
|
834
|
+
barCount: barCount,
|
|
835
|
+
tintColor: tint,
|
|
836
|
+
maxHeight: maxHeight,
|
|
837
|
+
barWidth: barWidth,
|
|
838
|
+
spacing: spacing
|
|
839
|
+
)
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
@ViewBuilder
|
|
843
|
+
private func renderCustom() -> some View {
|
|
844
|
+
if let registeredView = DynamicNodeRegistry.shared.resolve(node) {
|
|
845
|
+
registeredView
|
|
846
|
+
} else if let children = node.children, !children.isEmpty {
|
|
847
|
+
VStack(alignment: .leading, spacing: 6) {
|
|
848
|
+
ForEach(0..<children.count, id: \.self) { index in
|
|
849
|
+
DynamicNodeView(node: children[index])
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
853
|
+
} else {
|
|
854
|
+
let compName = node.props?["componentName"]?.stringValue ?? "CustomView"
|
|
855
|
+
let title = node.props?["title"]?.stringValue ?? compName
|
|
856
|
+
let subtitle = node.props?["subtitle"]?.stringValue
|
|
857
|
+
VStack(alignment: .leading, spacing: 2) {
|
|
858
|
+
Text(title)
|
|
859
|
+
.font(.system(size: 12, weight: .bold))
|
|
860
|
+
.foregroundColor(.primary)
|
|
861
|
+
if let subtitle = subtitle {
|
|
862
|
+
Text(subtitle)
|
|
863
|
+
.font(.system(size: 10))
|
|
864
|
+
.foregroundColor(.secondary)
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
.modifier(ContainerStyleModifier(props: node.props))
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
private func parseFontWeight(_ str: String) -> Font.Weight {
|
|
872
|
+
switch str {
|
|
873
|
+
case "bold": return .bold
|
|
874
|
+
case "semibold": return .semibold
|
|
875
|
+
case "medium": return .medium
|
|
876
|
+
default: return .regular
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// Container padding, background modifier
|
|
882
|
+
struct ContainerStyleModifier: ViewModifier {
|
|
883
|
+
@Environment(\.widgetFamily) var family
|
|
884
|
+
@Environment(\.colorScheme) var colorScheme
|
|
885
|
+
let props: [String: AnyCodable]?
|
|
886
|
+
|
|
887
|
+
private var isAccessory: Bool {
|
|
888
|
+
if #available(iOS 16.0, *) {
|
|
889
|
+
return family == .accessoryRectangular || family == .accessoryCircular || family == .accessoryInline
|
|
890
|
+
}
|
|
891
|
+
return false
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
func body(content: Content) -> some View {
|
|
895
|
+
let rawPad = props?["padding"]?.doubleValue.map { CGFloat($0) } ?? 0
|
|
896
|
+
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
|
|
900
|
+
|
|
901
|
+
content
|
|
902
|
+
.padding(pad)
|
|
903
|
+
.background(
|
|
904
|
+
Group {
|
|
905
|
+
if let color = bgColor {
|
|
906
|
+
RoundedRectangle(cornerRadius: cornerRadius).fill(color)
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
)
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// MARK: - Animated Equalizer Components (Audio Waveform for Live Activity & Dynamic Island)
|
|
914
|
+
|
|
915
|
+
@available(iOS 15.0, *)
|
|
916
|
+
struct AnimatedEqualizerView: View {
|
|
917
|
+
let isPlaying: Bool
|
|
918
|
+
let barCount: Int
|
|
919
|
+
let tintColor: Color
|
|
920
|
+
let maxHeight: CGFloat
|
|
921
|
+
let barWidth: CGFloat
|
|
922
|
+
let spacing: CGFloat
|
|
923
|
+
|
|
924
|
+
var body: some View {
|
|
925
|
+
if isPlaying {
|
|
926
|
+
TimelineView(.animation(minimumInterval: 0.16)) { timeline in
|
|
927
|
+
let time = timeline.date.timeIntervalSinceReferenceDate
|
|
928
|
+
HStack(alignment: .bottom, spacing: spacing) {
|
|
929
|
+
ForEach(0..<barCount, id: \.self) { idx in
|
|
930
|
+
let phase = time * 6.5 + Double(idx) * 1.85
|
|
931
|
+
let wave = sin(phase) * 0.44 + 0.56
|
|
932
|
+
let h = max(barWidth, CGFloat(wave) * maxHeight)
|
|
933
|
+
RoundedRectangle(cornerRadius: barWidth / 2)
|
|
934
|
+
.fill(tintColor)
|
|
935
|
+
.frame(width: barWidth, height: h)
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
.frame(height: maxHeight)
|
|
940
|
+
} else {
|
|
941
|
+
StaticEqualizerView(
|
|
942
|
+
barCount: barCount,
|
|
943
|
+
tintColor: tintColor.opacity(0.5),
|
|
944
|
+
maxHeight: maxHeight,
|
|
945
|
+
barWidth: barWidth,
|
|
946
|
+
spacing: spacing
|
|
947
|
+
)
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
struct StaticEqualizerView: View {
|
|
953
|
+
let barCount: Int
|
|
954
|
+
let tintColor: Color
|
|
955
|
+
let maxHeight: CGFloat
|
|
956
|
+
let barWidth: CGFloat
|
|
957
|
+
let spacing: CGFloat
|
|
958
|
+
|
|
959
|
+
private let defaultRatios: [CGFloat] = [0.4, 0.85, 0.55, 0.7, 0.35, 0.9, 0.6]
|
|
960
|
+
|
|
961
|
+
var body: some View {
|
|
962
|
+
HStack(alignment: .bottom, spacing: spacing) {
|
|
963
|
+
ForEach(0..<barCount, id: \.self) { idx in
|
|
964
|
+
let ratio = defaultRatios[idx % defaultRatios.count]
|
|
965
|
+
RoundedRectangle(cornerRadius: barWidth / 2)
|
|
966
|
+
.fill(tintColor)
|
|
967
|
+
.frame(width: barWidth, height: max(barWidth, maxHeight * ratio))
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
.frame(height: maxHeight)
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
/// Equalizer điều khiển theo frame: mỗi lần Live Activity nhận `phase`/`levels` mới,
|
|
976
|
+
/// SwiftUI animate độ cao các thanh (Live Activity cho phép animation chuyển tiếp ≤ 2s khi content thay đổi).
|
|
977
|
+
struct PhaseEqualizerView: View {
|
|
978
|
+
let isPlaying: Bool
|
|
979
|
+
let phase: Int
|
|
980
|
+
let levels: [Double]?
|
|
981
|
+
let barCount: Int
|
|
982
|
+
let tintColor: Color
|
|
983
|
+
let maxHeight: CGFloat
|
|
984
|
+
let barWidth: CGFloat
|
|
985
|
+
let spacing: CGFloat
|
|
986
|
+
|
|
987
|
+
private func ratio(for idx: Int) -> CGFloat {
|
|
988
|
+
guard isPlaying else { return 0.25 }
|
|
989
|
+
if let levels = levels, !levels.isEmpty {
|
|
990
|
+
return CGFloat(max(0.12, min(1.0, levels[idx % levels.count])))
|
|
991
|
+
}
|
|
992
|
+
// Hash ổn định (deterministic) theo phase + index -> 0.2...1.0
|
|
993
|
+
var x = UInt64(bitPattern: Int64(phase &* 7919 &+ idx &* 104729 &+ 12345))
|
|
994
|
+
x ^= x >> 33
|
|
995
|
+
x = x &* 0xff51afd7ed558ccd
|
|
996
|
+
x ^= x >> 33
|
|
997
|
+
x = x &* 0xc4ceb9fe1a85ec53
|
|
998
|
+
x ^= x >> 33
|
|
999
|
+
return 0.2 + CGFloat(Double(x % 1000) / 1000.0) * 0.8
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
var body: some View {
|
|
1003
|
+
HStack(alignment: .bottom, spacing: spacing) {
|
|
1004
|
+
ForEach(0..<barCount, id: \.self) { idx in
|
|
1005
|
+
RoundedRectangle(cornerRadius: barWidth / 2)
|
|
1006
|
+
.fill(isPlaying ? tintColor : tintColor.opacity(0.5))
|
|
1007
|
+
.frame(width: barWidth, height: max(barWidth, maxHeight * ratio(for: idx)))
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
.frame(height: maxHeight, alignment: .bottom)
|
|
1011
|
+
.animation(.easeInOut(duration: 0.6), value: phase)
|
|
1012
|
+
.animation(.easeInOut(duration: 0.3), value: isPlaying)
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
// MARK: - View Modifiers for iOS 15-18 Capabilities
|
|
1017
|
+
|
|
1018
|
+
extension View {
|
|
1019
|
+
@ViewBuilder
|
|
1020
|
+
func applyPrivacySensitive(_ isSensitive: Bool, isRedacted: Bool = false) -> some View {
|
|
1021
|
+
if isRedacted {
|
|
1022
|
+
self.redacted(reason: .placeholder)
|
|
1023
|
+
} else if isSensitive {
|
|
1024
|
+
if #available(iOS 15.0, *) {
|
|
1025
|
+
self.privacySensitive()
|
|
1026
|
+
} else {
|
|
1027
|
+
self
|
|
1028
|
+
}
|
|
1029
|
+
} else {
|
|
1030
|
+
self
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
@ViewBuilder
|
|
1035
|
+
func applySymbolEffect(_ effect: String?) -> some View {
|
|
1036
|
+
if #available(iOS 17.0, *), let effect = effect {
|
|
1037
|
+
switch effect {
|
|
1038
|
+
case "bounce":
|
|
1039
|
+
self.symbolEffect(.bounce)
|
|
1040
|
+
case "pulse":
|
|
1041
|
+
self.symbolEffect(.pulse)
|
|
1042
|
+
case "variableColor":
|
|
1043
|
+
self.symbolEffect(.variableColor.iterative)
|
|
1044
|
+
default:
|
|
1045
|
+
self
|
|
1046
|
+
}
|
|
1047
|
+
} else {
|
|
1048
|
+
self
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
@ViewBuilder
|
|
1053
|
+
func applyWidgetAccentable(_ accentable: Bool) -> some View {
|
|
1054
|
+
if #available(iOS 16.0, *), accentable {
|
|
1055
|
+
self.widgetAccentable()
|
|
1056
|
+
} else {
|
|
1057
|
+
self
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|