@onekeyfe/react-native-native-list 3.0.158 → 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,268 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ import android.graphics.Bitmap
4
+ import android.graphics.Rect
5
+ import android.view.ViewTreeObserver
6
+ import android.view.Gravity
7
+ import android.view.TextureView
8
+ import android.view.View
9
+ import android.widget.FrameLayout
10
+ import android.widget.ImageView
11
+ import androidx.annotation.OptIn
12
+ import androidx.media3.common.MediaItem
13
+ import androidx.media3.common.PlaybackException
14
+ import androidx.media3.common.Player
15
+ import androidx.media3.common.VideoSize
16
+ import androidx.media3.common.util.UnstableApi
17
+ import androidx.media3.common.util.Util
18
+ import androidx.media3.datasource.DefaultDataSource
19
+ import androidx.media3.datasource.okhttp.OkHttpDataSource
20
+ import androidx.media3.exoplayer.ExoPlayer
21
+ import androidx.media3.exoplayer.DefaultLoadControl
22
+ import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
23
+ import androidx.media3.ui.AspectRatioFrameLayout
24
+ import com.facebook.react.modules.network.ForwardingCookieHandler
25
+ import com.facebook.react.modules.network.OkHttpClientProvider
26
+ import com.facebook.react.uimanager.ThemedReactContext
27
+ import okhttp3.JavaNetCookieJar
28
+ import org.json.JSONObject
29
+
30
+ /**
31
+ * Uses Media3 like native Video, but never starts playback or requests audio focus. A player
32
+ * lives only until its first frame is captured; the tile then shows that still frame.
33
+ */
34
+ @OptIn(UnstableApi::class)
35
+ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContext) {
36
+ val view: FrameLayout = object : FrameLayout(context) {
37
+ override fun onAttachedToWindow() { super.onAttachedToWindow(); observeViewport(); updateVisibility() }
38
+ override fun onDetachedFromWindow() { stopObservingViewport(); releasePlayer(); super.onDetachedFromWindow() }
39
+ override fun onWindowVisibilityChanged(visibility: Int) { super.onWindowVisibilityChanged(visibility); updateVisibility() }
40
+ override fun onVisibilityAggregated(isVisible: Boolean) { super.onVisibilityAggregated(isVisible); if (isVisible) updateVisibility() else releasePlayer() }
41
+ }
42
+ private val image = NativeListImageSlot(context)
43
+ private val video = AspectRatioFrameLayout(context)
44
+ private val texture = TextureView(context)
45
+ private val frame = ImageView(context)
46
+ private var identity: String? = null
47
+ private var epoch = 0
48
+ private var candidateGeneration = 0
49
+ private var playerGeneration = 0
50
+ private var result: Boolean? = null
51
+ private var completion: ((Boolean) -> Unit)? = null
52
+ private var pendingCompletion: Runnable? = null
53
+ private var player: ExoPlayer? = null
54
+ private var resumeVideo: (() -> Unit)? = null
55
+ private var viewportObserver: ViewTreeObserver? = null
56
+ private val visibleRect = Rect()
57
+ private val scrollListener = ViewTreeObserver.OnScrollChangedListener { updateVisibility() }
58
+ private val layoutListener = ViewTreeObserver.OnGlobalLayoutListener { updateVisibility() }
59
+ // Native property animations can move a retained pager page without a layout pass.
60
+ private val drawListener = ViewTreeObserver.OnPreDrawListener { updateVisibility(); true }
61
+
62
+ init {
63
+ view.isClickable = false
64
+ view.isFocusable = false
65
+ view.descendantFocusability = android.view.ViewGroup.FOCUS_BLOCK_DESCENDANTS
66
+ video.addView(texture, FrameLayout.LayoutParams(-1, -1))
67
+ frame.scaleType = ImageView.ScaleType.FIT_XY
68
+ frame.visibility = View.GONE
69
+ video.addView(frame, FrameLayout.LayoutParams(-1, -1))
70
+ view.addView(image.view, FrameLayout.LayoutParams(-1, -1))
71
+ view.addView(video, FrameLayout.LayoutParams(-1, -1, Gravity.CENTER))
72
+ video.visibility = View.GONE
73
+ video.setBackgroundColor(android.graphics.Color.BLACK)
74
+ texture.isOpaque = false
75
+ }
76
+
77
+ fun bind(source: JSONObject, key: String, fit: String? = null,
78
+ placeholder: String = "#0000000F", probeOrder: List<String> = listOf("image"),
79
+ completion: ((Boolean) -> Unit)? = null) {
80
+ val next = nativeListCanonical(JSONObject().put("source", source).put("key", key)
81
+ .put("fit", fit ?: "").put("order", org.json.JSONArray(probeOrder)).put("placeholder", placeholder))
82
+ this.completion = completion
83
+ if (identity == next) {
84
+ result?.let { completion?.invoke(it) }
85
+ updateVisibility()
86
+ return
87
+ }
88
+ recycle()
89
+ identity = next
90
+ this.completion = completion
91
+ video.resizeMode = if ((fit ?: source.optString("contentFit")) == "cover") AspectRatioFrameLayout.RESIZE_MODE_ZOOM else AspectRatioFrameLayout.RESIZE_MODE_FIT
92
+ probe(source, key, fit, placeholder, probeOrder, 0, epoch)
93
+ }
94
+
95
+ private fun probe(source: JSONObject, key: String, fit: String?, placeholder: String,
96
+ order: List<String>, attempt: Int, token: Int) {
97
+ if (token != epoch) return
98
+ candidateGeneration++
99
+ val candidate = candidateGeneration
100
+ resumeVideo = null
101
+ stopObservingViewport()
102
+ releasePlayer()
103
+ if (attempt >= order.size) {
104
+ result = false
105
+ completion?.invoke(false)
106
+ return
107
+ }
108
+ fun finished(loaded: Boolean, expectedPlayerGeneration: Int? = null) {
109
+ if (epoch != token || candidateGeneration != candidate) return
110
+ pendingCompletion?.let(view::removeCallbacks)
111
+ // Glide forbids clearing/rebinding a request from either terminal callback.
112
+ // Media3 callbacks also defer release so the emitting player is not reentered.
113
+ val task = Runnable {
114
+ pendingCompletion = null
115
+ if (epoch != token || candidateGeneration != candidate ||
116
+ (expectedPlayerGeneration != null && playerGeneration != expectedPlayerGeneration)) return@Runnable
117
+ if (loaded) {
118
+ if (result != true) { result = true; completion?.invoke(true) }
119
+ } else probe(source, key, fit, placeholder, order, attempt + 1, token)
120
+ }
121
+ pendingCompletion = task
122
+ view.post(task)
123
+ }
124
+ if (order[attempt] == "image") {
125
+ video.visibility = View.GONE
126
+ image.view.visibility = View.VISIBLE
127
+ image.bind(source, key, fit = fit, placeholder = placeholder, completion = { loaded -> finished(loaded) })
128
+ return
129
+ }
130
+ image.recycle()
131
+ image.view.visibility = View.GONE
132
+ video.visibility = View.VISIBLE
133
+ val uri = source.optString("uri")
134
+ if (uri.isBlank()) { finished(false); return }
135
+ val headers = source.optJSONObject("headers")?.let { values -> values.keys().asSequence().associateWith { values.optString(it) } } ?: emptyMap()
136
+ resumeVideo = start@{
137
+ if (epoch != token || candidateGeneration != candidate || player != null) return@start
138
+ playerGeneration++
139
+ val generation = playerGeneration
140
+ try {
141
+ val client = OkHttpClientProvider.getOkHttpClient().newBuilder()
142
+ .cookieJar(JavaNetCookieJar(ForwardingCookieHandler(context))).build()
143
+ val http = OkHttpDataSource.Factory(client).setDefaultRequestProperties(headers)
144
+ if (headers.keys.none { it.equals("User-Agent", ignoreCase = true) }) http.setUserAgent(Util.getUserAgent(context, context.packageName))
145
+ val sources = DefaultMediaSourceFactory(DefaultDataSource.Factory(context, http))
146
+ val buffers = DefaultLoadControl.Builder()
147
+ .setBufferDurationsMs(250, 1000, 250, 250)
148
+ .setTargetBufferBytes(2 * 1024 * 1024)
149
+ .setPrioritizeTimeOverSizeThresholds(false)
150
+ .setBackBuffer(0, false).build()
151
+ val next = ExoPlayer.Builder(context.applicationContext)
152
+ .setMediaSourceFactory(sources).setLoadControl(buffers).build()
153
+ player = next
154
+ next.volume = 0f
155
+ next.playWhenReady = false
156
+ next.setAudioAttributes(androidx.media3.common.AudioAttributes.DEFAULT, false)
157
+ next.setVideoTextureView(texture)
158
+ next.addListener(object : Player.Listener {
159
+ override fun onRenderedFirstFrame() {
160
+ if (playerGeneration != generation) return
161
+ finished(true, generation)
162
+ captureFirstFrame(token, candidate, generation, FIRST_FRAME_CAPTURE_ATTEMPTS)
163
+ }
164
+ override fun onPlayerError(error: PlaybackException) { if (playerGeneration == generation) finished(false, generation) }
165
+ override fun onVideoSizeChanged(size: VideoSize) {
166
+ if (playerGeneration == generation && size.height > 0) video.setAspectRatio(size.width * size.pixelWidthHeightRatio / size.height)
167
+ }
168
+ })
169
+ next.setMediaItem(MediaItem.fromUri(uri))
170
+ next.prepare()
171
+ next.pause()
172
+ } catch (_: Exception) {
173
+ releasePlayer()
174
+ finished(false)
175
+ }
176
+ }
177
+ observeViewport()
178
+ updateVisibility()
179
+ }
180
+
181
+ /**
182
+ * The rendered frame reaches the TextureView asynchronously, so a transparent copy is
183
+ * retried on later frames. If no frame can be copied the paused player stays attached.
184
+ */
185
+ private fun captureFirstFrame(token: Int, candidate: Int, generation: Int, attemptsLeft: Int) {
186
+ view.postOnAnimation {
187
+ if (epoch != token || candidateGeneration != candidate || playerGeneration != generation) return@postOnAnimation
188
+ // Releasing first would make the queued success completion drop itself as stale.
189
+ if (result != true) {
190
+ captureFirstFrame(token, candidate, generation, attemptsLeft)
191
+ return@postOnAnimation
192
+ }
193
+ val still = if (texture.isAvailable && texture.width > 0 && texture.height > 0) {
194
+ runCatching { texture.getBitmap(texture.width, texture.height) }.getOrNull()
195
+ } else null
196
+ if (still == null || !hasVisiblePixels(still)) {
197
+ still?.recycle()
198
+ if (attemptsLeft > 1) captureFirstFrame(token, candidate, generation, attemptsLeft - 1)
199
+ return@postOnAnimation
200
+ }
201
+ frame.setImageBitmap(still)
202
+ frame.visibility = View.VISIBLE
203
+ resumeVideo = null
204
+ stopObservingViewport()
205
+ releasePlayer()
206
+ }
207
+ }
208
+
209
+ private fun hasVisiblePixels(bitmap: Bitmap): Boolean {
210
+ val columns = intArrayOf(bitmap.width / 4, bitmap.width / 2, bitmap.width * 3 / 4)
211
+ val rows = intArrayOf(bitmap.height / 4, bitmap.height / 2, bitmap.height * 3 / 4)
212
+ return columns.any { x -> rows.any { y -> (bitmap.getPixel(x, y) ushr 24) != 0 } }
213
+ }
214
+
215
+ private fun observeViewport() {
216
+ if (viewportObserver != null || resumeVideo == null || !view.isAttachedToWindow) return
217
+ view.viewTreeObserver.takeIf { it.isAlive }?.let {
218
+ it.addOnScrollChangedListener(scrollListener)
219
+ it.addOnGlobalLayoutListener(layoutListener)
220
+ it.addOnPreDrawListener(drawListener)
221
+ viewportObserver = it
222
+ }
223
+ }
224
+ private fun stopObservingViewport() {
225
+ viewportObserver?.takeIf { it.isAlive }?.let {
226
+ it.removeOnScrollChangedListener(scrollListener)
227
+ it.removeOnGlobalLayoutListener(layoutListener)
228
+ it.removeOnPreDrawListener(drawListener)
229
+ }
230
+ viewportObserver = null
231
+ }
232
+ private fun updateVisibility() {
233
+ var ancestor: View? = view
234
+ var opaque = true
235
+ while (ancestor != null) {
236
+ if (ancestor.alpha <= 0.01f) { opaque = false; break }
237
+ ancestor = ancestor.parent as? View
238
+ }
239
+ if (opaque && view.isAttachedToWindow && view.windowVisibility == View.VISIBLE && view.isShown &&
240
+ view.getGlobalVisibleRect(visibleRect) && !visibleRect.isEmpty) resumeVideo?.invoke()
241
+ else releasePlayer()
242
+ }
243
+ private fun releasePlayer() {
244
+ playerGeneration++
245
+ player?.let { it.pause(); it.clearVideoTextureView(texture); it.release() }
246
+ player = null
247
+ }
248
+ fun recycle() {
249
+ epoch++; candidateGeneration++
250
+ pendingCompletion?.let(view::removeCallbacks)
251
+ pendingCompletion = null
252
+ resumeVideo = null
253
+ stopObservingViewport()
254
+ releasePlayer()
255
+ image.recycle()
256
+ image.view.visibility = View.VISIBLE
257
+ video.visibility = View.GONE
258
+ video.setAspectRatio(0f)
259
+ frame.setImageDrawable(null)
260
+ frame.visibility = View.GONE
261
+ identity = null; result = null; completion = null
262
+ }
263
+
264
+ private companion object {
265
+ const val FIRST_FRAME_CAPTURE_ATTEMPTS = 6
266
+ }
267
+ fun dispose() { recycle(); image.dispose() }
268
+ }
@@ -0,0 +1,243 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ import android.graphics.Color
4
+ import android.graphics.Outline
5
+ import android.graphics.drawable.GradientDrawable
6
+ import android.view.Gravity
7
+ import android.view.View
8
+ import android.view.ViewOutlineProvider
9
+ import android.widget.FrameLayout
10
+ import android.widget.LinearLayout
11
+ import com.facebook.react.uimanager.ThemedReactContext
12
+ import org.json.JSONObject
13
+
14
+ internal class NativeListMediaTileRowView(context: ThemedReactContext) :
15
+ NativeListRendererRowView(context) {
16
+ private val picture = FrameLayout(context)
17
+ private val image = NativeListMediaPreviewSlot(context)
18
+ private val network = NativeListImageSlot(context)
19
+ private val errorIcon = OneKeyIconView(context)
20
+ private val title = NativeListTextView(context)
21
+ private val subtitle = NativeListTextView(context)
22
+ private val badge = NativeListTextView(context)
23
+ private val metadata = LinearLayout(context).apply { orientation = VERTICAL }
24
+ private val subtitleLine =
25
+ LinearLayout(context).apply {
26
+ orientation = HORIZONTAL
27
+ gravity = Gravity.CENTER_VERTICAL
28
+ }
29
+ private val close = NativeListTextView(context)
30
+ private var imageStyle = JSONObject()
31
+ private var closeKey = ""
32
+ override val defaultCornerRadius = 16
33
+ override val showsSelection = false
34
+ override val pressChangesBackground = false
35
+ override val assetFields = listOf("image", "media", "networkImage")
36
+
37
+ override fun horizontalWidth(item: NativeListItem) = dp(200)
38
+
39
+ override fun pressContent(pressed: Boolean) {
40
+ picture.alpha = if (pressed) 0.8f else 1f
41
+ }
42
+
43
+ init {
44
+ orientation = VERTICAL
45
+ picture.addView(image.view, FrameLayout.LayoutParams(-1, -1))
46
+ picture.addView(errorIcon)
47
+ picture.addView(badge)
48
+ subtitleLine.addView(subtitle, LayoutParams(0, -2, 1f))
49
+ subtitleLine.addView(network.view)
50
+ metadata.addView(subtitleLine, LayoutParams(-1, -2))
51
+ metadata.addView(title, LayoutParams(-1, -2))
52
+ addView(picture)
53
+ addView(metadata, LayoutParams(-1, -2))
54
+ addView(close, LayoutParams(-1, -2))
55
+ close.setOnClickListener { emitAction(closeKey, close, "mediaClose") }
56
+ close.gravity = Gravity.END
57
+ }
58
+
59
+ override fun bindContent(
60
+ item: NativeListItem,
61
+ theme: JSONObject?,
62
+ layout: String,
63
+ checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String,
64
+ ) {
65
+ val style = item.json.optJSONObject("style") ?: JSONObject()
66
+ imageStyle = style.optJSONObject("image") ?: JSONObject()
67
+ fun size(name: String, fallback: Int) =
68
+ if (style.has(name)) stylePx(style.optDouble(name)) else dp(fallback)
69
+ fun tint(name: String, fallback: String) =
70
+ parseNativeListColor(theme?.optString(name, fallback) ?: fallback)
71
+ setPadding(
72
+ size("horizontalPadding", 10),
73
+ size("verticalPadding", 10),
74
+ size("horizontalPadding", 10),
75
+ size("verticalPadding", 10),
76
+ )
77
+ gravity =
78
+ Gravity.START or
79
+ when (style.optJSONObject("container")?.optString("contentVerticalAlignment")) {
80
+ "bottom" -> Gravity.BOTTOM
81
+ "center" -> Gravity.CENTER_VERTICAL
82
+ else -> Gravity.TOP
83
+ }
84
+ NativeListResolvedText.resolve(
85
+ reactContext,
86
+ item.json.optString("title"),
87
+ style.optJSONObject("title"),
88
+ 16f,
89
+ "medium",
90
+ tint("primaryText", "#000000DF"),
91
+ 24,
92
+ 1,
93
+ sourceScale,
94
+ )
95
+ .bind(title)
96
+ NativeListResolvedText.resolve(
97
+ reactContext,
98
+ item.json.optString("subtitle"),
99
+ style.optJSONObject("subtitle"),
100
+ 12f,
101
+ "regular",
102
+ tint("secondaryText", "#0000009B"),
103
+ 16,
104
+ 1,
105
+ sourceScale,
106
+ )
107
+ .bind(subtitle)
108
+ NativeListResolvedText.resolve(
109
+ reactContext,
110
+ item.json.optJSONObject("badge")?.optString("text") ?: "",
111
+ style.optJSONObject("badge"),
112
+ 14f,
113
+ "regular",
114
+ parseNativeListColor("#FCFCFC"),
115
+ 0,
116
+ 1,
117
+ sourceScale,
118
+ )
119
+ .bind(badge)
120
+ if (style.optJSONObject("badge") == null) {
121
+ // Legacy badge text keeps the platform font padding and proportional digits.
122
+ badge.includeFontPadding = true
123
+ badge.fontFeatureSettings = null
124
+ }
125
+ if (!style.optJSONObject("badge").let { it?.has("alignment") == true })
126
+ badge.gravity = Gravity.CENTER
127
+ badge.setPadding(dp(8), 0, dp(8), 0)
128
+ badge.background =
129
+ GradientDrawable().apply {
130
+ setColor(parseNativeListColor("#000000DF"))
131
+ cornerRadius = dp(10).toFloat()
132
+ setStroke(dp(2), Color.WHITE)
133
+ }
134
+ badge.layoutParams = FrameLayout.LayoutParams(-2, dp(24), Gravity.END or Gravity.BOTTOM)
135
+ title.layoutParams = LayoutParams(-1, -2).apply { topMargin = size("lineGap", 0) }
136
+ metadata.layoutParams = LayoutParams(-1, -2).apply { topMargin = size("leadingGap", 8) }
137
+ (subtitle.layoutParams as LayoutParams).marginEnd = dp(8)
138
+ val state = item.json.optString("imageState")
139
+ errorIcon.visibility = if (state == "error") VISIBLE else GONE
140
+ errorIcon.iconName = "ImageSquareWavesOutline"
141
+ errorIcon.tintColor = parseNativeListColor("#00000044")
142
+ errorIcon.useSourceScale = sourceScale
143
+ errorIcon.layoutParams = FrameLayout.LayoutParams(dp(24), dp(24), Gravity.CENTER)
144
+ val media = item.json.optJSONObject("media")
145
+ val source = media?.optJSONObject("source") ?: item.json.optJSONObject("image")
146
+ val order = media?.optJSONArray("probeOrder")?.let { values ->
147
+ (0 until values.length()).map { values.optString(it) }
148
+ } ?: listOf("image")
149
+ if (state != "empty" && state != "error" && source != null)
150
+ image.bind(
151
+ source,
152
+ "${item.key}:media",
153
+ fit = imageStyle.optString("contentFit").takeIf { it.isNotEmpty() },
154
+ placeholder =
155
+ theme?.optString("strongBackground", "#0000000F")?.takeIf(String::isNotEmpty)
156
+ ?: "#0000000F",
157
+ probeOrder = order,
158
+ ) { loaded ->
159
+ errorIcon.visibility = if (loaded) GONE else VISIBLE
160
+ if (!loaded) image.view.setBackgroundColor(tint("strongBackground", "#0000000F"))
161
+ }
162
+ else image.recycle()
163
+ // Legacy: only the empty/error states paint a slot fill; loaded images show
164
+ // their own placeholder without a white backing.
165
+ image.view.background =
166
+ when (state) {
167
+ "error" -> GradientDrawable().apply { setColor(tint("strongBackground", "#0000000F")) }
168
+ "empty" -> GradientDrawable().apply { setColor(Color.WHITE) }
169
+ else -> if (errorIcon.visibility == VISIBLE) GradientDrawable().apply { setColor(tint("strongBackground", "#0000000F")) } else null
170
+ }
171
+ image.view.outlineProvider =
172
+ object : ViewOutlineProvider() {
173
+ override fun getOutline(view: View, outline: Outline) {
174
+ if (
175
+ imageStyle.optString("shape") == "circle" && !imageStyle.has("cornerRadius")
176
+ )
177
+ outline.setOval(0, 0, view.width, view.height)
178
+ else
179
+ outline.setRoundRect(
180
+ 0,
181
+ 0,
182
+ view.width,
183
+ view.height,
184
+ if (imageStyle.has("cornerRadius"))
185
+ stylePx(imageStyle.optDouble("cornerRadius")).toFloat()
186
+ else if (imageStyle.optString("shape") == "square") 0f
187
+ else dp(10).toFloat(),
188
+ )
189
+ }
190
+ }
191
+ image.view.clipToOutline = true
192
+ network.view.layoutParams = LayoutParams(dp(14), dp(14))
193
+ network.view.outlineProvider =
194
+ object : ViewOutlineProvider() {
195
+ override fun getOutline(view: View, outline: Outline) {
196
+ outline.setOval(0, 0, view.width, view.height)
197
+ }
198
+ }
199
+ network.view.clipToOutline = true
200
+ item.json.optJSONObject("networkImage")?.let { source ->
201
+ network.view.visibility = VISIBLE
202
+ network.view.alpha = 0f
203
+ network.bind(source, "${item.key}:network", variant = "network", round = true) { loaded
204
+ ->
205
+ network.view.alpha = if (loaded) 1f else 0f
206
+ }
207
+ }
208
+ ?: run {
209
+ network.recycle()
210
+ network.view.visibility = GONE
211
+ }
212
+ closeKey = item.json.optString("closeActionKey")
213
+ close.text = "×"
214
+ close.textSize = if (sourceScale) 22f else NativeListScale.font(resources, 22f)
215
+ close.setTextColor(tint("primaryText", "#000000DF"))
216
+ close.visibility = if (closeKey.isEmpty()) GONE else VISIBLE
217
+ }
218
+
219
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
220
+ val width =
221
+ if (imageStyle.has("width")) stylePx(imageStyle.optDouble("width"))
222
+ else
223
+ (MeasureSpec.getSize(widthMeasureSpec) - paddingLeft - paddingRight).coerceAtLeast(
224
+ 0
225
+ )
226
+ val height =
227
+ if (imageStyle.has("height")) stylePx(imageStyle.optDouble("height")) else width
228
+ picture.layoutParams = LayoutParams(width, height)
229
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec)
230
+ }
231
+
232
+ override fun recycleContent() {
233
+ image.recycle()
234
+ network.recycle()
235
+ closeKey = ""
236
+ picture.alpha = 1f
237
+ }
238
+
239
+ override fun disposeContent() {
240
+ image.dispose()
241
+ network.dispose()
242
+ }
243
+ }