diff --git a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt index 40894f82e..d9aca7673 100644 --- a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt +++ b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt @@ -11,14 +11,16 @@ class EnrichedTextInputViewLayoutManager( val text = view.text val paint = view.paint - val needUpdate = MeasurementStore.store(view.id, text, paint) - if (!needUpdate) return + MeasurementStore.store(view.id, text, paint) { + val stateWrapper = view.stateWrapper ?: return@store false - val counter = forceHeightRecalculationCounter - forceHeightRecalculationCounter++ - val state = Arguments.createMap() - state.putInt("forceHeightRecalculationCounter", counter) - view.stateWrapper?.updateState(state) + forceHeightRecalculationCounter++ + val state = Arguments.createMap() + state.putInt("forceHeightRecalculationCounter", forceHeightRecalculationCounter) + stateWrapper.updateState(state) + + true + } } fun releaseMeasurementStore() { diff --git a/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt b/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt index 09363e08a..11e9016f3 100644 --- a/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt +++ b/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt @@ -44,7 +44,8 @@ object MeasurementStore { id: Int, spannable: Spannable?, paint: TextPaint, - ): Boolean { + invalidateShadowNode: () -> Boolean, + ) { val cachedWidth = data[id]?.cachedWidth ?: 0f val cachedSize = data[id]?.cachedSize ?: 0L val initialized = data[id]?.initialized ?: true @@ -52,8 +53,14 @@ object MeasurementStore { val size = measure(cachedWidth, spannable, paint) val paintParams = PaintParams(paint.typeface, paint.textSize) - data[id] = MeasurementParams(initialized, cachedWidth, size, spannable, paintParams) - return cachedSize != size + // stateWrapper might be null during the first measures, + // we want to cache the size only when it was actually + // handled in the shadow node. The cached size and the actual + // rendered one should be in sync. + val invalidated = cachedSize != size && invalidateShadowNode() + val storedSize = if (invalidated) size else cachedSize + + data[id] = MeasurementParams(initialized, cachedWidth, storedSize, spannable, paintParams) } fun release(id: Int) { @@ -130,7 +137,7 @@ object MeasurementStore { props: ReadableMap?, ): Float { val propsFontSize = props?.getDouble("fontSize")?.toFloat() - if (propsFontSize == null) return defaultView.textSize + if (propsFontSize == null || propsFontSize <= 0) return defaultView.textSize return ceil(pixelFromSpOrDp(propsFontSize, allowFontScalingFromProps(props))) } diff --git a/android/src/main/new_arch/react/renderer/components/ReactNativeEnrichedSpec/EnrichedTextInputShadowNode.h b/android/src/main/new_arch/react/renderer/components/ReactNativeEnrichedSpec/EnrichedTextInputShadowNode.h index 79aa0135b..f90e7911e 100644 --- a/android/src/main/new_arch/react/renderer/components/ReactNativeEnrichedSpec/EnrichedTextInputShadowNode.h +++ b/android/src/main/new_arch/react/renderer/components/ReactNativeEnrichedSpec/EnrichedTextInputShadowNode.h @@ -47,7 +47,7 @@ class EnrichedTextInputShadowNode final const LayoutConstraints &layoutConstraints) const override; private: - int forceHeightRecalculationCounter_; + int forceHeightRecalculationCounter_{0}; std::shared_ptr measurementsManager_; }; } // namespace facebook::react