Allow update of state to receive null objects

Summary:
This diff changes updateState method to support null stateWrappers, before this method would crash with a NullPointerException, now it allows the null object to reach the view manager.

Changelog: Add support for update of nullable localState in Fabric

Reviewed By: JoshuaGross

Differential Revision: D17939651

fbshipit-source-id: c62555905e39f9e0db75b9e1d1b93f33d0560266
This commit is contained in:
David Vacca
2019-10-15 18:50:31 -07:00
committed by Facebook Github Bot
parent 79b573511b
commit c65267ca6f
6 changed files with 13 additions and 10 deletions
@@ -339,7 +339,7 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
@DoNotStrip
@SuppressWarnings("unused")
private MountItem updateStateMountItem(int reactTag, Object stateWrapper) {
private MountItem updateStateMountItem(int reactTag, @Nullable Object stateWrapper) {
return new UpdateStateMountItem(reactTag, (StateWrapper) stateWrapper);
}
@@ -369,11 +369,12 @@ public class MountingManager {
}
@UiThread
public void updateState(final int reactTag, StateWrapper stateWrapper) {
public void updateState(final int reactTag, @Nullable StateWrapper stateWrapper) {
UiThreadUtil.assertOnUiThread();
ViewState viewState = getViewState(reactTag);
ReadableNativeMap newState = stateWrapper.getState();
if (viewState.mCurrentState != null && viewState.mCurrentState.equals(newState)) {
@Nullable ReadableNativeMap newState = stateWrapper == null ? null : stateWrapper.getState();
if ((viewState.mCurrentState != null && viewState.mCurrentState.equals(newState))
|| (viewState.mCurrentState == null && stateWrapper == null)) {
return;
}
viewState.mCurrentState = newState;
@@ -6,15 +6,16 @@
*/
package com.facebook.react.fabric.mounting.mountitems;
import androidx.annotation.Nullable;
import com.facebook.react.fabric.mounting.MountingManager;
import com.facebook.react.uimanager.StateWrapper;
public class UpdateStateMountItem implements MountItem {
private final int mReactTag;
private final StateWrapper mStateWrapper;
@Nullable private final StateWrapper mStateWrapper;
public UpdateStateMountItem(int reactTag, StateWrapper stateWrapper) {
public UpdateStateMountItem(int reactTag, @Nullable StateWrapper stateWrapper) {
mReactTag = reactTag;
mStateWrapper = stateWrapper;
}
@@ -282,7 +282,7 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
* this component type.
*/
public @Nullable Object updateState(
@NonNull T view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
@NonNull T view, ReactStylesDiffMap props, @Nullable StateWrapper stateWrapper) {
return null;
}
@@ -84,7 +84,6 @@ public class ReactModalHostManager extends ViewGroupManager<ReactModalHostView>
view.setStatusBarTranslucent(statusBarTranslucent);
}
@Override
@ReactProp(name = "hardwareAccelerated")
public void setHardwareAccelerated(ReactModalHostView view, boolean hardwareAccelerated) {
@@ -139,7 +138,8 @@ public class ReactModalHostManager extends ViewGroupManager<ReactModalHostView>
@Override
public Object updateState(
ReactModalHostView view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
ReactModalHostView view, ReactStylesDiffMap props, @Nullable StateWrapper stateWrapper) {
// TODO T55794595: Add support for updating state with null stateWrapper
Point modalSize = ModalHostHelper.getModalHostSize(view.getContext());
view.updateState(stateWrapper, modalSize.x, modalSize.y);
return null;
@@ -74,7 +74,8 @@ public class ReactTextViewManager
@Override
public Object updateState(
ReactTextView view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
ReactTextView view, ReactStylesDiffMap props, @Nullable StateWrapper stateWrapper) {
// TODO T55794595: Add support for updating state with null stateWrapper
ReadableMap attributedString = stateWrapper.getState().getMap("attributedString");
Spannable spanned =