Bladeren bron

[v2][Android] Adding height and width as options for side menu to make it consistent with IOS (#3671)

dpnolte 6 jaren geleden
bovenliggende
commit
3d06fece5c

+ 14
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/SideMenuOptions.java Bestand weergeven

@@ -2,18 +2,26 @@ package com.reactnativenavigation.parse;
2 2
 
3 3
 import com.reactnativenavigation.parse.params.Bool;
4 4
 import com.reactnativenavigation.parse.params.NullBool;
5
+import com.reactnativenavigation.parse.params.NullNumber;
6
+import com.reactnativenavigation.parse.params.Number;
5 7
 import com.reactnativenavigation.parse.parsers.BoolParser;
8
+import com.reactnativenavigation.parse.parsers.NumberParser;
6 9
 
7 10
 import org.json.JSONObject;
8 11
 
9 12
 public class SideMenuOptions {
10 13
     public Bool visible = new NullBool();
14
+    public Number height = new NullNumber();
15
+    public Number width = new NullNumber();
11 16
 
12 17
     public static SideMenuOptions parse(JSONObject json) {
13 18
         SideMenuOptions options = new SideMenuOptions();
14 19
         if (json == null) return options;
15 20
 
16 21
         options.visible = BoolParser.parse(json, "visible");
22
+        options.height = NumberParser.parse(json, "height");
23
+        options.width = NumberParser.parse(json, "width");
24
+
17 25
         return options;
18 26
     }
19 27
 
@@ -21,5 +29,11 @@ public class SideMenuOptions {
21 29
         if (other.visible.hasValue()) {
22 30
             visible = other.visible;
23 31
         }
32
+        if (other.height.hasValue()) {
33
+            height = other.height;
34
+        }
35
+        if (other.width.hasValue()) {
36
+            width = other.width;
37
+        }
24 38
     }
25 39
 }

+ 31
- 8
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/SideMenuController.java Bestand weergeven

@@ -1,13 +1,16 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.content.res.Resources;
4 5
 import android.support.annotation.NonNull;
5 6
 import android.support.v4.widget.DrawerLayout;
6 7
 import android.support.v4.widget.DrawerLayout.LayoutParams;
8
+import android.util.TypedValue;
7 9
 import android.view.Gravity;
8 10
 import android.view.View;
9 11
 
10 12
 import com.reactnativenavigation.parse.Options;
13
+import com.reactnativenavigation.parse.SideMenuOptions;
11 14
 import com.reactnativenavigation.presentation.OptionsPresenter;
12 15
 import com.reactnativenavigation.presentation.SideMenuOptionsPresenter;
13 16
 import com.reactnativenavigation.views.Component;
@@ -87,13 +90,33 @@ public class SideMenuController extends ParentController<DrawerLayout> {
87 90
 		getView().addView(childView);
88 91
 	}
89 92
 
90
-	public void setLeftController(ViewController controller) {
91
-		this.leftController = controller;
92
-        getView().addView(controller.getView(), new LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.LEFT));
93
-	}
93
+    public void setLeftController(ViewController controller) {
94
+        this.leftController = controller;
95
+        int height = this.getHeight(options.sideMenuRootOptions.left);
96
+        int width = this.getWidth(options.sideMenuRootOptions.left);
97
+        getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.LEFT));
98
+    }
94 99
 
95
-	public void setRightController(ViewController controller) {
96
-		this.rightController = controller;
97
-        getView().addView(controller.getView(), new LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.RIGHT));
98
-	}
100
+    public void setRightController(ViewController controller) {
101
+        this.rightController = controller;
102
+        int height = this.getHeight(options.sideMenuRootOptions.right);
103
+        int width = this.getWidth(options.sideMenuRootOptions.right);
104
+        getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
105
+    }
106
+
107
+    protected int getWidth(SideMenuOptions sideMenuOptions) {
108
+        int width = MATCH_PARENT;
109
+        if (sideMenuOptions.width.hasValue()) {
110
+            width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sideMenuOptions.width.get(), Resources.getSystem().getDisplayMetrics());
111
+        }
112
+        return width;
113
+    }
114
+
115
+    protected int getHeight(SideMenuOptions sideMenuOptions) {
116
+        int height = MATCH_PARENT;
117
+        if (sideMenuOptions.height.hasValue()) {
118
+            height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sideMenuOptions.height.get(), Resources.getSystem().getDisplayMetrics());
119
+        }
120
+        return height;
121
+    }
99 122
 }

+ 70
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/SideMenuControllerTest.java Bestand weergeven

@@ -1,16 +1,23 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.content.res.Resources;
5
+import android.view.ViewGroup.LayoutParams;
6
+import android.util.TypedValue;
4 7
 import android.view.Gravity;
5 8
 
6 9
 import com.reactnativenavigation.BaseTest;
7 10
 import com.reactnativenavigation.mocks.SimpleComponentViewController;
8 11
 import com.reactnativenavigation.parse.Options;
12
+import com.reactnativenavigation.parse.SideMenuOptions;
9 13
 import com.reactnativenavigation.parse.params.Bool;
14
+import com.reactnativenavigation.parse.params.Number;
10 15
 import com.reactnativenavigation.presentation.OptionsPresenter;
11 16
 
17
+import org.junit.Assert;
12 18
 import org.junit.Test;
13 19
 
20
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
14 21
 import static org.assertj.core.api.Java6Assertions.assertThat;
15 22
 
16 23
 public class SideMenuControllerTest extends BaseTest {
@@ -55,4 +62,67 @@ public class SideMenuControllerTest extends BaseTest {
55 62
         uut.mergeOptions(options);
56 63
         assertThat(uut.options.sideMenuRootOptions).isNotEqualTo(initialOptions.sideMenuRootOptions);
57 64
     }
65
+
66
+    @Test
67
+    public void setLeftController_matchesParentByDefault() {
68
+        SideMenuOptions options = new SideMenuOptions();
69
+        assertThat(options.width.hasValue()).isFalse();
70
+        assertThat(options.height.hasValue()).isFalse();
71
+        uut.options.sideMenuRootOptions.left = options;
72
+
73
+        SimpleComponentViewController componentViewController = new SimpleComponentViewController(activity, childRegistry, "left", new Options());
74
+        uut.setLeftController(componentViewController);
75
+
76
+        LayoutParams params = componentViewController.getView().getLayoutParams();
77
+        assertThat(params.width).isEqualTo(MATCH_PARENT);
78
+        assertThat(params.height).isEqualTo(MATCH_PARENT);
79
+    }
80
+    @Test
81
+    public void setLeftController_setHeightAndWidthWithOptions() {
82
+        SideMenuOptions options = new SideMenuOptions();
83
+        options.height = new Number(100);
84
+        options.width = new Number(200);
85
+        uut.options.sideMenuRootOptions.left = options;
86
+
87
+        SimpleComponentViewController componentViewController = new SimpleComponentViewController(activity, childRegistry, "left", new Options());
88
+        uut.setLeftController(componentViewController);
89
+
90
+        int heightInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, Resources.getSystem().getDisplayMetrics());
91
+        int widthInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, Resources.getSystem().getDisplayMetrics());
92
+
93
+        LayoutParams params = componentViewController.getView().getLayoutParams();
94
+        assertThat(params.width).isEqualTo(widthInDp);
95
+        assertThat(params.height).isEqualTo(heightInDp);
96
+    }
97
+    @Test
98
+    public void setRightController_matchesParentByDefault() {
99
+        SideMenuOptions options = new SideMenuOptions();
100
+        assertThat(options.width.hasValue()).isFalse();
101
+        assertThat(options.height.hasValue()).isFalse();
102
+        uut.options.sideMenuRootOptions.left = options;
103
+
104
+        SimpleComponentViewController componentViewController = new SimpleComponentViewController(activity, childRegistry, "right", new Options());
105
+        uut.setRightController(componentViewController);
106
+
107
+        LayoutParams params = componentViewController.getView().getLayoutParams();
108
+        assertThat(params.width).isEqualTo(MATCH_PARENT);
109
+        assertThat(params.height).isEqualTo(MATCH_PARENT);
110
+    }
111
+    @Test
112
+    public void setRightController_setHeightAndWidthWithOptions() {
113
+        SideMenuOptions options = new SideMenuOptions();
114
+        options.height = new Number(100);
115
+        options.width = new Number(200);
116
+        uut.options.sideMenuRootOptions.left = options;
117
+
118
+        SimpleComponentViewController componentViewController = new SimpleComponentViewController(activity, childRegistry, "left", new Options());
119
+        uut.setLeftController(componentViewController);
120
+
121
+        int heightInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, Resources.getSystem().getDisplayMetrics());
122
+        int widthInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, Resources.getSystem().getDisplayMetrics());
123
+
124
+        LayoutParams params = componentViewController.getView().getLayoutParams();
125
+        assertThat(params.width).isEqualTo(widthInDp);
126
+        assertThat(params.height).isEqualTo(heightInDp);
127
+    }
58 128
 }