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
 import static com.reactnativenavigation.utils.CollectionUtils.*;
21
 import static com.reactnativenavigation.utils.CollectionUtils.*;
22
 import static com.reactnativenavigation.utils.ObjectUtils.perform;
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
 		super(activity, childRegistry, id, presenter, initialOptions);
27
 		super(activity, childRegistry, id, presenter, initialOptions);
28
 	}
28
 	}
29
 
29
 
30
     @Override
30
     @Override
31
     public void setWaitForRender(Bool waitForRender) {
31
     public void setWaitForRender(Bool waitForRender) {
32
         super.setWaitForRender(waitForRender);
32
         super.setWaitForRender(waitForRender);
33
-        applyOnController(getCurrentChild(), controller -> ((ViewController) controller).setWaitForRender(waitForRender));
33
+        applyOnController(getCurrentChild(), currentChild -> currentChild.setWaitForRender(waitForRender));
34
     }
34
     }
35
 
35
 
36
     @Override
36
     @Override
37
     public void setDefaultOptions(Options defaultOptions) {
37
     public void setDefaultOptions(Options defaultOptions) {
38
 	    super.setDefaultOptions(defaultOptions);
38
 	    super.setDefaultOptions(defaultOptions);
39
-	    forEach(getChildControllers(), (child) -> child.setDefaultOptions(defaultOptions));
39
+	    forEach(getChildControllers(), child -> child.setDefaultOptions(defaultOptions));
40
     }
40
     }
41
 
41
 
42
     @Override
42
     @Override
69
 
69
 
70
     protected abstract ViewController getCurrentChild();
70
     protected abstract ViewController getCurrentChild();
71
 
71
 
72
-    @NonNull
73
-	@Override
74
-	public T getView() {
75
-		return (T) super.getView();
76
-	}
77
-
78
 	@NonNull
72
 	@NonNull
79
 	@Override
73
 	@Override
80
 	protected abstract T createView();
74
 	protected abstract T createView();
127
     }
121
     }
128
 
122
 
129
     @CallSuper
123
     @CallSuper
130
-    public void mergeChildOptions(Options options, ViewController childController) {
131
-
124
+    public void mergeChildOptions(Options options, ViewController child) {
132
     }
125
     }
133
 
126
 
134
 	@Override
127
 	@Override
140
 	@SuppressWarnings("WeakerAccess")
133
 	@SuppressWarnings("WeakerAccess")
141
     @CallSuper
134
     @CallSuper
142
     protected void clearOptions() {
135
     protected void clearOptions() {
143
-	    performOnParentController(parent -> ((ParentController) parent).clearOptions());
136
+	    performOnParentController(ParentController::clearOptions);
144
         options = initialOptions.copy().clearOneTimeOptions();
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
 
37
 
38
 public abstract class ViewController<T extends ViewGroup> implements ViewTreeObserver.OnGlobalLayoutListener,
38
 public abstract class ViewController<T extends ViewGroup> implements ViewTreeObserver.OnGlobalLayoutListener,
39
         ViewGroup.OnHierarchyChangeListener,
39
         ViewGroup.OnHierarchyChangeListener,
40
-        BehaviourAdapter<T> {
40
+        BehaviourAdapter {
41
 
41
 
42
     private final List<Runnable> onAppearedListeners = new ArrayList();
42
     private final List<Runnable> onAppearedListeners = new ArrayList();
43
     private boolean appearEventPosted;
43
     private boolean appearEventPosted;
146
         if (view != null) task.run(view);
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
         if (parentController != null) task.run(parentController);
150
         if (parentController != null) task.run(parentController);
151
     }
151
     }
152
 
152
 
326
 
326
 
327
     @Override
327
     @Override
328
     @CallSuper
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
         perform(findController(child), ViewController::applyTopInset);
330
         perform(findController(child), ViewController::applyTopInset);
331
         return false;
331
         return false;
332
     }
332
     }
333
 
333
 
334
     @Override
334
     @Override
335
-    public boolean onDependentViewChanged(CoordinatorLayout parent, T child, View dependency) {
335
+    public boolean onDependentViewChanged(CoordinatorLayout parent, ViewGroup child, View dependency) {
336
         return false;
336
         return false;
337
     }
337
     }
338
 
338
 

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

109
     public void applyChildOptions(Options options, ViewController child) {
109
     public void applyChildOptions(Options options, ViewController child) {
110
         super.applyChildOptions(options, child);
110
         super.applyChildOptions(options, child);
111
         presenter.applyChildOptions(resolveCurrentOptions(), child);
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
                         child
116
                         child
118
                 )
117
                 )
119
         );
118
         );
124
         super.mergeChildOptions(options, child);
123
         super.mergeChildOptions(options, child);
125
         presenter.mergeChildOptions(options, child);
124
         presenter.mergeChildOptions(options, child);
126
         tabPresenter.mergeChildOptions(options, child);
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
     @Override
129
     @Override
165
         });
162
         });
166
 	}
163
 	}
167
 
164
 
168
-    public int getSelectedIndex() {
165
+    int getSelectedIndex() {
169
 		return bottomTabs.getCurrentItem();
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
     public void applyChildOptions(Options options, ViewController child) {
87
     public void applyChildOptions(Options options, ViewController child) {
88
         super.applyChildOptions(options, child);
88
         super.applyChildOptions(options, child);
89
         presenter.applyChildOptions(resolveCurrentOptions());
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
     @Override
93
     @Override
96
     public void mergeChildOptions(Options options, ViewController child) {
94
     public void mergeChildOptions(Options options, ViewController child) {
97
         super.mergeChildOptions(options, child);
95
         super.mergeChildOptions(options, child);
98
         presenter.mergeOptions(options.sideMenuRootOptions);
96
         presenter.mergeOptions(options.sideMenuRootOptions);
97
+        performOnParentController(parent -> parent.mergeChildOptions(options, child));
99
     }
98
     }
100
 
99
 
101
     @Override
100
     @Override
120
         return options;
119
         return options;
121
     }
120
     }
122
 
121
 
123
-    public boolean isDrawerOpen(int gravity) {
122
+    private boolean isDrawerOpen(int gravity) {
124
         return !isDestroyed() && getView().isDrawerOpen(gravity);
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
         if (child.getView() instanceof ReactComponent) {
101
         if (child.getView() instanceof ReactComponent) {
102
             fabOptionsPresenter.applyOptions(this.options.fabOptions, (ReactComponent) child.getView(), getView());
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
                         this.options.copy()
106
                         this.options.copy()
107
                                 .clearTopBarOptions()
107
                                 .clearTopBarOptions()
108
                                 .clearAnimationOptions()
108
                                 .clearAnimationOptions()
123
                 fabOptionsPresenter.mergeOptions(options.fabOptions, (ReactComponent) child, getView());
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
                         options.copy()
128
                         options.copy()
129
                                 .clearTopBarOptions()
129
                                 .clearTopBarOptions()
130
                                 .clearAnimationOptions()
130
                                 .clearAnimationOptions()
221
         if (toRemove != null && resolvedOptions.animations.setStackRoot.enabled.isTrueOrUndefined()) {
221
         if (toRemove != null && resolvedOptions.animations.setStackRoot.enabled.isTrueOrUndefined()) {
222
             if (resolvedOptions.animations.setStackRoot.waitForRender.isTrue()) {
222
             if (resolvedOptions.animations.setStackRoot.waitForRender.isTrue()) {
223
                 child.getView().setAlpha(0);
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
             } else {
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
         } else {
236
         } else {
233
             listenerAdapter.onSuccess(child.getId());
237
             listenerAdapter.onSuccess(child.getId());

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

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
4
 import android.view.View;
3
 import android.view.View;
5
 import android.view.ViewGroup;
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
      * @see CoordinatorLayout.Behavior#onMeasureChild
10
      * @see CoordinatorLayout.Behavior#onMeasureChild
10
      * @return true if the Behavior measured the child view, false if the CoordinatorLayout should perform its default measurement
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
      * @see CoordinatorLayout.Behavior#onDependentViewChanged
16
      * @see CoordinatorLayout.Behavior#onDependentViewChanged
16
      * @return true if the Behavior changed the child view's size or position, false otherwise
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
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
4
 import android.view.View;
3
 import android.view.View;
5
 import android.view.ViewGroup;
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
     private BehaviourAdapter delegate;
11
     private BehaviourAdapter delegate;
10
 
12
 
11
-    public BehaviourDelegate(BehaviourAdapter<V> delegate) {
13
+    public BehaviourDelegate(BehaviourAdapter delegate) {
12
         this.delegate = delegate;
14
         this.delegate = delegate;
13
     }
15
     }
14
 
16
 
15
     @Override
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
         return delegate.onDependentViewChanged(parent, child, dependency);
19
         return delegate.onDependentViewChanged(parent, child, dependency);
18
     }
20
     }
19
 
21
 
20
     @Override
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
         return delegate.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
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
 package com.reactnativenavigation.views.bottomtabs;
1
 package com.reactnativenavigation.views.bottomtabs;
2
 
2
 
3
-import androidx.annotation.NonNull;
4
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
5
 import android.view.View;
3
 import android.view.View;
6
 import android.view.ViewGroup;
4
 import android.view.ViewGroup;
7
 
5
 
9
 import com.reactnativenavigation.views.BehaviourDelegate;
7
 import com.reactnativenavigation.views.BehaviourDelegate;
10
 import com.reactnativenavigation.views.BottomTabs;
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
         super(delegate);
15
         super(delegate);
15
     }
16
     }
16
 
17
 
17
     @Override
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
         return dependency instanceof BottomTabs;
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
 package com.reactnativenavigation.views.stack;
1
 package com.reactnativenavigation.views.stack;
2
 
2
 
3
-import androidx.coordinatorlayout.widget.CoordinatorLayout;
4
 import android.view.View;
3
 import android.view.View;
5
 import android.view.ViewGroup;
4
 import android.view.ViewGroup;
6
 
5
 
8
 import com.reactnativenavigation.views.BehaviourDelegate;
7
 import com.reactnativenavigation.views.BehaviourDelegate;
9
 import com.reactnativenavigation.views.topbar.TopBar;
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
         super(delegate);
15
         super(delegate);
14
     }
16
     }
15
 
17
 
16
     @Override
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
         return dependency instanceof TopBar;
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
         Options options = new Options();
166
         Options options = new Options();
167
         uut.mergeChildOptions(options, child);
167
         uut.mergeChildOptions(options, child);
168
         verify(presenter).mergeOptions(options.sideMenuRootOptions);
168
         verify(presenter).mergeOptions(options.sideMenuRootOptions);
169
+        verify(parent).mergeChildOptions(options, child);
169
     }
170
     }
170
 
171
 
171
     @Test
172
     @Test