Skip to content

Commit

Permalink
Refactor isFabric() -> getUIManagerType()
Browse files Browse the repository at this point in the history
Reviewed By: achen1

Differential Revision: D7897855

fbshipit-source-id: 6b52d989187124c81ab8ee4a732703b46b05dc65
  • Loading branch information
mdvacca authored and macdoum1 committed Jun 28, 2018
1 parent 15abee5 commit 7e3b3ee
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.facebook.react.bridge.ReactMarkerConstants.REACT_CONTEXT_THREAD_START;
import static com.facebook.react.bridge.ReactMarkerConstants.SETUP_REACT_CONTEXT_END;
import static com.facebook.react.bridge.ReactMarkerConstants.SETUP_REACT_CONTEXT_START;
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_APPS;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JS_VM_CALLS;
Expand Down Expand Up @@ -1011,7 +1012,7 @@ private void attachRootViewToInstance(
CatalystInstance catalystInstance) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.attachRootViewToInstance()");
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance");
UIManager uiManagerModule = UIManagerHelper.getUIManager(mCurrentReactContext, rootView.isFabric());
UIManager uiManagerModule = UIManagerHelper.getUIManager(mCurrentReactContext, rootView.getUIManagerType());
final int rootTag = uiManagerModule.addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.invokeJSEntryPoint();
Expand All @@ -1037,7 +1038,7 @@ private void detachViewFromInstance(
CatalystInstance catalystInstance) {
Log.d(ReactConstants.TAG, "ReactInstanceManager.detachViewFromInstance()");
UiThreadUtil.assertOnUiThread();
if (rootView.isFabric()) {
if (rootView.getUIManagerType() == FABRIC) {
catalystInstance.getJSModule(ReactFabric.class)
.unmountComponentAtNode(rootView.getId());
} else {
Expand Down
15 changes: 9 additions & 6 deletions ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package com.facebook.react;

import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
import static com.facebook.react.uimanager.common.UIManagerType.DEFAULT;

import android.content.Context;
import android.graphics.Canvas;
Expand Down Expand Up @@ -50,6 +52,7 @@
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace;
import com.facebook.react.uimanager.common.UIManagerType;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -91,7 +94,7 @@ public interface ReactRootViewEventListener {
private int mWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
private int mHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
private @Nullable Runnable mJSEntryPoint;
private boolean mIsFabric = false;
private @UIManagerType int mUIManagerType = DEFAULT;

public ReactRootView(Context context) {
super(context);
Expand Down Expand Up @@ -404,7 +407,7 @@ private void updateRootLayoutSpecs(final int widthMeasureSpec, final int heightM
@Override
public void runGuarded() {
UIManagerHelper
.getUIManager(reactApplicationContext, isFabric())
.getUIManager(reactApplicationContext, getUIManagerType())
.updateRootLayoutSpecs(getRootViewTag(), widthMeasureSpec, heightMeasureSpec);
}
});
Expand Down Expand Up @@ -507,7 +510,7 @@ private void defaultJSEntryPoint() {
if (appProperties != null) {
appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
}
if (isFabric()) {
if (getUIManagerType() == FABRIC) {
appParams.putBoolean("fabric", true);
}

Expand Down Expand Up @@ -584,11 +587,11 @@ public void handleException(final Throwable t) {
}

public void setIsFabric(boolean isFabric) {
mIsFabric = isFabric;
mUIManagerType = isFabric ? FABRIC : DEFAULT;
}

public boolean isFabric() {
return mIsFabric;
public @UIManagerType int getUIManagerType() {
return mUIManagerType;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface UIManager extends JSIModule {
* @param commandId {@link int} command id
* @param commandArgs {@link ReadableArray} parameters associated with the command
*/
void dispatchViewManagerCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs);
void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ public synchronized void completeRoot(int rootTag, @Nullable List<ReactShadowNod
}
}

@Deprecated
public void dispatchViewManagerCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs) {
public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs) {
mUIViewOperationQueue.enqueueDispatchCommand(reactTag, commandId, commandArgs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.facebook.react.uimanager;

import static com.facebook.react.uimanager.common.ViewType.FABRIC;
import static com.facebook.react.uimanager.common.ViewUtil.getViewType;
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
import static com.facebook.react.uimanager.common.UIManagerType.DEFAULT;
import static com.facebook.react.uimanager.common.ViewUtil.getUIManagerType;

import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.uimanager.common.UIManagerType;

/**
* Helper class for {@link UIManager}.
Expand All @@ -15,18 +17,27 @@ public class UIManagerHelper {
/**
* @return a {@link UIManager} that can handle the react tag received by parameter.
*/
public static UIManager getUIManager(ReactContext context, int reactTag) {
return getUIManager(context, getViewType(reactTag) == FABRIC);
public static UIManager getUIManagerForReactTag(ReactContext context, int reactTag) {
return getUIManager(context, getUIManagerType(reactTag));
}

/**
* @return a {@link UIManager} that can handle the react tag received by parameter.
*/
public static UIManager getUIManager(ReactContext context, boolean isFabric) {
public static UIManager getUIManager(ReactContext context, @UIManagerType int uiManagerType) {
CatalystInstance catalystInstance = context.getCatalystInstance();
return isFabric ?
catalystInstance.getJSIModule(UIManager.class) :
catalystInstance.getNativeModule(UIManagerModule.class);
UIManager uiManager;
switch (uiManagerType) {
case FABRIC:
uiManager = catalystInstance.getJSIModule(UIManager.class);
break;
case DEFAULT:
uiManager = catalystInstance.getNativeModule(UIManagerModule.class);
break;
default:
throw new IllegalArgumentException("Invalid UIManagerType: " + uiManagerType);
}
return uiManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_CONSTANTS_END;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_CONSTANTS_START;

import static com.facebook.react.uimanager.common.ViewType.FABRIC;
import static com.facebook.react.uimanager.common.ViewType.PAPER;
import static com.facebook.react.uimanager.common.UIManagerType.DEFAULT;

import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
Expand Down Expand Up @@ -586,19 +585,19 @@ public void clearJSResponder() {
mUIImplementation.clearJSResponder();
}

@Override
@ReactMethod
public void dispatchViewManagerCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs) {
//TODO: this is a temporary approach to support ViewManagerCommands in Fabric until
// the dispatchViewManagerCommand() method is supported by Fabric JS API.
if (ViewUtil.getViewType(reactTag) == FABRIC) {
UIManagerHelper.getUIManager(getReactApplicationContext(), true)
.dispatchViewManagerCommand(reactTag, commandId, commandArgs);
} else {
mUIImplementation.dispatchViewManagerCommand(reactTag, commandId, commandArgs);
}
UIManagerHelper.getUIManager(getReactApplicationContext(), ViewUtil.getUIManagerType(reactTag))
.dispatchCommand(reactTag, commandId, commandArgs);
}

@Override
public void dispatchCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs) {
mUIImplementation.dispatchViewManagerCommand(reactTag, commandId, commandArgs);
}

@ReactMethod
public void playTouchSound() {
AudioManager audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
*/
package com.facebook.react.uimanager.common;

import static com.facebook.react.uimanager.common.ViewType.FABRIC;
import static com.facebook.react.uimanager.common.ViewType.PAPER;
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
import static com.facebook.react.uimanager.common.UIManagerType.DEFAULT;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Retention;
import android.support.annotation.IntDef;

@Retention(SOURCE)
@IntDef({PAPER, FABRIC})
public @interface ViewType {
int PAPER = 1;
@IntDef({DEFAULT, FABRIC})
public @interface UIManagerType {
int DEFAULT = 1;
int FABRIC = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@

package com.facebook.react.uimanager.common;

import static com.facebook.react.uimanager.common.ViewType.FABRIC;
import static com.facebook.react.uimanager.common.ViewType.PAPER;
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
import static com.facebook.react.uimanager.common.UIManagerType.DEFAULT;

public class ViewUtil {

/**
* Counter for uniquely identifying views.
* - % 10 === 1 means it is a rootTag.
* - % 2 === 0 means it is a Fabric tag.
* See https://github.com/facebook/react/pull/12587
*
* @param reactTag {@link }
*/
@ViewType
public static int getViewType(int reactTag) {
@UIManagerType
public static int getUIManagerType(int reactTag) {
if (reactTag % 2 == 0) return FABRIC;
return PAPER;
return DEFAULT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.facebook.react.common.MapBuilder;
import com.facebook.react.modules.core.ChoreographerCompat;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.uimanager.common.UIManagerType;
import com.facebook.systrace.Systrace;

/**
Expand Down

0 comments on commit 7e3b3ee

Please sign in to comment.