Browse Source

TopTabs icons (#680)

This commit adds support for icons in TopTabs.
It’s possible to have icons with text, or only icons.
Use topTabIconColor and selectedTopTabIconColor to color the icons
Guy Carmeli 8 years ago
parent
commit
75b5d53f35

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/BaseScreenParams.java View File

1
 package com.reactnativenavigation.params;
1
 package com.reactnativenavigation.params;
2
 
2
 
3
+import android.graphics.drawable.Drawable;
3
 import android.os.Bundle;
4
 import android.os.Bundle;
4
 
5
 
5
 import java.util.List;
6
 import java.util.List;
8
     public String screenId;
9
     public String screenId;
9
     public String title;
10
     public String title;
10
     public String subtitle;
11
     public String subtitle;
12
+    public Drawable tabIcon;
11
     public NavigationParams navigationParams;
13
     public NavigationParams navigationParams;
12
     public List<TitleBarButtonParams> rightButtons;
14
     public List<TitleBarButtonParams> rightButtons;
13
     public TitleBarLeftButtonParams leftButton;
15
     public TitleBarLeftButtonParams leftButton;

+ 0
- 3
android/app/src/main/java/com/reactnativenavigation/params/ScreenParams.java View File

1
 package com.reactnativenavigation.params;
1
 package com.reactnativenavigation.params;
2
 
2
 
3
-import android.graphics.drawable.Drawable;
4
-
5
 import java.util.List;
3
 import java.util.List;
6
 
4
 
7
 public class ScreenParams extends BaseScreenParams {
5
 public class ScreenParams extends BaseScreenParams {
8
     public String tabLabel;
6
     public String tabLabel;
9
-    public Drawable tabIcon;
10
     public List<PageParams> topTabParams;
7
     public List<PageParams> topTabParams;
11
 
8
 
12
     public boolean hasTopTabs() {
9
     public boolean hasTopTabs() {

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

56
     public boolean backButtonHidden;
56
     public boolean backButtonHidden;
57
 
57
 
58
     public Color topTabTextColor;
58
     public Color topTabTextColor;
59
+    public Color topTabIconColor;
59
     public Color selectedTopTabTextColor;
60
     public Color selectedTopTabTextColor;
61
+    public Color selectedTopTabIconColor;
60
     public int selectedTopTabIndicatorHeight;
62
     public int selectedTopTabIndicatorHeight;
61
     public Color selectedTopTabIndicatorColor;
63
     public Color selectedTopTabIndicatorColor;
62
 
64
 

+ 2
- 12
android/app/src/main/java/com/reactnativenavigation/params/parsers/ScreenParamsParser.java View File

1
 package com.reactnativenavigation.params.parsers;
1
 package com.reactnativenavigation.params.parsers;
2
 
2
 
3
-import android.graphics.drawable.Drawable;
4
 import android.os.Bundle;
3
 import android.os.Bundle;
5
 
4
 
6
 import com.reactnativenavigation.params.NavigationParams;
5
 import com.reactnativenavigation.params.NavigationParams;
7
-import com.reactnativenavigation.params.ScreenParams;
8
 import com.reactnativenavigation.params.PageParams;
6
 import com.reactnativenavigation.params.PageParams;
9
-import com.reactnativenavigation.react.ImageLoader;
7
+import com.reactnativenavigation.params.ScreenParams;
10
 
8
 
11
 import java.util.List;
9
 import java.util.List;
12
 
10
 
46
         result.fabParams = ButtonParser.parseFab(params, result.navigationParams.navigatorEventId, result.navigationParams.screenInstanceId);
44
         result.fabParams = ButtonParser.parseFab(params, result.navigationParams.navigatorEventId, result.navigationParams.screenInstanceId);
47
 
45
 
48
         result.tabLabel = getTabLabel(params);
46
         result.tabLabel = getTabLabel(params);
49
-        result.tabIcon = getTabIcon(params);
47
+        result.tabIcon = new TabIconParser(params).parse();
50
 
48
 
51
         result.animateScreenTransitions = params.getBoolean("animated", true);
49
         result.animateScreenTransitions = params.getBoolean("animated", true);
52
 
50
 
53
         return result;
51
         return result;
54
     }
52
     }
55
 
53
 
56
-    private static Drawable getTabIcon(Bundle params) {
57
-        Drawable tabIcon = null;
58
-        if (hasKey(params, "icon")) {
59
-            tabIcon = ImageLoader.loadImage(params.getString("icon"));
60
-        }
61
-        return tabIcon;
62
-    }
63
-
64
     private static String getTabLabel(Bundle params) {
54
     private static String getTabLabel(Bundle params) {
65
         String tabLabel = null;
55
         String tabLabel = null;
66
         if (hasKey(params, "label")) {
56
         if (hasKey(params, "label")) {

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

44
         result.topTabsHidden = getBoolean("topTabsHidden", getDefaultTopTabsHidden());
44
         result.topTabsHidden = getBoolean("topTabsHidden", getDefaultTopTabsHidden());
45
 
45
 
46
         result.topTabTextColor = getColor("topTabTextColor", getDefaultTopTabTextColor());
46
         result.topTabTextColor = getColor("topTabTextColor", getDefaultTopTabTextColor());
47
+        result.topTabIconColor = getColor("topTabIconColor", getDefaultTopTabIconColor());
48
+        result.selectedTopTabIconColor = getColor("selectedTopTabIconColor", getDefaultSelectedTopTabIconColor());
47
         result.selectedTopTabTextColor = getColor("selectedTopTabTextColor", getDefaultSelectedTopTabTextColor());
49
         result.selectedTopTabTextColor = getColor("selectedTopTabTextColor", getDefaultSelectedTopTabTextColor());
48
         result.selectedTopTabIndicatorHeight = getInt("selectedTopTabIndicatorHeight", getDefaultSelectedTopTabIndicatorHeight());
50
         result.selectedTopTabIndicatorHeight = getInt("selectedTopTabIndicatorHeight", getDefaultSelectedTopTabIndicatorHeight());
49
         result.selectedTopTabIndicatorColor = getColor("selectedTopTabIndicatorColor", getDefaultSelectedTopTabIndicatorColor());
51
         result.selectedTopTabIndicatorColor = getColor("selectedTopTabIndicatorColor", getDefaultSelectedTopTabIndicatorColor());
106
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedTopTabTextColor;
108
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedTopTabTextColor;
107
     }
109
     }
108
 
110
 
111
+    private StyleParams.Color getDefaultSelectedTopTabIconColor() {
112
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedTopTabIconColor;
113
+    }
114
+
109
     private StyleParams.Color getDefaultNavigationColor() {
115
     private StyleParams.Color getDefaultNavigationColor() {
110
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.navigationBarColor;
116
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.navigationBarColor;
111
     }
117
     }
158
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.topTabTextColor;
164
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.topTabTextColor;
159
     }
165
     }
160
 
166
 
167
+    private StyleParams.Color getDefaultTopTabIconColor() {
168
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.topTabIconColor;
169
+    }
170
+
161
     private boolean getDefaultBackButtonHidden() {
171
     private boolean getDefaultBackButtonHidden() {
162
         return AppStyle.appStyle != null && AppStyle.appStyle.backButtonHidden;
172
         return AppStyle.appStyle != null && AppStyle.appStyle.backButtonHidden;
163
     }
173
     }

+ 23
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/TabIconParser.java View File

1
+package com.reactnativenavigation.params.parsers;
2
+
3
+import android.graphics.drawable.Drawable;
4
+import android.os.Bundle;
5
+
6
+import com.reactnativenavigation.react.ImageLoader;
7
+
8
+class TabIconParser extends Parser {
9
+
10
+    private Bundle params;
11
+
12
+    TabIconParser(Bundle params) {
13
+        this.params = params;
14
+    }
15
+
16
+    public Drawable parse() {
17
+        Drawable tabIcon = null;
18
+        if (hasKey(params, "icon")) {
19
+            tabIcon = ImageLoader.loadImage(params.getString("icon"));
20
+        }
21
+        return tabIcon;
22
+    }
23
+}

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/params/parsers/TopTabParamsParser.java View File

8
 
8
 
9
 import java.util.List;
9
 import java.util.List;
10
 
10
 
11
-public class TopTabParamsParser extends Parser {
11
+class TopTabParamsParser extends Parser {
12
     private static final String KEY_SCREEN_ID = "screenId";
12
     private static final String KEY_SCREEN_ID = "screenId";
13
     private static final String KEY_TITLE = "title";
13
     private static final String KEY_TITLE = "title";
14
     private static final String NAVIGATION_PARAMS = "navigationParams";
14
     private static final String NAVIGATION_PARAMS = "navigationParams";
28
         PageParams result = new PageParams();
28
         PageParams result = new PageParams();
29
         result.screenId = params.getString(KEY_SCREEN_ID);
29
         result.screenId = params.getString(KEY_SCREEN_ID);
30
         result.title = params.getString(KEY_TITLE);
30
         result.title = params.getString(KEY_TITLE);
31
+        result.tabIcon = new TabIconParser(params).parse();
31
         result.navigationParams = new NavigationParams(params.getBundle(NAVIGATION_PARAMS));
32
         result.navigationParams = new NavigationParams(params.getBundle(NAVIGATION_PARAMS));
32
         result.leftButton = ButtonParser.parseLeftButton(params);
33
         result.leftButton = ButtonParser.parseLeftButton(params);
33
         result.rightButtons = ButtonParser.parseRightButton(params);
34
         result.rightButtons = ButtonParser.parseRightButton(params);

+ 14
- 4
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java View File

5
 import android.support.v4.view.ViewPager;
5
 import android.support.v4.view.ViewPager;
6
 import android.support.v7.app.AppCompatActivity;
6
 import android.support.v7.app.AppCompatActivity;
7
 
7
 
8
-import com.reactnativenavigation.events.Event;
9
-import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
10
 import com.reactnativenavigation.params.BaseScreenParams;
8
 import com.reactnativenavigation.params.BaseScreenParams;
11
 import com.reactnativenavigation.params.PageParams;
9
 import com.reactnativenavigation.params.PageParams;
12
 import com.reactnativenavigation.params.ScreenParams;
10
 import com.reactnativenavigation.params.ScreenParams;
13
 import com.reactnativenavigation.views.ContentView;
11
 import com.reactnativenavigation.views.ContentView;
14
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
12
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
13
+import com.reactnativenavigation.views.TopTabs;
15
 
14
 
16
 import java.util.ArrayList;
15
 import java.util.ArrayList;
17
 import java.util.List;
16
 import java.util.List;
35
 
34
 
36
     @Override
35
     @Override
37
     protected void createContent() {
36
     protected void createContent() {
38
-        TabLayout tabLayout = topBar.initTabs();
37
+        TopTabs topTabs = topBar.initTabs();
39
         createViewPager();
38
         createViewPager();
40
         addPages();
39
         addPages();
41
-        setupViewPager(tabLayout);
40
+        setupViewPager(topTabs);
41
+        setTopTabIcons(topTabs);
42
     }
42
     }
43
 
43
 
44
     private void createViewPager() {
44
     private void createViewPager() {
79
         tabLayout.setupWithViewPager(viewPager);
79
         tabLayout.setupWithViewPager(viewPager);
80
     }
80
     }
81
 
81
 
82
+    private void setTopTabIcons(TopTabs topTabs) {
83
+        for (int i = 0; i < topTabs.getTabCount(); i++) {
84
+            PageParams pageParams = screenParams.topTabParams.get(i);
85
+            if (pageParams.tabIcon != null) {
86
+                topTabs.getTabAt(i).setIcon(pageParams.tabIcon);
87
+            }
88
+        }
89
+        topTabs.setTopTabsIconColor(screenParams.styleParams);
90
+    }
91
+
82
     private void addContent(ContentView contentView) {
92
     private void addContent(ContentView contentView) {
83
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
93
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
84
         viewPager.addView(contentView, params);
94
         viewPager.addView(contentView, params);

+ 1
- 2
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

4
 import android.graphics.Color;
4
 import android.graphics.Color;
5
 import android.os.Build;
5
 import android.os.Build;
6
 import android.support.design.widget.AppBarLayout;
6
 import android.support.design.widget.AppBarLayout;
7
-import android.support.design.widget.TabLayout;
8
 import android.view.ViewGroup;
7
 import android.view.ViewGroup;
9
 import android.widget.FrameLayout;
8
 import android.widget.FrameLayout;
10
 
9
 
93
         titleBar.setRightButtons(titleBarButtons, navigatorEventId);
92
         titleBar.setRightButtons(titleBarButtons, navigatorEventId);
94
     }
93
     }
95
 
94
 
96
-    public TabLayout initTabs() {
95
+    public TopTabs initTabs() {
97
         topTabs = new TopTabs(getContext());
96
         topTabs = new TopTabs(getContext());
98
         addView(topTabs);
97
         addView(topTabs);
99
         return topTabs;
98
         return topTabs;

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/views/TopTabs.java View File

5
 import android.support.design.widget.TabLayout;
5
 import android.support.design.widget.TabLayout;
6
 
6
 
7
 import com.reactnativenavigation.params.StyleParams;
7
 import com.reactnativenavigation.params.StyleParams;
8
+import com.reactnativenavigation.views.utils.TopTabsIconColorHelper;
8
 
9
 
9
 public class TopTabs extends TabLayout {
10
 public class TopTabs extends TabLayout {
10
 
11
 
37
 
38
 
38
         setTabTextColors(tabTextColor, selectedTabColor);
39
         setTabTextColors(tabTextColor, selectedTabColor);
39
     }
40
     }
41
+
42
+    public void setTopTabsIconColor(StyleParams style) {
43
+        new TopTabsIconColorHelper(this, style).colorIcons(getSelectedIconColor(), getUnselectedIconColor());
44
+    }
45
+
46
+    private int getSelectedIconColor() {
47
+        ColorStateList originalTabColors = getTabTextColors();
48
+        return originalTabColors != null ? originalTabColors.getColorForState(TabLayout.SELECTED_STATE_SET, -1) : -1;
49
+    }
50
+
51
+    private int getUnselectedIconColor() {
52
+        ColorStateList originalTabColors = getTabTextColors();
53
+        return originalTabColors != null ? originalTabColors.getColorForState(TabLayout.SELECTED_STATE_SET, -1) : -1;
54
+    }
40
 }
55
 }

+ 65
- 0
android/app/src/main/java/com/reactnativenavigation/views/utils/TopTabsIconColorHelper.java View File

1
+package com.reactnativenavigation.views.utils;
2
+
3
+import android.content.res.ColorStateList;
4
+import android.graphics.drawable.Drawable;
5
+import android.support.v4.graphics.drawable.DrawableCompat;
6
+
7
+import com.reactnativenavigation.params.StyleParams;
8
+import com.reactnativenavigation.views.TopTabs;
9
+
10
+public class TopTabsIconColorHelper {
11
+    private final TopTabs topTabs;
12
+    private final StyleParams styleParams;
13
+
14
+    public TopTabsIconColorHelper(TopTabs topTabs, StyleParams styleParams) {
15
+        this.topTabs = topTabs;
16
+        this.styleParams = styleParams;
17
+    }
18
+
19
+    public void colorIcons(int selectedIconColor, int unselectedIconColor) {
20
+        ColorStateList colorStateList = getIconColorStateList(styleParams, selectedIconColor, unselectedIconColor);
21
+        colorIcons(colorStateList);
22
+    }
23
+
24
+    private ColorStateList getIconColorStateList(StyleParams style, int selectedIconColor, int unselectedIconColor) {
25
+        int selectedColor = selectedIconColor;
26
+        int unselectedColor = unselectedIconColor;
27
+        if (style.topTabIconColor.hasColor()) {
28
+            unselectedColor = style.topTabIconColor.getColor();
29
+        }
30
+        if (style.selectedTopTabIconColor.hasColor()) {
31
+            selectedColor = style.selectedTopTabIconColor.getColor();
32
+        }
33
+        return createColorStateList(selectedColor, unselectedColor);
34
+    }
35
+
36
+    private ColorStateList createColorStateList(int selectedColor, int unselectedIconColor) {
37
+        int[][] states = new int[][] {
38
+                new int[]{android.R.attr.state_pressed},
39
+                new int[]{android.R.attr.state_selected},
40
+                new int[]{android.R.attr.state_enabled},
41
+                new int[]{android.R.attr.state_focused, android.R.attr.state_pressed},
42
+                new int[]{-android.R.attr.state_enabled},
43
+                new int[]{}
44
+        };
45
+        int[] colors = new int[]{
46
+                selectedColor,
47
+                selectedColor,
48
+                unselectedIconColor,
49
+                unselectedIconColor,
50
+                unselectedIconColor,
51
+                unselectedIconColor
52
+        };
53
+        return new ColorStateList(states, colors);
54
+    }
55
+
56
+    private void colorIcons(ColorStateList colorStateList) {
57
+        for (int i = 0; i < topTabs.getTabCount(); i++) {
58
+            Drawable icon = topTabs.getTabAt(i).getIcon();
59
+            if (icon != null) {
60
+                icon = DrawableCompat.wrap(icon);
61
+                DrawableCompat.setTintList(icon, colorStateList);
62
+            }
63
+        }
64
+    }
65
+}

+ 3
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

43
       tab.navigatorID = navigatorID;
43
       tab.navigatorID = navigatorID;
44
     }
44
     }
45
     tab.screen = tab.screenId;
45
     tab.screen = tab.screenId;
46
+    addTabIcon(tab);
46
     addNavigatorButtons(tab);
47
     addNavigatorButtons(tab);
47
     adaptNavigationParams(tab);
48
     adaptNavigationParams(tab);
48
     addNavigationStyleParams(tab);
49
     addNavigationStyleParams(tab);
139
     drawBelowTopBar: !originalStyleObject.drawUnderNavBar,
140
     drawBelowTopBar: !originalStyleObject.drawUnderNavBar,
140
 
141
 
141
     topTabTextColor: processColor(originalStyleObject.topTabTextColor),
142
     topTabTextColor: processColor(originalStyleObject.topTabTextColor),
143
+    topTabIconColor: processColor(originalStyleObject.topTabIconColor),
144
+    selectedTopTabIconColor: processColor(originalStyleObject.selectedTopTabIconColor),
142
     selectedTopTabTextColor: processColor(originalStyleObject.selectedTopTabTextColor),
145
     selectedTopTabTextColor: processColor(originalStyleObject.selectedTopTabTextColor),
143
     selectedTopTabIndicatorHeight: originalStyleObject.selectedTopTabIndicatorHeight,
146
     selectedTopTabIndicatorHeight: originalStyleObject.selectedTopTabIndicatorHeight,
144
     selectedTopTabIndicatorColor: processColor(originalStyleObject.selectedTopTabIndicatorColor),
147
     selectedTopTabIndicatorColor: processColor(originalStyleObject.selectedTopTabIndicatorColor),