Skip to content

Commit

Permalink
Cleanup ReactTextInputManager inner classes and helpers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javache authored and facebook-github-bot committed May 23, 2022
1 parent f2fa286 commit 071cae8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -642,7 +641,7 @@ private static boolean sameTextForSpan(
// 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;
}

Expand Down Expand Up @@ -748,9 +747,7 @@ private void setIntrinsicContentSize() {
// 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) {
Expand Down Expand Up @@ -986,7 +983,7 @@ public FabricViewStateManager getFabricViewStateManager() {
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -972,12 +971,11 @@ private static EventDispatcher getEventDispatcher(
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) {
Expand Down Expand Up @@ -1013,24 +1011,23 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
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.
Expand Down Expand Up @@ -1126,11 +1123,11 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {
}

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;
Expand Down Expand Up @@ -1174,17 +1171,15 @@ public void onLayout() {
}
}

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);
Expand Down Expand Up @@ -1213,12 +1208,11 @@ public void onSelectionChanged(int start, int end) {
}

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;
Expand Down Expand Up @@ -1272,14 +1266,6 @@ public void setPadding(ReactEditText view, int left, int top, int right, int bot
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) {
Expand All @@ -1290,17 +1276,15 @@ public Object updateState(
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");
}
Expand Down

0 comments on commit 071cae8

Please sign in to comment.