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

Reviewed By: mdvacca

Differential Revision: D6735460

fbshipit-source-id: e038a68637a00fda7058fc82e253ae68cdf67c6e
This commit is contained in:
Ram N 2018-01-18 11:32:10 -08:00 committed by Facebook Github Bot
parent 71ec85f24c
commit f5efc460ad
1 changed files with 44 additions and 7 deletions

View File

@ -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<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 @@ 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<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 class ReactFindViewUtil {
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) {