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
 	private ViewController left;
31
 	private ViewController left;
32
 	private ViewController right;
32
 	private ViewController right;
33
     private SideMenuPresenter presenter;
33
     private SideMenuPresenter presenter;
34
+    private float prevLeftSlideOffset = 0;
35
+    private float prevRightSlideOffset = 0;
34
 
36
 
35
     public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, SideMenuPresenter sideMenuOptionsPresenter, Presenter presenter) {
37
     public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, SideMenuPresenter sideMenuOptionsPresenter, Presenter presenter) {
36
 		super(activity, childRegistry, id, presenter, initialOptions);
38
 		super(activity, childRegistry, id, presenter, initialOptions);
111
     @Override
113
     @Override
112
     public void onDrawerOpened(@NonNull View drawerView) {
114
     public void onDrawerOpened(@NonNull View drawerView) {
113
         ViewController view = this.getMatchingView(drawerView);
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
     @Override
119
     @Override
119
     public void onDrawerClosed(@NonNull View drawerView) {
120
     public void onDrawerClosed(@NonNull View drawerView) {
120
         ViewController view = this.getMatchingView(drawerView);
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
     @Override
142
     @Override
135
 
152
 
136
     public void setLeftController(ViewController controller) {
153
     public void setLeftController(ViewController controller) {
137
         this.left = controller;
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
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.LEFT));
157
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.LEFT));
141
     }
158
     }
142
 
159
 
143
     public void setRightController(ViewController controller) {
160
     public void setRightController(ViewController controller) {
144
         this.right = controller;
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
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
164
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
148
     }
165
     }
149
 
166
 
171
         return (left != null && drawerView.equals(left.getView()));
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
         Options options = new Options();
196
         Options options = new Options();
176
         if (isLeft) {
197
         if (isLeft) {
177
             options.sideMenuRootOptions.left.visible = new Bool(visible);
198
             options.sideMenuRootOptions.left.visible = new Bool(visible);
181
         return options;
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
 import android.content.res.Resources;
4
 import android.content.res.Resources;
5
 import android.util.TypedValue;
5
 import android.util.TypedValue;
6
 import android.view.Gravity;
6
 import android.view.Gravity;
7
+import android.view.View;
7
 import android.view.ViewGroup.LayoutParams;
8
 import android.view.ViewGroup.LayoutParams;
9
+import android.view.Window;
8
 
10
 
9
 import com.reactnativenavigation.BaseTest;
11
 import com.reactnativenavigation.BaseTest;
10
 import com.reactnativenavigation.mocks.SimpleComponentViewController;
12
 import com.reactnativenavigation.mocks.SimpleComponentViewController;
11
-import com.reactnativenavigation.parse.*;
13
+import com.reactnativenavigation.parse.Options;
14
+import com.reactnativenavigation.parse.SideMenuOptions;
12
 import com.reactnativenavigation.parse.params.Bool;
15
 import com.reactnativenavigation.parse.params.Bool;
13
 import com.reactnativenavigation.parse.params.Number;
16
 import com.reactnativenavigation.parse.params.Number;
14
 import com.reactnativenavigation.parse.params.Text;
17
 import com.reactnativenavigation.parse.params.Text;
15
 import com.reactnativenavigation.presentation.Presenter;
18
 import com.reactnativenavigation.presentation.Presenter;
16
 import com.reactnativenavigation.presentation.SideMenuPresenter;
19
 import com.reactnativenavigation.presentation.SideMenuPresenter;
17
-import com.reactnativenavigation.utils.*;
20
+import com.reactnativenavigation.utils.CommandListenerAdapter;
21
+import com.reactnativenavigation.utils.Functions;
18
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
22
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
19
 import com.reactnativenavigation.viewcontrollers.ParentController;
23
 import com.reactnativenavigation.viewcontrollers.ParentController;
20
 import com.reactnativenavigation.viewcontrollers.ViewController;
24
 import com.reactnativenavigation.viewcontrollers.ViewController;
30
 import static org.mockito.Mockito.spy;
34
 import static org.mockito.Mockito.spy;
31
 import static org.mockito.Mockito.times;
35
 import static org.mockito.Mockito.times;
32
 import static org.mockito.Mockito.verify;
36
 import static org.mockito.Mockito.verify;
37
+import static org.mockito.Mockito.when;
33
 
38
 
34
 @SuppressWarnings("MagicNumber")
39
 @SuppressWarnings("MagicNumber")
35
 public class SideMenuControllerTest extends BaseTest {
40
 public class SideMenuControllerTest extends BaseTest {
46
 
51
 
47
     @Override
52
     @Override
48
     public void beforeEach() {
53
     public void beforeEach() {
49
-        activity = newActivity();
54
+        activity = createActivity();
55
+
50
         childRegistry = new ChildControllersRegistry();
56
         childRegistry = new ChildControllersRegistry();
51
         presenter = spy(new SideMenuPresenter());
57
         presenter = spy(new SideMenuPresenter());
52
         child = new SimpleComponentViewController(activity, childRegistry, "child", new Options());
58
         child = new SimpleComponentViewController(activity, childRegistry, "child", new Options());
305
         options.sideMenuRootOptions.right.animate = new Bool(false);
311
         options.sideMenuRootOptions.right.animate = new Bool(false);
306
         uut.mergeOptions(options);
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
 }