Parcourir la source

Hide YellowBox in TopBar components

Guy Carmeli il y a 6 ans
Parent
révision
5eee64a0ea

+ 5
- 1
lib/android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java Voir le fichier

@@ -32,13 +32,17 @@ public class ViewUtils {
32 32
     }
33 33
 
34 34
     public static <T> List<T> findChildrenByClassRecursive(ViewGroup root, Class clazz) {
35
+        return findChildrenByClassRecursive(root, clazz, child -> true);
36
+    }
37
+
38
+    public static <T> List<T> findChildrenByClassRecursive(ViewGroup root, Class clazz, Matcher<T> matcher) {
35 39
         ArrayList<T> ret = new ArrayList<>();
36 40
         for (int i = 0; i < root.getChildCount(); i++) {
37 41
             View view = root.getChildAt(i);
38 42
             if (view instanceof ViewGroup) {
39 43
                 ret.addAll(findChildrenByClassRecursive((ViewGroup) view, clazz));
40 44
             }
41
-            if (clazz.isAssignableFrom(view.getClass())) {
45
+            if (clazz.isAssignableFrom(view.getClass()) && matcher.match((T) view)) {
42 46
                 ret.add((T) view);
43 47
             }
44 48
         }

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TitleBarReactViewController.java Voir le fichier

@@ -1,6 +1,8 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.view.View;
5
+import android.view.ViewManager;
4 6
 
5 7
 import com.reactnativenavigation.parse.Component;
6 8
 import com.reactnativenavigation.parse.Options;
@@ -41,6 +43,11 @@ public class TitleBarReactViewController extends ViewController<TitleBarReactVie
41 43
 
42 44
     }
43 45
 
46
+    @Override
47
+    protected void onYellowBoxAdded(View yellowBox) {
48
+        ((ViewManager) yellowBox.getParent()).removeView(yellowBox);
49
+    }
50
+
44 51
     public void setComponent(Component component) {
45 52
         this.component = component;
46 53
     }

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TopBarButtonController.java Voir le fichier

@@ -8,6 +8,8 @@ import android.support.annotation.NonNull;
8 8
 import android.support.v7.widget.ActionMenuView;
9 9
 import android.support.v7.widget.Toolbar;
10 10
 import android.view.MenuItem;
11
+import android.view.View;
12
+import android.view.ViewManager;
11 13
 import android.widget.ImageButton;
12 14
 import android.widget.TextView;
13 15
 
@@ -85,6 +87,11 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
85 87
         return true;
86 88
     }
87 89
 
90
+    @Override
91
+    protected void onYellowBoxAdded(View yellowBox) {
92
+        ((ViewManager) yellowBox.getParent()).removeView(yellowBox);
93
+    }
94
+
88 95
     public void applyNavigationIcon(Toolbar toolbar) {
89 96
         navigationIconResolver.resolve(button, icon -> {
90 97
             setIconColor(icon);

+ 21
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Voir le fichier

@@ -27,7 +27,7 @@ import com.reactnativenavigation.views.element.Element;
27 27
 import java.util.Collections;
28 28
 import java.util.List;
29 29
 
30
-public abstract class ViewController<T extends ViewGroup> implements ViewTreeObserver.OnGlobalLayoutListener {
30
+public abstract class ViewController<T extends ViewGroup> implements ViewTreeObserver.OnGlobalLayoutListener, ViewGroup.OnHierarchyChangeListener {
31 31
 
32 32
     private Runnable onAppearedListener;
33 33
     private boolean appearEventPosted;
@@ -155,6 +155,7 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
155 155
                 throw new RuntimeException("Tried to create view after it has already been destroyed");
156 156
             }
157 157
             view = createView();
158
+            view.setOnHierarchyChangeListener(this);
158 159
             view.getViewTreeObserver().addOnGlobalLayoutListener(this);
159 160
         }
160 161
         return view;
@@ -226,6 +227,7 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
226 227
         }
227 228
         if (view != null) {
228 229
             view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
230
+            view.setOnHierarchyChangeListener(null);
229 231
             if (view.getParent() instanceof ViewGroup) {
230 232
                 ((ViewManager) view.getParent()).removeView(view);
231 233
             }
@@ -253,6 +255,24 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
253 255
         }
254 256
     }
255 257
 
258
+    @Override
259
+    public void onChildViewAdded(View parent, View child) {
260
+        if (parent instanceof ViewGroup &&
261
+            child instanceof ViewGroup &&
262
+            YellowBoxHelper.isYellowBox((ViewGroup) parent, (ViewGroup) child)) {
263
+            onYellowBoxAdded(child);
264
+        }
265
+    }
266
+
267
+    @Override
268
+    public void onChildViewRemoved(View view, View view1) {
269
+
270
+    }
271
+
272
+    protected void onYellowBoxAdded(View yellowBox) {
273
+
274
+    }
275
+
256 276
     void runOnPreDraw(Task<T> task) {
257 277
         UiUtils.runOnPreDrawOnce(getView(), () -> task.run(getView()));
258 278
     }

+ 22
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/YellowBoxHelper.java Voir le fichier

@@ -0,0 +1,22 @@
1
+package com.reactnativenavigation.viewcontrollers;
2
+
3
+import android.support.annotation.NonNull;
4
+import android.view.View;
5
+import android.view.ViewGroup;
6
+
7
+import com.facebook.react.views.view.ReactViewBackgroundDrawable;
8
+import com.reactnativenavigation.utils.ViewUtils;
9
+
10
+public class YellowBoxHelper {
11
+    private final static int YELLOW_BOX_COLOR = -218449360;
12
+
13
+    public static boolean isYellowBox(ViewGroup parent, ViewGroup child) {
14
+        return parent.getChildCount() > 1 &&
15
+               !ViewUtils.findChildrenByClassRecursive(child, View.class, YellowBackgroundMather()).isEmpty();
16
+    }
17
+
18
+    @NonNull
19
+    private static ViewUtils.Matcher<View> YellowBackgroundMather() {
20
+        return child1 -> child1.getBackground() instanceof ReactViewBackgroundDrawable && ((ReactViewBackgroundDrawable) child1.getBackground()).getColor() == YELLOW_BOX_COLOR;
21
+    }
22
+}

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/topbar/TopBarBackgroundViewController.java Voir le fichier

@@ -1,6 +1,8 @@
1 1
 package com.reactnativenavigation.viewcontrollers.topbar;
2 2
 
3 3
 import android.app.Activity;
4
+import android.view.View;
5
+import android.view.ViewManager;
4 6
 
5 7
 import com.reactnativenavigation.parse.Component;
6 8
 import com.reactnativenavigation.parse.Options;
@@ -41,6 +43,11 @@ public class TopBarBackgroundViewController extends ViewController<TopBarBackgro
41 43
         super.onViewDisappear();
42 44
     }
43 45
 
46
+    @Override
47
+    protected void onYellowBoxAdded(View yellowBox) {
48
+        ((ViewManager) yellowBox.getParent()).removeView(yellowBox);
49
+    }
50
+
44 51
     @Override
45 52
     public void sendOnNavigationButtonPressed(String buttonId) {
46 53