Cleanup ReactTextInputManager inner classes and helpers

Summary:
* Make inner classes static where possible
* Make member variables final when set from constructor
* Remove Nullable on `mFabricViewStateManager` and associated checks
* Remove `createInternalEditText` which has moved to the ShadowNode (paper-only)

Changelog: [Internal]

Reviewed By: genkikondo

Differential Revision: D36545807

fbshipit-source-id: 85517511d1734f0e55de5caa012e32feb40e8492
This commit is contained in:
Pieter De Baets
2022-05-23 03:59:47 -07:00
committed by Facebook GitHub Bot
parent f2fa2860d1
commit 071cae8251
2 changed files with 31 additions and 50 deletions
@@ -114,8 +114,7 @@ public class ReactEditText extends AppCompatEditText
private ReactViewBackgroundManager mReactBackgroundManager;
private final @Nullable FabricViewStateManager mFabricViewStateManager =
new FabricViewStateManager();
private final FabricViewStateManager mFabricViewStateManager = new FabricViewStateManager();
protected boolean mDisableTextDiffing = false;
protected boolean mIsSettingTextFromState = false;
@@ -642,7 +641,7 @@ public class ReactEditText extends AppCompatEditText
// This is hacked in for Fabric. When we delete non-Fabric code, we might be able to simplify or
// clean this up a bit.
private void addSpansForMeasurement(Spannable spannable) {
if (mFabricViewStateManager != null && !mFabricViewStateManager.hasStateWrapper()) {
if (!mFabricViewStateManager.hasStateWrapper()) {
return;
}
@@ -748,9 +747,7 @@ public class ReactEditText extends AppCompatEditText
// view, we don't need to construct one or apply it at all - it provides no use in Fabric.
ReactContext reactContext = getReactContext(this);
if (mFabricViewStateManager != null
&& !mFabricViewStateManager.hasStateWrapper()
&& !reactContext.isBridgeless()) {
if (!mFabricViewStateManager.hasStateWrapper() && !reactContext.isBridgeless()) {
final ReactTextInputLocalData localData = new ReactTextInputLocalData(this);
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
if (uiManager != null) {
@@ -986,7 +983,7 @@ public class ReactEditText extends AppCompatEditText
*/
private void updateCachedSpannable(boolean resetStyles) {
// Noops in non-Fabric
if (mFabricViewStateManager != null && !mFabricViewStateManager.hasStateWrapper()) {
if (!mFabricViewStateManager.hasStateWrapper()) {
return;
}
// If this view doesn't have an ID yet, we don't have a cache key, so bail here
@@ -27,7 +27,6 @@ import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.autofill.HintConstants;
@@ -972,12 +971,11 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
return UIManagerHelper.getEventDispatcherForReactTag(reactContext, editText.getId());
}
private class ReactTextInputTextWatcher implements TextWatcher {
private EventDispatcher mEventDispatcher;
private ReactEditText mEditText;
private final class ReactTextInputTextWatcher implements TextWatcher {
private final ReactEditText mEditText;
private final EventDispatcher mEventDispatcher;
private final int mSurfaceId;
private String mPreviousText;
private int mSurfaceId;
public ReactTextInputTextWatcher(
final ReactContext reactContext, final ReactEditText editText) {
@@ -1013,24 +1011,23 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
return;
}
if (mEditText.getFabricViewStateManager().hasStateWrapper()) {
FabricViewStateManager stateManager = mEditText.getFabricViewStateManager();
if (stateManager.hasStateWrapper()) {
// Fabric: communicate to C++ layer that text has changed
// We need to call `incrementAndGetEventCounter` here explicitly because this
// update may race with other updates.
// We simply pass in the cache ID, which never changes, but UpdateState will still be called
// on the native side, triggering a measure.
mEditText
.getFabricViewStateManager()
.setState(
new FabricViewStateManager.StateUpdateCallback() {
@Override
public WritableMap getStateUpdate() {
WritableMap map = new WritableNativeMap();
map.putInt("mostRecentEventCount", mEditText.incrementAndGetEventCounter());
map.putInt("opaqueCacheId", mEditText.getId());
return map;
}
});
stateManager.setState(
new FabricViewStateManager.StateUpdateCallback() {
@Override
public WritableMap getStateUpdate() {
WritableMap map = new WritableNativeMap();
map.putInt("mostRecentEventCount", mEditText.incrementAndGetEventCounter());
map.putInt("opaqueCacheId", mEditText.getId());
return map;
}
});
}
// The event that contains the event counter and updates it must be sent first.
@@ -1126,11 +1123,11 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
}
private static class ReactContentSizeWatcher implements ContentSizeWatcher {
private ReactEditText mEditText;
private @Nullable EventDispatcher mEventDispatcher;
private final ReactEditText mEditText;
private final EventDispatcher mEventDispatcher;
private final int mSurfaceId;
private int mPreviousContentWidth = 0;
private int mPreviousContentHeight = 0;
private int mSurfaceId;
public ReactContentSizeWatcher(ReactEditText editText) {
mEditText = editText;
@@ -1174,17 +1171,15 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
}
}
private class ReactSelectionWatcher implements SelectionWatcher {
private ReactEditText mReactEditText;
private EventDispatcher mEventDispatcher;
private static class ReactSelectionWatcher implements SelectionWatcher {
private final ReactEditText mReactEditText;
private final EventDispatcher mEventDispatcher;
private final int mSurfaceId;
private int mPreviousSelectionStart;
private int mPreviousSelectionEnd;
private int mSurfaceId;
public ReactSelectionWatcher(ReactEditText editText) {
mReactEditText = editText;
ReactContext reactContext = getReactContext(editText);
mEventDispatcher = getEventDispatcher(reactContext, editText);
mSurfaceId = UIManagerHelper.getSurfaceId(reactContext);
@@ -1213,12 +1208,11 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
}
private static class ReactScrollWatcher implements ScrollWatcher {
private ReactEditText mReactEditText;
private EventDispatcher mEventDispatcher;
private final ReactEditText mReactEditText;
private final EventDispatcher mEventDispatcher;
private final int mSurfaceId;
private int mPreviousHoriz;
private int mPreviousVert;
private int mSurfaceId;
public ReactScrollWatcher(ReactEditText editText) {
mReactEditText = editText;
@@ -1272,14 +1266,6 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
view.setPadding(left, top, right, bottom);
}
/**
* May be overridden by subclasses that would like to provide their own instance of the internal
* {@code EditText} this class uses to determine the expected size of the view.
*/
protected EditText createInternalEditText(ThemedReactContext themedReactContext) {
return new EditText(themedReactContext);
}
@Override
public Object updateState(
ReactEditText view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
@@ -1290,17 +1276,15 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
view.getFabricViewStateManager().setStateWrapper(stateWrapper);
ReadableNativeMap state = stateWrapper.getStateData();
if (state == null) {
return null;
}
if (!state.hasKey("attributedString")) {
return null;
}
ReadableMap attributedString = state.getMap("attributedString");
ReadableMap paragraphAttributes = state.getMap("paragraphAttributes");
if (attributedString == null || paragraphAttributes == null) {
throw new IllegalArgumentException("Invalid TextInput State was received as a parameters");
}