瀏覽代碼

Close opened sideMenu on hardware back press

Guy Carmeli 6 年之前
父節點
當前提交
6388421331

+ 3
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/LayoutFactory.java 查看文件

@@ -6,13 +6,14 @@ import com.facebook.react.ReactInstanceManager;
6 6
 import com.reactnativenavigation.presentation.BottomTabOptionsPresenter;
7 7
 import com.reactnativenavigation.presentation.BottomTabsOptionsPresenter;
8 8
 import com.reactnativenavigation.presentation.OptionsPresenter;
9
+import com.reactnativenavigation.presentation.SideMenuOptionsPresenter;
9 10
 import com.reactnativenavigation.presentation.StackOptionsPresenter;
10 11
 import com.reactnativenavigation.react.EventEmitter;
11 12
 import com.reactnativenavigation.utils.ImageLoader;
12 13
 import com.reactnativenavigation.utils.TypefaceLoader;
13 14
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
14 15
 import com.reactnativenavigation.viewcontrollers.ComponentViewController;
15
-import com.reactnativenavigation.viewcontrollers.SideMenuController;
16
+import com.reactnativenavigation.viewcontrollers.sidemenu.SideMenuController;
16 17
 import com.reactnativenavigation.viewcontrollers.ViewController;
17 18
 import com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabsController;
18 19
 import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalComponentCreator;
@@ -83,6 +84,7 @@ public class LayoutFactory {
83 84
                 childRegistry,
84 85
                 node.id,
85 86
                 parse(typefaceManager, node.getOptions()),
87
+                new SideMenuOptionsPresenter(),
86 88
                 new OptionsPresenter(activity, defaultOptions)
87 89
         );
88 90
 		ViewController childControllerCenter = null, childControllerLeft = null, childControllerRight = null;

+ 13
- 1
lib/android/app/src/main/java/com/reactnativenavigation/presentation/SideMenuOptionsPresenter.java 查看文件

@@ -9,7 +9,7 @@ public class SideMenuOptionsPresenter {
9 9
 
10 10
     private DrawerLayout sideMenu;
11 11
 
12
-    public SideMenuOptionsPresenter(DrawerLayout sideMenu) {
12
+    public void bindView(DrawerLayout sideMenu) {
13 13
         this.sideMenu = sideMenu;
14 14
     }
15 15
 
@@ -28,4 +28,16 @@ public class SideMenuOptionsPresenter {
28 28
             sideMenu.closeDrawer(Gravity.RIGHT);
29 29
         }
30 30
     }
31
+
32
+    public boolean handleBack() {
33
+        if (sideMenu.isDrawerOpen(Gravity.LEFT)) {
34
+            sideMenu.closeDrawer(Gravity.LEFT);
35
+            return true;
36
+        }
37
+        if (sideMenu.isDrawerOpen(Gravity.RIGHT)) {
38
+            sideMenu.closeDrawer(Gravity.RIGHT);
39
+            return true;
40
+        }
41
+        return false;
42
+    }
31 43
 }

lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/SideMenuController.java → lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuController.java 查看文件

@@ -1,4 +1,4 @@
1
-package com.reactnativenavigation.viewcontrollers;
1
+package com.reactnativenavigation.viewcontrollers.sidemenu;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.content.res.Resources;
@@ -13,6 +13,10 @@ import com.reactnativenavigation.parse.Options;
13 13
 import com.reactnativenavigation.parse.SideMenuOptions;
14 14
 import com.reactnativenavigation.presentation.OptionsPresenter;
15 15
 import com.reactnativenavigation.presentation.SideMenuOptionsPresenter;
16
+import com.reactnativenavigation.utils.CommandListener;
17
+import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
18
+import com.reactnativenavigation.viewcontrollers.ParentController;
19
+import com.reactnativenavigation.viewcontrollers.ViewController;
16 20
 import com.reactnativenavigation.views.Component;
17 21
 
18 22
 import java.util.ArrayList;
@@ -25,10 +29,12 @@ public class SideMenuController extends ParentController<DrawerLayout> {
25 29
 	private ViewController centerController;
26 30
 	private ViewController leftController;
27 31
 	private ViewController rightController;
32
+    private SideMenuOptionsPresenter presenter;
28 33
 
29
-	public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, OptionsPresenter presenter) {
34
+    public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, SideMenuOptionsPresenter sideMenuOptionsPresenter, OptionsPresenter presenter) {
30 35
 		super(activity, childRegistry, id, presenter, initialOptions);
31
-	}
36
+        this.presenter = sideMenuOptionsPresenter;
37
+    }
32 38
 
33 39
     @Override
34 40
     protected ViewController getCurrentChild() {
@@ -43,7 +49,9 @@ public class SideMenuController extends ParentController<DrawerLayout> {
43 49
     @NonNull
44 50
 	@Override
45 51
 	protected DrawerLayout createView() {
46
-        return new DrawerLayout(getActivity());
52
+        DrawerLayout sideMenu = new DrawerLayout(getActivity());
53
+        presenter.bindView(sideMenu);
54
+        return sideMenu;
47 55
 	}
48 56
 
49 57
     @Override
@@ -72,7 +80,7 @@ public class SideMenuController extends ParentController<DrawerLayout> {
72 80
     @Override
73 81
     public void mergeChildOptions(Options options, ViewController childController, Component child) {
74 82
         super.mergeChildOptions(options, childController, child);
75
-        new SideMenuOptionsPresenter(getView()).present(options.sideMenuRootOptions);
83
+        presenter.present(options.sideMenuRootOptions);
76 84
         performOnParentController(parentController ->
77 85
                 ((ParentController) parentController).mergeChildOptions(options.copy().clearSideMenuOptions(), childController, child)
78 86
         );
@@ -81,7 +89,12 @@ public class SideMenuController extends ParentController<DrawerLayout> {
81 89
     @Override
82 90
     public void mergeOptions(Options options) {
83 91
         super.mergeOptions(options);
84
-        new SideMenuOptionsPresenter(getView()).present(this.options.sideMenuRootOptions);
92
+        presenter.present(this.options.sideMenuRootOptions);
93
+    }
94
+
95
+    @Override
96
+    public boolean handleBack(CommandListener listener) {
97
+        return presenter.handleBack() || super.handleBack(listener);
85 98
     }
86 99
 
87 100
     public void setCenterController(ViewController centerController) {
@@ -104,7 +117,7 @@ public class SideMenuController extends ParentController<DrawerLayout> {
104 117
         getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
105 118
     }
106 119
 
107
-    protected int getWidth(SideMenuOptions sideMenuOptions) {
120
+    private int getWidth(SideMenuOptions sideMenuOptions) {
108 121
         int width = MATCH_PARENT;
109 122
         if (sideMenuOptions.width.hasValue()) {
110 123
             width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sideMenuOptions.width.get(), Resources.getSystem().getDisplayMetrics());
@@ -112,7 +125,7 @@ public class SideMenuController extends ParentController<DrawerLayout> {
112 125
         return width;
113 126
     }
114 127
 
115
-    protected int getHeight(SideMenuOptions sideMenuOptions) {
128
+    private int getHeight(SideMenuOptions sideMenuOptions) {
116 129
         int height = MATCH_PARENT;
117 130
         if (sideMenuOptions.height.hasValue()) {
118 131
             height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sideMenuOptions.height.get(), Resources.getSystem().getDisplayMetrics());

lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/SideMenuControllerTest.java → lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuControllerTest.java 查看文件

@@ -1,4 +1,4 @@
1
-package com.reactnativenavigation.viewcontrollers;
1
+package com.reactnativenavigation.viewcontrollers.sidemenu;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.content.res.Resources;
@@ -13,24 +13,36 @@ import com.reactnativenavigation.parse.SideMenuOptions;
13 13
 import com.reactnativenavigation.parse.params.Bool;
14 14
 import com.reactnativenavigation.parse.params.Number;
15 15
 import com.reactnativenavigation.presentation.OptionsPresenter;
16
+import com.reactnativenavigation.presentation.SideMenuOptionsPresenter;
17
+import com.reactnativenavigation.utils.CommandListenerAdapter;
18
+import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
16 19
 
17
-import org.junit.Assert;
18 20
 import org.junit.Test;
19 21
 
20 22
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
21 23
 import static org.assertj.core.api.Java6Assertions.assertThat;
24
+import static org.mockito.Mockito.spy;
25
+import static org.mockito.Mockito.verify;
22 26
 
27
+@SuppressWarnings("MagicNumber")
23 28
 public class SideMenuControllerTest extends BaseTest {
24 29
     private SideMenuController uut;
25 30
     private Activity activity;
26 31
     private ChildControllersRegistry childRegistry;
32
+    private SideMenuOptionsPresenter presenter;
27 33
 
28 34
     @Override
29 35
     public void beforeEach() {
30 36
         activity = newActivity();
31 37
         childRegistry = new ChildControllersRegistry();
32
-        OptionsPresenter presenter = new OptionsPresenter(activity, new Options());
33
-        uut = new SideMenuController(activity, childRegistry, "sideMenu", new Options(), presenter);
38
+        presenter = spy(new SideMenuOptionsPresenter());
39
+        uut = new SideMenuController(activity, childRegistry, "sideMenu", new Options(), presenter, new OptionsPresenter(activity, new Options()));
40
+    }
41
+
42
+    @Test
43
+    public void createView_bindView() {
44
+        uut.ensureViewIsCreated();
45
+        verify(presenter).bindView(uut.getView());
34 46
     }
35 47
 
36 48
     @Test
@@ -125,4 +137,22 @@ public class SideMenuControllerTest extends BaseTest {
125 137
         assertThat(params.width).isEqualTo(widthInDp);
126 138
         assertThat(params.height).isEqualTo(heightInDp);
127 139
     }
140
+
141
+    @Test
142
+    public void handleBack_closesLeftMenu() {
143
+        uut.setLeftController(new SimpleComponentViewController(activity, childRegistry, "left", new Options()));
144
+        assertThat(uut.handleBack(new CommandListenerAdapter())).isFalse();
145
+
146
+        uut.mergeOptions(SideMenuTestHelper.LEFT_OPEN);
147
+        assertThat(uut.handleBack(new CommandListenerAdapter())).isTrue();
148
+    }
149
+
150
+    @Test
151
+    public void handleBack_closesRightMenu() {
152
+        uut.setRightController(new SimpleComponentViewController(activity, childRegistry, "right", new Options()));
153
+        assertThat(uut.handleBack(new CommandListenerAdapter())).isFalse();
154
+
155
+        uut.mergeOptions(SideMenuTestHelper.RIGHT_OPEN);
156
+        assertThat(uut.handleBack(new CommandListenerAdapter())).isTrue();
157
+    }
128 158
 }

+ 14
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuTestHelper.java 查看文件

@@ -0,0 +1,14 @@
1
+package com.reactnativenavigation.viewcontrollers.sidemenu;
2
+
3
+import com.reactnativenavigation.parse.Options;
4
+import com.reactnativenavigation.parse.params.Bool;
5
+
6
+public class SideMenuTestHelper {
7
+    static final Options LEFT_OPEN = new Options();
8
+    static final Options RIGHT_OPEN = new Options();
9
+
10
+    static {
11
+        LEFT_OPEN.sideMenuRootOptions.left.visible = new Bool(true);
12
+        RIGHT_OPEN.sideMenuRootOptions.right.visible = new Bool(true);
13
+    }
14
+}