Bladeren bron

Support setting BottomTab text and icon dynamically with mergeOptions

Guy Carmeli 6 jaren geleden
bovenliggende
commit
e5177fd18d

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/LayoutFactory.java Bestand weergeven

@@ -195,7 +195,7 @@ public class LayoutFactory {
195 195
                 parse(typefaceManager, node.getOptions()),
196 196
                 new Presenter(activity, defaultOptions),
197 197
                 new BottomTabsPresenter(tabs, defaultOptions),
198
-                new BottomTabPresenter(activity, tabs, defaultOptions));
198
+                new BottomTabPresenter(activity, tabs, new ImageLoader(), defaultOptions));
199 199
 	}
200 200
 
201 201
     private ViewController createTopTabs(LayoutNode node) {

+ 24
- 9
lib/android/app/src/main/java/com/reactnativenavigation/presentation/BottomTabPresenter.java Bestand weergeven

@@ -1,11 +1,15 @@
1 1
 package com.reactnativenavigation.presentation;
2 2
 
3 3
 import android.content.Context;
4
+import android.graphics.drawable.Drawable;
5
+import android.support.annotation.NonNull;
4 6
 import android.support.annotation.VisibleForTesting;
5 7
 import android.support.v4.content.ContextCompat;
6 8
 
7 9
 import com.reactnativenavigation.parse.BottomTabOptions;
8 10
 import com.reactnativenavigation.parse.Options;
11
+import com.reactnativenavigation.utils.ImageLoader;
12
+import com.reactnativenavigation.utils.ImageLoadingListenerAdapter;
9 13
 import com.reactnativenavigation.viewcontrollers.ViewController;
10 14
 import com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabFinder;
11 15
 import com.reactnativenavigation.views.BottomTabs;
@@ -14,6 +18,8 @@ import com.reactnativenavigation.views.Component;
14 18
 import java.util.List;
15 19
 
16 20
 public class BottomTabPresenter {
21
+    private final Context context;
22
+    private ImageLoader imageLoader;
17 23
     private Options defaultOptions;
18 24
     private final BottomTabFinder bottomTabFinder;
19 25
     private BottomTabs bottomTabs;
@@ -21,9 +27,11 @@ public class BottomTabPresenter {
21 27
     private final int defaultTextColor;
22 28
     private final List<ViewController> tabs;
23 29
 
24
-    public BottomTabPresenter(Context context, List<ViewController> tabs, Options defaultOptions) {
30
+    public BottomTabPresenter(Context context, List<ViewController> tabs, ImageLoader imageLoader, Options defaultOptions) {
25 31
         this.tabs = tabs;
32
+        this.context = context;
26 33
         this.bottomTabFinder = new BottomTabFinder(tabs);
34
+        this.imageLoader = imageLoader;
27 35
         this.defaultOptions = defaultOptions;
28 36
         defaultSelectedTextColor = defaultOptions.bottomTabOptions.selectedIconColor.get(ContextCompat.getColor(context, com.aurelhubert.ahbottomnavigation.R.color.colorBottomNavigationAccent));
29 37
         defaultTextColor = defaultOptions.bottomTabOptions.iconColor.get(ContextCompat.getColor(context, com.aurelhubert.ahbottomnavigation.R.color.colorBottomNavigationInactive));
@@ -55,14 +63,21 @@ public class BottomTabPresenter {
55 63
     public void mergeChildOptions(Options options, Component child) {
56 64
         int index = bottomTabFinder.findByComponent(child);
57 65
         if (index >= 0) {
58
-            BottomTabOptions withDefaultOptions = options.withDefaultOptions(defaultOptions).bottomTabOptions;
59
-            if (withDefaultOptions.badge.hasValue()) bottomTabs.setBadge(index, withDefaultOptions.badge.get());
60
-            if (withDefaultOptions.badgeColor.hasValue()) bottomTabs.setBadgeColor(withDefaultOptions.badgeColor.get());
61
-            if (withDefaultOptions.fontFamily != null) bottomTabs.setTitleTypeface(index, withDefaultOptions.fontFamily);
62
-            if (withDefaultOptions.selectedIconColor.hasValue()) bottomTabs.setIconActiveColor(index, withDefaultOptions.selectedIconColor.get());
63
-            if (withDefaultOptions.iconColor.hasValue()) bottomTabs.setIconInactiveColor(index, withDefaultOptions.iconColor.get());
64
-            if (withDefaultOptions.selectedTextColor.hasValue()) bottomTabs.setTitleActiveColor(index, withDefaultOptions.selectedTextColor.get());
65
-            if (withDefaultOptions.textColor.hasValue()) bottomTabs.setTitleInactiveColor(index, withDefaultOptions.textColor.get());
66
+            BottomTabOptions bto = options.bottomTabOptions;
67
+            if (bto.badge.hasValue()) bottomTabs.setBadge(index, bto.badge.get());
68
+            if (bto.badgeColor.hasValue()) bottomTabs.setBadgeColor(bto.badgeColor.get());
69
+            if (bto.fontFamily != null) bottomTabs.setTitleTypeface(index, bto.fontFamily);
70
+            if (bto.selectedIconColor.hasValue()) bottomTabs.setIconActiveColor(index, bto.selectedIconColor.get());
71
+            if (bto.iconColor.hasValue()) bottomTabs.setIconInactiveColor(index, bto.iconColor.get());
72
+            if (bto.selectedTextColor.hasValue()) bottomTabs.setTitleActiveColor(index, bto.selectedTextColor.get());
73
+            if (bto.textColor.hasValue()) bottomTabs.setTitleInactiveColor(index, bto.textColor.get());
74
+            if (bto.text.hasValue()) bottomTabs.setText(index, bto.text.get());
75
+            if (bto.icon.hasValue()) imageLoader.loadIcon(context, bto.icon.get(), new ImageLoadingListenerAdapter() {
76
+                @Override
77
+                public void onComplete(@NonNull Drawable drawable) {
78
+                    bottomTabs.setIcon(index, drawable);
79
+                }
80
+            });
66 81
         }
67 82
     }
68 83
 

+ 18
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java Bestand weergeven

@@ -2,10 +2,12 @@ package com.reactnativenavigation.views;
2 2
 
3 3
 import android.annotation.SuppressLint;
4 4
 import android.content.Context;
5
+import android.graphics.drawable.Drawable;
5 6
 import android.support.annotation.ColorInt;
6 7
 import android.support.annotation.IntRange;
7 8
 
8 9
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
10
+import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
9 11
 import com.reactnativenavigation.BuildConfig;
10 12
 import com.reactnativenavigation.parse.params.Text;
11 13
 import com.reactnativenavigation.utils.CompatUtils;
@@ -46,4 +48,20 @@ public class BottomTabs extends AHBottomNavigation {
46 48
     public void setTitleState(TitleState titleState) {
47 49
         if (getTitleState() != titleState) super.setTitleState(titleState);
48 50
     }
51
+
52
+    public void setText(int index, String text) {
53
+        AHBottomNavigationItem item = getItem(index);
54
+        if (!item.getTitle(getContext()).equals(text)) {
55
+            item.setTitle(text);
56
+            refresh();
57
+        }
58
+    }
59
+
60
+    public void setIcon(int index, Drawable icon) {
61
+        AHBottomNavigationItem item = getItem(index);
62
+        if (!item.getDrawable(getContext()).equals(icon)) {
63
+            item.setDrawable(icon);
64
+            refresh();
65
+        }
66
+    }
49 67
 }

+ 8
- 8
playground/src/screens/TextScreen.js Bestand weergeven

@@ -42,6 +42,14 @@ class TextScreen extends Component {
42 42
     );
43 43
   }
44 44
 
45
+  onClickSetBadge() {
46
+    Navigation.mergeOptions(this.props.componentId, {
47
+      bottomTab: {
48
+        badge: `TeSt`
49
+      }
50
+    });
51
+  }
52
+
45 53
   onClickPush = async () => {
46 54
     await Navigation.push(this.props.componentId, {
47 55
       component: {
@@ -63,14 +71,6 @@ class TextScreen extends Component {
63 71
     );
64 72
   }
65 73
 
66
-  onClickSetBadge() {
67
-    Navigation.mergeOptions(this.props.componentId, {
68
-      bottomTab: {
69
-        badge: `TeSt`
70
-      }
71
-    });
72
-  }
73
-
74 74
   onClickSwitchToTab() {
75 75
     Navigation.mergeOptions(this.props.componentId, {
76 76
       bottomTabs: {