Browse Source

SideMenu visible android (#2782)

Guy Carmeli 6 years ago
parent
commit
3268f256c5
No account linked to committer's email address

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/Options.java View File

@@ -27,6 +27,7 @@ public class Options implements DEFAULT_VALUES {
27 27
         result.overlayOptions = OverlayOptions.parse(json.optJSONObject("overlay"));
28 28
         result.fabOptions = FabOptions.parse(json.optJSONObject("fab"));
29 29
         result.animationsOptions = AnimationsOptions.parse(json.optJSONObject("animations"));
30
+        result.sideMenuRootOptions = SideMenuRootOptions.parse(json.optJSONObject("sideMenu"));
30 31
 
31 32
         return result.withDefaultOptions(defaultOptions);
32 33
     }
@@ -39,6 +40,7 @@ public class Options implements DEFAULT_VALUES {
39 40
     @NonNull public OverlayOptions overlayOptions = new OverlayOptions();
40 41
     @NonNull public FabOptions fabOptions = new FabOptions();
41 42
     @NonNull public AnimationsOptions animationsOptions = new AnimationsOptions();
43
+    @NonNull public SideMenuRootOptions sideMenuRootOptions = new SideMenuRootOptions();
42 44
 
43 45
     void setTopTabIndex(int i) {
44 46
         topTabOptions.tabIndex = i;
@@ -55,6 +57,7 @@ public class Options implements DEFAULT_VALUES {
55 57
         result.overlayOptions = overlayOptions;
56 58
         result.fabOptions.mergeWith(fabOptions);
57 59
         result.animationsOptions.mergeWith(animationsOptions);
60
+        result.sideMenuRootOptions.mergeWith(sideMenuRootOptions);
58 61
         return result;
59 62
     }
60 63
 
@@ -68,6 +71,7 @@ public class Options implements DEFAULT_VALUES {
68 71
         result.bottomTabsOptions.mergeWith(other.bottomTabsOptions);
69 72
         result.fabOptions.mergeWith(other.fabOptions);
70 73
         result.animationsOptions.mergeWith(other.animationsOptions);
74
+        result.sideMenuRootOptions.mergeWith(other.sideMenuRootOptions);
71 75
         return result;
72 76
     }
73 77
 
@@ -79,6 +83,7 @@ public class Options implements DEFAULT_VALUES {
79 83
         bottomTabsOptions.mergeWithDefault(other.bottomTabsOptions);
80 84
         fabOptions.mergeWithDefault(other.fabOptions);
81 85
         animationsOptions.mergeWithDefault(other.animationsOptions);
86
+        sideMenuRootOptions.mergeWithDefault(other.sideMenuRootOptions);
82 87
         return this;
83 88
     }
84 89
 
@@ -106,4 +111,9 @@ public class Options implements DEFAULT_VALUES {
106 111
         bottomTabOptions = new BottomTabOptions();
107 112
         return this;
108 113
     }
114
+
115
+    public Options clearSideMenuOptions() {
116
+        sideMenuRootOptions = new SideMenuRootOptions();
117
+        return this;
118
+    }
109 119
 }

+ 25
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/SideMenuOptions.java View File

@@ -0,0 +1,25 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import com.reactnativenavigation.parse.params.Bool;
4
+import com.reactnativenavigation.parse.params.NullBool;
5
+import com.reactnativenavigation.parse.parsers.BoolParser;
6
+
7
+import org.json.JSONObject;
8
+
9
+public class SideMenuOptions {
10
+    public Bool visible = new NullBool();
11
+
12
+    public static SideMenuOptions parse(JSONObject json) {
13
+        SideMenuOptions options = new SideMenuOptions();
14
+        if (json == null) return options;
15
+
16
+        options.visible = BoolParser.parse(json, "visible");
17
+        return options;
18
+    }
19
+
20
+    public void mergeWith(SideMenuOptions other) {
21
+        if (other.visible.hasValue()) {
22
+            visible = other.visible;
23
+        }
24
+    }
25
+}

+ 27
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/SideMenuRootOptions.java View File

@@ -0,0 +1,27 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import org.json.JSONObject;
4
+
5
+public class SideMenuRootOptions {
6
+    public SideMenuOptions left = new SideMenuOptions();
7
+    public SideMenuOptions right = new SideMenuOptions();
8
+
9
+    public static SideMenuRootOptions parse(JSONObject json) {
10
+        SideMenuRootOptions options = new SideMenuRootOptions();
11
+        if (json == null) return options;
12
+
13
+        options.left = SideMenuOptions.parse(json.optJSONObject("left"));
14
+        options.right = SideMenuOptions.parse(json.optJSONObject("right"));
15
+
16
+        return options;
17
+    }
18
+
19
+    public void mergeWith(SideMenuRootOptions other) {
20
+        left.mergeWith(other.left);
21
+        right.mergeWith(other.right);
22
+    }
23
+
24
+    public void mergeWithDefault(SideMenuRootOptions sideMenuRootOptions) {
25
+
26
+    }
27
+}

+ 24
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/SideMenuOptionsPresenter.java View File

@@ -0,0 +1,24 @@
1
+package com.reactnativenavigation.presentation;
2
+
3
+import android.support.v4.widget.DrawerLayout;
4
+import android.view.Gravity;
5
+
6
+import com.reactnativenavigation.parse.SideMenuRootOptions;
7
+
8
+public class SideMenuOptionsPresenter {
9
+
10
+    private DrawerLayout sideMenu;
11
+
12
+    public SideMenuOptionsPresenter(DrawerLayout sideMenu) {
13
+        this.sideMenu = sideMenu;
14
+    }
15
+
16
+    public void present(SideMenuRootOptions options) {
17
+        if (options.left.visible.isTrue()) {
18
+            sideMenu.openDrawer(Gravity.LEFT);
19
+        }
20
+        if (options.right.visible.isTrue()) {
21
+            sideMenu.openDrawer(Gravity.RIGHT);
22
+        }
23
+    }
24
+}

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

@@ -5,9 +5,10 @@ import android.support.annotation.NonNull;
5 5
 import android.support.v4.widget.DrawerLayout;
6 6
 import android.view.Gravity;
7 7
 import android.view.View;
8
-import android.view.ViewGroup;
9 8
 
10 9
 import com.reactnativenavigation.parse.Options;
10
+import com.reactnativenavigation.presentation.NavigationOptionsListener;
11
+import com.reactnativenavigation.presentation.SideMenuOptionsPresenter;
11 12
 import com.reactnativenavigation.views.ReactComponent;
12 13
 
13 14
 import java.util.ArrayList;
@@ -16,7 +17,7 @@ import java.util.Collection;
16 17
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
17 18
 import static android.widget.ListPopupWindow.WRAP_CONTENT;
18 19
 
19
-public class SideMenuController extends ParentController {
20
+public class SideMenuController extends ParentController<DrawerLayout> implements NavigationOptionsListener {
20 21
 
21 22
 	private ViewController centerController;
22 23
 	private ViewController leftController;
@@ -28,7 +29,7 @@ public class SideMenuController extends ParentController {
28 29
 
29 30
 	@NonNull
30 31
 	@Override
31
-	protected ViewGroup createView() {
32
+	protected DrawerLayout createView() {
32 33
         return new DrawerLayout(getActivity());
33 34
 	}
34 35
 
@@ -55,6 +56,13 @@ public class SideMenuController extends ParentController {
55 56
         );
56 57
     }
57 58
 
59
+    @Override
60
+    public void mergeOptions(Options options) {
61
+        this.options = this.options.mergeWith(options);
62
+        new SideMenuOptionsPresenter(getView()).present(this.options.sideMenuRootOptions);
63
+        this.options = this.options.copy().clearSideMenuOptions();
64
+    }
65
+
58 66
     public void setCenterController(ViewController centerController) {
59 67
 		this.centerController = centerController;
60 68
 		View childView = centerController.getView();

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

@@ -0,0 +1,54 @@
1
+package com.reactnativenavigation.viewcontrollers;
2
+
3
+import android.app.Activity;
4
+import android.view.Gravity;
5
+
6
+import com.reactnativenavigation.BaseTest;
7
+import com.reactnativenavigation.mocks.SimpleComponentViewController;
8
+import com.reactnativenavigation.parse.Options;
9
+import com.reactnativenavigation.parse.params.Bool;
10
+
11
+import org.junit.Test;
12
+
13
+import static org.assertj.core.api.Java6Assertions.assertThat;
14
+
15
+public class SideMenuControllerTest extends BaseTest {
16
+    private SideMenuController uut;
17
+    private Activity activity;
18
+
19
+    @Override
20
+    public void beforeEach() {
21
+        activity = newActivity();
22
+        uut = new SideMenuController(activity, "sideMenu", new Options());
23
+    }
24
+
25
+    @Test
26
+    public void mergeOptions_openLeftSideMenu() throws Exception {
27
+        uut.setLeftController(new SimpleComponentViewController(activity, "left", new Options()));
28
+
29
+        Options options = new Options();
30
+        options.sideMenuRootOptions.left.visible = new Bool(true);
31
+        assertThat(uut.getView().isDrawerOpen(Gravity.LEFT)).isFalse();
32
+        uut.mergeOptions(options);
33
+        assertThat(uut.getView().isDrawerOpen(Gravity.LEFT)).isTrue();
34
+    }
35
+
36
+    @Test
37
+    public void mergeOptions_openRightSideMenu() throws Exception {
38
+        uut.setRightController(new SimpleComponentViewController(activity, "right", new Options()));
39
+
40
+        Options options = new Options();
41
+        options.sideMenuRootOptions.right.visible = new Bool(true);
42
+        assertThat(uut.getView().isDrawerOpen(Gravity.RIGHT)).isFalse();
43
+        uut.mergeOptions(options);
44
+        assertThat(uut.getView().isDrawerOpen(Gravity.RIGHT)).isTrue();
45
+    }
46
+
47
+    @Test
48
+    public void mergeOptions_optionsAreClearedAfterMerge() throws Exception {
49
+        Options initialOptions = uut.options;
50
+        Options options = new Options();
51
+        uut.mergeOptions(options);
52
+        assertThat(uut.options.sideMenuRootOptions).isNotEqualTo(initialOptions.sideMenuRootOptions);
53
+    }
54
+}