Skip to content
Permalink
Browse files

Add support for finding multiple views with NativeIds using a single …

…listener

Reviewed By: mdvacca

Differential Revision: D6735460

fbshipit-source-id: e038a68637a00fda7058fc82e253ae68cdf67c6e
  • Loading branch information...
axe-fb authored and facebook-github-bot committed Jan 18, 2018
1 parent 71ec85f commit f5efc460ad30cc60a62edd540c3b0f45c67bcda3
@@ -2,23 +2,25 @@

package com.facebook.react.uimanager.util;

import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;

import com.facebook.react.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

/**
* Finds views in React Native view hierarchies
*/
public class ReactFindViewUtil {

private static final List<OnViewFoundListener> mOnViewFoundListeners = new ArrayList<>();
private static final Map<OnMultipleViewsFoundListener, Set<String>>
mOnMultipleViewsFoundListener = new HashMap<>();

/**
* Callback to be invoked when a react native view has been found
@@ -37,6 +39,18 @@
void onViewFound(View view);
}

/**
* Callback to be invoked when all react native views with geiven NativeIds have been found
*/
public interface OnMultipleViewsFoundListener{

void onViewFound(View view, String nativeId);
/**
* Called when all teh views have been found
* @param map
*/
}

/**
* Finds a view that is tagged with {@param nativeId} as its nativeID prop
* under the {@param root} view hierarchy. Returns the view if found, null otherwise.
@@ -90,6 +104,14 @@ public static void removeViewListener(OnViewFoundListener onViewFoundListener) {
mOnViewFoundListeners.remove(onViewFoundListener);
}

public static void addViewsListener(OnMultipleViewsFoundListener listener, Set<String> ids) {
mOnMultipleViewsFoundListener.put(listener, ids);
}

public static void removeViewsListener(OnMultipleViewsFoundListener listener) {
mOnMultipleViewsFoundListener.remove(listener);
}

/**
* Invokes any listeners that are listening on this {@param view}'s native id
*/
@@ -106,6 +128,21 @@ public static void notifyViewRendered(View view) {
iterator.remove();
}
}

Iterator<Map.Entry<OnMultipleViewsFoundListener, Set<String>>>
viewIterator = mOnMultipleViewsFoundListener.entrySet().iterator();
while (viewIterator.hasNext()) {
Map.Entry<OnMultipleViewsFoundListener, Set<String>> entry =
viewIterator.next();
Set<String> nativeIds = entry.getValue();
if (nativeIds.contains(nativeId)) {
entry.getKey().onViewFound(view, nativeId);
nativeIds.remove(nativeId); // remove it from list of NativeIds to search for.
}
if (nativeIds.isEmpty()) {
viewIterator.remove();
}
}
}

private static @Nullable String getNativeId(View view) {

0 comments on commit f5efc46

Please sign in to comment.
You can’t perform that action at this time.