Allow extending NativeViewHierarchyManager

Summary: public This diff makes a few small changes to NativeViewHierarchyManager to allow extending it:
a) makes the class public so it can be constructed from outside of the package
b) adds resolveView and resolveViewManager to access mTagsToViews and mTagsToViewManagers
c) changes addRootView signature to make root view a ViewGroup instead of SizeMonitoringFrameLayout

The reason behind change c) is that in FlatUIImplementation I want to use a root view that does not extend SizeMonitoringFrameLayout. NativeViewHierarchyManager doesn't really use any of the root view properties, so it could be even a View, but ViewGroup seems more fitting.

This diff should contain no functional changes or other side-effects.

Reviewed By: astreet

Differential Revision: D2554841

fb-gh-sync-id: cce748707cf7485d456e4a057dae1db87aa17160
This commit is contained in:
Denis Koroskin
2015-11-30 18:03:09 -08:00
committed by facebook-github-bot-7
parent fe28118a48
commit 00046bc832

View File

@@ -56,7 +56,7 @@ import com.facebook.react.touch.JSResponderHandler;
* TODO(5483031): Only dispatch updates when shadow views have changed * TODO(5483031): Only dispatch updates when shadow views have changed
*/ */
@NotThreadSafe @NotThreadSafe
/* package */ final class NativeViewHierarchyManager { public class NativeViewHierarchyManager {
private final AnimationRegistry mAnimationRegistry; private final AnimationRegistry mAnimationRegistry;
private final SparseArray<View> mTagsToViews; private final SparseArray<View> mTagsToViews;
@@ -74,6 +74,23 @@ import com.facebook.react.touch.JSResponderHandler;
mRootTags = new SparseBooleanArray(); mRootTags = new SparseBooleanArray();
} }
protected final View resolveView(int tag) {
View view = mTagsToViews.get(tag);
if (view == null) {
throw new IllegalViewOperationException("Trying to resolve view with tag " + tag
+ " which doesn't exist");
}
return view;
}
protected final ViewManager resolveViewManager(int tag) {
ViewManager viewManager = mTagsToViewManagers.get(tag);
if (viewManager == null) {
throw new IllegalViewOperationException("ViewManager for tag " + tag + " could not be found");
}
return viewManager;
}
public AnimationRegistry getAnimationRegistry() { public AnimationRegistry getAnimationRegistry() {
return mAnimationRegistry; return mAnimationRegistry;
} }
@@ -81,32 +98,16 @@ import com.facebook.react.touch.JSResponderHandler;
public void updateProperties(int tag, CatalystStylesDiffMap props) { public void updateProperties(int tag, CatalystStylesDiffMap props) {
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
ViewManager viewManager = mTagsToViewManagers.get(tag); ViewManager viewManager = resolveViewManager(tag);
if (viewManager == null) { View viewToUpdate = resolveView(tag);
throw new IllegalViewOperationException("ViewManager for tag " + tag + " could not be found");
}
View viewToUpdate = mTagsToViews.get(tag);
if (viewToUpdate == null) {
throw new IllegalViewOperationException("Trying to update view with tag " + tag
+ " which doesn't exist");
}
viewManager.updateProperties(viewToUpdate, props); viewManager.updateProperties(viewToUpdate, props);
} }
public void updateViewExtraData(int tag, Object extraData) { public void updateViewExtraData(int tag, Object extraData) {
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
ViewManager viewManager = mTagsToViewManagers.get(tag); ViewManager viewManager = resolveViewManager(tag);
if (viewManager == null) { View viewToUpdate = resolveView(tag);
throw new IllegalViewOperationException("ViewManager for tag " + tag + " could not be found");
}
View viewToUpdate = mTagsToViews.get(tag);
if (viewToUpdate == null) {
throw new IllegalViewOperationException("Trying to update view with tag " + tag + " which " +
"doesn't exist");
}
viewManager.updateExtraData(viewToUpdate, extraData); viewManager.updateExtraData(viewToUpdate, extraData);
} }
@@ -119,11 +120,7 @@ import com.facebook.react.touch.JSResponderHandler;
int height) { int height) {
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
View viewToUpdate = mTagsToViews.get(tag); View viewToUpdate = resolveView(tag);
if (viewToUpdate == null) {
throw new IllegalViewOperationException("Trying to update view with tag " + tag + " which " +
"doesn't exist");
}
// Even though we have exact dimensions, we still call measure because some platform views (e.g. // Even though we have exact dimensions, we still call measure because some platform views (e.g.
// Switch) assume that method will always be called before onLayout and onDraw. They use it to // Switch) assume that method will always be called before onLayout and onDraw. They use it to
@@ -257,10 +254,7 @@ import com.facebook.react.touch.JSResponderHandler;
@Nullable ViewAtIndex[] viewsToAdd, @Nullable ViewAtIndex[] viewsToAdd,
@Nullable int[] tagsToDelete) { @Nullable int[] tagsToDelete) {
ViewGroup viewToManage = (ViewGroup) mTagsToViews.get(tag); ViewGroup viewToManage = (ViewGroup) mTagsToViews.get(tag);
ViewGroupManager viewManager = (ViewGroupManager) mTagsToViewManagers.get(tag); ViewGroupManager viewManager = (ViewGroupManager) resolveViewManager(tag);
if (viewManager == null) {
throw new IllegalViewOperationException("ViewManager for tag " + tag + " could not be found");
}
if (viewToManage == null) { if (viewToManage == null) {
throw new IllegalViewOperationException("Trying to manageChildren view with tag " + tag + throw new IllegalViewOperationException("Trying to manageChildren view with tag " + tag +
" which doesn't exist\n detail: " + " which doesn't exist\n detail: " +
@@ -362,6 +356,13 @@ import com.facebook.react.touch.JSResponderHandler;
int tag, int tag,
SizeMonitoringFrameLayout view, SizeMonitoringFrameLayout view,
ThemedReactContext themedContext) { ThemedReactContext themedContext) {
addRootViewGroup(tag, view, themedContext);
}
protected final void addRootViewGroup(
int tag,
ViewGroup view,
ThemedReactContext themedContext) {
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
if (view.getId() != View.NO_ID) { if (view.getId() != View.NO_ID) {
throw new IllegalViewOperationException( throw new IllegalViewOperationException(
@@ -383,7 +384,7 @@ import com.facebook.react.touch.JSResponderHandler;
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
if (!mRootTags.get(view.getId())) { if (!mRootTags.get(view.getId())) {
// For non-root views we notify viewmanager with {@link ViewManager#onDropInstance} // For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
Assertions.assertNotNull(mTagsToViewManagers.get(view.getId())).onDropViewInstance( resolveViewManager(view.getId()).onDropViewInstance(
(ThemedReactContext) view.getContext(), (ThemedReactContext) view.getContext(),
view); view);
} }
@@ -512,12 +513,7 @@ import com.facebook.react.touch.JSResponderHandler;
"with tag " + reactTag); "with tag " + reactTag);
} }
ViewManager viewManager = mTagsToViewManagers.get(reactTag); ViewManager viewManager = resolveViewManager(reactTag);
if (viewManager == null) {
throw new IllegalViewOperationException(
"ViewManager for view tag " + reactTag + " could not be found");
}
viewManager.receiveCommand(view, commandId, args); viewManager.receiveCommand(view, commandId, args);
} }