Browse Source

Support selectedIcon options on Android

Guy Carmeli 5 years ago
parent
commit
45e8389b2b

+ 1
- 1
lib/android/app/build.gradle View File

97
     implementation 'androidx.annotation:annotation:1.1.0'
97
     implementation 'androidx.annotation:annotation:1.1.0'
98
     implementation 'com.google.android.material:material:1.1.0-alpha08'
98
     implementation 'com.google.android.material:material:1.1.0-alpha08'
99
 
99
 
100
-    implementation 'com.github.wix-playground:ahbottomnavigation:3.0.2'
100
+    implementation 'com.github.wix-playground:ahbottomnavigation:3.0.5'
101
     implementation 'com.github.wix-playground:reflow-animator:1.0.4'
101
     implementation 'com.github.wix-playground:reflow-animator:1.0.4'
102
     implementation 'com.github.clans:fab:1.6.4'
102
     implementation 'com.github.clans:fab:1.6.4'
103
 
103
 

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

30
         options.textColor = ColorParser.parse(json, "textColor");
30
         options.textColor = ColorParser.parse(json, "textColor");
31
         options.selectedTextColor = ColorParser.parse(json, "selectedTextColor");
31
         options.selectedTextColor = ColorParser.parse(json, "selectedTextColor");
32
         if (json.has("icon")) options.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
32
         if (json.has("icon")) options.icon = TextParser.parse(json.optJSONObject("icon"), "uri");
33
+        if (json.has("selectedIcon")) options.selectedIcon = TextParser.parse(json.optJSONObject("selectedIcon"), "uri");
33
         options.iconColor = ColorParser.parse(json, "iconColor");
34
         options.iconColor = ColorParser.parse(json, "iconColor");
34
         options.selectedIconColor = ColorParser.parse(json, "selectedIconColor");
35
         options.selectedIconColor = ColorParser.parse(json, "selectedIconColor");
35
         options.badge = TextParser.parse(json, "badge");
36
         options.badge = TextParser.parse(json, "badge");
47
     public Colour textColor = new NullColor();
48
     public Colour textColor = new NullColor();
48
     public Colour selectedTextColor = new NullColor();
49
     public Colour selectedTextColor = new NullColor();
49
     public Text icon = new NullText();
50
     public Text icon = new NullText();
51
+    public Text selectedIcon = new NullText();
50
     public Colour iconColor = new NullColor();
52
     public Colour iconColor = new NullColor();
51
     public Colour selectedIconColor = new NullColor();
53
     public Colour selectedIconColor = new NullColor();
52
     public Text testId = new NullText();
54
     public Text testId = new NullText();
64
         if (other.textColor.hasValue()) textColor = other.textColor;
66
         if (other.textColor.hasValue()) textColor = other.textColor;
65
         if (other.selectedTextColor.hasValue()) selectedTextColor = other.selectedTextColor;
67
         if (other.selectedTextColor.hasValue()) selectedTextColor = other.selectedTextColor;
66
         if (other.icon.hasValue()) icon = other.icon;
68
         if (other.icon.hasValue()) icon = other.icon;
69
+        if (other.selectedIcon.hasValue()) selectedIcon = other.selectedIcon;
67
         if (other.iconColor.hasValue()) iconColor = other.iconColor;
70
         if (other.iconColor.hasValue()) iconColor = other.iconColor;
68
         if (other.selectedIconColor.hasValue()) selectedIconColor = other.selectedIconColor;
71
         if (other.selectedIconColor.hasValue()) selectedIconColor = other.selectedIconColor;
69
         if (other.badge.hasValue()) badge = other.badge;
72
         if (other.badge.hasValue()) badge = other.badge;
81
         if (!textColor.hasValue()) textColor = defaultOptions.textColor;
84
         if (!textColor.hasValue()) textColor = defaultOptions.textColor;
82
         if (!selectedTextColor.hasValue()) selectedTextColor = defaultOptions.selectedTextColor;
85
         if (!selectedTextColor.hasValue()) selectedTextColor = defaultOptions.selectedTextColor;
83
         if (!icon.hasValue()) icon = defaultOptions.icon;
86
         if (!icon.hasValue()) icon = defaultOptions.icon;
87
+        if (!selectedIcon.hasValue()) selectedIcon = defaultOptions.selectedIcon;
84
         if (!iconColor.hasValue()) iconColor = defaultOptions.iconColor;
88
         if (!iconColor.hasValue()) iconColor = defaultOptions.iconColor;
85
         if (!selectedIconColor.hasValue()) selectedIconColor = defaultOptions.selectedIconColor;
89
         if (!selectedIconColor.hasValue()) selectedIconColor = defaultOptions.selectedIconColor;
86
         if (!badge.hasValue()) badge = defaultOptions.badge;
90
         if (!badge.hasValue()) badge = defaultOptions.badge;

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

89
                         bottomTabs.setIcon(index, drawable);
89
                         bottomTabs.setIcon(index, drawable);
90
                     }
90
                     }
91
                 });
91
                 });
92
+                if (tab.selectedIcon.hasValue()) imageLoader.loadIcon(context, tab.selectedIcon.get(), new ImageLoadingListenerAdapter() {
93
+                    @Override
94
+                    public void onComplete(@NonNull Drawable drawable) {
95
+                        bottomTabs.setSelectedIcon(index, drawable);
96
+                    }
97
+                });
92
                 if (tab.testId.hasValue()) bottomTabs.setTag(index, tab.testId.get());
98
                 if (tab.testId.hasValue()) bottomTabs.setTag(index, tab.testId.get());
93
                 if (shouldApplyDot(tab)) mergeDotIndicator(index, tab.dotIndicator); else mergeBadge(index, tab);
99
                 if (shouldApplyDot(tab)) mergeDotIndicator(index, tab.dotIndicator); else mergeBadge(index, tab);
94
             }
100
             }

+ 6
- 4
lib/android/app/src/main/java/com/reactnativenavigation/utils/ImageLoader.java View File

7
 import android.graphics.drawable.Drawable;
7
 import android.graphics.drawable.Drawable;
8
 import android.net.Uri;
8
 import android.net.Uri;
9
 import android.os.StrictMode;
9
 import android.os.StrictMode;
10
-import androidx.annotation.NonNull;
11
-import androidx.annotation.Nullable;
12
 
10
 
13
 import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
11
 import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
14
 import com.reactnativenavigation.NavigationApplication;
12
 import com.reactnativenavigation.NavigationApplication;
20
 import java.util.ArrayList;
18
 import java.util.ArrayList;
21
 import java.util.List;
19
 import java.util.List;
22
 
20
 
21
+import androidx.annotation.NonNull;
22
+import androidx.annotation.Nullable;
23
+
23
 public class ImageLoader {
24
 public class ImageLoader {
24
 
25
 
25
     public interface ImagesLoadingListener {
26
     public interface ImagesLoadingListener {
33
     private static final String FILE_SCHEME = "file";
34
     private static final String FILE_SCHEME = "file";
34
 
35
 
35
     @Nullable
36
     @Nullable
36
-    public Drawable loadIcon(Context context, String uri) {
37
+    public Drawable loadIcon(Context context, @Nullable String uri) {
38
+        if (uri == null) return null;
37
         try {
39
         try {
38
             return getDrawable(context, uri);
40
             return getDrawable(context, uri);
39
         } catch (IOException e) {
41
         } catch (IOException e) {
64
     }
66
     }
65
 
67
 
66
     @NonNull
68
     @NonNull
67
-    private Drawable getDrawable(Context context, String source) throws IOException {
69
+    private Drawable getDrawable(Context context, @NonNull String source) throws IOException {
68
         Drawable drawable;
70
         Drawable drawable;
69
         if (isLocalFile(Uri.parse(source))) {
71
         if (isLocalFile(Uri.parse(source))) {
70
             drawable = loadFile(source);
72
             drawable = loadFile(source);

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

159
             return new AHBottomNavigationItem(
159
             return new AHBottomNavigationItem(
160
                     options.text.get(""),
160
                     options.text.get(""),
161
                     imageLoader.loadIcon(getActivity(), options.icon.get()),
161
                     imageLoader.loadIcon(getActivity(), options.icon.get()),
162
+                    imageLoader.loadIcon(getActivity(), options.selectedIcon.get(null)),
162
                     options.testId.get("")
163
                     options.testId.get("")
163
             );
164
             );
164
         });
165
         });

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

75
     public void setIcon(int index, Drawable icon) {
75
     public void setIcon(int index, Drawable icon) {
76
         AHBottomNavigationItem item = getItem(index);
76
         AHBottomNavigationItem item = getItem(index);
77
         if (!item.getDrawable(getContext()).equals(icon)) {
77
         if (!item.getDrawable(getContext()).equals(icon)) {
78
-            item.setDrawable(icon);
78
+            item.setIcon(icon);
79
+            refresh();
80
+        }
81
+    }
82
+
83
+    public void setSelectedIcon(int index, Drawable icon) {
84
+        AHBottomNavigationItem item = getItem(index);
85
+        if (!item.getDrawable(getContext()).equals(icon)) {
86
+            item.setSelectedIcon(icon);
79
             refresh();
87
             refresh();
80
         }
88
         }
81
     }
89
     }

BIN
playground/img/layouts@2x.android.png View File


BIN
playground/img/layouts@2x.ios.png View File


BIN
playground/img/layouts_selected@2x.android.png View File


BIN
playground/img/layouts_selected@2x.ios.png View File


BIN
playground/img/options@2x.android.png View File


BIN
playground/img/options@2x.ios.png View File


BIN
playground/img/options_selected@2x.android.png View File


BIN
playground/img/options_selected@2x.ios.png View File


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

49
                   bottomTab: {
49
                   bottomTab: {
50
                     text: 'Layouts',
50
                     text: 'Layouts',
51
                     icon: require('../img/layouts.png'),
51
                     icon: require('../img/layouts.png'),
52
-                    fontSize: 10,
52
+                    selectedIcon: require('../img/layouts_selected.png'),
53
                     testID: testIDs.LAYOUTS_TAB
53
                     testID: testIDs.LAYOUTS_TAB
54
                   }
54
                   }
55
                 }
55
                 }
73
                   bottomTab: {
73
                   bottomTab: {
74
                     text: 'Options',
74
                     text: 'Options',
75
                     icon: require('../img/options.png'),
75
                     icon: require('../img/options.png'),
76
+                    selectedIcon: require('../img/options_selected.png'),
76
                     testID: testIDs.OPTIONS_TAB
77
                     testID: testIDs.OPTIONS_TAB
77
                   }
78
                   }
78
                 }
79
                 }