Browse Source

Emit SideMenu visibility events (#5149)

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

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

@@ -31,6 +31,8 @@ public class SideMenuController extends ParentController<DrawerLayout> implement
31 31
 	private ViewController left;
32 32
 	private ViewController right;
33 33
     private SideMenuPresenter presenter;
34
+    private float prevLeftSlideOffset = 0;
35
+    private float prevRightSlideOffset = 0;
34 36
 
35 37
     public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, SideMenuPresenter sideMenuOptionsPresenter, Presenter presenter) {
36 38
 		super(activity, childRegistry, id, presenter, initialOptions);
@@ -111,15 +113,30 @@ public class SideMenuController extends ParentController<DrawerLayout> implement
111 113
     @Override
112 114
     public void onDrawerOpened(@NonNull View drawerView) {
113 115
         ViewController view = this.getMatchingView(drawerView);
114
-        view.mergeOptions(this.getOptionsWithVisability(this.viewIsLeft(drawerView), true));
115
-        view.onViewAppeared();
116
+        view.mergeOptions(this.getOptionsWithVisibility(this.viewIsLeft(drawerView), true));
116 117
     }
117 118
 
118 119
     @Override
119 120
     public void onDrawerClosed(@NonNull View drawerView) {
120 121
         ViewController view = this.getMatchingView(drawerView);
121
-        view.mergeOptions(this.getOptionsWithVisability(this.viewIsLeft(drawerView), false));
122
-        view.onViewDisappear();
122
+        view.mergeOptions(this.getOptionsWithVisibility(this.viewIsLeft(drawerView), false));
123
+    }
124
+
125
+    @Override
126
+    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
127
+        int gravity = getSideMenuGravity(drawerView);
128
+        if (gravity == Gravity.LEFT) {
129
+            dispatchSideMenuVisibilityEvents(left, prevLeftSlideOffset, slideOffset);
130
+            prevLeftSlideOffset = slideOffset;
131
+        } else if (gravity == Gravity.RIGHT) {
132
+            dispatchSideMenuVisibilityEvents(right, prevRightSlideOffset, slideOffset);
133
+            prevRightSlideOffset = slideOffset;
134
+        }
135
+    }
136
+
137
+    @Override
138
+    public void onDrawerStateChanged(int newState) {
139
+
123 140
     }
124 141
 
125 142
     @Override
@@ -135,15 +152,15 @@ public class SideMenuController extends ParentController<DrawerLayout> implement
135 152
 
136 153
     public void setLeftController(ViewController controller) {
137 154
         this.left = controller;
138
-        int height = this.getHeight(options.sideMenuRootOptions.left);
139
-        int width = this.getWidth(options.sideMenuRootOptions.left);
155
+        int height = getHeight(options.sideMenuRootOptions.left);
156
+        int width = getWidth(options.sideMenuRootOptions.left);
140 157
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.LEFT));
141 158
     }
142 159
 
143 160
     public void setRightController(ViewController controller) {
144 161
         this.right = controller;
145
-        int height = this.getHeight(options.sideMenuRootOptions.right);
146
-        int width = this.getWidth(options.sideMenuRootOptions.right);
162
+        int height = getHeight(options.sideMenuRootOptions.right);
163
+        int width = getWidth(options.sideMenuRootOptions.right);
147 164
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
148 165
     }
149 166
 
@@ -171,7 +188,11 @@ public class SideMenuController extends ParentController<DrawerLayout> implement
171 188
         return (left != null && drawerView.equals(left.getView()));
172 189
     }
173 190
 
174
-    private Options getOptionsWithVisability ( boolean isLeft, boolean visible ) {
191
+    private int getSideMenuGravity(View drawerView) {
192
+        return ((LayoutParams) drawerView.getLayoutParams()).gravity;
193
+    }
194
+
195
+    private Options getOptionsWithVisibility(boolean isLeft, boolean visible ) {
175 196
         Options options = new Options();
176 197
         if (isLeft) {
177 198
             options.sideMenuRootOptions.left.visible = new Bool(visible);
@@ -181,13 +202,11 @@ public class SideMenuController extends ParentController<DrawerLayout> implement
181 202
         return options;
182 203
     }
183 204
 
184
-    @Override
185
-    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
186
-
187
-    }
188
-
189
-    @Override
190
-    public void onDrawerStateChanged(int newState) {
191
-
205
+    private void dispatchSideMenuVisibilityEvents(ViewController drawer, float prevOffset, float offset) {
206
+        if (prevOffset == 0 && offset> 0) {
207
+            drawer.onViewAppeared();
208
+        } else if (prevOffset > 0 && offset == 0) {
209
+            drawer.onViewDisappear();
210
+        }
192 211
     }
193 212
 }

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

@@ -4,17 +4,21 @@ import android.app.Activity;
4 4
 import android.content.res.Resources;
5 5
 import android.util.TypedValue;
6 6
 import android.view.Gravity;
7
+import android.view.View;
7 8
 import android.view.ViewGroup.LayoutParams;
9
+import android.view.Window;
8 10
 
9 11
 import com.reactnativenavigation.BaseTest;
10 12
 import com.reactnativenavigation.mocks.SimpleComponentViewController;
11
-import com.reactnativenavigation.parse.*;
13
+import com.reactnativenavigation.parse.Options;
14
+import com.reactnativenavigation.parse.SideMenuOptions;
12 15
 import com.reactnativenavigation.parse.params.Bool;
13 16
 import com.reactnativenavigation.parse.params.Number;
14 17
 import com.reactnativenavigation.parse.params.Text;
15 18
 import com.reactnativenavigation.presentation.Presenter;
16 19
 import com.reactnativenavigation.presentation.SideMenuPresenter;
17
-import com.reactnativenavigation.utils.*;
20
+import com.reactnativenavigation.utils.CommandListenerAdapter;
21
+import com.reactnativenavigation.utils.Functions;
18 22
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
19 23
 import com.reactnativenavigation.viewcontrollers.ParentController;
20 24
 import com.reactnativenavigation.viewcontrollers.ViewController;
@@ -30,6 +34,7 @@ import static org.mockito.ArgumentMatchers.eq;
30 34
 import static org.mockito.Mockito.spy;
31 35
 import static org.mockito.Mockito.times;
32 36
 import static org.mockito.Mockito.verify;
37
+import static org.mockito.Mockito.when;
33 38
 
34 39
 @SuppressWarnings("MagicNumber")
35 40
 public class SideMenuControllerTest extends BaseTest {
@@ -46,7 +51,8 @@ public class SideMenuControllerTest extends BaseTest {
46 51
 
47 52
     @Override
48 53
     public void beforeEach() {
49
-        activity = newActivity();
54
+        activity = createActivity();
55
+
50 56
         childRegistry = new ChildControllersRegistry();
51 57
         presenter = spy(new SideMenuPresenter());
52 58
         child = new SimpleComponentViewController(activity, childRegistry, "child", new Options());
@@ -305,4 +311,12 @@ public class SideMenuControllerTest extends BaseTest {
305 311
         options.sideMenuRootOptions.right.animate = new Bool(false);
306 312
         uut.mergeOptions(options);
307 313
     }
314
+
315
+    private Activity createActivity() {
316
+        Activity activity = spy(newActivity());
317
+        Window window = Mockito.mock(Window.class);
318
+        when(window.getDecorView()).thenReturn(Mockito.mock(View.class));
319
+        when(activity.getWindow()).thenReturn(window);
320
+        return activity;
321
+    }
308 322
 }