Skip to content

Commit 43b8287

Browse files
committed
fix: desynced cache issue
1 parent 3a2701a commit 43b8287

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ class EnrichedTextInputViewLayoutManager(
1111
val text = view.text
1212
val paint = view.paint
1313

14-
val needUpdate = MeasurementStore.store(view.id, text, paint)
15-
if (!needUpdate) return
14+
MeasurementStore.store(view.id, text, paint) {
15+
val stateWrapper = view.stateWrapper ?: return@store false
1616

17-
val counter = forceHeightRecalculationCounter
18-
forceHeightRecalculationCounter++
19-
val state = Arguments.createMap()
20-
state.putInt("forceHeightRecalculationCounter", counter)
21-
view.stateWrapper?.updateState(state)
17+
forceHeightRecalculationCounter++
18+
val state = Arguments.createMap()
19+
state.putInt("forceHeightRecalculationCounter", forceHeightRecalculationCounter)
20+
stateWrapper.updateState(state)
21+
22+
true
23+
}
2224
}
2325

2426
fun releaseMeasurementStore() {

android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,23 @@ object MeasurementStore {
4444
id: Int,
4545
spannable: Spannable?,
4646
paint: TextPaint,
47-
): Boolean {
47+
invalidateShadowNode: () -> Boolean,
48+
) {
4849
val cachedWidth = data[id]?.cachedWidth ?: 0f
4950
val cachedSize = data[id]?.cachedSize ?: 0L
5051
val initialized = data[id]?.initialized ?: true
5152

5253
val size = measure(cachedWidth, spannable, paint)
5354
val paintParams = PaintParams(paint.typeface, paint.textSize)
5455

55-
data[id] = MeasurementParams(initialized, cachedWidth, size, spannable, paintParams)
56-
return cachedSize != size
56+
// stateWrapper might be null during the first measures,
57+
// we want to cache the size only when it was actually
58+
// handled in the shadow node. The cached size and the actual
59+
// rendered one should be in sync.
60+
val invalidated = cachedSize != size && invalidateShadowNode()
61+
val storedSize = if (invalidated) size else cachedSize
62+
63+
data[id] = MeasurementParams(initialized, cachedWidth, storedSize, spannable, paintParams)
5764
}
5865

5966
fun release(id: Int) {

0 commit comments

Comments
 (0)