Register ReactRootView into FabricUIManagerModule

Reviewed By: achen1

Differential Revision: D7043902

fbshipit-source-id: fecef5a019dadd3d2802baa20dd8a3711e566ed3
This commit is contained in:
David Vacca 2018-02-22 22:48:48 -08:00 committed by Facebook Github Bot
parent b57a78c3de
commit d352c93487
6 changed files with 73 additions and 26 deletions

View File

@ -1,6 +1,6 @@
load("//ReactNative:DEFS.bzl", "rn_android_library", "react_native_dep", "react_native_target") load("//ReactNative:DEFS.bzl", "rn_android_library", "react_native_dep", "react_native_target")
android_library( rn_android_library(
name = "fabric", name = "fabric",
srcs = glob(["*.java"]), srcs = glob(["*.java"]),
provided_deps = [ provided_deps = [
@ -13,10 +13,11 @@ android_library(
deps = [ deps = [
react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"), react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react:reactAndroid"), react_native_target("java/com/facebook/react:react"),
react_native_target("java/com/facebook/react/bridge:bridgeAndroid"), react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/uimanager:uimanagerAndroid"), react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/annotations:annotationsAndroid"), react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
react_native_target("java/com/facebook/react/module/annotations:annotationsAndroid"), react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/modules/i18nmanager:i18nmanager"),
], ],
) )

View File

@ -4,12 +4,14 @@ package com.facebook.react.fabric;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.uimanager.MeasureSpecProvider; import com.facebook.react.uimanager.MeasureSpecProvider;
import com.facebook.react.uimanager.NativeViewHierarchyOptimizer;
import com.facebook.react.uimanager.ReactRootViewTagGenerator; import com.facebook.react.uimanager.ReactRootViewTagGenerator;
import com.facebook.react.uimanager.ReactShadowNode; import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.ReactShadowNodeImpl;
import com.facebook.react.uimanager.ReactStylesDiffMap; import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.SizeMonitoringFrameLayout; import com.facebook.react.uimanager.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIModule; import com.facebook.react.uimanager.UIModule;
import com.facebook.react.uimanager.ViewManager; import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerRegistry; import com.facebook.react.uimanager.ViewManagerRegistry;
@ -24,6 +26,7 @@ import javax.annotation.Nullable;
@SuppressWarnings("unused") // used from JNI @SuppressWarnings("unused") // used from JNI
public class FabricUIManagerModule implements UIModule { public class FabricUIManagerModule implements UIModule {
private final RootShadowNodeRegistry mRootShadowNodeRegistry = new RootShadowNodeRegistry();
private final ReactApplicationContext mReactApplicationContext; private final ReactApplicationContext mReactApplicationContext;
private final ViewManagerRegistry mViewManagerRegistry; private final ViewManagerRegistry mViewManagerRegistry;
@ -43,19 +46,23 @@ public class FabricUIManagerModule implements UIModule {
ReadableMap props) { ReadableMap props) {
ViewManager viewManager = mViewManagerRegistry.get(viewName); ViewManager viewManager = mViewManagerRegistry.get(viewName);
ReactShadowNode shadowNode = viewManager.createShadowNodeInstance(mReactApplicationContext); ReactShadowNode node = viewManager.createShadowNodeInstance(mReactApplicationContext);
shadowNode.setRootTag(rootTag); node.setRootNode(getRootNode(rootTag));
shadowNode.setReactTag(reactTag); node.setReactTag(reactTag);
ReactStylesDiffMap styles = updateProps(props, shadowNode); ReactStylesDiffMap styles = updateProps(props, node);
return shadowNode; return node;
} }
private ReactStylesDiffMap updateProps(ReadableMap props, ReactShadowNode shadowNode) { private ReactShadowNode getRootNode(int rootTag) {
return mRootShadowNodeRegistry.getNode(rootTag);
}
private ReactStylesDiffMap updateProps(ReadableMap props, ReactShadowNode node) {
ReactStylesDiffMap styles = null; ReactStylesDiffMap styles = null;
if (props != null) { if (props != null) {
styles = new ReactStylesDiffMap(props); styles = new ReactStylesDiffMap(props);
shadowNode.updateProperties(styles); node.updateProperties(styles);
} }
return styles; return styles;
} }
@ -134,15 +141,34 @@ public class FabricUIManagerModule implements UIModule {
} }
public void completeRoot(int rootTag, List<ReactShadowNode> childList) { public void completeRoot(int rootTag, List<ReactShadowNode> childList) {
// TODO Diffing old Tree with new Tree? // TODO Diffing old Tree with new Tree
// Do we need to hold references to old and new tree? // Do we need to hold references to old and new tree?
} }
@Override @Override
public <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView( public <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(
final T rootView) { final T rootView) {
// TODO: complete with actual implementation int rootTag = ReactRootViewTagGenerator.getNextRootViewTag();
return ReactRootViewTagGenerator.getNextRootViewTag(); ThemedReactContext themedRootContext = new ThemedReactContext(
mReactApplicationContext,
rootView.getContext());
mRootShadowNodeRegistry.addNode(createRootShadowNode(rootTag, themedRootContext));
return rootTag;
}
public void removeRootView(int rootTag) {
mRootShadowNodeRegistry.removeNode(rootTag);
}
private ReactShadowNode createRootShadowNode(int rootTag, ThemedReactContext themedReactContext) {
ReactShadowNode rootNode = new ReactShadowNodeImpl();
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
// TODO: setLayoutDirection for the rootNode
rootNode.setViewClassName("Root");
rootNode.setReactTag(rootTag);
rootNode.setThemedContext(themedReactContext);
return rootNode;
} }
} }

View File

@ -0,0 +1,29 @@
package com.facebook.react.fabric;
import android.util.SparseArray;
import com.facebook.react.uimanager.ReactShadowNode;
/**
* Simple container class to keep track of {@link ReactShadowNode}s that represents the Root
* Shadow Nodes of a {@link FabricUIManagerModule}
*/
public class RootShadowNodeRegistry {
private final SparseArray<ReactShadowNode> mTagsToRootNodes;
public RootShadowNodeRegistry() {
mTagsToRootNodes = new SparseArray<>();
}
public void addNode(ReactShadowNode node) {
mTagsToRootNodes.put(node.getReactTag(), node);
}
public void removeNode(int tag) {
mTagsToRootNodes.remove(tag);
}
public ReactShadowNode getNode(int tag) {
return mTagsToRootNodes.get(tag);
}
}

View File

@ -132,11 +132,8 @@ public interface ReactShadowNode<T extends ReactShadowNode> {
T getRootNode(); T getRootNode();
@Deprecated() //Replaced by setRootTag method.
void setRootNode(T rootNode); void setRootNode(T rootNode);
void setRootTag(int rootTag);
void setViewClassName(String viewClassName); void setViewClassName(String viewClassName);
@Nullable @Nullable

View File

@ -410,11 +410,6 @@ public class ReactShadowNodeImpl implements ReactShadowNode<ReactShadowNodeImpl>
mRootNode = rootNode; mRootNode = rootNode;
} }
@Override
public void setRootTag(int rootTag) {
mRootTag = rootTag;
}
@Override @Override
public final void setViewClassName(String viewClassName) { public final void setViewClassName(String viewClassName) {
mViewClassName = viewClassName; mViewClassName = viewClassName;

View File

@ -277,7 +277,6 @@ public class UIImplementation {
cssNode.setReactTag(tag); cssNode.setReactTag(tag);
cssNode.setViewClassName(className); cssNode.setViewClassName(className);
cssNode.setRootNode(rootNode); cssNode.setRootNode(rootNode);
if (rootNode != null) cssNode.setRootTag(rootNode.getReactTag());
cssNode.setThemedContext(rootNode.getThemedContext()); cssNode.setThemedContext(rootNode.getThemedContext());
mShadowNodeRegistry.addNode(cssNode); mShadowNodeRegistry.addNode(cssNode);