@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
@@ -13,6 +13,9 @@ import java.util.WeakHashMap
13
13
  import kotlin.math.ceil
14
14
  import kotlin.math.roundToInt
15
15
 
16
+ internal class NativeListViewHolder(val rowView: NativeListRowHost) :
17
+ androidx.recyclerview.widget.RecyclerView.ViewHolder(rowView)
18
+
16
19
  internal class NativeListAdapter(
17
20
  private val context: ThemedReactContext,
18
21
  ) : RecyclerView.Adapter<NativeListViewHolder>() {
@@ -44,7 +47,7 @@ internal class NativeListAdapter(
44
47
  private var marketSourceItems: List<NativeListItem>? = null
45
48
  private var marketSourceEdges = DoubleArray(0)
46
49
  private val createdRows = Collections.newSetFromMap(
47
- WeakHashMap<NativeListRowView, Boolean>(),
50
+ WeakHashMap<NativeListRowHost, Boolean>(),
48
51
  )
49
52
  var usesSelectorSourceScale = false
50
53
  var theme: JSONObject? = null
@@ -52,18 +55,28 @@ internal class NativeListAdapter(
52
55
  if (field?.toString() != value?.toString()) needsThemeRebind = true
53
56
  field = value
54
57
  }
58
+ // Reuses the theme rebind path: chrome lives outside the row payload, so a
59
+ // changed list style would otherwise not reach rows DiffUtil considers equal.
60
+ var listStyle: JSONObject? = null
61
+ set(value) {
62
+ if (field?.toString() != value?.toString()) needsThemeRebind = true
63
+ field = value
64
+ }
55
65
  var layout: String = "linear"
56
66
  var orientation: String = "vertical"
57
67
  var selectedKeys: Set<String> = emptySet()
58
68
  var checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String = { _, _, fallback -> fallback }
59
69
  var onRowPress: ((NativeListItem, NativeListActionOrigin) -> Unit)? = null
60
70
  var onAction: ((NativeListItem, String, NativeSelectionTarget?, NativeListActionOrigin?) -> Unit)? = null
61
- var onBindingInvalidated: ((NativeListRowView, Long) -> Unit)? = null
71
+ var onBindingInvalidated: ((NativeListRowHost, Long) -> Unit)? = null
62
72
 
63
73
  override fun getItemCount(): Int = reorderItems?.size ?: differ.currentList.size
64
74
 
75
+ override fun getItemViewType(position: Int): Int =
76
+ requireNotNull(itemAt(position)).rendererKey.ordinal
77
+
65
78
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NativeListViewHolder {
66
- val view = NativeListRowView(context)
79
+ val view = NativeListRendererRegistry.create(context, viewType)
67
80
  view.layoutParams = ViewGroup.LayoutParams(
68
81
  ViewGroup.LayoutParams.MATCH_PARENT,
69
82
  ViewGroup.LayoutParams.WRAP_CONTENT,
@@ -79,6 +92,7 @@ internal class NativeListAdapter(
79
92
 
80
93
  override fun onBindViewHolder(holder: NativeListViewHolder, position: Int) {
81
94
  val item = itemAt(position) ?: return
95
+ holder.rowView.listStyle = listStyle
82
96
  holder.rowView.bind(
83
97
  item,
84
98
  theme,
@@ -135,7 +149,7 @@ internal class NativeListAdapter(
135
149
  items.forEachIndexed { index, item ->
136
150
  val style = item.json.optJSONObject("style")
137
151
  val height = item.json.optDouble("height", 0.0)
138
- val sourceHeight = if (item.type == "market" && style != null) {
152
+ val sourceHeight = item.styledHeight ?: if (item.type == "market" && style != null) {
139
153
  val titleHeight = ceil((style.optJSONObject("title")?.optDouble("lineHeight", 24.0) ?: 24.0) * density) / density
140
154
  val subtitleHeight = if (item.json.optString("subtitle").isNotEmpty() || item.json.optJSONObject("subtitlePrefix") != null) {
141
155
  ceil((style.optJSONObject("subtitle")?.optDouble("lineHeight", 20.0) ?: 20.0) * density) / density + style.optDouble("lineGap", 0.0)
@@ -201,14 +215,14 @@ internal class NativeListAdapter(
201
215
  marketSourceEdges = DoubleArray(0)
202
216
  reorderItems = null
203
217
  suppressDifferUpdates = false
204
- createdRows.forEach(NativeListRowView::dispose)
218
+ createdRows.forEach(NativeListRowHost::dispose)
205
219
  createdRows.clear()
206
220
  }
207
221
 
208
222
  companion object {
209
223
  private val DIFF = object : DiffUtil.ItemCallback<NativeListItem>() {
210
224
  override fun areItemsTheSame(oldItem: NativeListItem, newItem: NativeListItem): Boolean =
211
- oldItem.key == newItem.key && oldItem.type == newItem.type
225
+ oldItem.key == newItem.key
212
226
 
213
227
  override fun areContentsTheSame(oldItem: NativeListItem, newItem: NativeListItem): Boolean =
214
228
  oldItem.revision == newItem.revision && oldItem.content == newItem.content
@@ -0,0 +1,53 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ import android.view.View
4
+ import android.view.ViewGroup
5
+ import android.widget.FrameLayout
6
+ import androidx.recyclerview.widget.RecyclerView
7
+
8
+ /** A persistent React container slot; never a template row or a selectable item. */
9
+ internal class NativeListContainerSlotAdapter : RecyclerView.Adapter<NativeListContainerSlotAdapter.Holder>() {
10
+ class Holder(val container: FrameLayout) : RecyclerView.ViewHolder(container)
11
+ private var content: View? = null
12
+ private var height = 0
13
+ private var enabled = true
14
+
15
+ fun update(view: View?, heightPx: Int, visible: Boolean) {
16
+ val previousCount = itemCount
17
+ val changed = content !== view || height != heightPx || enabled != visible
18
+ content = view
19
+ height = heightPx.coerceAtLeast(0)
20
+ enabled = visible
21
+ val nextCount = itemCount
22
+ if (previousCount == 0 && nextCount == 1) notifyItemInserted(0)
23
+ else if (previousCount == 1 && nextCount == 0) notifyItemRemoved(0)
24
+ else if (nextCount == 1 && changed) notifyItemChanged(0)
25
+ }
26
+
27
+ override fun getItemCount(): Int = if (enabled && content != null && height > 0) 1 else 0
28
+
29
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
30
+ val container = object : FrameLayout(parent.context) {
31
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
32
+ for (index in 0 until childCount) {
33
+ getChildAt(index).layout(0, 0, right - left, bottom - top)
34
+ }
35
+ }
36
+ }
37
+ container.layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height)
38
+ return Holder(container)
39
+ }
40
+
41
+ override fun onBindViewHolder(holder: Holder, position: Int) {
42
+ holder.container.layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height)
43
+ holder.container.removeAllViews()
44
+ content?.let { view ->
45
+ (view.parent as? ViewGroup)?.removeView(view)
46
+ holder.container.addView(view, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height))
47
+ }
48
+ }
49
+
50
+ override fun onViewRecycled(holder: Holder) {
51
+ holder.container.removeAllViews()
52
+ }
53
+ }
@@ -0,0 +1,487 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ import android.graphics.drawable.GradientDrawable
4
+ import android.text.TextUtils
5
+ import android.view.Gravity
6
+ import android.widget.LinearLayout
7
+ import android.widget.TextView
8
+ import androidx.core.widget.TextViewCompat
9
+ import com.facebook.react.uimanager.ThemedReactContext
10
+ import kotlin.math.roundToInt
11
+ import org.json.JSONArray
12
+ import org.json.JSONObject
13
+
14
+ internal class NativeListDataRowView(context: ThemedReactContext) :
15
+ NativeListRendererRowView(context) {
16
+ private val visual = NativeListLeadingVisual(context)
17
+ private val favorite = OneKeyIconView(context)
18
+ private val accessories = NativeListAccessoryStack(context)
19
+ private val columns =
20
+ LinearLayout(context).apply {
21
+ orientation = HORIZONTAL
22
+ gravity = Gravity.CENTER_VERTICAL
23
+ }
24
+ private val cells = List(4) { NativeListTableColumnView(context) }
25
+
26
+ override fun unselectedBackground(
27
+ item: NativeListItem,
28
+ theme: JSONObject?,
29
+ layout: String,
30
+ itemIndex: Int?,
31
+ ): Int {
32
+ val index = if (item.json.has("index")) item.json.optInt("index") else itemIndex
33
+ return if (layout == "table" && index?.rem(2) == 0)
34
+ parseNativeListColor(theme?.optString("subduedBackground", "#F9F9F9") ?: "#F9F9F9")
35
+ else super.unselectedBackground(item, theme, layout, itemIndex)
36
+ }
37
+
38
+ override fun backgroundGroupPosition(
39
+ item: NativeListItem,
40
+ layout: String,
41
+ itemIndex: Int?,
42
+ selected: Boolean,
43
+ ): String {
44
+ val index = if (item.json.has("index")) item.json.optInt("index") else itemIndex
45
+ return if (layout == "table" && index?.rem(2) == 0 && !selected) ""
46
+ else super.backgroundGroupPosition(item, layout, itemIndex, selected)
47
+ }
48
+
49
+ override val assetFields = listOf("leading")
50
+
51
+ init {
52
+ cells.forEach(columns::addView)
53
+ addView(favorite)
54
+ addView(visual)
55
+ addView(accessories)
56
+ addView(columns, LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f))
57
+ accessories.onAction = { key, view, slot, target ->
58
+ emitAction(key, view, "trailingAccessory", slot, target)
59
+ }
60
+ }
61
+
62
+ override fun defaultHeight(item: NativeListItem, layout: String): Int {
63
+ val values = item.json.optJSONArray("columns") ?: JSONArray()
64
+ val secondary =
65
+ (0 until values.length()).any {
66
+ values.getJSONObject(it).optString("secondaryText").isNotEmpty()
67
+ }
68
+ return if (layout == "table") if (secondary) 60 else 52 else if (secondary) 64 else 56
69
+ }
70
+
71
+ override fun bindContent(
72
+ item: NativeListItem,
73
+ theme: JSONObject?,
74
+ layout: String,
75
+ checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String,
76
+ ) {
77
+ val data = item.json
78
+ val style = data.optJSONObject("style") ?: JSONObject()
79
+ val image = style.optJSONObject("image") ?: JSONObject()
80
+ orientation = HORIZONTAL
81
+ gravity =
82
+ when (style.optJSONObject("container")?.optString("contentVerticalAlignment")) {
83
+ "top" -> Gravity.TOP
84
+ "bottom" -> Gravity.BOTTOM
85
+ else -> Gravity.CENTER_VERTICAL
86
+ }
87
+ val hp =
88
+ if (style.has("horizontalPadding")) stylePx(style.optDouble("horizontalPadding"))
89
+ else dp(if (layout == "table") 20 else 12)
90
+ val vp =
91
+ if (style.has("verticalPadding")) stylePx(style.optDouble("verticalPadding"))
92
+ else dp(if (layout == "table") 10 else 8)
93
+ setPadding(hp, vp, hp, vp)
94
+ val active = data.optBoolean("favoriteActive")
95
+ favorite.visibility = if (active || data.optBoolean("favorite")) VISIBLE else GONE
96
+ favorite.iconName = if (active) "StarSolid" else "StarOutline"
97
+ favorite.useSourceScale = sourceScale
98
+ favorite.tintColor =
99
+ parseNativeListColor(
100
+ theme?.optString(
101
+ if (active) "icon" else "iconSubdued",
102
+ if (active) "#0000009B" else "#00000072",
103
+ ) ?: if (active) "#0000009B" else "#00000072"
104
+ )
105
+ favorite.layoutParams =
106
+ LayoutParams(dp(20), dp(20)).apply { marginEnd = dp(if (layout == "table") 8 else 12) }
107
+ val leading = data.optJSONObject("leading")
108
+ visual.visibility = if (leading == null) GONE else VISIBLE
109
+ visual.layoutParams =
110
+ LayoutParams(
111
+ if (image.has("width")) stylePx(image.optDouble("width")) else dp(40),
112
+ if (image.has("height")) stylePx(image.optDouble("height")) else dp(40),
113
+ )
114
+ .apply {
115
+ marginEnd =
116
+ if (style.has("leadingGap")) stylePx(style.optDouble("leadingGap"))
117
+ else dp(if (layout == "table") 10 else 12)
118
+ }
119
+ if (leading != null) visual.bind(leading, image, item.key, theme, false, sourceScale)
120
+ else visual.recycle()
121
+ val descriptors = JSONArray()
122
+ if (data.has("index"))
123
+ descriptors.put(
124
+ JSONObject().put("kind", "value").put("text", data.optInt("index").toString())
125
+ )
126
+ data.optJSONObject("checkbox")?.let(descriptors::put)
127
+ val accessoryStyle = JSONObject().put("value", style.optJSONObject("index"))
128
+ accessories.bind(item, descriptors, theme, accessoryStyle, sourceScale, checkboxState)
129
+ accessories.visibility = if (descriptors.length() == 0) GONE else VISIBLE
130
+ accessories.layoutParams =
131
+ LayoutParams(dp(32), LayoutParams.WRAP_CONTENT).apply { marginEnd = dp(10) }
132
+ val values = data.getJSONArray("columns")
133
+ // Only the keys NativeListTableColumnView consumes switch a row off the legacy
134
+ // single-label / fixed-geometry text layout; container, padding, image and
135
+ // index styles keep legacy text rendering.
136
+ val unstyled = TEXT_STYLE_KEYS.none(style::has)
137
+ cells.forEachIndexed { index, cell ->
138
+ cell.visibility = if (index < values.length()) VISIBLE else GONE
139
+ if (index < values.length()) {
140
+ val value = values.getJSONObject(index)
141
+ val infoColor = parseNativeListColor(theme?.optString("info", "#006DCBF2") ?: "#006DCBF2")
142
+ if (unstyled && layout != "table")
143
+ // Legacy linear default: one label per column with appended spans.
144
+ cell.bindLegacyLinear(
145
+ value,
146
+ if (index == 0) data.optJSONArray("badges") else null,
147
+ dataColor(value.optString("tone"), theme),
148
+ dataColor(value.optString("secondaryTone", "secondary"), theme),
149
+ infoColor,
150
+ )
151
+ else
152
+ cell.bind(
153
+ value,
154
+ if (index == 0) data.optJSONArray("badges") else null,
155
+ dataColor(value.optString("tone"), theme),
156
+ dataColor(value.optString("secondaryTone", "secondary"), theme),
157
+ infoColor,
158
+ if (layout == "table") 14f else 16f,
159
+ style,
160
+ fixedTableGeometry = unstyled,
161
+ )
162
+ // Legacy table default: every column is a fixed 40dp, top-aligned block.
163
+ cell.layoutParams =
164
+ LayoutParams(
165
+ 0,
166
+ if (unstyled && layout == "table") dp(40) else LayoutParams.WRAP_CONTENT,
167
+ value.optInt("weight", 1).toFloat(),
168
+ )
169
+ } else cell.reset()
170
+ }
171
+ }
172
+
173
+ private fun dataColor(tone: String, theme: JSONObject?): Int {
174
+ // Legacy tone mapping: any other tone renders as primaryText.
175
+ val token =
176
+ when (tone) {
177
+ "positive" -> "positive"
178
+ "negative" -> "negative"
179
+ "secondary" -> "secondaryText"
180
+ else -> "primaryText"
181
+ }
182
+ val fallback =
183
+ when (token) {
184
+ "positive" -> "#00713FDE"
185
+ "negative" -> "#C40006D3"
186
+ "secondaryText" -> "#0000009B"
187
+ else -> "#000000DF"
188
+ }
189
+ return parseNativeListColor(theme?.optString(token, fallback) ?: fallback)
190
+ }
191
+
192
+ override fun bindSelectionContent(
193
+ item: NativeListItem,
194
+ checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String,
195
+ ) {
196
+ accessories.updateSelection(item, checkboxState)
197
+ }
198
+
199
+ override fun recycleContent() {
200
+ visual.recycle()
201
+ accessories.reset()
202
+ cells.forEach { it.reset() }
203
+ }
204
+
205
+ override fun disposeContent() {
206
+ visual.dispose()
207
+ accessories.reset()
208
+ }
209
+
210
+ private companion object {
211
+ val TEXT_STYLE_KEYS = listOf("columns", "columnSecondary", "lineGap", "titleBadgeGap")
212
+ }
213
+ }
214
+
215
+ private class NativeListTableColumnView(context: android.content.Context) : LinearLayout(context) {
216
+ private val primaryLine = LinearLayout(context)
217
+ private val primary = NativeListTextView(context)
218
+ private val badges = LinearLayout(context)
219
+ private val secondaryLine = LinearLayout(context)
220
+ private val secondaryLeading = NativeListTextView(context)
221
+ private val secondary = NativeListTextView(context)
222
+
223
+ init {
224
+ orientation = VERTICAL
225
+ primaryLine.orientation = HORIZONTAL
226
+ primaryLine.gravity = Gravity.CENTER_VERTICAL
227
+ badges.orientation = HORIZONTAL
228
+ badges.gravity = Gravity.CENTER_VERTICAL
229
+ primary.includeFontPadding = false
230
+ primary.fontFeatureSettings = "tnum"
231
+ primary.maxLines = 1
232
+ primary.ellipsize = TextUtils.TruncateAt.END
233
+ primary.textSize = sp(14f)
234
+ primary.typeface = NativeListFonts.medium(context)
235
+ TextViewCompat.setLineHeight(primary, dp(20))
236
+ secondaryLine.orientation = HORIZONTAL
237
+ secondaryLine.gravity = Gravity.CENTER_VERTICAL
238
+ listOf(secondaryLeading, secondary).forEach { label ->
239
+ label.includeFontPadding = false
240
+ label.fontFeatureSettings = "tnum"
241
+ label.maxLines = 1
242
+ label.ellipsize = TextUtils.TruncateAt.END
243
+ label.textSize = sp(12f)
244
+ label.typeface = NativeListFonts.regular(context)
245
+ TextViewCompat.setLineHeight(label, dp(16))
246
+ }
247
+ secondaryLeading.maxWidth = dp(120)
248
+ secondaryLine.addView(
249
+ secondaryLeading,
250
+ LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT),
251
+ )
252
+ secondaryLine.addView(
253
+ secondary,
254
+ LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
255
+ marginStart = dp(4)
256
+ },
257
+ )
258
+ primaryLine.addView(primary, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
259
+ primaryLine.addView(
260
+ badges,
261
+ LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
262
+ marginStart = dp(6)
263
+ },
264
+ )
265
+ addView(primaryLine, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
266
+ addView(
267
+ secondaryLine,
268
+ LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT).apply { topMargin = dp(4) },
269
+ )
270
+ }
271
+
272
+ fun reset() {
273
+ primaryLine.layoutParams.width = LayoutParams.WRAP_CONTENT
274
+ primaryLine.layoutParams.height = LayoutParams.WRAP_CONTENT
275
+ secondaryLine.layoutParams.height = LayoutParams.WRAP_CONTENT
276
+ badges.layoutParams.height = LayoutParams.WRAP_CONTENT
277
+ secondaryLeading.layoutParams.height = LayoutParams.WRAP_CONTENT
278
+ primary.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
279
+ secondary.layoutParams =
280
+ LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
281
+ marginStart = dp(4)
282
+ }
283
+ (secondaryLine.layoutParams as MarginLayoutParams).topMargin = dp(4)
284
+ (badges.layoutParams as MarginLayoutParams).marginStart = dp(6)
285
+ primary.text = ""
286
+ secondaryLeading.text = ""
287
+ secondary.text = ""
288
+ secondaryLeading.visibility = GONE
289
+ secondary.visibility = GONE
290
+ secondaryLine.visibility = GONE
291
+ badges.removeAllViews()
292
+ badges.visibility = GONE
293
+ }
294
+
295
+ fun bind(
296
+ column: JSONObject,
297
+ rowBadges: JSONArray?,
298
+ primaryColor: Int,
299
+ secondaryColor: Int,
300
+ infoColor: Int,
301
+ primarySize: Float,
302
+ style: JSONObject,
303
+ fixedTableGeometry: Boolean = false,
304
+ ) {
305
+ reset()
306
+ gravity =
307
+ when (column.optString("alignment", "start")) {
308
+ "center" -> Gravity.CENTER_HORIZONTAL
309
+ "end" -> Gravity.END
310
+ else -> Gravity.START
311
+ }
312
+ fun bindText(
313
+ view: NativeListTextView,
314
+ value: String,
315
+ slot: String,
316
+ size: Float,
317
+ weight: String,
318
+ color: Int,
319
+ line: Int,
320
+ ) {
321
+ val resolved =
322
+ JSONObject(style.optJSONObject(slot)?.toString() ?: "{}").apply {
323
+ if (!has("alignment")) put("alignment", column.optString("alignment", "start"))
324
+ }
325
+ NativeListResolvedText.resolve(context, value, resolved, size, weight, color, line, 1, false)
326
+ .bind(view)
327
+ if (resolved.optString("truncate") != "clip") view.ellipsize = TextUtils.TruncateAt.END
328
+ }
329
+ bindText(primary, column.optString("text"), "columns", primarySize, "medium", primaryColor, 20)
330
+ bindText(
331
+ secondaryLeading,
332
+ column.optString("secondaryLeadingText"),
333
+ "columnSecondary",
334
+ 12f,
335
+ "regular",
336
+ secondaryColor,
337
+ 16,
338
+ )
339
+ bindText(
340
+ secondary,
341
+ column.optString("secondaryText"),
342
+ "columnSecondary",
343
+ 12f,
344
+ "regular",
345
+ secondaryColor,
346
+ 16,
347
+ )
348
+ if (rowBadges != null && rowBadges.length() > 0) {
349
+ badges.visibility = VISIBLE
350
+ for (index in 0 until minOf(2, rowBadges.length())) {
351
+ val badge =
352
+ TextView(context).apply {
353
+ includeFontPadding = false
354
+ fontFeatureSettings = "tnum"
355
+ gravity = Gravity.CENTER
356
+ text = rowBadges.getJSONObject(index).optString("text")
357
+ textSize = sp(10f)
358
+ typeface = NativeListFonts.regular(context)
359
+ setTextColor(infoColor)
360
+ setPadding(dp(6), 0, dp(6), 0)
361
+ background =
362
+ GradientDrawable().apply {
363
+ setColor(parseNativeListColor("#008FF519"))
364
+ cornerRadius = dp(4).toFloat()
365
+ }
366
+ }
367
+ badges.addView(
368
+ badge,
369
+ LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
370
+ if (index > 0) marginStart = dp(4)
371
+ },
372
+ )
373
+ }
374
+ }
375
+ val secondaryLeadingText = column.optString("secondaryLeadingText")
376
+ if (secondaryLeadingText.isNotEmpty()) {
377
+ secondaryLeading.visibility = VISIBLE
378
+ secondaryLine.visibility = VISIBLE
379
+ }
380
+ val secondaryText = column.optString("secondaryText")
381
+ if (secondaryText.isNotEmpty()) {
382
+ secondary.visibility = VISIBLE
383
+ secondaryLine.visibility = VISIBLE
384
+ }
385
+ if (!fixedTableGeometry) applyStyle(style)
386
+ if (fixedTableGeometry) {
387
+ // Legacy table lines: fixed 20dp primary and 16dp secondary/badge boxes.
388
+ primaryLine.layoutParams.height = dp(20)
389
+ primary.layoutParams.height = dp(20)
390
+ badges.layoutParams.height = dp(16)
391
+ for (index in 0 until badges.childCount) badges.getChildAt(index).layoutParams.height = dp(16)
392
+ secondaryLine.layoutParams.height = dp(16)
393
+ secondaryLeading.layoutParams.height = dp(16)
394
+ secondary.layoutParams.height = dp(16)
395
+ }
396
+ }
397
+
398
+ fun bindLegacyLinear(
399
+ column: JSONObject,
400
+ rowBadges: JSONArray?,
401
+ primaryColor: Int,
402
+ secondaryColor: Int,
403
+ infoColor: Int,
404
+ ) {
405
+ reset()
406
+ gravity = Gravity.START
407
+ val value = android.text.SpannableStringBuilder(column.optString("text"))
408
+ value.setSpan(
409
+ android.text.style.ForegroundColorSpan(primaryColor),
410
+ 0,
411
+ value.length,
412
+ android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
413
+ )
414
+ if (rowBadges != null)
415
+ for (index in 0 until minOf(2, rowBadges.length())) {
416
+ val start = value.length
417
+ value.append(" ${rowBadges.getJSONObject(index).optString("text")} ")
418
+ value.setSpan(
419
+ android.text.style.ForegroundColorSpan(infoColor),
420
+ start,
421
+ value.length,
422
+ android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
423
+ )
424
+ value.setSpan(
425
+ android.text.style.BackgroundColorSpan(parseNativeListColor("#008FF519")),
426
+ start,
427
+ value.length,
428
+ android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
429
+ )
430
+ }
431
+ val secondaryText = column.optString("secondaryText")
432
+ if (secondaryText.isNotEmpty()) {
433
+ val start = value.length
434
+ value.append("\n$secondaryText")
435
+ value.setSpan(
436
+ android.text.style.ForegroundColorSpan(secondaryColor),
437
+ start,
438
+ value.length,
439
+ android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
440
+ )
441
+ }
442
+ primaryLine.layoutParams.width = LayoutParams.MATCH_PARENT
443
+ primary.layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)
444
+ primary.includeFontPadding = true
445
+ primary.fontFeatureSettings = "tnum"
446
+ primary.textSize = sp(16f)
447
+ primary.typeface = NativeListFonts.medium(context)
448
+ primary.setLineSpacing(0f, 1f)
449
+ primary.letterSpacing = 0f
450
+ primary.opticalOffsetY = 0f
451
+ primary.setHorizontallyScrolling(false)
452
+ primary.ellipsize = null
453
+ primary.maxLines = if (secondaryText.isEmpty()) 1 else 2
454
+ primary.gravity =
455
+ when (column.optString("alignment", "start")) {
456
+ "center" -> Gravity.CENTER
457
+ "end" -> Gravity.END
458
+ else -> Gravity.START
459
+ }
460
+ primary.text = value
461
+ primary.visibility = VISIBLE
462
+ }
463
+
464
+ private fun applyStyle(style: JSONObject) {
465
+ style.optJSONObject("columns")?.let {
466
+ if (it.has("alignment")) {
467
+ primaryLine.layoutParams.width = LayoutParams.MATCH_PARENT
468
+ primary.layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)
469
+ }
470
+ }
471
+ style.optJSONObject("columnSecondary")?.let {
472
+ if (it.has("alignment"))
473
+ secondary.layoutParams =
474
+ LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f).apply { marginStart = dp(4) }
475
+ }
476
+ (secondaryLine.layoutParams as MarginLayoutParams).topMargin =
477
+ style.optDouble("lineGap", 4.0).let { (it * resources.displayMetrics.density).roundToInt() }
478
+ (badges.layoutParams as MarginLayoutParams).marginStart =
479
+ style.optDouble("titleBadgeGap", 6.0).let {
480
+ (it * resources.displayMetrics.density).roundToInt()
481
+ }
482
+ }
483
+
484
+ private fun dp(value: Int): Int = NativeListScale.dp(resources, value)
485
+
486
+ private fun sp(value: Float): Float = NativeListScale.font(resources, value)
487
+ }
@@ -0,0 +1,5 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ // Negative offsets are required when a container slot is taller than the viewport.
4
+ internal fun nativeListEndAlignmentOffset(viewportEnd: Int, contentEnd: Int): Int =
5
+ viewportEnd - contentEnd