Skip to content

Commit

Permalink
Implement addUIBlock to allow third party to resolve a view
Browse files Browse the repository at this point in the history
Summary:
This PR follows the work started in #6431 but instead of implementing the snapshot for Android, will just allow that feature to be implemented by a third party library.

The missing feature is the ability to resolve a View for a given tag integer. which is now possible with a `addUIBlock` on the `UIManagerModule` method where you give a `UIBlock` instance (a new class) that implements a `execute(NativeViewHierarchyManager)` function.

This is already possible in iOS API. a third party can use the `addUIBlock` too.
I have kept the name `addUIBlock` so it's the same as in the [iOS' API](https://github.com/facebook/react-native/search?q=addUIBlock&type=Code&utf8=%E2%9C%93).

 ---

With this PR a third party lib can now do...

```java
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
  public void execute (NativeViewHierarchyManager nvhm) {
    View view = nvhm.resolveView(tag);
    ...do something with view... like... screenshot t
Closes #8217

Differential Revision: D3469311

Pulled By: astreet

fbshipit-source-id: bb56ecc7e8936299337af47ca8114875ee1fd2b0
  • Loading branch information
gre authored and Facebook Github Bot 2 committed Jun 22, 2016
1 parent 4c83237 commit af28de5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public NativeViewHierarchyManager(ViewManagerRegistry viewManagers) {
mRootTags = new SparseBooleanArray();
}

protected final View resolveView(int tag) {
public final View resolveView(int tag) {
View view = mTagsToViews.get(tag);
if (view == null) {
throw new IllegalViewOperationException("Trying to resolve view with tag " + tag
Expand All @@ -92,7 +92,7 @@ protected final View resolveView(int tag) {
return view;
}

protected final ViewManager resolveViewManager(int tag) {
public final ViewManager resolveViewManager(int tag) {
ViewManager viewManager = mTagsToViewManagers.get(tag);
if (viewManager == null) {
throw new IllegalViewOperationException("ViewManager for tag " + tag + " could not be found");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.facebook.react.uimanager;

import android.view.View;

/**
* A task to execute on the UI View for third party libraries.
*/
public interface UIBlock {
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,8 @@ protected void applyUpdatesRecursive(
}
cssNode.markUpdateSeen();
}

public void addUIBlock(UIBlock block) {
mOperationsQueue.enqueueUIBlock(block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,24 @@ public EventDispatcher getEventDispatcher() {
public void sendAccessibilityEvent(int tag, int eventType) {
mUIImplementation.sendAccessibilityEvent(tag, eventType);
}

/**
* Schedule a block to be executed on the UI thread. Useful if you need to execute
* view logic after all currently queued view updates have completed.
*
* @param block that contains UI logic you want to execute.
*
* Usage Example:
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute (NativeViewHierarchyManager nvhm) {
View view = nvhm.resolveView(tag);
// ...execute your code on View (e.g. snapshot the view)
}
});
*/
public void addUIBlock (UIBlock block) {
mUIImplementation.addUIBlock(block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ public void execute() {
}
}

private class UIBlockOperation implements UIOperation {
private final UIBlock mBlock;
public UIBlockOperation (UIBlock block) {
mBlock = block;
}

@Override
public void execute() {
mBlock.execute(mNativeViewHierarchyManager);
}
}

private final class SendAccessibilityEvent extends ViewOperation {

private final int mEventType;
Expand Down Expand Up @@ -721,6 +733,10 @@ public void enqueueSendAccessibilityEvent(int tag, int eventType) {
mOperations.add(new SendAccessibilityEvent(tag, eventType));
}

public void enqueueUIBlock(UIBlock block) {
mOperations.add(new UIBlockOperation(block));
}

/* package */ void dispatchViewUpdates(final int batchId) {
// Store the current operation queues to dispatch and create new empty ones to continue
// receiving new operations
Expand Down

0 comments on commit af28de5

Please sign in to comment.