Browse Source

Support changing TopTabs font size (#2449)

* Currently doesn't work - need to implement custom TopTab view
Guy Carmeli 6 years ago
parent
commit
9f1337c2a1
No account linked to committer's email address
23 changed files with 127 additions and 63 deletions
  1. 1
    0
      docs/_sidebar.md
  2. 1
    0
      docs/docs/Container.md
  3. 3
    3
      docs/docs/Navigation.md
  4. 1
    0
      docs/docs/options/BottomTabs.md
  5. 1
    0
      docs/docs/options/NavigationOptions.md
  6. 1
    0
      docs/docs/options/TopBar.md
  7. 10
    0
      docs/docs/options/TopTabs.md
  8. 16
    0
      lib/android/app/src/main/java/com/reactnativenavigation/parse/Color.java
  9. 0
    3
      lib/android/app/src/main/java/com/reactnativenavigation/parse/ColorParser.java
  10. 4
    4
      lib/android/app/src/main/java/com/reactnativenavigation/parse/NullColor.java
  11. 12
    0
      lib/android/app/src/main/java/com/reactnativenavigation/parse/NullNumber.java
  12. 8
    0
      lib/android/app/src/main/java/com/reactnativenavigation/parse/Number.java
  13. 9
    0
      lib/android/app/src/main/java/com/reactnativenavigation/parse/NumberParser.java
  14. 20
    0
      lib/android/app/src/main/java/com/reactnativenavigation/parse/Param.java
  15. 8
    15
      lib/android/app/src/main/java/com/reactnativenavigation/parse/TopTabsOptions.java
  16. 1
    0
      lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java
  17. 6
    1
      lib/android/app/src/main/java/com/reactnativenavigation/views/TopBar.java
  18. 6
    1
      lib/android/app/src/main/java/com/reactnativenavigation/views/TopTabs.java
  19. 12
    4
      lib/android/app/src/main/java/com/reactnativenavigation/views/TopTabsStyleHelper.java
  20. 0
    30
      lib/android/app/src/main/java/com/reactnativenavigation/views/style/Color.java
  21. 2
    0
      lib/src/params/options/TopTabs.js
  22. 3
    1
      lib/src/params/options/TopTabs.test.js
  23. 2
    1
      playground/src/containers/WelcomeScreen.js

+ 1
- 0
docs/_sidebar.md View File

@@ -11,6 +11,7 @@
11 11
 - Options
12 12
  - [NavigationOptions](/docs/options/NavigationOptions)
13 13
  - [TopBar](/docs/options/TopBar)
14
+ - [TopTabs](/docs/options/TopTabs)
14 15
  - [TopTab](/docs/options/TopTab)
15 16
  - [Button](/docs/options/Button)
16 17
  - [BottomTabs](/docs/options/BottomTabs)

+ 1
- 0
docs/docs/Container.md View File

@@ -5,6 +5,7 @@
5 5
 | Name | Type | Description |
6 6
 | --- | --- | --- |
7 7
 | name | <code>string</code> | The container's registered name |
8
+| topTabs | [<code>Array.&lt;Container&gt;</code>](#Container) |  |
8 9
 | passProps | <code>object</code> | props |
9 10
 | navigationOptions | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/NavigationOptions">NavigationOptions</a> |  |
10 11
 

+ 3
- 3
docs/docs/Navigation.md View File

@@ -10,7 +10,7 @@
10 10
     * [.showModal(params)](#Navigation+showModal)
11 11
     * [.dismissModal(containerId)](#Navigation+dismissModal)
12 12
     * [.dismissAllModals()](#Navigation+dismissAllModals)
13
-    * [.push(containerId, params)](#Navigation+push)
13
+    * [.push(containerId, container)](#Navigation+push)
14 14
     * [.pop(containerId, params)](#Navigation+pop)
15 15
     * [.popTo(containerId)](#Navigation+popTo)
16 16
     * [.popToRoot(containerId)](#Navigation+popToRoot)
@@ -109,14 +109,14 @@ Dismiss all Modals
109 109
 
110 110
 <a name="Navigation+push"></a>
111 111
 
112
-## navigation.push(containerId, params)
112
+## navigation.push(containerId, container)
113 113
 Push a new screen into this screen's navigation stack.
114 114
 
115 115
 
116 116
 | Param | Type | Description |
117 117
 | --- | --- | --- |
118 118
 | containerId | <code>string</code> | The container's id. |
119
-| params | <code>*</code> |  |
119
+| container | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/Container">Container</a> |  |
120 120
 
121 121
 
122 122
 * * *

+ 1
- 0
docs/docs/options/BottomTabs.md View File

@@ -9,4 +9,5 @@
9 9
 | hidden | <code>boolean</code> | 
10 10
 | animateHide | <code>boolean</code> | 
11 11
 | testID | <code>string</code> | 
12
+| drawUnder | <code>boolean</code> | 
12 13
 

+ 1
- 0
docs/docs/options/NavigationOptions.md View File

@@ -8,6 +8,7 @@
8 8
 | bottomTabs | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/options/BottomTabs">BottomTabs</a> | 
9 9
 | bottomTab | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/options/BottomTab">BottomTab</a> | 
10 10
 | orientation | <code>string</code> | 
11
+| topTabs | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/options/TopTabs">TopTabs</a> | 
11 12
 | rightButtons | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/options/Button">Button[]</a> | 
12 13
 | leftButtons | <a href="https://wix.github.io/react-native-navigation/v2/#/docs/options/Button">Button[]</a> | 
13 14
 

+ 1
- 0
docs/docs/options/TopBar.md View File

@@ -19,4 +19,5 @@
19 19
 | blur | <code>boolean</code> | 
20 20
 | noBorder | <code>boolean</code> | 
21 21
 | largeTitle | <code>boolean</code> | 
22
+| drawUnder | <code>boolean</code> | 
22 23
 

+ 10
- 0
docs/docs/options/TopTabs.md View File

@@ -0,0 +1,10 @@
1
+<h1>TopTabs</h1>
2
+
3
+**Properties**
4
+
5
+| Name | Type | Description |
6
+| --- | --- | --- |
7
+| selectedTabColor | <code>string</code> | Selected tab color |
8
+| unselectedTabColor | <code>string</code> | Unselected tab color |
9
+| fontSize | <code>int</code> |  |
10
+

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

@@ -0,0 +1,16 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import android.support.annotation.ColorInt;
4
+
5
+public class Color extends Param<Integer>{
6
+
7
+    public Color(@ColorInt int color) {
8
+        super(color);
9
+    }
10
+
11
+    @SuppressWarnings("MagicNumber")
12
+    @Override
13
+    public String toString() {
14
+        return String.format("#%06X", (0xFFFFFF & get()));
15
+    }
16
+}

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

@@ -1,8 +1,5 @@
1 1
 package com.reactnativenavigation.parse;
2 2
 
3
-import com.reactnativenavigation.views.style.Color;
4
-import com.reactnativenavigation.views.style.NullColor;
5
-
6 3
 import org.json.JSONObject;
7 4
 
8 5
 public class ColorParser {

lib/android/app/src/main/java/com/reactnativenavigation/views/style/NullColor.java → lib/android/app/src/main/java/com/reactnativenavigation/parse/NullColor.java View File

@@ -1,16 +1,16 @@
1
-package com.reactnativenavigation.views.style;
1
+package com.reactnativenavigation.parse;
2 2
 
3 3
 public class NullColor extends Color {
4 4
 
5
-    public NullColor() {
5
+    NullColor() {
6 6
         super(0);
7 7
     }
8 8
 
9
-    public boolean hasColor() {
9
+    @Override
10
+    public boolean hasValue() {
10 11
         return false;
11 12
     }
12 13
 
13
-    @SuppressWarnings("MagicNumber")
14 14
     @Override
15 15
     public String toString() {
16 16
         return "Null Color";

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

@@ -0,0 +1,12 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+public class NullNumber extends Number {
4
+    public NullNumber() {
5
+        super(0);
6
+    }
7
+
8
+    @Override
9
+    public boolean hasValue() {
10
+        return false;
11
+    }
12
+}

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

@@ -0,0 +1,8 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+public class Number extends Param<Integer> {
4
+
5
+    public Number(int value) {
6
+        super(value);
7
+    }
8
+}

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

@@ -0,0 +1,9 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import org.json.JSONObject;
4
+
5
+public class NumberParser {
6
+    public static Number parse(JSONObject json, String number) {
7
+        return json.has(number) ? new Number(json.optInt(number)) : new NullNumber();
8
+    }
9
+}

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

@@ -0,0 +1,20 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+public abstract class Param<T> {
4
+    protected T value;
5
+
6
+    public Param(T value) {
7
+        this.value = value;
8
+    }
9
+
10
+    public T get() {
11
+        if (hasValue()) {
12
+            return value;
13
+        }
14
+        throw new RuntimeException("Tried to get null value!");
15
+    }
16
+
17
+    public boolean hasValue() {
18
+        return value != null;
19
+    }
20
+}

+ 8
- 15
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopTabsOptions.java View File

@@ -3,39 +3,32 @@ package com.reactnativenavigation.parse;
3 3
 import android.support.annotation.NonNull;
4 4
 import android.support.annotation.Nullable;
5 5
 
6
-import com.reactnativenavigation.views.style.Color;
7
-import com.reactnativenavigation.views.style.NullColor;
8
-
9 6
 import org.json.JSONObject;
10 7
 
11 8
 public class TopTabsOptions implements DEFAULT_VALUES {
12 9
 
13 10
     @NonNull public Color selectedTabColor = new NullColor();
14 11
     @NonNull public Color unselectedTabColor = new NullColor();
12
+    @NonNull public Number fontSize = new NullNumber();
15 13
 
16 14
     public static TopTabsOptions parse(@Nullable JSONObject json) {
17 15
         TopTabsOptions result = new TopTabsOptions();
18 16
         if (json == null) return result;
19 17
         result.selectedTabColor = ColorParser.parse(json, "selectedTabColor");
20 18
         result.unselectedTabColor = ColorParser.parse(json, "unselectedTabColor");
19
+        result.fontSize = NumberParser.parse(json, "fontSize");
21 20
         return result;
22 21
     }
23 22
 
24 23
     void mergeWith(TopTabsOptions other) {
25
-        if (other.selectedTabColor.hasColor()) {
26
-            selectedTabColor = other.selectedTabColor;
27
-        }
28
-        if (other.unselectedTabColor.hasColor()) {
29
-            unselectedTabColor = other.unselectedTabColor;
30
-        }
24
+        if (other.selectedTabColor.hasValue()) selectedTabColor = other.selectedTabColor;
25
+        if (other.unselectedTabColor.hasValue()) unselectedTabColor = other.unselectedTabColor;
26
+        if (other.fontSize.hasValue()) fontSize = other.fontSize;
31 27
     }
32 28
 
33 29
     void mergeWithDefault(TopTabsOptions defaultOptions) {
34
-        if (!selectedTabColor.hasColor()) {
35
-            selectedTabColor = defaultOptions.selectedTabColor;
36
-        }
37
-        if (!unselectedTabColor.hasColor()) {
38
-            unselectedTabColor = defaultOptions.unselectedTabColor;
39
-        }
30
+        if (!selectedTabColor.hasValue()) selectedTabColor = defaultOptions.selectedTabColor;
31
+        if (!unselectedTabColor.hasValue()) unselectedTabColor = defaultOptions.unselectedTabColor;
32
+        if (!fontSize.hasValue()) fontSize = defaultOptions.fontSize;
40 33
     }
41 34
 }

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

@@ -74,6 +74,7 @@ public class OptionsPresenter {
74 74
 
75 75
     private void applyTopTabsOptions(TopTabsOptions options) {
76 76
         topBar.applyTopTabsColors(options.selectedTabColor, options.unselectedTabColor);
77
+        topBar.applyTopTabsFontSize(options.fontSize);
77 78
     }
78 79
 
79 80
     private void applyTopTabOptions(TopTabOptions topTabOptions) {

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

@@ -14,8 +14,9 @@ import android.view.ViewGroup;
14 14
 import android.widget.TextView;
15 15
 
16 16
 import com.reactnativenavigation.parse.Button;
17
+import com.reactnativenavigation.parse.Number;
17 18
 import com.reactnativenavigation.viewcontrollers.toptabs.TopTabsViewPager;
18
-import com.reactnativenavigation.views.style.Color;
19
+import com.reactnativenavigation.parse.Color;
19 20
 
20 21
 import java.util.ArrayList;
21 22
 
@@ -67,6 +68,10 @@ public class TopBar extends AppBarLayout {
67 68
         topTabs.applyTopTabsColors(selectedTabColor, unselectedTabColor);
68 69
     }
69 70
 
71
+    public void applyTopTabsFontSize(Number fontSize) {
72
+        topTabs.applyTopTabsFontSize(fontSize);
73
+    }
74
+
70 75
 	public void setButtons(ArrayList<Button> leftButtons, ArrayList<Button> rightButtons) {
71 76
 		setLeftButtons(leftButtons);
72 77
 		setRightButtons(rightButtons);

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

@@ -4,7 +4,8 @@ import android.content.Context;
4 4
 import android.graphics.Typeface;
5 5
 import android.support.design.widget.TabLayout;
6 6
 
7
-import com.reactnativenavigation.views.style.Color;
7
+import com.reactnativenavigation.parse.Color;
8
+import com.reactnativenavigation.parse.Number;
8 9
 
9 10
 public class TopTabs extends TabLayout {
10 11
     private final TopTabsStyleHelper styleHelper;
@@ -29,4 +30,8 @@ public class TopTabs extends TabLayout {
29 30
     public void applyTopTabsColors(Color selectedTabColor, Color unselectedTabColor) {
30 31
         styleHelper.applyTopTabsColors(selectedTabColor, unselectedTabColor);
31 32
     }
33
+
34
+    public void applyTopTabsFontSize(Number fontSize) {
35
+        styleHelper.applyTopTabsFontSize(fontSize);
36
+    }
32 37
 }

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

@@ -5,9 +5,10 @@ import android.graphics.Typeface;
5 5
 import android.view.ViewGroup;
6 6
 import android.widget.TextView;
7 7
 
8
+import com.reactnativenavigation.parse.Color;
9
+import com.reactnativenavigation.parse.Number;
8 10
 import com.reactnativenavigation.utils.Task;
9 11
 import com.reactnativenavigation.utils.ViewUtils;
10
-import com.reactnativenavigation.views.style.Color;
11 12
 
12 13
 class TopTabsStyleHelper {
13 14
     private TopTabs topTabs;
@@ -16,18 +17,25 @@ class TopTabsStyleHelper {
16 17
         this.topTabs = topTabs;
17 18
     }
18 19
 
20
+    void applyTopTabsFontSize(Number fontSize) {
21
+        if (!fontSize.hasValue()) return;
22
+        for (int i = 0; i < topTabs.getTabCount(); i++) {
23
+            applyOnTabTitle(i, (title) -> title.setTextSize(fontSize.get()));
24
+        }
25
+    }
26
+
19 27
     void applyTopTabsColors(Color selected, Color unselected) {
20
-        if (!selected.hasColor() && !unselected.hasColor()) return;
28
+        if (!selected.hasValue() && !unselected.hasValue()) return;
21 29
 
22 30
         ColorStateList originalColors = topTabs.getTabTextColors();
23 31
         int selectedTabColor = originalColors != null ? originalColors.getColorForState(topTabs.getSelectedTabColors(), -1) : -1;
24 32
         int tabTextColor = originalColors != null ? originalColors.getColorForState(topTabs.getDefaultTabColors(), -1) : -1;
25 33
 
26
-        if (selected.hasColor()) {
34
+        if (selected.hasValue()) {
27 35
             tabTextColor = selected.get();
28 36
         }
29 37
 
30
-        if (unselected.hasColor()) {
38
+        if (unselected.hasValue()) {
31 39
             selectedTabColor = unselected.get();
32 40
         }
33 41
 

+ 0
- 30
lib/android/app/src/main/java/com/reactnativenavigation/views/style/Color.java View File

@@ -1,30 +0,0 @@
1
-package com.reactnativenavigation.views.style;
2
-
3
-import android.support.annotation.ColorInt;
4
-
5
-public class Color {
6
-
7
-    private Integer color;
8
-
9
-    public Color(@ColorInt int color) {
10
-        this.color = color;
11
-    }
12
-
13
-    public boolean hasColor() {
14
-        return color != null;
15
-    }
16
-
17
-    @ColorInt
18
-    public int get() {
19
-        if (hasColor()) {
20
-            return color;
21
-        }
22
-        throw new RuntimeException("Tried to get null color!");
23
-    }
24
-
25
-    @SuppressWarnings("MagicNumber")
26
-    @Override
27
-    public String toString() {
28
-        return String.format("#%06X", (0xFFFFFF & get()));
29
-    }
30
-}

+ 2
- 0
lib/src/params/options/TopTabs.js View File

@@ -2,10 +2,12 @@ class TopTabs {
2 2
   /**
3 3
    * @property {string} [selectedTabColor] Selected tab color
4 4
    * @property {string} unselectedTabColor Unselected tab color
5
+   * @property {int} fontSize
5 6
    */
6 7
   constructor(topTabs) {
7 8
     this.selectedTabColor = topTabs.selectedTabColor;
8 9
     this.unselectedTabColor = topTabs.unselectedTabColor;
10
+    this.fontSize = topTabs.fontSize;
9 11
   }
10 12
 }
11 13
 

+ 3
- 1
lib/src/params/options/TopTabs.test.js View File

@@ -2,7 +2,8 @@ const TopTabs = require('./TopTabs');
2 2
 
3 3
 const TOP_TABS = {
4 4
   selectedTabColor: 'red',
5
-  unselectedTabColor: 'blue'
5
+  unselectedTabColor: 'blue',
6
+  fontSize: 11
6 7
 };
7 8
 
8 9
 describe('TopTabs', () => {
@@ -10,5 +11,6 @@ describe('TopTabs', () => {
10 11
     const uut = new TopTabs(TOP_TABS);
11 12
     expect(uut.selectedTabColor).toEqual('red');
12 13
     expect(uut.unselectedTabColor).toEqual('blue');
14
+    expect(uut.fontSize).toEqual(11);
13 15
   });
14 16
 });

+ 2
- 1
playground/src/containers/WelcomeScreen.js View File

@@ -203,7 +203,8 @@ class WelcomeScreen extends Component {
203 203
       navigationOptions: {
204 204
         topTabs: {
205 205
           selectedTabColor: '#12766b',
206
-          unselectedTabColor: 'red'
206
+          unselectedTabColor: 'red',
207
+          fontSize: 6
207 208
         }
208 209
       }
209 210
     });