Implement FabricUIManagerModule in Android

Reviewed By: achen1

Differential Revision: D7031902

fbshipit-source-id: a8d9d505f981ac4268760efa32f4cbc7955aec32
This commit is contained in:
David Vacca 2018-02-21 09:21:34 -08:00 committed by Facebook Github Bot
parent 47addd8e34
commit 4371d1e1d0
6 changed files with 75 additions and 24 deletions

View File

@ -170,7 +170,7 @@ import javax.inject.Provider;
} else {
return new UIManagerModule(
reactContext,
mReactInstanceManager.createAllViewManagers(reactContext),
mReactInstanceManager.getOrCreateViewManagers(reactContext),
mUIImplementationProvider,
mMinTimeLeftInFrameForNonBatchedOperationMs);
}

View File

@ -156,6 +156,7 @@ public class ReactInstanceManager {
private final boolean mLazyNativeModulesEnabled;
private final boolean mDelayViewManagerClassLoadsEnabled;
private final @Nullable BridgeListener mBridgeListener;
private List<ViewManager> mViewManagers;
private class ReactContextInitParams {
private final JavaScriptExecutorFactory mJsExecutorFactory;
@ -774,18 +775,23 @@ public class ReactInstanceManager {
/**
* Uses configured {@link ReactPackage} instances to create all view managers.
*/
public List<ViewManager> createAllViewManagers(
public List<ViewManager> getOrCreateViewManagers(
ReactApplicationContext catalystApplicationContext) {
ReactMarker.logMarker(CREATE_VIEW_MANAGERS_START);
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAllViewManagers");
try {
synchronized (mPackages) {
List<ViewManager> allViewManagers = new ArrayList<>();
for (ReactPackage reactPackage : mPackages) {
allViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext));
if (mViewManagers == null) {
synchronized (mPackages) {
if (mViewManagers == null) {
mViewManagers = new ArrayList<>();
for (ReactPackage reactPackage : mPackages) {
mViewManagers.addAll(reactPackage.createViewManagers(catalystApplicationContext));
}
return mViewManagers;
}
}
return allViewManagers;
}
return mViewManagers;
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
ReactMarker.logMarker(CREATE_VIEW_MANAGERS_END);

View File

@ -4,7 +4,15 @@ package com.facebook.react.fabric;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.MeasureSpecProvider;
import com.facebook.react.uimanager.NativeViewHierarchyOptimizer;
import com.facebook.react.uimanager.ReactRootViewTagGenerator;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.UIModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerRegistry;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
@ -13,12 +21,16 @@ import javax.annotation.Nullable;
* This class is responsible to create, clone and update {@link ReactShadowNode} using the
* Fabric API.
*/
public class FabricUIManagerModule {
@SuppressWarnings("unused") // used from JNI
public class FabricUIManagerModule implements UIModule {
private final ReactApplicationContext mReactApplicationContext;
private final ViewManagerRegistry mViewManagerRegistry;
public FabricUIManagerModule(ReactApplicationContext reactContext) {
public FabricUIManagerModule(ReactApplicationContext reactContext,
ViewManagerRegistry viewManagerRegistry) {
mReactApplicationContext = reactContext;
mViewManagerRegistry = viewManagerRegistry;
}
/**
@ -28,10 +40,24 @@ public class FabricUIManagerModule {
public ReactShadowNode createNode(int reactTag,
String viewName,
int rootTag,
ReadableMap props,
int instanceHandle) {
//TODO T25560658
return null;
ReadableMap props) {
ViewManager viewManager = mViewManagerRegistry.get(viewName);
ReactShadowNode shadowNode = viewManager.createShadowNodeInstance(mReactApplicationContext);
shadowNode.setRootTag(rootTag);
shadowNode.setReactTag(reactTag);
ReactStylesDiffMap styles = updateProps(props, shadowNode);
return shadowNode;
}
private ReactStylesDiffMap updateProps(ReadableMap props, ReactShadowNode shadowNode) {
ReactStylesDiffMap styles = null;
if (props != null) {
styles = new ReactStylesDiffMap(props);
shadowNode.updateProperties(styles);
}
return styles;
}
/**
@ -41,8 +67,7 @@ public class FabricUIManagerModule {
*/
@Nullable
public ReactShadowNode cloneNode(ReactShadowNode node) {
//TODO T25560658
return null;
return node.mutableCopy();
}
/**
@ -52,8 +77,9 @@ public class FabricUIManagerModule {
*/
@Nullable
public ReactShadowNode cloneNodeWithNewChildren(ReactShadowNode node) {
//TODO T25560658
return null;
ReactShadowNode clone = cloneNode(node);
clone.removeAllChildren();
return clone;
}
/**
@ -63,8 +89,9 @@ public class FabricUIManagerModule {
*/
@Nullable
public ReactShadowNode cloneNodeWithNewProps(ReactShadowNode node, ReadableMap newProps) {
//TODO T25560658
return null;
ReactShadowNode clone = cloneNode(node);
updateProps(newProps, clone);
return clone;
}
/**
@ -77,8 +104,9 @@ public class FabricUIManagerModule {
public ReactShadowNode cloneNodeWithNewChildrenAndProps(
ReactShadowNode node,
ReadableMap newProps) {
//TODO T25560658
return null;
ReactShadowNode clone = cloneNodeWithNewChildren(node);
updateProps(newProps, clone);
return clone;
}
/**
@ -87,7 +115,7 @@ public class FabricUIManagerModule {
*/
@Nullable
public void appendChild(ReactShadowNode parent, ReactShadowNode child) {
//TODO T25560658
parent.addChildAt(child, parent.getChildCount());
}
/**
@ -106,7 +134,15 @@ public class FabricUIManagerModule {
}
public void completeRoot(int rootTag, List<ReactShadowNode> childList) {
//TODO T25560658
// TODO Diffing old Tree with new Tree?
// Do we need to hold references to old and new tree?
}
@Override
public <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(
final T rootView) {
// TODO: complete with actual implementation
return ReactRootViewTagGenerator.getNextRootViewTag();
}
}

View File

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

View File

@ -72,7 +72,7 @@ import javax.annotation.Nullable;
*/
@ReactModule(name = UIManagerModule.NAME)
public class UIManagerModule extends ReactContextBaseJavaModule implements
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter {
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter, UIModule {
/**
* Enables lazy discovery of a specific {@link ViewManager} by its name.
@ -287,6 +287,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
*
* <p>TODO(6242243): Make addRootView thread safe NB: this method is horribly not-thread-safe.
*/
@Override
public <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(
final T rootView) {
Systrace.beginSection(

View File

@ -0,0 +1,7 @@
package com.facebook.react.uimanager;
public interface UIModule {
<T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(final T rootView);
}