Browse Source

Hide YellowBox in TopBar components

Guy Carmeli 6 years ago
parent
commit
5eee64a0ea

+ 5
- 1
lib/android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

32
     }
32
     }
33
 
33
 
34
     public static <T> List<T> findChildrenByClassRecursive(ViewGroup root, Class clazz) {
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
         ArrayList<T> ret = new ArrayList<>();
39
         ArrayList<T> ret = new ArrayList<>();
36
         for (int i = 0; i < root.getChildCount(); i++) {
40
         for (int i = 0; i < root.getChildCount(); i++) {
37
             View view = root.getChildAt(i);
41
             View view = root.getChildAt(i);
38
             if (view instanceof ViewGroup) {
42
             if (view instanceof ViewGroup) {
39
                 ret.addAll(findChildrenByClassRecursive((ViewGroup) view, clazz));
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
                 ret.add((T) view);
46
                 ret.add((T) view);
43
             }
47
             }
44
         }
48
         }

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TitleBarReactViewController.java View File

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

+ 7
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TopBarButtonController.java View File

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

+ 21
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

27
 import java.util.Collections;
27
 import java.util.Collections;
28
 import java.util.List;
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
     private Runnable onAppearedListener;
32
     private Runnable onAppearedListener;
33
     private boolean appearEventPosted;
33
     private boolean appearEventPosted;
155
                 throw new RuntimeException("Tried to create view after it has already been destroyed");
155
                 throw new RuntimeException("Tried to create view after it has already been destroyed");
156
             }
156
             }
157
             view = createView();
157
             view = createView();
158
+            view.setOnHierarchyChangeListener(this);
158
             view.getViewTreeObserver().addOnGlobalLayoutListener(this);
159
             view.getViewTreeObserver().addOnGlobalLayoutListener(this);
159
         }
160
         }
160
         return view;
161
         return view;
226
         }
227
         }
227
         if (view != null) {
228
         if (view != null) {
228
             view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
229
             view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
230
+            view.setOnHierarchyChangeListener(null);
229
             if (view.getParent() instanceof ViewGroup) {
231
             if (view.getParent() instanceof ViewGroup) {
230
                 ((ViewManager) view.getParent()).removeView(view);
232
                 ((ViewManager) view.getParent()).removeView(view);
231
             }
233
             }
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
     void runOnPreDraw(Task<T> task) {
276
     void runOnPreDraw(Task<T> task) {
257
         UiUtils.runOnPreDrawOnce(getView(), () -> task.run(getView()));
277
         UiUtils.runOnPreDrawOnce(getView(), () -> task.run(getView()));
258
     }
278
     }

+ 22
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/YellowBoxHelper.java View File

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 View File

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