@onekeyfe/react-native-native-list 3.0.157 → 3.0.159

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 (199) hide show
  1. package/README.md +21 -5
  2. package/android/build.gradle +7 -0
  3. package/android/src/main/java/com/onekey/nativelist/HybridNativeList.kt +20 -0
  4. package/android/src/main/java/com/onekey/nativelist/NativeListAccessoryStack.kt +711 -0
  5. package/android/src/main/java/com/onekey/nativelist/NativeListActionRenderer.kt +169 -0
  6. package/android/src/main/java/com/onekey/nativelist/NativeListActivityDetailsView.kt +179 -0
  7. package/android/src/main/java/com/onekey/nativelist/NativeListActivityRenderer.kt +264 -0
  8. package/android/src/main/java/com/onekey/nativelist/NativeListAdapter.kt +20 -6
  9. package/android/src/main/java/com/onekey/nativelist/NativeListContainerSlotAdapter.kt +53 -0
  10. package/android/src/main/java/com/onekey/nativelist/NativeListDataRowRenderer.kt +487 -0
  11. package/android/src/main/java/com/onekey/nativelist/NativeListEndAlignment.kt +5 -0
  12. package/android/src/main/java/com/onekey/nativelist/NativeListIdentityRenderer.kt +820 -0
  13. package/android/src/main/java/com/onekey/nativelist/NativeListImageSlot.kt +147 -0
  14. package/android/src/main/java/com/onekey/nativelist/NativeListLeadingVisual.kt +453 -0
  15. package/android/src/main/java/com/onekey/nativelist/NativeListMarketRenderer.kt +997 -0
  16. package/android/src/main/java/com/onekey/nativelist/NativeListMediaPreviewSlot.kt +268 -0
  17. package/android/src/main/java/com/onekey/nativelist/NativeListMediaTileRenderer.kt +243 -0
  18. package/android/src/main/java/com/onekey/nativelist/NativeListMessageRenderer.kt +232 -0
  19. package/android/src/main/java/com/onekey/nativelist/NativeListMetricCardRenderer.kt +551 -0
  20. package/android/src/main/java/com/onekey/nativelist/NativeListModels.kt +27 -1
  21. package/android/src/main/java/com/onekey/nativelist/NativeListPrimitives.kt +475 -0
  22. package/android/src/main/java/com/onekey/nativelist/NativeListRailRenderer.kt +143 -0
  23. package/android/src/main/java/com/onekey/nativelist/NativeListRendererRegistry.kt +42 -0
  24. package/android/src/main/java/com/onekey/nativelist/NativeListRendererRowView.kt +415 -0
  25. package/android/src/main/java/com/onekey/nativelist/NativeListResolvedText.kt +163 -0
  26. package/android/src/main/java/com/onekey/nativelist/NativeListRowHost.kt +70 -0
  27. package/android/src/main/java/com/onekey/nativelist/NativeListScrollPositionTracker.kt +59 -0
  28. package/android/src/main/java/com/onekey/nativelist/NativeListSectionHeaderRenderer.kt +763 -0
  29. package/android/src/main/java/com/onekey/nativelist/NativeListSystemRenderer.kt +464 -0
  30. package/android/src/main/java/com/onekey/nativelist/NativeListTextView.kt +25 -0
  31. package/android/src/main/java/com/onekey/nativelist/NativeListView.kt +432 -124
  32. package/android/src/main/java/com/onekey/nativelist/NativeListWalletGroupRenderer.kt +407 -0
  33. package/android/src/main/res/values/native_scroll_coordinator_ids.xml +5 -0
  34. package/android/src/test/java/com/margelo/nitro/nativelist/NativeListEndAlignmentTest.kt +34 -0
  35. package/android/src/test/java/com/margelo/nitro/nativelist/NativeListScrollPositionTrackerTest.kt +44 -0
  36. package/android/src/test/java/com/margelo/nitro/nativelist/NativeListSelectorBackgroundColorTest.kt +34 -0
  37. package/docs/DESIGN.md +520 -9
  38. package/docs/MIGRATION_BASELINE.md +117 -0
  39. package/docs/ROW_TEMPLATES.md +404 -0
  40. package/docs/SPEC.md +621 -0
  41. package/docs/STYLE_SPEC.md +988 -0
  42. package/docs/images/list-capabilities.svg +39 -0
  43. package/docs/images/row-action.svg +17 -0
  44. package/docs/images/row-activity.svg +24 -0
  45. package/docs/images/row-data.svg +24 -0
  46. package/docs/images/row-identity.svg +25 -0
  47. package/docs/images/row-market.svg +23 -0
  48. package/docs/images/row-media-tile.svg +26 -0
  49. package/docs/images/row-message.svg +20 -0
  50. package/docs/images/row-metric-card.svg +33 -0
  51. package/docs/images/row-rail.svg +18 -0
  52. package/docs/images/row-section-header.svg +19 -0
  53. package/docs/images/row-style-regions.svg +30 -0
  54. package/docs/images/row-system.svg +30 -0
  55. package/docs/images/row-wallet-group.svg +26 -0
  56. package/ios/HybridNativeList.swift +30 -0
  57. package/ios/NativeListAccessoryStack.swift +680 -0
  58. package/ios/NativeListActionRenderer.swift +108 -0
  59. package/ios/NativeListActivityDetailsView.swift +291 -0
  60. package/ios/NativeListActivityRenderer.swift +201 -0
  61. package/ios/NativeListDataRowRenderer.swift +444 -0
  62. package/ios/NativeListIdentityRenderer.swift +685 -0
  63. package/ios/NativeListImageSlot.swift +140 -0
  64. package/ios/NativeListLeadingVisual.swift +372 -0
  65. package/ios/NativeListMarketRenderer.swift +698 -0
  66. package/ios/NativeListMediaPreviewSlot.swift +259 -0
  67. package/ios/NativeListMediaTileRenderer.swift +190 -0
  68. package/ios/NativeListMessageRenderer.swift +168 -0
  69. package/ios/NativeListMetricCardRenderer.swift +420 -0
  70. package/ios/NativeListModels.swift +36 -1
  71. package/ios/NativeListRailRenderer.swift +152 -0
  72. package/ios/NativeListRendererCell.swift +311 -0
  73. package/ios/NativeListRendererRegistry.swift +50 -0
  74. package/ios/NativeListResolvedText.swift +130 -0
  75. package/ios/NativeListRowHost.swift +63 -0
  76. package/ios/NativeListScrollPositionTracker.swift +30 -0
  77. package/ios/NativeListSectionHeaderRenderer.swift +734 -0
  78. package/ios/NativeListSystemRenderer.swift +568 -0
  79. package/ios/NativeListTextLabel.swift +42 -0
  80. package/ios/NativeListTextStyles.swift +236 -0
  81. package/ios/NativeListWalletGroupRenderer.swift +255 -0
  82. package/ios/RNCNativeListView.swift +282 -184
  83. package/ios/tests/NativeListActivityPresentationTests.swift +97 -0
  84. package/ios/tests/NativeListLifecycleTests.swift +241 -0
  85. package/ios/tests/NativeListMediaVisibilityTests.swift +109 -0
  86. package/ios/tests/NativeListScrollPositionTrackerTests.swift +25 -0
  87. package/lib/module/NativeList.js +56 -3
  88. package/lib/module/NativeList.web.js +5 -0
  89. package/lib/module/models.js +54 -0
  90. package/lib/module/validation.js +386 -28
  91. package/lib/module/web/NativeListWebEngine.js +148 -936
  92. package/lib/module/web/templates/ActionRowRenderer.js +97 -0
  93. package/lib/module/web/templates/ActivityRowRenderer.js +95 -0
  94. package/lib/module/web/templates/DataRowRenderer.js +145 -0
  95. package/lib/module/web/templates/IdentityRowRenderer.js +289 -0
  96. package/lib/module/web/templates/MarketRowRenderer.js +310 -0
  97. package/lib/module/web/templates/MediaTileRowRenderer.js +132 -0
  98. package/lib/module/web/templates/MessageRowRenderer.js +165 -0
  99. package/lib/module/web/templates/MetricCardRowRenderer.js +172 -0
  100. package/lib/module/web/templates/RailRowRenderer.js +98 -0
  101. package/lib/module/web/templates/RowContainerStyle.js +100 -0
  102. package/lib/module/web/templates/RowElements.js +93 -0
  103. package/lib/module/web/templates/RowRendererRegistry.js +70 -0
  104. package/lib/module/web/templates/RowText.js +121 -0
  105. package/lib/module/web/templates/RowVisual.js +43 -0
  106. package/lib/module/web/templates/SectionHeaderRowRenderer.js +146 -0
  107. package/lib/module/web/templates/SystemRowRenderer.js +161 -0
  108. package/lib/module/web/templates/WalletGroupRowRenderer.js +96 -0
  109. package/lib/nitrogen/generated/android/c++/JFunc_void_bool.hpp +75 -0
  110. package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +39 -3
  111. package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +6 -0
  112. package/lib/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +15 -0
  113. package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/Func_void_bool.kt +78 -0
  114. package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +26 -0
  115. package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/views/HybridNativeListManager.kt +24 -9
  116. package/lib/nitrogen/generated/android/nativelistOnLoad.cpp +2 -0
  117. package/lib/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.cpp +8 -0
  118. package/lib/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.hpp +37 -0
  119. package/lib/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +23 -2
  120. package/lib/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +32 -0
  121. package/lib/nitrogen/generated/ios/swift/Func_void_bool.swift +46 -0
  122. package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +3 -0
  123. package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +54 -0
  124. package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +6 -0
  125. package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +8 -2
  126. package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +6 -0
  127. package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +11 -2
  128. package/lib/nitrogen/generated/shared/json/NativeListConfig.json +3 -0
  129. package/lib/typescript/src/NativeList.nitro.d.ts +3 -0
  130. package/lib/typescript/src/NativeList.types.d.ts +14 -1
  131. package/lib/typescript/src/models.d.ts +199 -18
  132. package/lib/typescript/src/web/NativeListWebEngine.d.ts +18 -16
  133. package/lib/typescript/src/web/templates/ActionRowRenderer.d.ts +15 -0
  134. package/lib/typescript/src/web/templates/ActivityRowRenderer.d.ts +15 -0
  135. package/lib/typescript/src/web/templates/DataRowRenderer.d.ts +15 -0
  136. package/lib/typescript/src/web/templates/IdentityRowRenderer.d.ts +16 -0
  137. package/lib/typescript/src/web/templates/MarketRowRenderer.d.ts +30 -0
  138. package/lib/typescript/src/web/templates/MediaTileRowRenderer.d.ts +15 -0
  139. package/lib/typescript/src/web/templates/MessageRowRenderer.d.ts +44 -0
  140. package/lib/typescript/src/web/templates/MetricCardRowRenderer.d.ts +15 -0
  141. package/lib/typescript/src/web/templates/RailRowRenderer.d.ts +28 -0
  142. package/lib/typescript/src/web/templates/RowContainerStyle.d.ts +9 -0
  143. package/lib/typescript/src/web/templates/RowElements.d.ts +38 -0
  144. package/lib/typescript/src/web/templates/RowRendererRegistry.d.ts +83 -0
  145. package/lib/typescript/src/web/templates/RowText.d.ts +9 -0
  146. package/lib/typescript/src/web/templates/RowVisual.d.ts +20 -0
  147. package/lib/typescript/src/web/templates/SectionHeaderRowRenderer.d.ts +15 -0
  148. package/lib/typescript/src/web/templates/SystemRowRenderer.d.ts +16 -0
  149. package/lib/typescript/src/web/templates/WalletGroupRowRenderer.d.ts +20 -0
  150. package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +75 -0
  151. package/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +39 -3
  152. package/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +6 -0
  153. package/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +15 -0
  154. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/Func_void_bool.kt +78 -0
  155. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +26 -0
  156. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/views/HybridNativeListManager.kt +24 -9
  157. package/nitrogen/generated/android/nativelistOnLoad.cpp +2 -0
  158. package/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.cpp +8 -0
  159. package/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.hpp +37 -0
  160. package/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +23 -2
  161. package/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +32 -0
  162. package/nitrogen/generated/ios/swift/Func_void_bool.swift +46 -0
  163. package/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +3 -0
  164. package/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +54 -0
  165. package/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +6 -0
  166. package/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +8 -2
  167. package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +6 -0
  168. package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +11 -2
  169. package/nitrogen/generated/shared/json/NativeListConfig.json +3 -0
  170. package/package.json +7 -6
  171. package/react-native-native-list.podspec +6 -0
  172. package/scripts/enable-container-slots.mjs +58 -0
  173. package/scripts/enable-container-slots.test.mjs +58 -0
  174. package/src/NativeList.nitro.ts +3 -0
  175. package/src/NativeList.tsx +84 -2
  176. package/src/NativeList.types.ts +14 -1
  177. package/src/NativeList.web.tsx +5 -0
  178. package/src/models.ts +320 -20
  179. package/src/validation.ts +641 -49
  180. package/src/web/NativeListWebEngine.ts +361 -1932
  181. package/src/web/templates/ActionRowRenderer.ts +123 -0
  182. package/src/web/templates/ActivityRowRenderer.ts +159 -0
  183. package/src/web/templates/DataRowRenderer.ts +212 -0
  184. package/src/web/templates/IdentityRowRenderer.ts +527 -0
  185. package/src/web/templates/MarketRowRenderer.ts +467 -0
  186. package/src/web/templates/MediaTileRowRenderer.ts +182 -0
  187. package/src/web/templates/MessageRowRenderer.ts +306 -0
  188. package/src/web/templates/MetricCardRowRenderer.ts +337 -0
  189. package/src/web/templates/RailRowRenderer.ts +136 -0
  190. package/src/web/templates/RowContainerStyle.ts +131 -0
  191. package/src/web/templates/RowElements.ts +160 -0
  192. package/src/web/templates/RowRendererRegistry.ts +111 -0
  193. package/src/web/templates/RowText.ts +181 -0
  194. package/src/web/templates/RowVisual.ts +115 -0
  195. package/src/web/templates/SectionHeaderRowRenderer.ts +243 -0
  196. package/src/web/templates/SystemRowRenderer.ts +282 -0
  197. package/src/web/templates/WalletGroupRowRenderer.ts +131 -0
  198. package/android/src/main/java/com/onekey/nativelist/NativeListRowView.kt +0 -4538
  199. package/ios/NativeListCell.swift +0 -4300
@@ -0,0 +1,259 @@
1
+ import AVFoundation
2
+ import UIKit
3
+
4
+ private final class NativeListPausedVideoView: UIView {
5
+ var onWindowChanged: (() -> Void)?
6
+ var onLayoutChanged: (() -> Void)?
7
+ override class var layerClass: AnyClass { AVPlayerLayer.self }
8
+ var playerLayer: AVPlayerLayer { layer as! AVPlayerLayer }
9
+ override func didMoveToWindow() { super.didMoveToWindow(); onWindowChanged?() }
10
+ override func layoutSubviews() { super.layoutSubviews(); onLayoutChanged?() }
11
+ }
12
+
13
+ /// Uses the same AVPlayer source semantics as native Video, always paused and muted. A player
14
+ /// lives only until its first frame is copied; the tile then shows that still frame.
15
+ final class NativeListMediaPreviewSlot {
16
+ let view = UIView()
17
+ private let image = NativeListImageSlot()
18
+ private let video = NativeListPausedVideoView()
19
+ private let frameView = UIImageView()
20
+ private var videoOutput: AVPlayerItemVideoOutput?
21
+ private static let frameContext = CIContext(options: [.cacheIntermediates: false])
22
+ private static let frameCaptureAttempts = 6
23
+ private var identity: String?
24
+ private var epoch = 0
25
+ private var candidateGeneration = 0
26
+ private var playerGeneration = 0
27
+ private var result: Bool?
28
+ private var completion: ((Bool) -> Void)?
29
+ private var player: AVPlayer?
30
+ private var itemObservation: NSKeyValueObservation?
31
+ private var readyObservation: NSKeyValueObservation?
32
+ private var resumeVideo: (() -> Void)?
33
+ private var lifecycleObservers: [NSObjectProtocol] = []
34
+ private var viewportObservations: [NSKeyValueObservation] = []
35
+
36
+ init() {
37
+ view.isUserInteractionEnabled = false
38
+ for child in [image.view, video] {
39
+ child.frame = view.bounds
40
+ child.autoresizingMask = [.flexibleWidth, .flexibleHeight]
41
+ view.addSubview(child)
42
+ }
43
+ video.clipsToBounds = true
44
+ video.isHidden = true
45
+ frameView.frame = video.bounds
46
+ frameView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
47
+ frameView.isHidden = true
48
+ video.addSubview(frameView)
49
+ video.onWindowChanged = { [weak self] in self?.observeViewport(); self?.updateVisibility() }
50
+ video.onLayoutChanged = { [weak self] in self?.updateVisibility() }
51
+ lifecycleObservers = [
52
+ NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: .main) { [weak self] _ in self?.releasePlayer() },
53
+ NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { [weak self] _ in self?.updateVisibility() },
54
+ ]
55
+ }
56
+
57
+ func bind(
58
+ _ source: [String: Any], key: String, fit: String? = nil,
59
+ placeholder: String = "#0000000F", probeOrder: [String] = ["image"],
60
+ completion: ((Bool) -> Void)? = nil
61
+ ) {
62
+ let next = NativeListImageSlot.signature([
63
+ "source": source, "key": key, "fit": fit ?? "", "order": probeOrder,
64
+ "placeholder": placeholder,
65
+ ])
66
+ self.completion = completion
67
+ if identity == next {
68
+ if let result { completion?(result) }
69
+ updateVisibility()
70
+ return
71
+ }
72
+ recycle()
73
+ identity = next
74
+ self.completion = completion
75
+ let cover = (fit ?? source.string("contentFit")) == "cover"
76
+ video.playerLayer.videoGravity = cover ? .resizeAspectFill : .resizeAspect
77
+ frameView.contentMode = cover ? .scaleAspectFill : .scaleAspectFit
78
+ probe(source, key: key, fit: fit, placeholder: placeholder, order: probeOrder, attempt: 0, token: epoch)
79
+ }
80
+
81
+ private func probe(
82
+ _ source: [String: Any], key: String, fit: String?, placeholder: String,
83
+ order: [String], attempt: Int, token: Int
84
+ ) {
85
+ guard token == epoch else { return }
86
+ candidateGeneration &+= 1
87
+ let candidate = candidateGeneration
88
+ resumeVideo = nil
89
+ viewportObservations.removeAll()
90
+ releasePlayer()
91
+ guard attempt < order.count else {
92
+ result = false
93
+ completion?(false)
94
+ return
95
+ }
96
+ let finished: (Bool) -> Void = { [weak self] loaded in
97
+ guard let self, self.epoch == token, self.candidateGeneration == candidate else { return }
98
+ if loaded {
99
+ guard self.result != true else { return }
100
+ self.result = true
101
+ self.completion?(true)
102
+ } else {
103
+ self.probe(source, key: key, fit: fit, placeholder: placeholder, order: order, attempt: attempt + 1, token: token)
104
+ }
105
+ }
106
+ if order[attempt] == "image" {
107
+ video.isHidden = true
108
+ image.view.isHidden = false
109
+ image.bind(source, key: key, fit: fit, placeholder: placeholder, completion: finished)
110
+ return
111
+ }
112
+ image.recycle()
113
+ image.view.isHidden = true
114
+ video.isHidden = false
115
+ guard let url = URL(string: source.string("uri")), !source.string("uri").isEmpty else { finished(false); return }
116
+ resumeVideo = { [weak self] in
117
+ guard let self, self.epoch == token, self.candidateGeneration == candidate, self.player == nil else { return }
118
+ self.playerGeneration &+= 1
119
+ let generation = self.playerGeneration
120
+ let asset = AVURLAsset(url: url, options: ["AVURLAssetHTTPHeaderFieldsKey": source.dictionary("headers") ?? [:]])
121
+ let item = AVPlayerItem(asset: asset)
122
+ item.preferredForwardBufferDuration = 1
123
+ let output = AVPlayerItemVideoOutput(pixelBufferAttributes: [
124
+ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
125
+ ])
126
+ item.add(output)
127
+ self.videoOutput = output
128
+ let player = AVPlayer(playerItem: item)
129
+ player.isMuted = true
130
+ player.pause()
131
+ self.player = player
132
+ self.video.playerLayer.player = player
133
+ self.itemObservation = item.observe(\.status, options: [.initial, .new]) { [weak self] item, _ in
134
+ DispatchQueue.main.async {
135
+ guard let self, self.playerGeneration == generation else { return }
136
+ if item.status == .failed { finished(false) }
137
+ }
138
+ }
139
+ self.readyObservation = self.video.playerLayer.observe(\.isReadyForDisplay, options: [.initial, .new]) { [weak self] layer, _ in
140
+ DispatchQueue.main.async {
141
+ guard let self, self.playerGeneration == generation else { return }
142
+ guard layer.isReadyForDisplay else { return }
143
+ finished(true)
144
+ self.captureFirstFrame(generation: generation, attemptsLeft: Self.frameCaptureAttempts)
145
+ }
146
+ }
147
+ }
148
+ observeViewport()
149
+ updateVisibility()
150
+ }
151
+
152
+ /// A paused item may not have handed its preroll frame to the output yet, so copying retries
153
+ /// on later runloop turns. If no frame arrives the paused player stays attached.
154
+ private func captureFirstFrame(generation: Int, attemptsLeft: Int) {
155
+ guard playerGeneration == generation, let player, let item = player.currentItem, let output = videoOutput else { return }
156
+ let time = item.currentTime()
157
+ if let buffer = output.copyPixelBuffer(forItemTime: time, itemTimeForDisplay: nil),
158
+ let still = Self.frameContext.createCGImage(CIImage(cvPixelBuffer: buffer), from: CGRect(
159
+ x: 0, y: 0, width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer))) {
160
+ let transform = item.tracks.lazy.compactMap(\.assetTrack).first { $0.mediaType == .video }?.preferredTransform
161
+ frameView.image = UIImage(cgImage: still, scale: 1, orientation: Self.orientation(transform))
162
+ frameView.isHidden = false
163
+ resumeVideo = nil
164
+ viewportObservations.removeAll()
165
+ releasePlayer()
166
+ return
167
+ }
168
+ guard attemptsLeft > 1 else { return }
169
+ // An exact seek re-delivers the paused preroll frame to newly attached outputs.
170
+ if attemptsLeft == Self.frameCaptureAttempts {
171
+ player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
172
+ }
173
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
174
+ self?.captureFirstFrame(generation: generation, attemptsLeft: attemptsLeft - 1)
175
+ }
176
+ }
177
+
178
+ private static func orientation(_ transform: CGAffineTransform?) -> UIImage.Orientation {
179
+ guard let transform else { return .up }
180
+ switch (transform.a, transform.b, transform.c, transform.d) {
181
+ case (0, 1, -1, 0): return .right
182
+ case (0, -1, 1, 0): return .left
183
+ case (-1, 0, 0, -1): return .down
184
+ default: return .up
185
+ }
186
+ }
187
+
188
+ private func observeViewport() {
189
+ viewportObservations.removeAll()
190
+ guard resumeVideo != nil, video.window != nil else { return }
191
+ var ancestor = video.superview
192
+ while let current = ancestor {
193
+ if let scrollView = current as? UIScrollView {
194
+ viewportObservations.append(scrollView.observe(\.contentOffset, options: [.new]) { [weak self] _, _ in self?.updateVisibility() })
195
+ viewportObservations.append(scrollView.observe(\.bounds, options: [.new]) { [weak self] _, _ in self?.updateVisibility() })
196
+ }
197
+ // Pager or host layouts may translate a retained page without scrolling it.
198
+ viewportObservations.append(current.layer.observe(\.transform, options: [.new]) { [weak self] _, _ in self?.updateVisibility() })
199
+ viewportObservations.append(current.layer.observe(\.position, options: [.new]) { [weak self] _, _ in self?.updateVisibility() })
200
+ viewportObservations.append(current.layer.observe(\.isHidden, options: [.new]) { [weak self] _, _ in self?.updateVisibility() })
201
+ viewportObservations.append(current.layer.observe(\.opacity, options: [.new]) { [weak self] _, _ in self?.updateVisibility() })
202
+ ancestor = current.superview
203
+ }
204
+ }
205
+
206
+ var hasLivePlayer: Bool { player != nil }
207
+ var capturedFrame: UIImage? { frameView.image }
208
+
209
+ /// Retained pager pages can have a window while lying outside its clipped viewport.
210
+ static func intersectsViewport(_ view: UIView) -> Bool {
211
+ guard let window = view.window, !view.bounds.isEmpty else { return false }
212
+ var visible = view.convert(view.bounds, to: window).intersection(window.bounds)
213
+ var ancestor: UIView? = view
214
+ while let current = ancestor {
215
+ if current.isHidden || current.alpha <= 0.01 { return false }
216
+ if current.clipsToBounds || current is UIScrollView {
217
+ visible = visible.intersection(current.convert(current.bounds, to: window))
218
+ }
219
+ if visible.isNull || visible.isEmpty { return false }
220
+ ancestor = current.superview
221
+ }
222
+ return true
223
+ }
224
+
225
+ private func updateVisibility() {
226
+ if Self.intersectsViewport(video) && UIApplication.shared.applicationState == .active { resumeVideo?() }
227
+ else { releasePlayer() }
228
+ }
229
+ private func releasePlayer() {
230
+ playerGeneration &+= 1
231
+ itemObservation = nil
232
+ readyObservation = nil
233
+ videoOutput = nil
234
+ player?.pause()
235
+ video.playerLayer.player = nil
236
+ player?.replaceCurrentItem(with: nil)
237
+ player = nil
238
+ }
239
+ func recycle() {
240
+ epoch &+= 1
241
+ candidateGeneration &+= 1
242
+ resumeVideo = nil
243
+ viewportObservations.removeAll()
244
+ releasePlayer()
245
+ image.recycle()
246
+ image.view.isHidden = false
247
+ video.isHidden = true
248
+ frameView.image = nil
249
+ frameView.isHidden = true
250
+ identity = nil
251
+ result = nil
252
+ completion = nil
253
+ }
254
+ deinit {
255
+ lifecycleObservers.forEach(NotificationCenter.default.removeObserver)
256
+ player?.pause()
257
+ player?.replaceCurrentItem(with: nil)
258
+ }
259
+ }
@@ -0,0 +1,190 @@
1
+ import UIKit
2
+
3
+ final class NativeListMediaTileCell: NativeListRendererCell {
4
+ override class func measure(
5
+ _ item: NativeListItem, width: CGFloat, theme: [String: Any]?, layout: String
6
+ ) -> CGFloat? {
7
+ return 244
8
+ }
9
+
10
+ private let picture = UIView()
11
+ private let image = NativeListMediaPreviewSlot()
12
+ private let network = NativeListImageSlot()
13
+ private let errorIcon = UIImageView()
14
+ private let title = NativeListTextLabel()
15
+ private let subtitle = NativeListTextLabel()
16
+ private let badge = NativeListTextLabel()
17
+ private let metadata = UIStackView()
18
+ private let subtitleLine = UIStackView()
19
+ private let close = UIButton(type: .system)
20
+ private let closeStack = UIStackView()
21
+ private let titleLine = UIStackView()
22
+ private var closeKey = ""
23
+ private var ownsImageFill = false
24
+ private var imageStyle: [String: Any] = [:]
25
+ private var imageWidth: NSLayoutConstraint!
26
+ private var imageHeight: NSLayoutConstraint!
27
+ override var defaultCornerRadius: CGFloat { 16 }
28
+ override var showsSelection: Bool { false }
29
+ override var pressChangesBackground: Bool { false }
30
+ override var assetFields: [String] { ["image", "media", "networkImage"] }
31
+ override var isHighlighted: Bool { didSet { picture.alpha = isHighlighted ? 0.8 : 1 } }
32
+
33
+ override init(frame: CGRect) {
34
+ super.init(frame: frame)
35
+ root.axis = .vertical
36
+ root.alignment = .fill
37
+ metadata.axis = .vertical
38
+ metadata.alignment = .fill
39
+ subtitleLine.axis = .horizontal
40
+ subtitleLine.alignment = .center
41
+ subtitleLine.spacing = 8
42
+ [subtitle, network.view].forEach(subtitleLine.addArrangedSubview)
43
+ titleLine.axis = .horizontal
44
+ titleLine.alignment = .center
45
+ titleLine.addArrangedSubview(title)
46
+ [subtitleLine, titleLine].forEach(metadata.addArrangedSubview)
47
+ closeStack.axis = .vertical
48
+ closeStack.alignment = .trailing
49
+ closeStack.addArrangedSubview(close)
50
+ [picture, metadata, closeStack].forEach(root.addArrangedSubview)
51
+ picture.translatesAutoresizingMaskIntoConstraints = false
52
+ [image.view, errorIcon, badge].forEach {
53
+ picture.addSubview($0)
54
+ $0.translatesAutoresizingMaskIntoConstraints = false
55
+ }
56
+ imageWidth = image.view.widthAnchor.constraint(equalToConstant: 0)
57
+ imageHeight = picture.heightAnchor.constraint(equalToConstant: 160)
58
+ NSLayoutConstraint.activate([
59
+ image.view.leadingAnchor.constraint(equalTo: picture.leadingAnchor),
60
+ image.view.topAnchor.constraint(equalTo: picture.topAnchor),
61
+ image.view.heightAnchor.constraint(equalTo: picture.heightAnchor), imageWidth, imageHeight,
62
+ errorIcon.centerXAnchor.constraint(equalTo: image.view.centerXAnchor),
63
+ errorIcon.centerYAnchor.constraint(equalTo: image.view.centerYAnchor),
64
+ errorIcon.widthAnchor.constraint(equalToConstant: 24),
65
+ errorIcon.heightAnchor.constraint(equalToConstant: 24),
66
+ badge.trailingAnchor.constraint(equalTo: image.view.trailingAnchor),
67
+ badge.bottomAnchor.constraint(equalTo: image.view.bottomAnchor),
68
+ badge.heightAnchor.constraint(equalToConstant: 24),
69
+ network.view.widthAnchor.constraint(equalToConstant: 14),
70
+ network.view.heightAnchor.constraint(equalToConstant: 14),
71
+ ])
72
+ errorIcon.contentMode = .scaleAspectFit
73
+ badge.textAlignment = .center
74
+ badge.layer.cornerRadius = 10
75
+ badge.layer.borderWidth = 2
76
+ badge.clipsToBounds = true
77
+ network.view.layer.cornerRadius = 7
78
+ network.view.clipsToBounds = true
79
+ subtitle.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
80
+ close.contentHorizontalAlignment = .trailing
81
+ close.addTarget(self, action: #selector(closePressed), for: .touchUpInside)
82
+ }
83
+ required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
84
+
85
+ override func bindContent(
86
+ _ item: NativeListItem, theme: [String: Any]?, layout: String,
87
+ checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String
88
+ ) {
89
+ let style = item.data.dictionary("style") ?? [:]
90
+ imageStyle = style.dictionary("image") ?? [:]
91
+ let hp = CGFloat(style.double("horizontalPadding", default: 10))
92
+ let vp = CGFloat(style.double("verticalPadding", default: 10))
93
+ contentInsets = UIEdgeInsets(top: vp, left: hp, bottom: vp, right: hp)
94
+ root.spacing = 8
95
+ root.setCustomSpacing(CGFloat(style.double("leadingGap", default: 8)), after: picture)
96
+ metadata.spacing = CGFloat(style.double("lineGap", default: 2))
97
+ NativeListResolvedText(
98
+ item.data.string("title"), style: style.dictionary("title"), size: 16,
99
+ weight: .medium, color: nativeListColor(theme, "primaryText", "#202020"),
100
+ lineHeight: nativeListFont(ofSize: 16, weight: .medium).lineHeight,
101
+ lines: 1, preservesBreakMode: true
102
+ ).bind(title)
103
+ NativeListResolvedText(
104
+ item.data.string("subtitle"), style: style.dictionary("subtitle"), size: 12,
105
+ color: nativeListColor(theme, "secondaryText", "#646464"),
106
+ lineHeight: nativeListFont(ofSize: 12).lineHeight, lines: 1, preservesBreakMode: true
107
+ ).bind(subtitle)
108
+ let badgeText = item.data.dictionary("badge")?.string("text") ?? ""
109
+ NativeListResolvedText(
110
+ badgeText.isEmpty ? "" : " \(badgeText) ", style: style.dictionary("badge"), size: 14,
111
+ weight: .medium, color: nativeListColor(theme, "inverseText", "#FCFCFC"),
112
+ lineHeight: nativeListFont(ofSize: 14, weight: .medium).lineHeight,
113
+ lines: 1, preservesBreakMode: true
114
+ ).bind(badge)
115
+ if style.dictionary("badge")?["alignment"] == nil { badge.textAlignment = .center }
116
+ badge.backgroundColor = nativeListColor(theme, "inverseBackground", "#202020")
117
+ badge.layer.borderColor = nativeListColor(theme, "rowBackground", "#FFFFFF").cgColor
118
+ let state = item.data.string("imageState")
119
+ let placeholder = theme?.string("strongBackground", default: "#0000000F") ?? "#0000000F"
120
+ errorIcon.isHidden = state != "error"
121
+ errorIcon.image = nativeListIcon(named: "ImageSquareWavesOutline")
122
+ errorIcon.tintColor = UIColor(nativeListHex: "#00000044", fallback: .lightGray)
123
+ let media = item.data.dictionary("media")
124
+ if state != "empty", state != "error", let source = media?.dictionary("source") ?? item.data.dictionary("image") {
125
+ // Legacy source-backed media had a transparent container. Clear only a fill this
126
+ // renderer applied, so OneKeyImage keeps ownership of its own placeholder color.
127
+ if ownsImageFill {
128
+ image.view.backgroundColor = .clear
129
+ ownsImageFill = false
130
+ }
131
+ image.bind(
132
+ source, key: "\(item.key):media", fit: imageStyle["contentFit"] as? String,
133
+ placeholder: placeholder, probeOrder: media?["probeOrder"] as? [String] ?? ["image"]
134
+ ) { [weak self] loaded in
135
+ self?.errorIcon.isHidden = loaded
136
+ self?.ownsImageFill = !loaded
137
+ self?.image.view.backgroundColor = loaded ? .clear : UIColor(nativeListHex: placeholder, fallback: .lightGray)
138
+ }
139
+ } else {
140
+ image.recycle()
141
+ image.view.backgroundColor =
142
+ state == "error" ? nativeListColor(theme, "strongBackground", "#0000000F") : .clear
143
+ ownsImageFill = true
144
+ }
145
+ if let source = item.data.dictionary("networkImage") {
146
+ // Legacy hid the network badge until it loaded, so it takes no layout space before.
147
+ network.view.isHidden = true
148
+ network.view.alpha = 1
149
+ network.bind(
150
+ source, key: "\(item.key):network", variant: "network", placeholder: placeholder
151
+ ) { [weak self] loaded in
152
+ self?.network.view.isHidden = !loaded
153
+ }
154
+ } else {
155
+ network.recycle()
156
+ network.view.isHidden = true
157
+ }
158
+ closeKey = item.data.string("closeActionKey")
159
+ closeStack.isHidden = closeKey.isEmpty
160
+ close.setTitle("×", for: .normal)
161
+ close.titleLabel?.font = nativeListFont(ofSize: 16, weight: .medium)
162
+ close.setTitleColor(nativeListColor(theme, "primaryText", "#202020"), for: .normal)
163
+ }
164
+ override func layoutSubviews() {
165
+ let width = CGFloat(
166
+ imageStyle.double(
167
+ "width",
168
+ default: Double(max(0, contentView.bounds.width - contentInsets.left - contentInsets.right))
169
+ ))
170
+ imageWidth.constant = width
171
+ imageHeight.constant = CGFloat(imageStyle.double("height", default: Double(width)))
172
+ let shape = imageStyle.string("shape", default: "rounded")
173
+ image.view.layer.cornerRadius = CGFloat(
174
+ imageStyle.double(
175
+ "cornerRadius",
176
+ default: shape == "circle"
177
+ ? Double(min(width, imageHeight.constant) / 2) : shape == "square" ? 0 : 10))
178
+ image.view.clipsToBounds = true
179
+ super.layoutSubviews()
180
+ }
181
+ @objc private func closePressed() {
182
+ emitAction(closeKey, from: close, source: "mediaClose", slot: 0)
183
+ }
184
+ override func recycleContent() {
185
+ image.recycle()
186
+ network.recycle()
187
+ closeKey = ""
188
+ picture.alpha = 1
189
+ }
190
+ }
@@ -0,0 +1,168 @@
1
+ import UIKit
2
+
3
+ enum NativeListMessageRenderer {
4
+ struct Resolved {
5
+ let title: NativeListResolvedText
6
+ let body: NativeListResolvedText
7
+ let time: NativeListResolvedText
8
+ let horizontalPadding: CGFloat
9
+ let verticalPadding: CGFloat
10
+ let gap: CGFloat
11
+ let timeInset: CGFloat
12
+ let leadingGap: CGFloat
13
+ let imageWidth: CGFloat
14
+ let imageHeight: CGFloat
15
+ let imageStyle: [String: Any]
16
+ let leading: [String: Any]?
17
+ let thumbnail: [String: Any]?
18
+ let unread: Bool
19
+ let alignment: UIStackView.Alignment
20
+
21
+ init(_ item: NativeListItem, theme: [String: Any]?, layout: String) {
22
+ let style = item.data.dictionary("style") ?? [:]
23
+ title = NativeListResolvedText(
24
+ item.data.string("title"), style: style.dictionary("title"), size: 14, weight: .semibold,
25
+ color: nativeListColor(theme, "primaryText", "#202020"), lineHeight: 20, lines: 2)
26
+ body = NativeListResolvedText(
27
+ item.data.string("body"), style: style.dictionary("body"), size: 14,
28
+ color: nativeListColor(theme, "secondaryText", "#646464"), lineHeight: 20,
29
+ lines: item.data.int("bodyLines", default: 3))
30
+ time = NativeListResolvedText(
31
+ item.data.string("time"), style: style.dictionary("time"), size: 12,
32
+ color: nativeListColor(theme, "disabledText", "#8D8D8D"), lineHeight: 16, lines: 1,
33
+ breakMode: .byWordWrapping)
34
+ horizontalPadding = CGFloat(
35
+ style.double("horizontalPadding", default: layout == "table" ? 16 : 12))
36
+ verticalPadding = CGFloat(style.double("verticalPadding", default: 16))
37
+ gap = CGFloat(style.double("lineGap", default: 2))
38
+ timeInset = style["lineGap"] == nil ? 2 : 0
39
+ leadingGap = CGFloat(style.double("leadingGap", default: 12))
40
+ imageStyle = style.dictionary("image") ?? [:]
41
+ imageWidth = CGFloat(imageStyle.double("width", default: 28))
42
+ imageHeight = CGFloat(imageStyle.double("height", default: 28))
43
+ leading = item.data.dictionary("leading")
44
+ thumbnail = item.data.dictionary("thumbnail")
45
+ unread = item.data.bool("unread")
46
+ switch style.dictionary("container")?.string("contentVerticalAlignment") {
47
+ case "center": alignment = .center
48
+ case "bottom": alignment = .bottom
49
+ default: alignment = .top
50
+ }
51
+ }
52
+
53
+ func measure(width: CGFloat) -> CGFloat {
54
+ let textWidth = max(
55
+ 1,
56
+ width - horizontalPadding * 2 - (leading == nil ? 0 : imageWidth + leadingGap)
57
+ - (thumbnail == nil ? 0 : 76))
58
+ let fields = [title, body, time].filter { !$0.text.isEmpty }
59
+ let textHeight =
60
+ fields.reduce(CGFloat(0)) { $0 + $1.measure(width: textWidth) }
61
+ + CGFloat(max(0, fields.count - 1)) * gap + (time.text.isEmpty ? 0 : timeInset)
62
+ return verticalPadding * 2
63
+ + max(textHeight, leading == nil ? 0 : imageHeight, thumbnail == nil ? 0 : 64)
64
+ }
65
+ }
66
+
67
+ final class Views {
68
+ let root: UIStackView
69
+ let column = UIStackView()
70
+ let title = NativeListTextLabel()
71
+ let body = NativeListTextLabel()
72
+ let time = NativeListInsetLabel()
73
+ private var leading: NativeListLeadingVisual?
74
+ private var thumbnail: NativeListImageSlot?
75
+ private var sizeConstraints: [NSLayoutConstraint] = []
76
+
77
+ init(root: UIStackView = UIStackView()) {
78
+ self.root = root
79
+ root.axis = .horizontal
80
+ root.spacing = 12
81
+ column.axis = .vertical
82
+ column.alignment = .fill
83
+ column.setContentHuggingPriority(UILayoutPriority(1), for: .horizontal)
84
+ column.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
85
+ title.setContentCompressionResistancePriority(.required, for: .horizontal)
86
+ [title, body, time].forEach(column.addArrangedSubview)
87
+ root.addArrangedSubview(column)
88
+ }
89
+
90
+ func bind(_ resolved: Resolved, item: NativeListItem, theme: [String: Any]?) {
91
+ resolved.title.bind(title)
92
+ resolved.body.bind(body)
93
+ resolved.time.bind(time)
94
+ time.topInset = resolved.timeInset
95
+ time.invalidateIntrinsicContentSize()
96
+ column.spacing = resolved.gap
97
+ root.alignment = resolved.alignment
98
+ NSLayoutConstraint.deactivate(sizeConstraints)
99
+ sizeConstraints.removeAll(keepingCapacity: true)
100
+ if let data = resolved.leading {
101
+ let visual = leading ?? NativeListLeadingVisual(frame: .zero)
102
+ leading = visual
103
+ if visual.superview == nil { root.insertArrangedSubview(visual, at: 0) }
104
+ visual.bind(
105
+ data, style: resolved.imageStyle, key: item.key, theme: theme, isUnread: resolved.unread)
106
+ root.setCustomSpacing(resolved.leadingGap, after: visual)
107
+ sizeConstraints += [
108
+ visual.widthAnchor.constraint(equalToConstant: resolved.imageWidth),
109
+ visual.heightAnchor.constraint(equalToConstant: resolved.imageHeight),
110
+ ]
111
+ } else if let leading {
112
+ leading.recycle()
113
+ root.removeArrangedSubview(leading)
114
+ leading.removeFromSuperview()
115
+ }
116
+ if let source = resolved.thumbnail {
117
+ let slot = thumbnail ?? NativeListImageSlot()
118
+ thumbnail = slot
119
+ if slot.view.superview == nil { root.addArrangedSubview(slot.view) }
120
+ slot.view.layer.cornerRadius = 6
121
+ slot.view.layer.borderWidth = 1 / UIScreen.main.scale
122
+ slot.view.layer.borderColor = UIColor(nativeListHex: "#0000000F", fallback: .clear).cgColor
123
+ sizeConstraints += [
124
+ slot.view.widthAnchor.constraint(equalToConstant: 64),
125
+ slot.view.heightAnchor.constraint(equalToConstant: 64),
126
+ ]
127
+ slot.bind(
128
+ source, key: "\(item.key):thumbnail",
129
+ placeholder: theme?.string("strongBackground", default: "#0000000F") ?? "#0000000F")
130
+ } else if let thumbnail {
131
+ thumbnail.recycle()
132
+ root.removeArrangedSubview(thumbnail.view)
133
+ thumbnail.view.removeFromSuperview()
134
+ }
135
+ NSLayoutConstraint.activate(sizeConstraints)
136
+ }
137
+ func recycle() {
138
+ leading?.recycle()
139
+ thumbnail?.recycle()
140
+ }
141
+ }
142
+
143
+ }
144
+
145
+ final class NativeListMessageCell: NativeListRendererCell {
146
+ override class func measure(
147
+ _ item: NativeListItem, width: CGFloat, theme: [String: Any]?, layout: String
148
+ ) -> CGFloat? {
149
+ return NativeListMessageRenderer.Resolved(item, theme: theme, layout: layout).measure(
150
+ width: width)
151
+ }
152
+
153
+ override var assetFields: [String] { ["leading", "thumbnail"] }
154
+ private lazy var views = NativeListMessageRenderer.Views(root: root)
155
+ override init(frame: CGRect) { super.init(frame: frame) }
156
+ required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
157
+ override func bindContent(
158
+ _ item: NativeListItem, theme: [String: Any]?, layout: String,
159
+ checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String
160
+ ) {
161
+ let resolved = NativeListMessageRenderer.Resolved(item, theme: theme, layout: layout)
162
+ views.bind(resolved, item: item, theme: theme)
163
+ contentInsets = UIEdgeInsets(
164
+ top: resolved.verticalPadding, left: resolved.horizontalPadding,
165
+ bottom: resolved.verticalPadding, right: resolved.horizontalPadding)
166
+ }
167
+ override func recycleContent() { views.recycle() }
168
+ }