From f5efc460ad30cc60a62edd540c3b0f45c67bcda3 Mon Sep 17 00:00:00 2001 From: Ram N Date: Thu, 18 Jan 2018 11:32:10 -0800 Subject: [PATCH] Add support for finding multiple views with NativeIds using a single listener Reviewed By: mdvacca Differential Revision: D6735460 fbshipit-source-id: e038a68637a00fda7058fc82e253ae68cdf67c6e --- .../uimanager/util/ReactFindViewUtil.java | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/util/ReactFindViewUtil.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/util/ReactFindViewUtil.java index 1e4918bb1..1d0d41d82 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/util/ReactFindViewUtil.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/util/ReactFindViewUtil.java @@ -2,16 +2,16 @@ 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 @@ -19,6 +19,8 @@ import com.facebook.react.R; public class ReactFindViewUtil { private static final List mOnViewFoundListeners = new ArrayList<>(); + private static final Map> + mOnMultipleViewsFoundListener = new HashMap<>(); /** * Callback to be invoked when a react native view has been found @@ -37,6 +39,18 @@ public class ReactFindViewUtil { 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 class ReactFindViewUtil { mOnViewFoundListeners.remove(onViewFoundListener); } + public static void addViewsListener(OnMultipleViewsFoundListener listener, Set 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 class ReactFindViewUtil { iterator.remove(); } } + + Iterator>> + viewIterator = mOnMultipleViewsFoundListener.entrySet().iterator(); + while (viewIterator.hasNext()) { + Map.Entry> entry = + viewIterator.next(); + Set 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) {