Browse Source

Apply BottomTabs options (#2763)

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

+ 24
- 16
lib/android/app/src/main/java/com/reactnativenavigation/parse/BottomTabsOptions.java View File

@@ -12,10 +12,11 @@ public class BottomTabsOptions implements DEFAULT_VALUES {
12 12
 		BottomTabsOptions options = new BottomTabsOptions();
13 13
 		if (json == null) return options;
14 14
 
15
-        options.color = ColorParser.parse(json, "tabColor");
16
-        options.selectedColor = ColorParser.parse(json, "selectedTabColor");
15
+        options.backgroundColor = ColorParser.parse(json, "backgroundColor");
16
+        options.tabColor = ColorParser.parse(json, "tabColor");
17
+        options.selectedTabColor = ColorParser.parse(json, "selectedTabColor");
17 18
         options.currentTabId = TextParser.parse(json, "currentTabId");
18
-		options.currentTabIndex = json.optInt("currentTabIndex", NO_INT_VALUE);
19
+		options.currentTabIndex = NumberParser.parse(json,"currentTabIndex");
19 20
 		options.visible = BoolParser.parse(json,"visible");
20 21
 		options.animateHide = BoolParser.parse(json,"animateHide");
21 22
         options.testId = TextParser.parse(json, "testID");
@@ -23,11 +24,12 @@ public class BottomTabsOptions implements DEFAULT_VALUES {
23 24
 		return options;
24 25
 	}
25 26
 
26
-    public Color color = new NullColor();
27
-    private Color selectedColor = new NullColor();
27
+    public Color backgroundColor = new NullColor();
28
+    public Color tabColor = new NullColor();
29
+    public Color selectedTabColor = new NullColor();
28 30
 	Bool visible = new NullBool();
29 31
 	Bool animateHide = new NullBool();
30
-	public int currentTabIndex = NO_INT_VALUE;
32
+	public Number currentTabIndex = new NullNumber();
31 33
 	public Text currentTabId = new NullText();
32 34
     public Text testId = new NullText();
33 35
 
@@ -35,7 +37,7 @@ public class BottomTabsOptions implements DEFAULT_VALUES {
35 37
 		if (other.currentTabId.hasValue()) {
36 38
 			currentTabId = other.currentTabId;
37 39
 		}
38
-		if (NO_INT_VALUE != other.currentTabIndex) {
40
+		if (other.currentTabIndex.hasValue()) {
39 41
             currentTabIndex = other.currentTabIndex;
40 42
 		}
41 43
 		if (other.visible.hasValue()) {
@@ -44,11 +46,14 @@ public class BottomTabsOptions implements DEFAULT_VALUES {
44 46
 		if (other.animateHide.hasValue()) {
45 47
 			animateHide = other.animateHide;
46 48
 		}
47
-        if (other.color.hasValue()) {
48
-            color = other.color;
49
+        if (other.tabColor.hasValue()) {
50
+            tabColor = other.tabColor;
49 51
         }
50
-        if (other.selectedColor.hasValue()) {
51
-            selectedColor = other.selectedColor;
52
+        if (other.selectedTabColor.hasValue()) {
53
+            selectedTabColor = other.selectedTabColor;
54
+        }
55
+        if (other.backgroundColor.hasValue()) {
56
+		    backgroundColor = other.backgroundColor;
52 57
         }
53 58
     }
54 59
 
@@ -56,7 +61,7 @@ public class BottomTabsOptions implements DEFAULT_VALUES {
56 61
         if (!currentTabId.hasValue()) {
57 62
             currentTabId = defaultOptions.currentTabId;
58 63
         }
59
-        if (NO_INT_VALUE == currentTabIndex) {
64
+        if (!currentTabIndex.hasValue()) {
60 65
             currentTabIndex = defaultOptions.currentTabIndex;
61 66
         }
62 67
         if (!visible.hasValue()) {
@@ -65,11 +70,14 @@ public class BottomTabsOptions implements DEFAULT_VALUES {
65 70
         if (!animateHide.hasValue()) {
66 71
             animateHide = defaultOptions.animateHide;
67 72
         }
68
-        if (!color.hasValue()) {
69
-            color = defaultOptions.color;
73
+        if (!tabColor.hasValue()) {
74
+            tabColor = defaultOptions.tabColor;
75
+        }
76
+        if (!selectedTabColor.hasValue()) {
77
+            selectedTabColor = defaultOptions.selectedTabColor;
70 78
         }
71
-        if (!selectedColor.hasValue()) {
72
-            selectedColor = defaultOptions.selectedColor;
79
+        if (!backgroundColor.hasValue()) {
80
+            backgroundColor = defaultOptions.backgroundColor;
73 81
         }
74 82
     }
75 83
 }

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

@@ -3,6 +3,7 @@ package com.reactnativenavigation.presentation;
3 3
 import android.support.annotation.IntRange;
4 4
 
5 5
 import com.reactnativenavigation.parse.BottomTabOptions;
6
+import com.reactnativenavigation.parse.BottomTabsOptions;
6 7
 import com.reactnativenavigation.parse.Options;
7 8
 import com.reactnativenavigation.views.BottomTabs;
8 9
 
@@ -13,6 +14,10 @@ public class BottomTabOptionsPresenter {
13 14
         this.bottomTabs = bottomTabs;
14 15
     }
15 16
 
17
+    public void present(Options options) {
18
+        applyBottomTabsOptions(options.bottomTabsOptions);
19
+    }
20
+
16 21
     public void present(Options options, @IntRange(from = 0) int bottomTabIndex) {
17 22
         applyBottomTabOptions(options.bottomTabOptions, bottomTabIndex);
18 23
     }
@@ -22,4 +27,22 @@ public class BottomTabOptionsPresenter {
22 27
             bottomTabs.setBadge(bottomTabIndex, options.badge);
23 28
         }
24 29
     }
30
+
31
+    private void applyBottomTabsOptions(BottomTabsOptions options) {
32
+        if (options.backgroundColor.hasValue()) {
33
+            bottomTabs.setBackgroundColor(options.backgroundColor.get());
34
+        }
35
+        if (options.currentTabIndex.hasValue()) {
36
+            bottomTabs.setCurrentItem(options.currentTabIndex.get());
37
+        }
38
+        if (options.testId.hasValue()) {
39
+            bottomTabs.setTag(options.testId.get());
40
+        }
41
+        if (options.selectedTabColor.hasValue()) {
42
+            bottomTabs.setAccentColor(options.selectedTabColor.get());
43
+        }
44
+        if (options.tabColor.hasValue()) {
45
+            bottomTabs.setInactiveColor(options.tabColor.get());
46
+        }
47
+    }
25 48
 }

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

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
-import android.graphics.Color;
5 4
 import android.graphics.drawable.Drawable;
6 5
 import android.support.annotation.IntRange;
7 6
 import android.support.annotation.NonNull;
@@ -11,13 +10,11 @@ import android.widget.RelativeLayout;
11 10
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
12 11
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
13 12
 import com.reactnativenavigation.parse.BottomTabOptions;
14
-import com.reactnativenavigation.parse.BottomTabsOptions;
15 13
 import com.reactnativenavigation.parse.Options;
16 14
 import com.reactnativenavigation.parse.Text;
17 15
 import com.reactnativenavigation.presentation.BottomTabOptionsPresenter;
18 16
 import com.reactnativenavigation.presentation.NavigationOptionsListener;
19 17
 import com.reactnativenavigation.utils.ImageLoader;
20
-import com.reactnativenavigation.utils.UiUtils;
21 18
 import com.reactnativenavigation.views.BottomTabs;
22 19
 import com.reactnativenavigation.views.ReactComponent;
23 20
 
@@ -29,7 +26,6 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
29 26
 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
30 27
 import static android.widget.RelativeLayout.ABOVE;
31 28
 import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
32
-import static com.reactnativenavigation.parse.DEFAULT_VALUES.NO_INT_VALUE;
33 29
 
34 30
 public class BottomTabsController extends ParentController implements AHBottomNavigation.OnTabSelectedListener, NavigationOptionsListener {
35 31
 	private BottomTabs bottomTabs;
@@ -45,7 +41,7 @@ public class BottomTabsController extends ParentController implements AHBottomNa
45 41
 	@Override
46 42
 	protected ViewGroup createView() {
47 43
 		RelativeLayout root = new RelativeLayout(getActivity());
48
-		bottomTabs = new BottomTabs(getActivity(), options.bottomTabsOptions);
44
+		bottomTabs = new BottomTabs(getActivity());
49 45
         bottomTabs.setOnTabSelectedListener(this);
50 46
 		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
51 47
 		lp.addRule(ALIGN_PARENT_BOTTOM);
@@ -53,6 +49,12 @@ public class BottomTabsController extends ParentController implements AHBottomNa
53 49
 		return root;
54 50
 	}
55 51
 
52
+    @Override
53
+    public void applyOptions(Options options) {
54
+        super.applyOptions(options);
55
+        new BottomTabOptionsPresenter(bottomTabs).present(options);
56
+    }
57
+
56 58
     @Override
57 59
     public void applyOptions(Options options, ReactComponent childComponent) {
58 60
         super.applyOptions(options, childComponent);
@@ -92,19 +94,18 @@ public class BottomTabsController extends ParentController implements AHBottomNa
92 94
 		getView();
93 95
 		for (int i = 0; i < tabs.size(); i++) {
94 96
 		    tabs.get(i).setParentController(this);
95
-			createTab(i, tabs.get(i).options.bottomTabOptions, tabs.get(i).options.bottomTabsOptions);
97
+			createTab(i, tabs.get(i).options.bottomTabOptions);
96 98
 		}
97 99
 		selectTabAtIndex(0);
98 100
 	}
99 101
 
100
-	private void createTab(int index, final BottomTabOptions tabOptions, final BottomTabsOptions bottomTabsOptions) {
102
+	private void createTab(int index, final BottomTabOptions tabOptions) {
101 103
 	    if (!tabOptions.icon.hasValue()) {
102 104
             throw new RuntimeException("BottomTab must have an icon");
103 105
         }
104 106
         imageLoader.loadIcon(getActivity(), tabOptions.icon.get(), new ImageLoader.ImageLoadingListener() {
105 107
             @Override
106 108
             public void onComplete(@NonNull Drawable drawable) {
107
-                setIconColor(drawable, bottomTabsOptions);
108 109
                 AHBottomNavigationItem item = new AHBottomNavigationItem(tabOptions.title.get(""), drawable);
109 110
                 bottomTabs.addItem(item);
110 111
                 bottomTabs.post(() -> bottomTabs.setTabTag(index, tabOptions.testId));
@@ -120,10 +121,6 @@ public class BottomTabsController extends ParentController implements AHBottomNa
120 121
         params.addRule(ABOVE, bottomTabs.getId());
121 122
 	}
122 123
 
123
-    private void setIconColor(Drawable drawable, BottomTabsOptions options) {
124
-        UiUtils.tintDrawable(drawable, Color.RED);
125
-    }
126
-
127 124
     int getSelectedIndex() {
128 125
 		return bottomTabs.getCurrentItem();
129 126
 	}
@@ -137,8 +134,8 @@ public class BottomTabsController extends ParentController implements AHBottomNa
137 134
 	@Override
138 135
 	public void mergeOptions(Options options) {
139 136
         this.options = this.options.mergeWith(options);
140
-        if (options.bottomTabsOptions.currentTabIndex != NO_INT_VALUE) {
141
-            selectTabAtIndex(options.bottomTabsOptions.currentTabIndex);
137
+        if (options.bottomTabsOptions.currentTabIndex.hasValue()) {
138
+            selectTabAtIndex(options.bottomTabsOptions.currentTabIndex.get());
142 139
         }
143 140
         if (options.bottomTabsOptions.currentTabId.hasValue()) {
144 141
             Text id = options.bottomTabsOptions.currentTabId;

+ 2
- 7
lib/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java View File

@@ -4,20 +4,15 @@ import android.annotation.SuppressLint;
4 4
 import android.content.Context;
5 5
 
6 6
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
7
-import com.reactnativenavigation.parse.BottomTabsOptions;
8 7
 import com.reactnativenavigation.parse.Text;
9 8
 import com.reactnativenavigation.utils.CompatUtils;
10 9
 
11 10
 @SuppressLint("ViewConstructor")
12 11
 public class BottomTabs extends AHBottomNavigation {
13
-    public BottomTabs(Context context, BottomTabsOptions bottomTabsOptions) {
12
+    public BottomTabs(Context context) {
14 13
         super(context);
15 14
         setId(CompatUtils.generateViewId());
16
-        setTestId(bottomTabsOptions.testId);
17
-    }
18
-
19
-    private void setTestId(Text testId) {
20
-        if (testId.hasValue()) setTag(testId.get());
15
+        setContentDescription("BottomTabs");
21 16
     }
22 17
 
23 18
     public void setTabTag(int index, Text testId) {

+ 5
- 5
lib/android/app/src/test/java/com/reactnativenavigation/parse/OptionsTest.java View File

@@ -37,7 +37,7 @@ public class OptionsTest extends BaseTest {
37 37
     private static final Bool BOTTOM_TABS_VISIBLE = new Bool(true);
38 38
     private static final String BOTTOM_TABS_BADGE = "3";
39 39
     private static final String BOTTOM_TABS_CURRENT_TAB_ID = "ComponentId";
40
-    private static final int BOTTOM_TABS_CURRENT_TAB_INDEX = 1;
40
+    private static final Number BOTTOM_TABS_CURRENT_TAB_INDEX = new Number(1);
41 41
     private TypefaceLoader mockLoader;
42 42
 
43 43
     @Override
@@ -73,7 +73,7 @@ public class OptionsTest extends BaseTest {
73 73
         assertThat(result.bottomTabsOptions.animateHide.get()).isEqualTo(BOTTOM_TABS_ANIMATE_HIDE.get());
74 74
         assertThat(result.bottomTabsOptions.visible.get()).isEqualTo(BOTTOM_TABS_VISIBLE.get());
75 75
         assertThat(result.bottomTabsOptions.currentTabId.get()).isEqualTo(BOTTOM_TABS_CURRENT_TAB_ID);
76
-        assertThat(result.bottomTabsOptions.currentTabIndex).isEqualTo(BOTTOM_TABS_CURRENT_TAB_INDEX);
76
+        assertThat(result.bottomTabsOptions.currentTabIndex.get()).isEqualTo(BOTTOM_TABS_CURRENT_TAB_INDEX.get());
77 77
         assertThat(result.fabOptions.id.get()).isEqualTo(FAB_ID);
78 78
         assertThat(result.fabOptions.backgroundColor.get()).isEqualTo(FAB_BACKGROUND_COLOR);
79 79
         assertThat(result.fabOptions.clickColor.get()).isEqualTo(FAB_CLICK_COLOR);
@@ -88,7 +88,7 @@ public class OptionsTest extends BaseTest {
88 88
     private JSONObject createBottomTabs() throws JSONException {
89 89
         return new JSONObject()
90 90
                 .put("currentTabId", BOTTOM_TABS_CURRENT_TAB_ID)
91
-                .put("currentTabIndex", BOTTOM_TABS_CURRENT_TAB_INDEX)
91
+                .put("currentTabIndex", BOTTOM_TABS_CURRENT_TAB_INDEX.get())
92 92
                 .put("visible", BOTTOM_TABS_VISIBLE.get())
93 93
                 .put("animateHide", BOTTOM_TABS_ANIMATE_HIDE.get());
94 94
     }
@@ -223,9 +223,9 @@ public class OptionsTest extends BaseTest {
223 223
     @Test
224 224
     public void clear_bottomTabsOptions() throws Exception {
225 225
         Options uut = new Options();
226
-        uut.bottomTabsOptions.color = new Color(android.graphics.Color.RED);
226
+        uut.bottomTabsOptions.tabColor = new Color(android.graphics.Color.RED);
227 227
         uut.clearBottomTabsOptions();
228
-        assertThat(uut.bottomTabsOptions.color.hasValue()).isFalse();
228
+        assertThat(uut.bottomTabsOptions.tabColor.hasValue()).isFalse();
229 229
     }
230 230
 
231 231
     @Test

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

@@ -124,7 +124,7 @@ public class BottomTabsControllerTest extends BaseTest {
124 124
     @Test
125 125
     public void applyOptions_bottomTabsOptionsAreClearedAfterApply() throws Exception {
126 126
         List<ViewController> tabs = createTabs();
127
-        child1.options.bottomTabsOptions.color = new Color(android.graphics.Color.RED);
127
+        child1.options.bottomTabsOptions.tabColor = new Color(android.graphics.Color.RED);
128 128
         uut.setTabs(tabs);
129 129
         uut.ensureViewIsCreated();
130 130
 
@@ -137,7 +137,7 @@ public class BottomTabsControllerTest extends BaseTest {
137 137
         ArgumentCaptor<ReactComponent> viewCaptor = ArgumentCaptor.forClass(ReactComponent.class);
138 138
         verify(stack, times(1)).applyOptions(optionsCaptor.capture(), viewCaptor.capture());
139 139
         assertThat(viewCaptor.getValue()).isEqualTo(child1.getView());
140
-        assertThat(optionsCaptor.getValue().bottomTabsOptions.color.hasValue()).isFalse();
140
+        assertThat(optionsCaptor.getValue().bottomTabsOptions.tabColor.hasValue()).isFalse();
141 141
     }
142 142
 
143 143
     @Test

+ 7
- 1
playground/src/app.js View File

@@ -31,7 +31,13 @@ function start() {
31 31
               name: 'navigation.playground.WelcomeScreen'
32 32
             }
33 33
           }
34
-        ]
34
+        ],
35
+        options: {
36
+          topBar: {
37
+            visible: false,
38
+            animateHide: false
39
+          }
40
+        }
35 41
       }
36 42
     });
37 43
   });