@onekeyfe/react-native-native-list 6.8.0-19 → 6.8.0-20

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.
@@ -75,6 +75,8 @@ android {
75
75
  disable "GradleCompatible"
76
76
  }
77
77
 
78
+ testOptions { unitTests.includeAndroidResources = true }
79
+
78
80
  compileOptions {
79
81
  sourceCompatibility JavaVersion.VERSION_1_8
80
82
  targetCompatibility JavaVersion.VERSION_1_8
@@ -110,4 +112,5 @@ dependencies {
110
112
  implementation project(":onekeyfe_react-native-native-logger")
111
113
  implementation project(":onekeyfe_react-native-image")
112
114
  testImplementation "junit:junit:4.13.2"
115
+ testImplementation "org.robolectric:robolectric:4.17"
113
116
  }
@@ -157,6 +157,26 @@ internal class NativeListIdentityRowView(context: ThemedReactContext, fonts: Nat
157
157
  private fun JSONArray?.hasAccessory(kind: String) =
158
158
  this != null && (0 until length()).any { optJSONObject(it)?.optString("kind") == kind }
159
159
 
160
+ override fun bindContentUpdate(
161
+ previous: NativeListItem,
162
+ item: NativeListItem,
163
+ theme: JSONObject?,
164
+ ): Boolean {
165
+ val segments = item.json.optJSONArray("subtitleSegments") ?: return false
166
+ if (semanticSubtitleLabels.isEmpty() || semanticSubtitleLabels.size != segments.length() ||
167
+ !canUpdateIdentitySubtitle(previous, item)
168
+ ) return false
169
+
170
+ val style = item.json.optJSONObject("style")?.optJSONObject("subtitle")
171
+ semanticSubtitleLabels.forEachIndexed { index, label ->
172
+ bindIdentitySubtitleLabel(label, segments.getJSONObject(index), theme)
173
+ style?.let { applyStyledText(label, it) }
174
+ applySelectorTextTypography(label, style)
175
+ label.requestLayout()
176
+ }
177
+ return true
178
+ }
179
+
160
180
  override fun bindContent(
161
181
  item: NativeListItem,
162
182
  theme: JSONObject?,
@@ -172,8 +192,9 @@ internal class NativeListIdentityRowView(context: ThemedReactContext, fonts: Nat
172
192
  setOnLongClickListener(
173
193
  if (longPressActionKey.isNotEmpty())
174
194
  OnLongClickListener {
195
+ val actionItem = currentItem ?: return@OnLongClickListener false
175
196
  val point = longPressPoint
176
- if (!isEnabled || !item.isRowPressEnabled || currentItem?.key != item.key ||
197
+ if (!isEnabled || !actionItem.isRowPressEnabled || actionItem.key != item.key ||
177
198
  bindingEpoch != boundEpoch ||
178
199
  (longPressPointEpoch == boundEpoch && point == null)) false
179
200
  else {
@@ -182,7 +203,7 @@ internal class NativeListIdentityRowView(context: ThemedReactContext, fonts: Nat
182
203
  getLocationInWindow(location)
183
204
  PointF(location[0] + it.x, location[1] + it.y)
184
205
  }
185
- onAction?.invoke(item, longPressActionKey, null,
206
+ onAction?.invoke(actionItem, longPressActionKey, null,
186
207
  NativeListActionOrigin(this, this, bindingEpoch, "row",
187
208
  windowPointPixels = windowPoint))
188
209
  longPressPoint = null
@@ -365,20 +386,21 @@ internal class NativeListIdentityRowView(context: ThemedReactContext, fonts: Nat
365
386
  )
366
387
  return
367
388
  fun visit(view: View) {
368
- if (view is TextView) {
369
- if (sourceScale) {
370
- paintDefaults.putIfAbsent(view, view.paintFlags)
371
- // Explicit style fontSize/lineHeight are kept as the caller set them.
372
- applyNativeListSourceTypography(view, explicitStyle(view))
373
- } else {
374
- applyNativeListTextSpacing(view, explicitStyle(view))
375
- }
376
- }
389
+ if (view is TextView) applySelectorTextTypography(view, explicitStyle(view))
377
390
  if (view is ViewGroup) for (i in 0 until view.childCount) visit(view.getChildAt(i))
378
391
  }
379
392
  visit(this)
380
393
  }
381
394
 
395
+ private fun applySelectorTextTypography(view: TextView, style: JSONObject?) {
396
+ if (sourceScale) {
397
+ paintDefaults.putIfAbsent(view, view.paintFlags)
398
+ applyNativeListSourceTypography(view, style)
399
+ } else {
400
+ applyNativeListTextSpacing(view, style)
401
+ }
402
+ }
403
+
382
404
  private fun addLeading(visual: JSONObject?, sizeDp: Int, spacingDp: Int = 12) {
383
405
  // Legacy: a missing visual still reserves an empty, transparent leading slot.
384
406
  val descriptor = visual ?: JSONObject().put("backgroundColor", "#00000000")
@@ -834,27 +856,11 @@ internal class NativeListIdentityRowView(context: ThemedReactContext, fonts: Nat
834
856
  }
835
857
  val label =
836
858
  NativeListTextView(context).apply {
837
- text = segment.optString("text")
838
- typeface = fonts.regular(context)
839
- textSize = sp(14f)
840
859
  includeFontPadding = false
841
860
  maxLines = 1
842
861
  ellipsize = TextUtils.TruncateAt.END
843
- val toneKey =
844
- when (segment.optString("tone")) {
845
- "primary" -> "primaryText"
846
- "disabled" -> "disabledText"
847
- "caution" -> "caution"
848
- "positive" -> "positive"
849
- "negative" -> "negative"
850
- else -> "secondaryText"
851
- }
852
- setTextColor(
853
- color(theme, toneKey, if (toneKey == "caution") "#AB6400" else "#0000009B")
854
- )
855
862
  }
856
- TextViewCompat.setLineHeight(label, dp(20))
857
- applyValueSegments(label, segment.optJSONArray("textSegments"), 14, 20, false)
863
+ bindIdentitySubtitleLabel(label, segment, theme)
858
864
  semanticSubtitleLabels.add(label)
859
865
  line.addView(label, LayoutParams(LayoutParams.WRAP_CONTENT, dp(20)))
860
866
  }
@@ -936,6 +942,23 @@ internal class NativeListIdentityRowView(context: ThemedReactContext, fonts: Nat
936
942
  }
937
943
  }
938
944
 
945
+ private fun bindIdentitySubtitleLabel(label: TextView, segment: JSONObject, theme: JSONObject?) {
946
+ label.text = segment.optString("text")
947
+ label.typeface = fonts.regular(context)
948
+ label.textSize = sp(14f)
949
+ TextViewCompat.setLineHeight(label, dp(20))
950
+ val toneKey = when (segment.optString("tone")) {
951
+ "primary" -> "primaryText"
952
+ "disabled" -> "disabledText"
953
+ "caution" -> "caution"
954
+ "positive" -> "positive"
955
+ "negative" -> "negative"
956
+ else -> "secondaryText"
957
+ }
958
+ label.setTextColor(color(theme, toneKey, if (toneKey == "caution") "#AB6400" else "#0000009B"))
959
+ applyValueSegments(label, segment.optJSONArray("textSegments"), 14, 20, false)
960
+ }
961
+
939
962
  private fun applyValueSegments(
940
963
  view: TextView,
941
964
  segments: JSONArray?,
@@ -28,6 +28,12 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
28
28
  checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String,
29
29
  )
30
30
 
31
+ protected open fun bindContentUpdate(
32
+ previous: NativeListItem,
33
+ item: NativeListItem,
34
+ theme: JSONObject?,
35
+ ): Boolean = false
36
+
31
37
  protected open fun bindSelectionContent(
32
38
  item: NativeListItem,
33
39
  checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String,
@@ -50,7 +56,8 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
50
56
  }
51
57
 
52
58
  protected fun retainBoundItem(item: NativeListItem): Boolean {
53
- if (current?.key != item.key) return false
59
+ if (renderedItem?.key != item.key) return false
60
+ renderedItem = item
54
61
  current = item
55
62
  tag = item
56
63
  return true
@@ -106,6 +113,8 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
106
113
  protected open val pressedColorName = "rowPressedBackground"
107
114
  protected open val pressedColorFallback = "#00000017"
108
115
  private var current: NativeListItem? = null
116
+ // Selection can advance current before RecyclerView binds the new content.
117
+ private var renderedItem: NativeListItem? = null
109
118
  protected val boundItem: NativeListItem?
110
119
  get() = current
111
120
  private var theme: JSONObject? = null
@@ -189,7 +198,8 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
189
198
  val iconUpdate = NativeListIconUpdate(this)
190
199
  try {
191
200
 
192
- val update = update(current, item)
201
+ val previous = renderedItem
202
+ val update = update(previous, item)
193
203
  val signature =
194
204
  nativeListCanonical(
195
205
  JSONObject()
@@ -201,8 +211,13 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
201
211
  .put("listStyle", listStyle)
202
212
  )
203
213
  sourceScale = usesSourceScale(item, useSourceScale)
204
- if (update != NativeListRendererUpdate.UNCHANGED || signature != inputSignature) {
205
- if (current != null) onBindingInvalidated?.invoke(this, bindingEpoch)
214
+ val signatureChanged = signature != inputSignature
215
+ val updatedContentInPlace = update == NativeListRendererUpdate.CONTENT &&
216
+ !signatureChanged && previous != null && bindContentUpdate(previous, item, theme)
217
+ if (!updatedContentInPlace &&
218
+ (update != NativeListRendererUpdate.UNCHANGED || signatureChanged)
219
+ ) {
220
+ if (previous != null) onBindingInvalidated?.invoke(this, bindingEpoch)
206
221
  bindingEpoch++
207
222
  if (update == NativeListRendererUpdate.REPLACE) {
208
223
  recycleContent()
@@ -211,6 +226,7 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
211
226
  bindContent(item, theme, layout, checkboxState)
212
227
  inputSignature = signature
213
228
  }
229
+ renderedItem = item
214
230
  current = item
215
231
  tag = item
216
232
  this.theme = theme
@@ -409,6 +425,7 @@ internal abstract class NativeListRendererRowView(protected val reactContext: Th
409
425
  if (current != null) onBindingInvalidated?.invoke(this, bindingEpoch)
410
426
  bindingEpoch++
411
427
  current = null
428
+ renderedItem = null
412
429
  tag = null
413
430
  inputSignature = ""
414
431
  touchPressed = false
@@ -0,0 +1,34 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ import org.json.JSONObject
4
+
5
+ /** Compare account-selector content that the subtitle and selection binders cannot refresh. */
6
+ internal fun canUpdateIdentitySubtitle(previous: NativeListItem, next: NativeListItem): Boolean {
7
+ if (previous.key != next.key || previous.type != "identity" || next.type != "identity") return false
8
+
9
+ fun structure(item: NativeListItem): String? {
10
+ val data = JSONObject(item.content)
11
+ if (data.optString("presentation") != "accountSelector") return null
12
+ val segments = data.optJSONArray("subtitleSegments") ?: return null
13
+ if (segments.length() == 0) return null
14
+ for (index in 0 until segments.length()) {
15
+ val segment = segments.optJSONObject(index) ?: return null
16
+ segment.remove("text")
17
+ segment.remove("textSegments")
18
+ segment.remove("tone")
19
+ }
20
+ data.remove("accessibilityLabel")
21
+ // Selection chrome and the existing checkbox are refreshed on every bind.
22
+ data.remove("selected")
23
+ data.optJSONArray("trailing")?.let { accessories ->
24
+ for (index in 0 until accessories.length()) {
25
+ val accessory = accessories.optJSONObject(index) ?: continue
26
+ if (accessory.optString("kind") == "checkbox") accessory.remove("state")
27
+ }
28
+ }
29
+ return nativeListCanonical(data)
30
+ }
31
+
32
+ val previousStructure = structure(previous) ?: return false
33
+ return previousStructure == structure(next)
34
+ }
@@ -0,0 +1,177 @@
1
+ package com.margelo.nitro.nativelist
2
+
3
+ import android.app.Activity
4
+ import android.view.View
5
+ import android.view.ViewGroup
6
+ import android.widget.FrameLayout
7
+ import android.widget.TextView
8
+ import com.facebook.react.bridge.BridgeReactContext
9
+ import com.facebook.react.uimanager.ThemedReactContext
10
+ import org.json.JSONArray
11
+ import org.json.JSONObject
12
+ import org.junit.After
13
+ import org.junit.Assert.assertEquals
14
+ import org.junit.Assert.assertFalse
15
+ import org.junit.Assert.assertNull
16
+ import org.junit.Assert.assertSame
17
+ import org.junit.Assert.assertTrue
18
+ import org.junit.Before
19
+ import org.junit.Test
20
+ import org.junit.runner.RunWith
21
+ import org.robolectric.Robolectric
22
+ import org.robolectric.RobolectricTestRunner
23
+ import org.robolectric.annotation.Config
24
+
25
+ @RunWith(RobolectricTestRunner::class)
26
+ @Config(sdk = [34])
27
+ class NativeListIdentitySubtitleBindingTest {
28
+ private val controller = Robolectric.buildActivity(Activity::class.java)
29
+ private lateinit var row: NativeListIdentityRowView
30
+ private val checkboxState: (NativeListItem, NativeSelectionTarget?, String) -> String =
31
+ { _, _, fallback -> fallback }
32
+
33
+ @Before
34
+ fun setUp() {
35
+ val activity = controller.setup().visible().get()
36
+ val context = ThemedReactContext(BridgeReactContext(activity.application), activity, "test", 1)
37
+ row = NativeListIdentityRowView(context, NativeListFonts())
38
+ activity.setContentView(FrameLayout(activity).apply { addView(row) })
39
+ }
40
+
41
+ @After
42
+ fun tearDown() {
43
+ row.dispose()
44
+ controller.pause().stop().destroy()
45
+ }
46
+
47
+ @Test
48
+ fun longerPlainBalanceRemeasuresTheExistingLabelWithoutDetachingAvatar() {
49
+ bind(account("--"))
50
+ measureRow()
51
+ val balance = requireNotNull(label("--"))
52
+ val originalWidth = balance.measuredWidth
53
+ assertTrue(originalWidth > 0)
54
+ assertFalse(balance.isLayoutRequested)
55
+ val originalEpoch = row.bindingEpoch
56
+ val avatarDetachments = watchAvatarDetachments()
57
+
58
+ val updated = account("12345678.90")
59
+ bind(updated)
60
+ assertTrue(balance.isLayoutRequested)
61
+ measureRow()
62
+
63
+ assertSame(balance, label("12345678.90"))
64
+ assertTrue(balance.measuredWidth > originalWidth)
65
+ assertSame(updated, row.tag)
66
+ assertEquals("Account, 12345678.90", row.contentDescription)
67
+ assertEquals(originalEpoch, row.bindingEpoch)
68
+ assertEquals(0, avatarDetachments())
69
+ }
70
+
71
+ @Test
72
+ fun selectionEchoThenBalanceKeepsTheSubtitleAndAvatarAttached() {
73
+ bind(account("--", checkbox = "unchecked"))
74
+ val balance = requireNotNull(label("--"))
75
+ val originalEpoch = row.bindingEpoch
76
+ val avatarDetachments = watchAvatarDetachments()
77
+ val checkbox = descendants(row).filterIsInstance<OneKeyCheckboxView>().single()
78
+
79
+ row.bindSelection(
80
+ account("--", selected = true, checkbox = "checked"), null, "linear", 0, true, checkboxState,
81
+ )
82
+ assertEquals("CheckboxCheckedCustom", checkbox.iconName)
83
+ bind(account("12.34", selected = true, checkbox = "checked"))
84
+
85
+ assertSame(balance, label("12.34"))
86
+ assertEquals("CheckboxCheckedCustom", checkbox.iconName)
87
+ assertEquals(originalEpoch, row.bindingEpoch)
88
+ assertEquals(0, avatarDetachments())
89
+ }
90
+
91
+ @Test
92
+ fun subtitleUpdateUsesRenderedContentAfterSelectionHasAdvancedTheModel() {
93
+ bind(account("--", showAction = true))
94
+ val originalEpoch = row.bindingEpoch
95
+ val previousAction = requireNotNull(label("Manage"))
96
+
97
+ row.bindSelection(account("--", selected = true), null, "linear", 0, true, checkboxState)
98
+ assertSame(previousAction, label("Manage"))
99
+ bind(account("12.34", selected = true))
100
+
101
+ assertNull(label("Manage"))
102
+ assertEquals("12.34", label("12.34")?.text.toString())
103
+ assertTrue(row.bindingEpoch > originalEpoch)
104
+ }
105
+
106
+ @Test
107
+ fun formattedBalanceUpdateReusesTheLabelAndClearsOldSpansOnPlainText() {
108
+ bind(account("--"))
109
+ val balance = requireNotNull(label("--"))
110
+ val originalEpoch = row.bindingEpoch
111
+ bind(account("0.0004", formatted = true))
112
+ assertSame(balance, label("0.04"))
113
+ bind(account("1.23"))
114
+ assertSame(balance, label("1.23"))
115
+ assertEquals(originalEpoch, row.bindingEpoch)
116
+ }
117
+
118
+ private fun bind(item: NativeListItem) {
119
+ row.bind(
120
+ item, null, "linear", "vertical", 0, item.json.optBoolean("selected"), checkboxState,
121
+ useSourceScale = false,
122
+ )
123
+ }
124
+
125
+ private fun measureRow() {
126
+ row.measure(
127
+ View.MeasureSpec.makeMeasureSpec(640, View.MeasureSpec.EXACTLY),
128
+ View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
129
+ )
130
+ row.layout(0, 0, row.measuredWidth, row.measuredHeight)
131
+ }
132
+
133
+ private fun account(
134
+ balance: String,
135
+ showAction: Boolean = false,
136
+ formatted: Boolean = false,
137
+ selected: Boolean = false,
138
+ checkbox: String? = null,
139
+ ): NativeListItem {
140
+ val json = JSONObject("""
141
+ {"key":"account","type":"identity","presentation":"accountSelector","title":"Account",
142
+ "leading":{"kind":"account","fallbackText":"AC"},
143
+ "subtitleSegments":[{"text":"$balance"},{"text":"0x1234","separatorBefore":true}]}
144
+ """).put("accessibilityLabel", "Account, $balance").put("selected", selected)
145
+ if (showAction) {
146
+ json.put("trailing", JSONArray("""[{"kind":"button","label":"Manage","actionKey":"manage"}]"""))
147
+ }
148
+ if (checkbox != null) {
149
+ json.put("trailing", JSONArray().put(JSONObject().put("kind", "checkbox").put("state", checkbox)))
150
+ }
151
+ if (formatted) {
152
+ json.getJSONArray("subtitleSegments").getJSONObject(0)
153
+ .put("textSegments", JSONArray("""[{"text":"0.0"},{"text":"4","style":"subscript"}]"""))
154
+ }
155
+ return NativeListItem.parse(json)
156
+ }
157
+
158
+ private fun label(text: String): TextView? =
159
+ descendants(row).filterIsInstance<TextView>().singleOrNull { it.text.toString() == text && it.visibility == View.VISIBLE }
160
+
161
+ private fun watchAvatarDetachments(): () -> Int {
162
+ val avatar = descendants(row).single { it is NativeListLeadingVisual }
163
+ var detachments = 0
164
+ avatar.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
165
+ override fun onViewAttachedToWindow(view: View) = Unit
166
+ override fun onViewDetachedFromWindow(view: View) { detachments++ }
167
+ })
168
+ return { detachments }
169
+ }
170
+
171
+ private fun descendants(view: View): Sequence<View> = sequence {
172
+ yield(view)
173
+ if (view is ViewGroup) {
174
+ for (index in 0 until view.childCount) yieldAll(descendants(view.getChildAt(index)))
175
+ }
176
+ }
177
+ }
package/docs/SPEC.md CHANGED
@@ -544,6 +544,20 @@ migration itself does not change any other protocol field.
544
544
 
545
545
  ## Lifecycle and concurrency
546
546
 
547
+ ### Android account-selector subtitle updates
548
+
549
+ An already bound `identity` row with `presentation: 'accountSelector'` updates
550
+ its existing subtitle labels in place when only their text, formatted text
551
+ segments, tone, or the row's accessibility label changes. The same avatar
552
+ view and its active or completed request stay in place. Selection and
553
+ accessibility reflect the accepted row, and the updated labels are remeasured
554
+ to allocate their new widths. The comparison uses the last rendered content
555
+ even when a newer selection callback has arrived. Row selection and existing
556
+ checkbox state are applied separately and do not require a rebuild. A
557
+ different key, subtitle slot structure, other row content, or binding inputs
558
+ (such as theme or layout) uses the normal binding path. Recycling still
559
+ releases the row's image work.
560
+
547
561
  ### Updates before native host binding
548
562
 
549
563
  Status: implemented in local source, unreleased. The delayed-host regression fails
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-native-list",
3
- "version": "6.8.0-19",
3
+ "version": "6.8.0-20",
4
4
  "description": "Template-driven native RecyclerView and UICollectionView for React Native",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/module/index.js",