Skip to content

Commit dd419ff

Browse files
committed
feat: higher level early return
1 parent 040a7a3 commit dd419ff

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ class EnrichedTextInputViewLayoutManager(
88
private var forceHeightRecalculationCounter: Int = 0
99

1010
fun invalidateLayout() {
11+
val stateWrapper = view.stateWrapper ?: return
12+
1113
val text = view.text
1214
val paint = view.paint
1315

14-
MeasurementStore.store(view.id, text, paint) {
15-
val stateWrapper = view.stateWrapper ?: return@store false
16-
17-
forceHeightRecalculationCounter++
18-
val state = Arguments.createMap()
19-
state.putInt("forceHeightRecalculationCounter", forceHeightRecalculationCounter)
20-
stateWrapper.updateState(state)
16+
val needUpdate = MeasurementStore.store(view.id, text, paint)
17+
if (!needUpdate) return
2118

22-
true
23-
}
19+
forceHeightRecalculationCounter++
20+
val state = Arguments.createMap()
21+
state.putInt("forceHeightRecalculationCounter", forceHeightRecalculationCounter)
22+
stateWrapper.updateState(state)
2423
}
2524

2625
fun releaseMeasurementStore() {

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

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

5352
val size = measure(cachedWidth, spannable, paint)
5453
val paintParams = PaintParams(paint.typeface, paint.textSize)
5554

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)
55+
data[id] = MeasurementParams(initialized, cachedWidth, size, spannable, paintParams)
56+
return cachedSize != size
6457
}
6558

6659
fun release(id: Int) {

0 commit comments

Comments
 (0)