Browse Source

BottomTab badge is applied

Guy Carmeli 6 years ago
parent
commit
7dd444cb09

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

@@ -9,10 +9,10 @@ public class BottomTabOptions implements DEFAULT_VALUES {
9 9
         if (json == null) return options;
10 10
 
11 11
         options.title = TextParser.parse(json, "title");
12
-        if (!json.has("icon")) {
13
-            throw new RuntimeException("BottomTab must have an icon");
12
+        if (json.has("icon")) {
13
+            options.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
14 14
         }
15
-        options.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
15
+        options.badge = TextParser.parse(json, "badge");
16 16
         options.testId = TextParser.parse(json, "testID");
17 17
         return options;
18 18
     }
@@ -20,6 +20,7 @@ public class BottomTabOptions implements DEFAULT_VALUES {
20 20
     public Text title = new NullText();
21 21
     public Text icon = new NullText();
22 22
     public Text testId = new NullText();
23
+    public Text badge = new NullText();
23 24
 
24 25
     void mergeWith(final BottomTabOptions other) {
25 26
         if (other.title.hasValue()) {
@@ -28,6 +29,9 @@ public class BottomTabOptions implements DEFAULT_VALUES {
28 29
         if (other.icon.hasValue()) {
29 30
             icon = other.icon;
30 31
         }
32
+        if (other.badge.hasValue()) {
33
+            badge = other.badge;
34
+        }
31 35
     }
32 36
 
33 37
     void mergeWithDefault(final BottomTabOptions defaultOptions) {
@@ -37,5 +41,8 @@ public class BottomTabOptions implements DEFAULT_VALUES {
37 41
         if (!icon.hasValue()) {
38 42
             icon = defaultOptions.icon;
39 43
         }
44
+        if (!badge.hasValue()) {
45
+            badge = defaultOptions.badge;
46
+        }
40 47
     }
41 48
 }

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

@@ -0,0 +1,25 @@
1
+package com.reactnativenavigation.presentation;
2
+
3
+import android.support.annotation.IntRange;
4
+
5
+import com.reactnativenavigation.parse.BottomTabOptions;
6
+import com.reactnativenavigation.parse.Options;
7
+import com.reactnativenavigation.views.BottomTabs;
8
+
9
+public class BottomTabOptionsPresenter {
10
+    private BottomTabs bottomTabs;
11
+
12
+    public BottomTabOptionsPresenter(BottomTabs bottomTabs) {
13
+        this.bottomTabs = bottomTabs;
14
+    }
15
+
16
+    public void present(Options options, @IntRange(from = 0) int bottomTabIndex) {
17
+        applyBottomTabOptions(options.bottomTabOptions, bottomTabIndex);
18
+    }
19
+
20
+    private void applyBottomTabOptions(BottomTabOptions options, int bottomTabIndex) {
21
+        if (options.badge.hasValue()) {
22
+            bottomTabs.setBadge(bottomTabIndex, options.badge);
23
+        }
24
+    }
25
+}

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

@@ -3,6 +3,7 @@ package com.reactnativenavigation.viewcontrollers;
3 3
 import android.app.Activity;
4 4
 import android.graphics.Color;
5 5
 import android.graphics.drawable.Drawable;
6
+import android.support.annotation.IntRange;
6 7
 import android.support.annotation.NonNull;
7 8
 import android.view.ViewGroup;
8 9
 import android.widget.RelativeLayout;
@@ -13,10 +14,12 @@ import com.reactnativenavigation.parse.BottomTabOptions;
13 14
 import com.reactnativenavigation.parse.BottomTabsOptions;
14 15
 import com.reactnativenavigation.parse.Options;
15 16
 import com.reactnativenavigation.parse.Text;
17
+import com.reactnativenavigation.presentation.BottomTabOptionsPresenter;
16 18
 import com.reactnativenavigation.presentation.NavigationOptionsListener;
17 19
 import com.reactnativenavigation.utils.ImageLoader;
18 20
 import com.reactnativenavigation.utils.UiUtils;
19 21
 import com.reactnativenavigation.views.BottomTabs;
22
+import com.reactnativenavigation.views.ReactComponent;
20 23
 
21 24
 import java.util.ArrayList;
22 25
 import java.util.Collection;
@@ -50,7 +53,13 @@ public class BottomTabsController extends ParentController implements AHBottomNa
50 53
 		return root;
51 54
 	}
52 55
 
53
-	@Override
56
+    @Override
57
+    public void applyOptions(Options options, ReactComponent childComponent) {
58
+        int tabIndex = findTabContainingComponent(childComponent);
59
+        if (tabIndex >= 0) new BottomTabOptionsPresenter(bottomTabs).present(options, tabIndex);
60
+    }
61
+
62
+    @Override
54 63
 	public boolean handleBack() {
55 64
 		return !tabs.isEmpty() && tabs.get(bottomTabs.getCurrentItem()).handleBack();
56 65
 	}
@@ -69,6 +78,7 @@ public class BottomTabsController extends ParentController implements AHBottomNa
69 78
 		this.tabs = tabs;
70 79
 		getView();
71 80
 		for (int i = 0; i < tabs.size(); i++) {
81
+		    tabs.get(i).setParentController(this);
72 82
 			createTab(i, tabs.get(i).options.bottomTabOptions, tabs.get(i).options.bottomTabsOptions);
73 83
 		}
74 84
 		selectTabAtIndex(0);
@@ -113,6 +123,7 @@ public class BottomTabsController extends ParentController implements AHBottomNa
113 123
 
114 124
 	@Override
115 125
 	public void mergeOptions(Options options) {
126
+        this.options.mergeWith(options);
116 127
         if (options.bottomTabsOptions.currentTabIndex != NO_INT_VALUE) {
117 128
             selectTabAtIndex(options.bottomTabsOptions.currentTabIndex);
118 129
         }
@@ -153,4 +164,14 @@ public class BottomTabsController extends ParentController implements AHBottomNa
153 164
 		}
154 165
 		return false;
155 166
 	}
167
+
168
+	@IntRange(from = -1)
169
+    private int findTabContainingComponent(ReactComponent component) {
170
+        for (int i = 0; i < tabs.size(); i++) {
171
+            if (tabs.get(i).containsComponent(component)) {
172
+                return i;
173
+            }
174
+        }
175
+        return -1;
176
+    }
156 177
 }

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

@@ -44,6 +44,17 @@ public abstract class ParentController<T extends ViewGroup> extends ViewControll
44 44
 		return null;
45 45
 	}
46 46
 
47
+	@Override
48
+    public boolean containsComponent(ReactComponent component) {
49
+        if (super.containsComponent(component)) {
50
+            return true;
51
+        }
52
+        for (ViewController child : getChildControllers()) {
53
+            if (child.containsComponent(component)) return true;
54
+        }
55
+        return false;
56
+    }
57
+
47 58
     public void applyOptions(Options options, ReactComponent childComponent) {
48 59
 
49 60
     }

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

@@ -37,6 +37,7 @@ public class StackController extends ParentController <StackLayout> {
37 37
     @Override
38 38
     public void applyOptions(Options options, ReactComponent component) {
39 39
         stackLayout.applyOptions(options, component);
40
+        applyOnParentController(parentController -> ((ParentController) parentController).applyOptions(options, component));
40 41
     }
41 42
 
42 43
     @Override

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

@@ -126,6 +126,10 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
126 126
         return isSameId(id) ? this : null;
127 127
     }
128 128
 
129
+    public boolean containsComponent(ReactComponent component) {
130
+        return getView().equals(component);
131
+    }
132
+
129 133
     public void onViewAppeared() {
130 134
         isShown = true;
131 135
         applyOnParentController(parentController -> {

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

@@ -25,4 +25,8 @@ public class BottomTabs extends AHBottomNavigation {
25 25
         if (testId.hasValue()) getViewAtPosition(index).setTag(testId.get());
26 26
         if (testId.hasValue()) getViewAtPosition(index).setContentDescription(testId.get());
27 27
     }
28
+
29
+    public void setBadge(int bottomTabIndex, Text badge) {
30
+        setNotification(badge.get(), bottomTabIndex);
31
+    }
28 32
 }

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

@@ -62,6 +62,15 @@ public class BottomTabsControllerTest extends BaseTest {
62 62
         uut.setTabs(tabs);
63 63
     }
64 64
 
65
+    @Test
66
+    public void setTab_controllerIsSetAsParent() throws Exception {
67
+        List<ViewController> tabs = createTabs();
68
+        uut.setTabs(tabs);
69
+        for (ViewController tab : tabs) {
70
+            assertThat(tab.getParentController()).isEqualTo(uut);
71
+        }
72
+    }
73
+
65 74
     @Test
66 75
     public void setTabs_AddAllViews() throws Exception {
67 76
         List<ViewController> tabs = createTabs();