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
 import android.support.annotation.Nullable;
3
 import android.support.annotation.Nullable;
4
 
4
 
5
 import com.reactnativenavigation.parse.params.Button;
5
 import com.reactnativenavigation.parse.params.Button;
6
+import com.reactnativenavigation.utils.CollectionUtils;
6
 import com.reactnativenavigation.utils.TypefaceLoader;
7
 import com.reactnativenavigation.utils.TypefaceLoader;
7
 
8
 
8
 import org.json.JSONObject;
9
 import org.json.JSONObject;
38
     }
39
     }
39
 
40
 
40
     void mergeWithDefault(TopBarButtons defaultOptions) {
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
         back.mergeWithDefault(defaultOptions.back);
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
     public Text text = new NullText();
21
     public Text text = new NullText();
22
     public Bool enabled = new NullBool();
22
     public Bool enabled = new NullBool();
23
     public Bool disableIconTint = new NullBool();
23
     public Bool disableIconTint = new NullBool();
24
-    public int showAsAction;
24
+    public Number showAsAction = new NullNumber();
25
     public Color color = new NullColor();
25
     public Color color = new NullColor();
26
     public Color disabledColor = new NullColor();
26
     public Color disabledColor = new NullColor();
27
     public Number fontSize = new NullNumber();
27
     public Number fontSize = new NullNumber();
86
         return icon.hasValue();
86
         return icon.hasValue();
87
     }
87
     }
88
 
88
 
89
-    private static int parseShowAsAction(JSONObject json) {
89
+    private static Number parseShowAsAction(JSONObject json) {
90
         final Text showAsAction = TextParser.parse(json, "showAsAction");
90
         final Text showAsAction = TextParser.parse(json, "showAsAction");
91
         if (!showAsAction.hasValue()) {
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
         switch (showAsAction.get()) {
95
         switch (showAsAction.get()) {
96
             case "always":
96
             case "always":
97
-                return MenuItem.SHOW_AS_ACTION_ALWAYS;
97
+                return new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
98
             case "never":
98
             case "never":
99
-                return MenuItem.SHOW_AS_ACTION_NEVER;
99
+                return new Number(MenuItem.SHOW_AS_ACTION_NEVER);
100
             case "withText":
100
             case "withText":
101
-                return MenuItem.SHOW_AS_ACTION_WITH_TEXT;
101
+                return new Number(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
102
             case "ifRoom":
102
             case "ifRoom":
103
             default:
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
     @NonNull
76
     @NonNull
77
     private ArrayList<View> findActualTextViewInMenu() {
77
     private ArrayList<View> findActualTextViewInMenu() {
78
         ArrayList<View> outViews = new ArrayList<>();
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
         return outViews;
82
         return outViews;
81
     }
83
     }
82
 
84
 

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

100
 
100
 
101
     public void addToMenu(Toolbar toolbar, int position) {
101
     public void addToMenu(Toolbar toolbar, int position) {
102
         MenuItem menuItem = toolbar.getMenu().add(0, position, position, button.text.get(""));
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
         menuItem.setEnabled(button.enabled.isTrueOrUndefined());
104
         menuItem.setEnabled(button.enabled.isTrueOrUndefined());
105
         menuItem.setOnMenuItemClickListener(this);
105
         menuItem.setOnMenuItemClickListener(this);
106
         if (button.hasComponent()) {
106
         if (button.hasComponent()) {

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

4
 import android.graphics.drawable.Drawable;
4
 import android.graphics.drawable.Drawable;
5
 import android.support.annotation.NonNull;
5
 import android.support.annotation.NonNull;
6
 import android.support.v4.content.ContextCompat;
6
 import android.support.v4.content.ContextCompat;
7
+import android.util.Log;
7
 
8
 
8
 import com.reactnativenavigation.R;
9
 import com.reactnativenavigation.R;
9
 import com.reactnativenavigation.parse.params.Button;
10
 import com.reactnativenavigation.parse.params.Button;
38
         } else if (Constants.BACK_BUTTON_ID.equals(button.id)) {
39
         } else if (Constants.BACK_BUTTON_ID.equals(button.id)) {
39
             onSuccess.run(ContextCompat.getDrawable(context, R.drawable.ic_arrow_back_black_24dp));
40
             onSuccess.run(ContextCompat.getDrawable(context, R.drawable.ic_arrow_back_black_24dp));
40
         } else {
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
         button.id = "btn1";
161
         button.id = "btn1";
162
         button.text = new Text("Button");
162
         button.text = new Text("Button");
163
         button.fontFamily = Typeface.MONOSPACE;
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
     private void setIconButton(boolean enabled) {
167
     private void setIconButton(boolean enabled) {
171
         button.component.name = new NullText();
171
         button.component.name = new NullText();
172
         button.component.componentId = new NullText();
172
         button.component.componentId = new NullText();
173
         button.enabled = new Bool(enabled);
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
     private void setReactComponentButton() {
177
     private void setReactComponentButton() {

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

12
 import com.reactnativenavigation.mocks.TopBarButtonCreatorMock;
12
 import com.reactnativenavigation.mocks.TopBarButtonCreatorMock;
13
 import com.reactnativenavigation.parse.AnimationOptions;
13
 import com.reactnativenavigation.parse.AnimationOptions;
14
 import com.reactnativenavigation.parse.params.Button;
14
 import com.reactnativenavigation.parse.params.Button;
15
+import com.reactnativenavigation.parse.params.Number;
15
 import com.reactnativenavigation.parse.params.Text;
16
 import com.reactnativenavigation.parse.params.Text;
16
 import com.reactnativenavigation.utils.TitleBarHelper;
17
 import com.reactnativenavigation.utils.TitleBarHelper;
17
 import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
18
 import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
73
             Button button = new Button();
74
             Button button = new Button();
74
             button.id = "rightButtons" + i;
75
             button.id = "rightButtons" + i;
75
             button.text = new Text("btn" + i);
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
             result.add(spy(button));
78
             result.add(spy(button));
78
         }
79
         }
79
         return result;
80
         return result;