Sfoglia il codice sorgente

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 anni fa
parent
commit
75b5d53f35

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

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

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

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

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

@@ -56,7 +56,9 @@ public class StyleParams {
56 56
     public boolean backButtonHidden;
57 57
 
58 58
     public Color topTabTextColor;
59
+    public Color topTabIconColor;
59 60
     public Color selectedTopTabTextColor;
61
+    public Color selectedTopTabIconColor;
60 62
     public int selectedTopTabIndicatorHeight;
61 63
     public Color selectedTopTabIndicatorColor;
62 64
 

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

@@ -1,12 +1,10 @@
1 1
 package com.reactnativenavigation.params.parsers;
2 2
 
3
-import android.graphics.drawable.Drawable;
4 3
 import android.os.Bundle;
5 4
 
6 5
 import com.reactnativenavigation.params.NavigationParams;
7
-import com.reactnativenavigation.params.ScreenParams;
8 6
 import com.reactnativenavigation.params.PageParams;
9
-import com.reactnativenavigation.react.ImageLoader;
7
+import com.reactnativenavigation.params.ScreenParams;
10 8
 
11 9
 import java.util.List;
12 10
 
@@ -46,21 +44,13 @@ public class ScreenParamsParser extends Parser {
46 44
         result.fabParams = ButtonParser.parseFab(params, result.navigationParams.navigatorEventId, result.navigationParams.screenInstanceId);
47 45
 
48 46
         result.tabLabel = getTabLabel(params);
49
-        result.tabIcon = getTabIcon(params);
47
+        result.tabIcon = new TabIconParser(params).parse();
50 48
 
51 49
         result.animateScreenTransitions = params.getBoolean("animated", true);
52 50
 
53 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 54
     private static String getTabLabel(Bundle params) {
65 55
         String tabLabel = null;
66 56
         if (hasKey(params, "label")) {

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

@@ -44,6 +44,8 @@ public class StyleParamsParser {
44 44
         result.topTabsHidden = getBoolean("topTabsHidden", getDefaultTopTabsHidden());
45 45
 
46 46
         result.topTabTextColor = getColor("topTabTextColor", getDefaultTopTabTextColor());
47
+        result.topTabIconColor = getColor("topTabIconColor", getDefaultTopTabIconColor());
48
+        result.selectedTopTabIconColor = getColor("selectedTopTabIconColor", getDefaultSelectedTopTabIconColor());
47 49
         result.selectedTopTabTextColor = getColor("selectedTopTabTextColor", getDefaultSelectedTopTabTextColor());
48 50
         result.selectedTopTabIndicatorHeight = getInt("selectedTopTabIndicatorHeight", getDefaultSelectedTopTabIndicatorHeight());
49 51
         result.selectedTopTabIndicatorColor = getColor("selectedTopTabIndicatorColor", getDefaultSelectedTopTabIndicatorColor());
@@ -106,6 +108,10 @@ public class StyleParamsParser {
106 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 115
     private StyleParams.Color getDefaultNavigationColor() {
110 116
         return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.navigationBarColor;
111 117
     }
@@ -158,6 +164,10 @@ public class StyleParamsParser {
158 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 171
     private boolean getDefaultBackButtonHidden() {
162 172
         return AppStyle.appStyle != null && AppStyle.appStyle.backButtonHidden;
163 173
     }

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

@@ -0,0 +1,23 @@
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 Vedi File

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

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

@@ -5,13 +5,12 @@ import android.support.design.widget.TabLayout;
5 5
 import android.support.v4.view.ViewPager;
6 6
 import android.support.v7.app.AppCompatActivity;
7 7
 
8
-import com.reactnativenavigation.events.Event;
9
-import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
10 8
 import com.reactnativenavigation.params.BaseScreenParams;
11 9
 import com.reactnativenavigation.params.PageParams;
12 10
 import com.reactnativenavigation.params.ScreenParams;
13 11
 import com.reactnativenavigation.views.ContentView;
14 12
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
13
+import com.reactnativenavigation.views.TopTabs;
15 14
 
16 15
 import java.util.ArrayList;
17 16
 import java.util.List;
@@ -35,10 +34,11 @@ public class ViewPagerScreen extends Screen {
35 34
 
36 35
     @Override
37 36
     protected void createContent() {
38
-        TabLayout tabLayout = topBar.initTabs();
37
+        TopTabs topTabs = topBar.initTabs();
39 38
         createViewPager();
40 39
         addPages();
41
-        setupViewPager(tabLayout);
40
+        setupViewPager(topTabs);
41
+        setTopTabIcons(topTabs);
42 42
     }
43 43
 
44 44
     private void createViewPager() {
@@ -79,6 +79,16 @@ public class ViewPagerScreen extends Screen {
79 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 92
     private void addContent(ContentView contentView) {
83 93
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
84 94
         viewPager.addView(contentView, params);

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

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

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

@@ -5,6 +5,7 @@ import android.content.res.ColorStateList;
5 5
 import android.support.design.widget.TabLayout;
6 6
 
7 7
 import com.reactnativenavigation.params.StyleParams;
8
+import com.reactnativenavigation.views.utils.TopTabsIconColorHelper;
8 9
 
9 10
 public class TopTabs extends TabLayout {
10 11
 
@@ -37,4 +38,18 @@ public class TopTabs extends TabLayout {
37 38
 
38 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 Vedi File

@@ -0,0 +1,65 @@
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 Vedi File

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