Skip to content

Commit f5efc46

Browse files
axe-fbfacebook-github-bot
authored andcommitted
Add support for finding multiple views with NativeIds using a single listener
Reviewed By: mdvacca Differential Revision: D6735460 fbshipit-source-id: e038a68637a00fda7058fc82e253ae68cdf67c6e
1 parent 71ec85f commit f5efc46

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/util/ReactFindViewUtil.java

+44-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
package com.facebook.react.uimanager.util;
44

5-
import javax.annotation.Nullable;
6-
7-
import java.util.ArrayList;
8-
import java.util.Iterator;
9-
import java.util.List;
10-
115
import android.view.View;
126
import android.view.ViewGroup;
13-
147
import com.facebook.react.R;
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Set;
14+
import javax.annotation.Nullable;
1515

1616
/**
1717
* Finds views in React Native view hierarchies
1818
*/
1919
public class ReactFindViewUtil {
2020

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

2325
/**
2426
* Callback to be invoked when a react native view has been found
@@ -37,6 +39,18 @@ public interface OnViewFoundListener {
3739
void onViewFound(View view);
3840
}
3941

42+
/**
43+
* Callback to be invoked when all react native views with geiven NativeIds have been found
44+
*/
45+
public interface OnMultipleViewsFoundListener{
46+
47+
void onViewFound(View view, String nativeId);
48+
/**
49+
* Called when all teh views have been found
50+
* @param map
51+
*/
52+
}
53+
4054
/**
4155
* Finds a view that is tagged with {@param nativeId} as its nativeID prop
4256
* under the {@param root} view hierarchy. Returns the view if found, null otherwise.
@@ -90,6 +104,14 @@ public static void removeViewListener(OnViewFoundListener onViewFoundListener) {
90104
mOnViewFoundListeners.remove(onViewFoundListener);
91105
}
92106

107+
public static void addViewsListener(OnMultipleViewsFoundListener listener, Set<String> ids) {
108+
mOnMultipleViewsFoundListener.put(listener, ids);
109+
}
110+
111+
public static void removeViewsListener(OnMultipleViewsFoundListener listener) {
112+
mOnMultipleViewsFoundListener.remove(listener);
113+
}
114+
93115
/**
94116
* Invokes any listeners that are listening on this {@param view}'s native id
95117
*/
@@ -106,6 +128,21 @@ public static void notifyViewRendered(View view) {
106128
iterator.remove();
107129
}
108130
}
131+
132+
Iterator<Map.Entry<OnMultipleViewsFoundListener, Set<String>>>
133+
viewIterator = mOnMultipleViewsFoundListener.entrySet().iterator();
134+
while (viewIterator.hasNext()) {
135+
Map.Entry<OnMultipleViewsFoundListener, Set<String>> entry =
136+
viewIterator.next();
137+
Set<String> nativeIds = entry.getValue();
138+
if (nativeIds.contains(nativeId)) {
139+
entry.getKey().onViewFound(view, nativeId);
140+
nativeIds.remove(nativeId); // remove it from list of NativeIds to search for.
141+
}
142+
if (nativeIds.isEmpty()) {
143+
viewIterator.remove();
144+
}
145+
}
109146
}
110147

111148
private static @Nullable String getNativeId(View view) {

0 commit comments

Comments
 (0)