Browse Source

Support default button options on Android

Related to #3054
Guy Carmeli 6 years ago
parent
commit
c7aaf5ccc7

+ 15
- 2
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarButtons.java View File

@@ -3,6 +3,7 @@ package com.reactnativenavigation.parse;
3 3
 import android.support.annotation.Nullable;
4 4
 
5 5
 import com.reactnativenavigation.parse.params.Button;
6
+import com.reactnativenavigation.utils.CollectionUtils;
6 7
 import com.reactnativenavigation.utils.TypefaceLoader;
7 8
 
8 9
 import org.json.JSONObject;
@@ -38,8 +39,20 @@ public class TopBarButtons {
38 39
     }
39 40
 
40 41
     void mergeWithDefault(TopBarButtons defaultOptions) {
41
-        if (left == null) left = defaultOptions.left;
42
-        if (right == null) right = defaultOptions.right;
42
+        if (left == null) {
43
+            left = defaultOptions.left;
44
+        } else if (!CollectionUtils.isNullOrEmpty(defaultOptions.left)){
45
+            for (Button button : left) {
46
+                button.mergeWithDefault(defaultOptions.left.get(0));
47
+            }
48
+        }
49
+        if (right == null) {
50
+            right = defaultOptions.right;
51
+        } else if (!CollectionUtils.isNullOrEmpty(defaultOptions.right)) {
52
+            for (Button button : right) {
53
+                button.mergeWithDefault(defaultOptions.right.get(0));
54
+            }
55
+        }
43 56
         back.mergeWithDefault(defaultOptions.back);
44 57
     }
45 58
 }

+ 22
- 7
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/Button.java View File

@@ -21,7 +21,7 @@ public class Button {
21 21
     public Text text = new NullText();
22 22
     public Bool enabled = new NullBool();
23 23
     public Bool disableIconTint = new NullBool();
24
-    public int showAsAction;
24
+    public Number showAsAction = new NullNumber();
25 25
     public Color color = new NullColor();
26 26
     public Color disabledColor = new NullColor();
27 27
     public Number fontSize = new NullNumber();
@@ -86,22 +86,37 @@ public class Button {
86 86
         return icon.hasValue();
87 87
     }
88 88
 
89
-    private static int parseShowAsAction(JSONObject json) {
89
+    private static Number parseShowAsAction(JSONObject json) {
90 90
         final Text showAsAction = TextParser.parse(json, "showAsAction");
91 91
         if (!showAsAction.hasValue()) {
92
-            return MenuItem.SHOW_AS_ACTION_IF_ROOM;
92
+            return new Number(MenuItem.SHOW_AS_ACTION_IF_ROOM);
93 93
         }
94 94
 
95 95
         switch (showAsAction.get()) {
96 96
             case "always":
97
-                return MenuItem.SHOW_AS_ACTION_ALWAYS;
97
+                return new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
98 98
             case "never":
99
-                return MenuItem.SHOW_AS_ACTION_NEVER;
99
+                return new Number(MenuItem.SHOW_AS_ACTION_NEVER);
100 100
             case "withText":
101
-                return MenuItem.SHOW_AS_ACTION_WITH_TEXT;
101
+                return new Number(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
102 102
             case "ifRoom":
103 103
             default:
104
-                return MenuItem.SHOW_AS_ACTION_IF_ROOM;
104
+                return new Number(MenuItem.SHOW_AS_ACTION_IF_ROOM);
105 105
         }
106 106
     }
107
+
108
+    public void mergeWithDefault(Button defaultOptions) {
109
+        if (!text.hasValue()) text = defaultOptions.text;
110
+        if (!enabled.hasValue()) enabled = defaultOptions.enabled;
111
+        if (!disableIconTint.hasValue()) disableIconTint = defaultOptions.disableIconTint;
112
+        if (!color.hasValue()) color = defaultOptions.color;
113
+        if (!disabledColor.hasValue()) disabledColor = defaultOptions.disabledColor;
114
+        if (!fontSize.hasValue()) fontSize = defaultOptions.fontSize;
115
+        if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
116
+        if (!fontWeight.hasValue()) fontWeight = defaultOptions.fontWeight;
117
+        if (!testId.hasValue()) testId = defaultOptions.testId;
118
+        if (!component.hasValue()) component = defaultOptions.component;
119
+        if (!showAsAction.hasValue()) showAsAction = defaultOptions.showAsAction;
120
+        if (!icon.hasValue()) icon = defaultOptions.icon;
121
+    }
107 122
 }

+ 3
- 1
lib/android/app/src/main/java/com/reactnativenavigation/utils/ButtonOptionsPresenter.java View File

@@ -76,7 +76,9 @@ public class ButtonOptionsPresenter {
76 76
     @NonNull
77 77
     private ArrayList<View> findActualTextViewInMenu() {
78 78
         ArrayList<View> outViews = new ArrayList<>();
79
-        toolbar.findViewsWithText(outViews, button.text.get(), View.FIND_VIEWS_WITH_TEXT);
79
+        if (button.text.hasValue()) {
80
+            toolbar.findViewsWithText(outViews, button.text.get(), View.FIND_VIEWS_WITH_TEXT);
81
+        }
80 82
         return outViews;
81 83
     }
82 84
 

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TopBarButtonController.java View File

@@ -100,7 +100,7 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
100 100
 
101 101
     public void addToMenu(Toolbar toolbar, int position) {
102 102
         MenuItem menuItem = toolbar.getMenu().add(0, position, position, button.text.get(""));
103
-        menuItem.setShowAsAction(button.showAsAction);
103
+        if (button.showAsAction.hasValue()) menuItem.setShowAsAction(button.showAsAction.get());
104 104
         menuItem.setEnabled(button.enabled.isTrueOrUndefined());
105 105
         menuItem.setOnMenuItemClickListener(this);
106 106
         if (button.hasComponent()) {

+ 2
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/button/NavigationIconResolver.java View File

@@ -4,6 +4,7 @@ import android.content.Context;
4 4
 import android.graphics.drawable.Drawable;
5 5
 import android.support.annotation.NonNull;
6 6
 import android.support.v4.content.ContextCompat;
7
+import android.util.Log;
7 8
 
8 9
 import com.reactnativenavigation.R;
9 10
 import com.reactnativenavigation.parse.params.Button;
@@ -38,7 +39,7 @@ public class NavigationIconResolver {
38 39
         } else if (Constants.BACK_BUTTON_ID.equals(button.id)) {
39 40
             onSuccess.run(ContextCompat.getDrawable(context, R.drawable.ic_arrow_back_black_24dp));
40 41
         } else {
41
-            throw new RuntimeException("Left button needs to have an icon");
42
+            Log.w("RNN", "Left button needs to have an icon");
42 43
         }
43 44
     }
44 45
 }

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopBarButtonControllerTest.java View File

@@ -161,7 +161,7 @@ public class TopBarButtonControllerTest extends BaseTest {
161 161
         button.id = "btn1";
162 162
         button.text = new Text("Button");
163 163
         button.fontFamily = Typeface.MONOSPACE;
164
-        button.showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS;
164
+        button.showAsAction = new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
165 165
     }
166 166
 
167 167
     private void setIconButton(boolean enabled) {
@@ -171,7 +171,7 @@ public class TopBarButtonControllerTest extends BaseTest {
171 171
         button.component.name = new NullText();
172 172
         button.component.componentId = new NullText();
173 173
         button.enabled = new Bool(enabled);
174
-        button.showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS;
174
+        button.showAsAction = new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
175 175
     }
176 176
 
177 177
     private void setReactComponentButton() {

+ 2
- 1
lib/android/app/src/test/java/com/reactnativenavigation/views/TopBarTest.java View File

@@ -12,6 +12,7 @@ import com.reactnativenavigation.mocks.TopBarBackgroundViewCreatorMock;
12 12
 import com.reactnativenavigation.mocks.TopBarButtonCreatorMock;
13 13
 import com.reactnativenavigation.parse.AnimationOptions;
14 14
 import com.reactnativenavigation.parse.params.Button;
15
+import com.reactnativenavigation.parse.params.Number;
15 16
 import com.reactnativenavigation.parse.params.Text;
16 17
 import com.reactnativenavigation.utils.TitleBarHelper;
17 18
 import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
@@ -73,7 +74,7 @@ public class TopBarTest extends BaseTest {
73 74
             Button button = new Button();
74 75
             button.id = "rightButtons" + i;
75 76
             button.text = new Text("btn" + i);
76
-            button.showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS;
77
+            button.showAsAction = new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
77 78
             result.add(spy(button));
78 79
         }
79 80
         return result;