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,38 @@
|
|
|
1
|
+
import AppIntents
|
|
2
|
+
import WidgetKit
|
|
3
|
+
|
|
4
|
+
// MARK: - AppIntent for Interactive Widgets & Live Activities (iOS 17+)
|
|
5
|
+
|
|
6
|
+
@available(iOS 17.0, *)
|
|
7
|
+
public struct DynamicWidgetActionIntent: AppIntent {
|
|
8
|
+
public static var title: LocalizedStringResource = "Interactive Widget Action"
|
|
9
|
+
public static var description = IntentDescription("Perform interactive tab/page switching, toggling, or background API sync right on the widget.")
|
|
10
|
+
|
|
11
|
+
@Parameter(title: "Action ID")
|
|
12
|
+
public var actionId: String
|
|
13
|
+
|
|
14
|
+
public init() {
|
|
15
|
+
self.actionId = ""
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public init(actionId: String) {
|
|
19
|
+
self.actionId = actionId
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public func perform() async throws -> some IntentResult {
|
|
23
|
+
if actionId == "refresh" || actionId.hasPrefix("refresh:") {
|
|
24
|
+
let key = actionId.hasPrefix("refresh:") ? String(actionId.dropFirst(8)) : "main"
|
|
25
|
+
await DynamicWidgetStateStore.performRemoteSync(widgetKey: key)
|
|
26
|
+
} else {
|
|
27
|
+
DynamicWidgetStateStore.handleAction(actionId)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Thông báo cho Main App nếu đang chạy ngầm hoặc nền
|
|
31
|
+
let center = CFNotificationCenterGetDarwinNotifyCenter()
|
|
32
|
+
CFNotificationCenterPostNotification(center, CFNotificationName("rn_live_widgets_action_clicked" as CFString), nil, nil, true)
|
|
33
|
+
|
|
34
|
+
WidgetCenter.shared.reloadAllTimelines()
|
|
35
|
+
return .result()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import ActivityKit
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
@available(iOS 16.1, *)
|
|
5
|
+
public struct DynamicActivityAttributes: ActivityAttributes {
|
|
6
|
+
public struct ContentState: Codable, Hashable {
|
|
7
|
+
/// Chuỗi JSON biểu diễn toàn bộ cây giao diện (LockScreen, DynamicIsland, Phases)
|
|
8
|
+
public var payloadJson: String
|
|
9
|
+
|
|
10
|
+
public init(payloadJson: String) {
|
|
11
|
+
self.payloadJson = payloadJson
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/// ID phân biệt các Activity khác nhau (vd orderId, shiftId)
|
|
16
|
+
public var activityId: String
|
|
17
|
+
|
|
18
|
+
public init(activityId: String) {
|
|
19
|
+
self.activityId = activityId
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
// MARK: - Declarative Layout Models (Shared between Live Activity & Home Screen Widgets)
|
|
4
|
+
|
|
5
|
+
public struct LayoutNodeDTO: Codable {
|
|
6
|
+
public let type: String
|
|
7
|
+
public let props: [String: AnyCodable]?
|
|
8
|
+
public let children: [LayoutNodeDTO]?
|
|
9
|
+
|
|
10
|
+
public init(type: String, props: [String: AnyCodable]? = nil, children: [LayoutNodeDTO]? = nil) {
|
|
11
|
+
self.type = type
|
|
12
|
+
self.props = props
|
|
13
|
+
self.children = children
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public struct DynamicPayloadDTO: Codable {
|
|
18
|
+
public let activityId: String?
|
|
19
|
+
public let lockScreen: LayoutNodeDTO?
|
|
20
|
+
public let dynamicIsland: DynamicIslandDTO?
|
|
21
|
+
public let staleTimestamp: Double?
|
|
22
|
+
|
|
23
|
+
public init(
|
|
24
|
+
activityId: String? = nil,
|
|
25
|
+
lockScreen: LayoutNodeDTO? = nil,
|
|
26
|
+
dynamicIsland: DynamicIslandDTO? = nil,
|
|
27
|
+
staleTimestamp: Double? = nil
|
|
28
|
+
) {
|
|
29
|
+
self.activityId = activityId
|
|
30
|
+
self.lockScreen = lockScreen
|
|
31
|
+
self.dynamicIsland = dynamicIsland
|
|
32
|
+
self.staleTimestamp = staleTimestamp
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public struct DynamicIslandDTO: Codable {
|
|
37
|
+
public let compactLeading: LayoutNodeDTO?
|
|
38
|
+
public let compactTrailing: LayoutNodeDTO?
|
|
39
|
+
public let minimal: LayoutNodeDTO?
|
|
40
|
+
public let expanded: ExpandedIslandDTO?
|
|
41
|
+
|
|
42
|
+
public init(
|
|
43
|
+
compactLeading: LayoutNodeDTO? = nil,
|
|
44
|
+
compactTrailing: LayoutNodeDTO? = nil,
|
|
45
|
+
minimal: LayoutNodeDTO? = nil,
|
|
46
|
+
expanded: ExpandedIslandDTO? = nil
|
|
47
|
+
) {
|
|
48
|
+
self.compactLeading = compactLeading
|
|
49
|
+
self.compactTrailing = compactTrailing
|
|
50
|
+
self.minimal = minimal
|
|
51
|
+
self.expanded = expanded
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public struct ExpandedIslandDTO: Codable {
|
|
56
|
+
public let leading: LayoutNodeDTO?
|
|
57
|
+
public let trailing: LayoutNodeDTO?
|
|
58
|
+
public let bottom: LayoutNodeDTO?
|
|
59
|
+
public let center: LayoutNodeDTO?
|
|
60
|
+
|
|
61
|
+
public init(
|
|
62
|
+
leading: LayoutNodeDTO? = nil,
|
|
63
|
+
trailing: LayoutNodeDTO? = nil,
|
|
64
|
+
bottom: LayoutNodeDTO? = nil,
|
|
65
|
+
center: LayoutNodeDTO? = nil
|
|
66
|
+
) {
|
|
67
|
+
self.leading = leading
|
|
68
|
+
self.trailing = trailing
|
|
69
|
+
self.bottom = bottom
|
|
70
|
+
self.center = center
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// MARK: - Multi-Page Home Screen Widget & Remote Sync DTOs
|
|
75
|
+
|
|
76
|
+
public struct WidgetSinglePageDTO: Codable {
|
|
77
|
+
public let small: LayoutNodeDTO?
|
|
78
|
+
public let medium: LayoutNodeDTO?
|
|
79
|
+
public let large: LayoutNodeDTO?
|
|
80
|
+
public let accessoryRectangular: LayoutNodeDTO?
|
|
81
|
+
public let accessoryCircular: LayoutNodeDTO?
|
|
82
|
+
public let accessoryInline: LayoutNodeDTO?
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public struct WidgetMultiPagePayloadDTO: Codable {
|
|
86
|
+
public let small: LayoutNodeDTO?
|
|
87
|
+
public let medium: LayoutNodeDTO?
|
|
88
|
+
public let large: LayoutNodeDTO?
|
|
89
|
+
public let accessoryRectangular: LayoutNodeDTO?
|
|
90
|
+
public let accessoryCircular: LayoutNodeDTO?
|
|
91
|
+
public let accessoryInline: LayoutNodeDTO?
|
|
92
|
+
public let pages: [String: WidgetSinglePageDTO]?
|
|
93
|
+
public let pageOrder: [String]?
|
|
94
|
+
public let initialPage: String?
|
|
95
|
+
public let rootDeepLink: String?
|
|
96
|
+
public let containerBackground: WidgetContainerBackgroundDTO?
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public struct WidgetContainerBackgroundDTO: Codable {
|
|
100
|
+
public let backgroundColor: AnyCodable?
|
|
101
|
+
public let gradientColors: [String]?
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// MARK: - Timeline Entries DTO (Widget Timeline Provider)
|
|
105
|
+
|
|
106
|
+
public struct WidgetTimelineEntryDTO: Codable {
|
|
107
|
+
public let date: Double
|
|
108
|
+
public let layout: WidgetMultiPagePayloadDTO
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public struct WidgetTimelineConfigDTO: Codable {
|
|
112
|
+
public let entries: [WidgetTimelineEntryDTO]
|
|
113
|
+
public let policy: String?
|
|
114
|
+
public let afterDate: Double?
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public struct WidgetRemoteConfigDTO: Codable {
|
|
118
|
+
public let url: String
|
|
119
|
+
public let method: String?
|
|
120
|
+
public let headers: [String: String]?
|
|
121
|
+
public let refreshIntervalMinutes: Int?
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public struct InteractiveWidgetState: Codable {
|
|
125
|
+
public var activePage: String?
|
|
126
|
+
public var pageOrder: [String]?
|
|
127
|
+
public var toggledItems: [String: Bool]
|
|
128
|
+
public var lastActionId: String?
|
|
129
|
+
public var lastSyncTime: String?
|
|
130
|
+
public var remoteVariables: [String: String]?
|
|
131
|
+
public var updatedAt: Double
|
|
132
|
+
|
|
133
|
+
public init(
|
|
134
|
+
activePage: String? = nil,
|
|
135
|
+
pageOrder: [String]? = nil,
|
|
136
|
+
toggledItems: [String: Bool] = [:],
|
|
137
|
+
lastActionId: String? = nil,
|
|
138
|
+
lastSyncTime: String? = nil,
|
|
139
|
+
remoteVariables: [String: String]? = nil,
|
|
140
|
+
updatedAt: Double = Date().timeIntervalSince1970
|
|
141
|
+
) {
|
|
142
|
+
self.activePage = activePage
|
|
143
|
+
self.pageOrder = pageOrder
|
|
144
|
+
self.toggledItems = toggledItems
|
|
145
|
+
self.lastActionId = lastActionId
|
|
146
|
+
self.lastSyncTime = lastSyncTime
|
|
147
|
+
self.remoteVariables = remoteVariables
|
|
148
|
+
self.updatedAt = updatedAt
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// MARK: - AnyCodable Helper
|
|
153
|
+
|
|
154
|
+
public struct AnyCodingKey: CodingKey {
|
|
155
|
+
public var stringValue: String
|
|
156
|
+
public var intValue: Int?
|
|
157
|
+
|
|
158
|
+
public init?(stringValue: String) {
|
|
159
|
+
self.stringValue = stringValue
|
|
160
|
+
self.intValue = nil
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public init?(intValue: Int) {
|
|
164
|
+
self.stringValue = "\(intValue)"
|
|
165
|
+
self.intValue = intValue
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public struct AnyCodable: Codable {
|
|
170
|
+
public let value: Any
|
|
171
|
+
|
|
172
|
+
public init(_ value: Any) {
|
|
173
|
+
self.value = value
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public init(from decoder: Decoder) throws {
|
|
177
|
+
let container = try decoder.singleValueContainer()
|
|
178
|
+
if let string = try? container.decode(String.self) {
|
|
179
|
+
value = string
|
|
180
|
+
} else if let double = try? container.decode(Double.self) {
|
|
181
|
+
value = double
|
|
182
|
+
} else if let bool = try? container.decode(Bool.self) {
|
|
183
|
+
value = bool
|
|
184
|
+
} else if let array = try? container.decode([AnyCodable].self) {
|
|
185
|
+
value = array.map { $0.value }
|
|
186
|
+
} else if let dict = try? container.decode([String: AnyCodable].self) {
|
|
187
|
+
value = dict.mapValues { $0.value }
|
|
188
|
+
} else {
|
|
189
|
+
value = ()
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public func encode(to encoder: Encoder) throws {
|
|
194
|
+
var container = encoder.singleValueContainer()
|
|
195
|
+
if let str = value as? String {
|
|
196
|
+
try container.encode(str)
|
|
197
|
+
} else if let bool = value as? Bool {
|
|
198
|
+
try container.encode(bool)
|
|
199
|
+
} else if let int = value as? Int {
|
|
200
|
+
try container.encode(int)
|
|
201
|
+
} else if let double = value as? Double {
|
|
202
|
+
try container.encode(double)
|
|
203
|
+
} else if let dict = value as? [String: Any] {
|
|
204
|
+
var keyedContainer = encoder.container(keyedBy: AnyCodingKey.self)
|
|
205
|
+
for (k, v) in dict {
|
|
206
|
+
if let key = AnyCodingKey(stringValue: k) {
|
|
207
|
+
try keyedContainer.encode(AnyCodable(v), forKey: key)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
} else if let arr = value as? [Any] {
|
|
211
|
+
var unkeyedContainer = encoder.unkeyedContainer()
|
|
212
|
+
for item in arr {
|
|
213
|
+
try unkeyedContainer.encode(AnyCodable(item))
|
|
214
|
+
}
|
|
215
|
+
} else {
|
|
216
|
+
try container.encodeNil()
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public var stringValue: String? { value as? String }
|
|
221
|
+
public var doubleValue: Double? { value as? Double }
|
|
222
|
+
public var boolValue: Bool? { value as? Bool }
|
|
223
|
+
public var arrayValue: [Any]? { value as? [Any] }
|
|
224
|
+
public var doubleArrayValue: [Double]? {
|
|
225
|
+
guard let arr = value as? [Any] else { return nil }
|
|
226
|
+
return arr.compactMap { item in
|
|
227
|
+
if let d = item as? Double { return d }
|
|
228
|
+
if let i = item as? Int { return Double(i) }
|
|
229
|
+
if let n = item as? NSNumber { return n.doubleValue }
|
|
230
|
+
if let s = item as? String { return Double(s) }
|
|
231
|
+
return nil
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
public var stringArrayValue: [String]? {
|
|
235
|
+
guard let arr = value as? [Any] else { return nil }
|
|
236
|
+
return arr.compactMap { item in
|
|
237
|
+
if let s = item as? String { return s }
|
|
238
|
+
if let n = item as? NSNumber { return n.stringValue }
|
|
239
|
+
return nil
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import SwiftUI
|
|
2
|
+
|
|
3
|
+
// MARK: - SwiftUI Color Hex Parser
|
|
4
|
+
|
|
5
|
+
extension Color {
|
|
6
|
+
init?(hex: String) {
|
|
7
|
+
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
|
|
8
|
+
if hexSanitized.hasPrefix("#") {
|
|
9
|
+
hexSanitized.remove(at: hexSanitized.startIndex)
|
|
10
|
+
}
|
|
11
|
+
var rgb: UInt64 = 0
|
|
12
|
+
guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { return nil }
|
|
13
|
+
|
|
14
|
+
let r, g, b, a: Double
|
|
15
|
+
if hexSanitized.count == 6 {
|
|
16
|
+
r = Double((rgb & 0xFF0000) >> 16) / 255.0
|
|
17
|
+
g = Double((rgb & 0x00FF00) >> 8) / 255.0
|
|
18
|
+
b = Double(rgb & 0x0000FF) / 255.0
|
|
19
|
+
a = 1.0
|
|
20
|
+
} else if hexSanitized.count == 8 {
|
|
21
|
+
r = Double((rgb & 0xFF000000) >> 24) / 255.0
|
|
22
|
+
g = Double((rgb & 0x00FF0000) >> 16) / 255.0
|
|
23
|
+
b = Double((rgb & 0x0000FF00) >> 8) / 255.0
|
|
24
|
+
a = Double(rgb & 0x000000FF) / 255.0
|
|
25
|
+
} else {
|
|
26
|
+
return nil
|
|
27
|
+
}
|
|
28
|
+
self.init(.sRGB, red: r, green: g, blue: b, opacity: a)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Tự động resolve màu theo chuỗi Hex đơn hoặc Object Dynamic Color { light: "...", dark: "..." }
|
|
32
|
+
static func resolve(from anyCodable: AnyCodable?, colorScheme: ColorScheme? = nil) -> Color? {
|
|
33
|
+
guard let anyCodable = anyCodable else { return nil }
|
|
34
|
+
if let hex = anyCodable.stringValue {
|
|
35
|
+
return Color(hex: hex)
|
|
36
|
+
}
|
|
37
|
+
if let dict = anyCodable.value as? [String: Any] {
|
|
38
|
+
let schemeKey = (colorScheme == .dark) ? "dark" : "light"
|
|
39
|
+
if let hex = dict[schemeKey] as? String {
|
|
40
|
+
return Color(hex: hex)
|
|
41
|
+
}
|
|
42
|
+
if let fallbackHex = dict["light"] as? String ?? dict["dark"] as? String {
|
|
43
|
+
return Color(hex: fallbackHex)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return nil
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import SwiftUI
|
|
3
|
+
|
|
4
|
+
// MARK: - Custom Native SwiftUI Component Registry
|
|
5
|
+
|
|
6
|
+
/// Cho phép Developer đăng ký thêm bất kỳ SwiftUI View tùy chỉnh nào để gọi từ React Native qua `UI.Custom("ComponentName", props)`
|
|
7
|
+
public final class DynamicNodeRegistry {
|
|
8
|
+
public static let shared = DynamicNodeRegistry()
|
|
9
|
+
public typealias CustomNodeBuilder = (LayoutNodeDTO) -> AnyView
|
|
10
|
+
|
|
11
|
+
private var builders: [String: CustomNodeBuilder] = [:]
|
|
12
|
+
private let lock = NSLock()
|
|
13
|
+
|
|
14
|
+
private init() {}
|
|
15
|
+
|
|
16
|
+
/// Đăng ký một SwiftUI View tùy chỉnh theo tên `componentName`
|
|
17
|
+
/// Ví dụ:
|
|
18
|
+
/// ```swift
|
|
19
|
+
/// DynamicNodeRegistry.shared.register("WeatherCard") { node in
|
|
20
|
+
/// let temp = node.props?["temp"]?.stringValue ?? "28°C"
|
|
21
|
+
/// return AnyView(Text(temp).font(.title.bold()))
|
|
22
|
+
/// }
|
|
23
|
+
/// ```
|
|
24
|
+
public func register(_ componentName: String, builder: @escaping CustomNodeBuilder) {
|
|
25
|
+
lock.lock()
|
|
26
|
+
defer { lock.unlock() }
|
|
27
|
+
builders[componentName] = builder
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// Đăng ký trực tiếp bằng `@ViewBuilder` mà không cần tự bọc `AnyView`
|
|
31
|
+
public func registerView<V: View>(
|
|
32
|
+
_ componentName: String,
|
|
33
|
+
@ViewBuilder builder: @escaping (LayoutNodeDTO) -> V
|
|
34
|
+
) {
|
|
35
|
+
register(componentName) { node in
|
|
36
|
+
AnyView(builder(node))
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public func resolve(_ node: LayoutNodeDTO) -> AnyView? {
|
|
41
|
+
let key: String = {
|
|
42
|
+
if node.type == "Custom", let name = node.props?["componentName"]?.stringValue {
|
|
43
|
+
return name
|
|
44
|
+
}
|
|
45
|
+
return node.type
|
|
46
|
+
}()
|
|
47
|
+
|
|
48
|
+
lock.lock()
|
|
49
|
+
let builder = builders[key]
|
|
50
|
+
lock.unlock()
|
|
51
|
+
return builder?(node)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|