Browse Source

Merge SideMenuController options with parent (#5754)

Guy Carmeli 5 years ago
parent
commit
afbaa9a4f5
No account linked to committer's email address

+ 6
- 13
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java View File

@@ -21,22 +21,22 @@ import androidx.viewpager.widget.ViewPager;
21 21
 import static com.reactnativenavigation.utils.CollectionUtils.*;
22 22
 import static com.reactnativenavigation.utils.ObjectUtils.perform;
23 23
 
24
-public abstract class ParentController<T extends ViewGroup> extends ChildController {
24
+public abstract class ParentController<T extends ViewGroup> extends ChildController<T> {
25 25
 
26
-	public ParentController(Activity activity, ChildControllersRegistry childRegistry, String id, Presenter presenter, Options initialOptions) {
26
+    public ParentController(Activity activity, ChildControllersRegistry childRegistry, String id, Presenter presenter, Options initialOptions) {
27 27
 		super(activity, childRegistry, id, presenter, initialOptions);
28 28
 	}
29 29
 
30 30
     @Override
31 31
     public void setWaitForRender(Bool waitForRender) {
32 32
         super.setWaitForRender(waitForRender);
33
-        applyOnController(getCurrentChild(), controller -> ((ViewController) controller).setWaitForRender(waitForRender));
33
+        applyOnController(getCurrentChild(), currentChild -> currentChild.setWaitForRender(waitForRender));
34 34
     }
35 35
 
36 36
     @Override
37 37
     public void setDefaultOptions(Options defaultOptions) {
38 38
 	    super.setDefaultOptions(defaultOptions);
39
-	    forEach(getChildControllers(), (child) -> child.setDefaultOptions(defaultOptions));
39
+	    forEach(getChildControllers(), child -> child.setDefaultOptions(defaultOptions));
40 40
     }
41 41
 
42 42
     @Override
@@ -69,12 +69,6 @@ public abstract class ParentController<T extends ViewGroup> extends ChildControl
69 69
 
70 70
     protected abstract ViewController getCurrentChild();
71 71
 
72
-    @NonNull
73
-	@Override
74
-	public T getView() {
75
-		return (T) super.getView();
76
-	}
77
-
78 72
 	@NonNull
79 73
 	@Override
80 74
 	protected abstract T createView();
@@ -127,8 +121,7 @@ public abstract class ParentController<T extends ViewGroup> extends ChildControl
127 121
     }
128 122
 
129 123
     @CallSuper
130
-    public void mergeChildOptions(Options options, ViewController childController) {
131
-
124
+    public void mergeChildOptions(Options options, ViewController child) {
132 125
     }
133 126
 
134 127
 	@Override
@@ -140,7 +133,7 @@ public abstract class ParentController<T extends ViewGroup> extends ChildControl
140 133
 	@SuppressWarnings("WeakerAccess")
141 134
     @CallSuper
142 135
     protected void clearOptions() {
143
-	    performOnParentController(parent -> ((ParentController) parent).clearOptions());
136
+	    performOnParentController(ParentController::clearOptions);
144 137
         options = initialOptions.copy().clearOneTimeOptions();
145 138
     }
146 139
 

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

@@ -37,7 +37,7 @@ import static com.reactnativenavigation.utils.ObjectUtils.perform;
37 37
 
38 38
 public abstract class ViewController<T extends ViewGroup> implements ViewTreeObserver.OnGlobalLayoutListener,
39 39
         ViewGroup.OnHierarchyChangeListener,
40
-        BehaviourAdapter<T> {
40
+        BehaviourAdapter {
41 41
 
42 42
     private final List<Runnable> onAppearedListeners = new ArrayList();
43 43
     private boolean appearEventPosted;
@@ -146,7 +146,7 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
146 146
         if (view != null) task.run(view);
147 147
     }
148 148
 
149
-    protected void performOnParentController(Func1<ParentController> task) {
149
+    public void performOnParentController(Func1<ParentController> task) {
150 150
         if (parentController != null) task.run(parentController);
151 151
     }
152 152
 
@@ -326,13 +326,13 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
326 326
 
327 327
     @Override
328 328
     @CallSuper
329
-    public boolean onMeasureChild(CoordinatorLayout parent, T child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
329
+    public boolean onMeasureChild(CoordinatorLayout parent, ViewGroup child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
330 330
         perform(findController(child), ViewController::applyTopInset);
331 331
         return false;
332 332
     }
333 333
 
334 334
     @Override
335
-    public boolean onDependentViewChanged(CoordinatorLayout parent, T child, View dependency) {
335
+    public boolean onDependentViewChanged(CoordinatorLayout parent, ViewGroup child, View dependency) {
336 336
         return false;
337 337
     }
338 338
 

+ 6
- 9
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsController.java View File

@@ -109,11 +109,10 @@ public class BottomTabsController extends ParentController<BottomTabsLayout> imp
109 109
     public void applyChildOptions(Options options, ViewController child) {
110 110
         super.applyChildOptions(options, child);
111 111
         presenter.applyChildOptions(resolveCurrentOptions(), child);
112
-        performOnParentController(parentController ->
113
-                ((ParentController) parentController).applyChildOptions(
114
-                        this.options.copy()
115
-                                .clearBottomTabsOptions()
116
-                                .clearBottomTabOptions(),
112
+        performOnParentController(parent -> parent.applyChildOptions(
113
+                this.options.copy()
114
+                        .clearBottomTabsOptions()
115
+                        .clearBottomTabOptions(),
117 116
                         child
118 117
                 )
119 118
         );
@@ -124,9 +123,7 @@ public class BottomTabsController extends ParentController<BottomTabsLayout> imp
124 123
         super.mergeChildOptions(options, child);
125 124
         presenter.mergeChildOptions(options, child);
126 125
         tabPresenter.mergeChildOptions(options, child);
127
-        performOnParentController(parentController ->
128
-                ((ParentController) parentController).mergeChildOptions(options.copy().clearBottomTabsOptions(), child)
129
-        );
126
+        performOnParentController(parent -> parent.mergeChildOptions(options.copy().clearBottomTabsOptions(), child));
130 127
     }
131 128
 
132 129
     @Override
@@ -165,7 +162,7 @@ public class BottomTabsController extends ParentController<BottomTabsLayout> imp
165 162
         });
166 163
 	}
167 164
 
168
-    public int getSelectedIndex() {
165
+    int getSelectedIndex() {
169 166
 		return bottomTabs.getCurrentItem();
170 167
 	}
171 168
 

+ 3
- 4
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuController.java View File

@@ -87,15 +87,14 @@ public class SideMenuController extends ParentController<SideMenuRoot> implement
87 87
     public void applyChildOptions(Options options, ViewController child) {
88 88
         super.applyChildOptions(options, child);
89 89
         presenter.applyChildOptions(resolveCurrentOptions());
90
-        performOnParentController(parentController ->
91
-                ((ParentController) parentController).applyChildOptions(this.options, child)
92
-        );
90
+        performOnParentController(parent -> parent.applyChildOptions(this.options, child));
93 91
     }
94 92
 
95 93
     @Override
96 94
     public void mergeChildOptions(Options options, ViewController child) {
97 95
         super.mergeChildOptions(options, child);
98 96
         presenter.mergeOptions(options.sideMenuRootOptions);
97
+        performOnParentController(parent -> parent.mergeChildOptions(options, child));
99 98
     }
100 99
 
101 100
     @Override
@@ -120,7 +119,7 @@ public class SideMenuController extends ParentController<SideMenuRoot> implement
120 119
         return options;
121 120
     }
122 121
 
123
-    public boolean isDrawerOpen(int gravity) {
122
+    private boolean isDrawerOpen(int gravity) {
124 123
         return !isDestroyed() && getView().isDrawerOpen(gravity);
125 124
     }
126 125
 

+ 14
- 10
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java View File

@@ -101,8 +101,8 @@ public class StackController extends ParentController<StackLayout> {
101 101
         if (child.getView() instanceof ReactComponent) {
102 102
             fabOptionsPresenter.applyOptions(this.options.fabOptions, (ReactComponent) child.getView(), getView());
103 103
         }
104
-        performOnParentController(parentController ->
105
-                ((ParentController) parentController).applyChildOptions(
104
+        performOnParentController(parent ->
105
+                parent.applyChildOptions(
106 106
                         this.options.copy()
107 107
                                 .clearTopBarOptions()
108 108
                                 .clearAnimationOptions()
@@ -123,8 +123,8 @@ public class StackController extends ParentController<StackLayout> {
123 123
                 fabOptionsPresenter.mergeOptions(options.fabOptions, (ReactComponent) child, getView());
124 124
             }
125 125
         }
126
-        performOnParentController(parentController ->
127
-                ((ParentController) parentController).mergeChildOptions(
126
+        performOnParentController(parent ->
127
+                parent.mergeChildOptions(
128 128
                         options.copy()
129 129
                                 .clearTopBarOptions()
130 130
                                 .clearAnimationOptions()
@@ -221,13 +221,17 @@ public class StackController extends ParentController<StackLayout> {
221 221
         if (toRemove != null && resolvedOptions.animations.setStackRoot.enabled.isTrueOrUndefined()) {
222 222
             if (resolvedOptions.animations.setStackRoot.waitForRender.isTrue()) {
223 223
                 child.getView().setAlpha(0);
224
-                child.addOnAppearedListener(() -> animator.push(child.getView(), resolvedOptions.animations.setStackRoot, resolvedOptions.transitions, toRemove.getElements(), child.getElements(), () -> {
225
-                    listenerAdapter.onSuccess(child.getId());
226
-                }));
224
+                child.addOnAppearedListener(() -> animator.push(
225
+                        child.getView(),
226
+                        resolvedOptions.animations.setStackRoot,
227
+                        resolvedOptions.transitions,
228
+                        toRemove.getElements(),
229
+                        child.getElements(),
230
+                        () -> listenerAdapter.onSuccess(child.getId())
231
+                    )
232
+                );
227 233
             } else {
228
-                animator.push(child.getView(), resolvedOptions.animations.setStackRoot, () -> {
229
-                    listenerAdapter.onSuccess(child.getId());
230
-                });
234
+                animator.push(child.getView(), resolvedOptions.animations.setStackRoot, () -> listenerAdapter.onSuccess(child.getId()));
231 235
             }
232 236
         } else {
233 237
             listenerAdapter.onSuccess(child.getId());

+ 5
- 4
lib/android/app/src/main/java/com/reactnativenavigation/views/BehaviourAdapter.java View File

@@ -1,19 +1,20 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
4 3
 import android.view.View;
5 4
 import android.view.ViewGroup;
6 5
 
7
-public interface BehaviourAdapter<V extends ViewGroup> {
6
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
7
+
8
+public interface BehaviourAdapter {
8 9
     /**
9 10
      * @see CoordinatorLayout.Behavior#onMeasureChild
10 11
      * @return true if the Behavior measured the child view, false if the CoordinatorLayout should perform its default measurement
11 12
      */
12
-    boolean onMeasureChild(CoordinatorLayout parent, V child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed);
13
+    boolean onMeasureChild(CoordinatorLayout parent, ViewGroup child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed);
13 14
 
14 15
     /**
15 16
      * @see CoordinatorLayout.Behavior#onDependentViewChanged
16 17
      * @return true if the Behavior changed the child view's size or position, false otherwise
17 18
      */
18
-    boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency);
19
+    boolean onDependentViewChanged(CoordinatorLayout parent, ViewGroup child, View dependency);
19 20
 }

+ 7
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/BehaviourDelegate.java View File

@@ -1,24 +1,26 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
4 3
 import android.view.View;
5 4
 import android.view.ViewGroup;
6 5
 
7
-public class BehaviourDelegate<V extends ViewGroup> extends CoordinatorLayout.Behavior<V> {
6
+import androidx.annotation.NonNull;
7
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
8
+
9
+public class BehaviourDelegate extends CoordinatorLayout.Behavior<ViewGroup> {
8 10
 
9 11
     private BehaviourAdapter delegate;
10 12
 
11
-    public BehaviourDelegate(BehaviourAdapter<V> delegate) {
13
+    public BehaviourDelegate(BehaviourAdapter delegate) {
12 14
         this.delegate = delegate;
13 15
     }
14 16
 
15 17
     @Override
16
-    public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {
18
+    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull ViewGroup child, @NonNull View dependency) {
17 19
         return delegate.onDependentViewChanged(parent, child, dependency);
18 20
     }
19 21
 
20 22
     @Override
21
-    public boolean onMeasureChild(CoordinatorLayout parent, V child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
23
+    public boolean onMeasureChild(@NonNull CoordinatorLayout parent, @NonNull ViewGroup child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
22 24
         return delegate.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
23 25
     }
24 26
 }

+ 6
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/bottomtabs/BottomTabsBehaviour.java View File

@@ -1,7 +1,5 @@
1 1
 package com.reactnativenavigation.views.bottomtabs;
2 2
 
3
-import androidx.annotation.NonNull;
4
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
5 3
 import android.view.View;
6 4
 import android.view.ViewGroup;
7 5
 
@@ -9,13 +7,16 @@ import com.reactnativenavigation.views.BehaviourAdapter;
9 7
 import com.reactnativenavigation.views.BehaviourDelegate;
10 8
 import com.reactnativenavigation.views.BottomTabs;
11 9
 
12
-public class BottomTabsBehaviour<V extends ViewGroup> extends BehaviourDelegate<V> {
13
-    public BottomTabsBehaviour(BehaviourAdapter<V> delegate) {
10
+import androidx.annotation.NonNull;
11
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
12
+
13
+public class BottomTabsBehaviour extends BehaviourDelegate {
14
+    public BottomTabsBehaviour(BehaviourAdapter delegate) {
14 15
         super(delegate);
15 16
     }
16 17
 
17 18
     @Override
18
-    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull View dependency) {
19
+    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull ViewGroup child, @NonNull View dependency) {
19 20
         return dependency instanceof BottomTabs;
20 21
     }
21 22
 }

+ 6
- 4
lib/android/app/src/main/java/com/reactnativenavigation/views/stack/StackBehaviour.java View File

@@ -1,6 +1,5 @@
1 1
 package com.reactnativenavigation.views.stack;
2 2
 
3
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
4 3
 import android.view.View;
5 4
 import android.view.ViewGroup;
6 5
 
@@ -8,13 +7,16 @@ import com.reactnativenavigation.views.BehaviourAdapter;
8 7
 import com.reactnativenavigation.views.BehaviourDelegate;
9 8
 import com.reactnativenavigation.views.topbar.TopBar;
10 9
 
11
-public class StackBehaviour<V extends ViewGroup> extends BehaviourDelegate<V> {
12
-    public StackBehaviour(BehaviourAdapter<V> delegate) {
10
+import androidx.annotation.NonNull;
11
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
12
+
13
+public class StackBehaviour<V extends ViewGroup> extends BehaviourDelegate {
14
+    public StackBehaviour(BehaviourAdapter delegate) {
13 15
         super(delegate);
14 16
     }
15 17
 
16 18
     @Override
17
-    public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) {
19
+    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull ViewGroup child, @NonNull View dependency) {
18 20
         return dependency instanceof TopBar;
19 21
     }
20 22
 }

+ 1
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuControllerTest.java View File

@@ -166,6 +166,7 @@ public class SideMenuControllerTest extends BaseTest {
166 166
         Options options = new Options();
167 167
         uut.mergeChildOptions(options, child);
168 168
         verify(presenter).mergeOptions(options.sideMenuRootOptions);
169
+        verify(parent).mergeChildOptions(options, child);
169 170
     }
170 171
 
171 172
     @Test