Quellcode durchsuchen

Button fontFamily Android

Guy Carmeli vor 6 Jahren
Ursprung
Commit
aeea586404

+ 5
- 5
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java Datei anzeigen

@@ -20,19 +20,19 @@ import java.util.ArrayList;
20 20
 
21 21
 public class TopBarOptions {
22 22
 
23
-    public static TopBarOptions parse(TypefaceLoader typefaceManager, JSONObject json) {
23
+    public static TopBarOptions parse(TypefaceLoader typefaceLoader, JSONObject json) {
24 24
         TopBarOptions options = new TopBarOptions();
25 25
         if (json == null) return options;
26 26
 
27
-        options.title = TitleOptions.parse(typefaceManager, json.optJSONObject("title"));
28
-        options.subtitle = SubtitleOptions.parse(typefaceManager, json.optJSONObject("subtitle"));
27
+        options.title = TitleOptions.parse(typefaceLoader, json.optJSONObject("title"));
28
+        options.subtitle = SubtitleOptions.parse(typefaceLoader, json.optJSONObject("subtitle"));
29 29
         options.background = TopBarBackgroundOptions.parse(json.optJSONObject("background"));
30 30
         options.visible = BoolParser.parse(json, "visible");
31 31
         options.animate = BoolParser.parse(json,"animate");
32 32
         options.hideOnScroll = BoolParser.parse(json,"hideOnScroll");
33 33
         options.drawBehind = BoolParser.parse(json,"drawBehind");
34
-        options.rightButtons = Button.parseJsonArray(json.optJSONArray("rightButtons"));
35
-        options.leftButtons = Button.parseJsonArray(json.optJSONArray("leftButtons"));
34
+        options.rightButtons = Button.parseJsonArray(json.optJSONArray("rightButtons"), typefaceLoader);
35
+        options.leftButtons = Button.parseJsonArray(json.optJSONArray("leftButtons"), typefaceLoader);
36 36
         options.testId = TextParser.parse(json, "testID");
37 37
 
38 38
         options.validate();

+ 13
- 8
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/Button.java Datei anzeigen

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation.parse.params;
2 2
 
3
+import android.graphics.Typeface;
4
+import android.support.annotation.Nullable;
3 5
 import android.view.MenuItem;
4 6
 
5 7
 import com.reactnativenavigation.parse.Component;
@@ -7,6 +9,7 @@ import com.reactnativenavigation.parse.parsers.BoolParser;
7 9
 import com.reactnativenavigation.parse.parsers.ColorParser;
8 10
 import com.reactnativenavigation.parse.parsers.NumberParser;
9 11
 import com.reactnativenavigation.parse.parsers.TextParser;
12
+import com.reactnativenavigation.utils.TypefaceLoader;
10 13
 
11 14
 import org.json.JSONArray;
12 15
 import org.json.JSONObject;
@@ -21,23 +24,25 @@ public class Button {
21 24
     public int showAsAction;
22 25
     public Color color = new NullColor();
23 26
     public Color disabledColor = new NullColor();
24
-    public Number buttonFontSize = new NullNumber();
25
-    private Text buttonFontWeight = new NullText();
27
+    public Number fontSize = new NullNumber();
28
+    private Text fontWeight = new NullText();
29
+    @Nullable public Typeface fontFamily;
26 30
     public Text icon = new NullText();
27 31
     public Text testId = new NullText();
28 32
     public Component component = new Component();
29 33
 
30
-    private static Button parseJson(JSONObject json) {
34
+    private static Button parseJson(JSONObject json, TypefaceLoader typefaceManager) {
31 35
         Button button = new Button();
32 36
         button.id = json.optString("id");
33 37
         button.title = TextParser.parse(json, "title");
34 38
         button.enabled = BoolParser.parse(json, "enabled");
35 39
         button.disableIconTint = BoolParser.parse(json, "disableIconTint");
36 40
         button.showAsAction = parseShowAsAction(json);
37
-        button.color = ColorParser.parse(json, "buttonColor");
41
+        button.color = ColorParser.parse(json, "color");
38 42
         button.disabledColor = ColorParser.parse(json, "disabledColor");
39
-        button.buttonFontSize = NumberParser.parse(json, "buttonFontSize");
40
-        button.buttonFontWeight = TextParser.parse(json, "buttonFontWeight");
43
+        button.fontSize = NumberParser.parse(json, "fontSize");
44
+        button.fontFamily = typefaceManager.getTypeFace(json.optString("fontFamily", ""));
45
+        button.fontWeight = TextParser.parse(json, "fontWeight");
41 46
         button.testId = TextParser.parse(json, "testID");
42 47
         button.component = Component.parse(json.optJSONObject("component"));
43 48
 
@@ -48,7 +53,7 @@ public class Button {
48 53
         return button;
49 54
     }
50 55
 
51
-    public static ArrayList<Button> parseJsonArray(JSONArray jsonArray) {
56
+    public static ArrayList<Button> parseJsonArray(JSONArray jsonArray, TypefaceLoader typefaceLoader) {
52 57
         ArrayList<Button> buttons = new ArrayList<>();
53 58
 
54 59
         if (jsonArray == null) {
@@ -57,7 +62,7 @@ public class Button {
57 62
 
58 63
         for (int i = 0; i < jsonArray.length(); i++) {
59 64
             JSONObject json = jsonArray.optJSONObject(i);
60
-            Button button = Button.parseJson(json);
65
+            Button button = Button.parseJson(json, typefaceLoader);
61 66
             buttons.add(button);
62 67
         }
63 68
 

+ 67
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/ButtonOptionsPresenter.java Datei anzeigen

@@ -0,0 +1,67 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.graphics.Color;
4
+import android.graphics.PorterDuff;
5
+import android.graphics.PorterDuffColorFilter;
6
+import android.graphics.Typeface;
7
+import android.graphics.drawable.Drawable;
8
+import android.support.annotation.NonNull;
9
+import android.support.v7.widget.Toolbar;
10
+import android.view.View;
11
+import android.widget.TextView;
12
+
13
+import com.reactnativenavigation.parse.params.Button;
14
+
15
+import java.util.ArrayList;
16
+
17
+public class ButtonOptionsPresenter {
18
+    private final Toolbar toolbar;
19
+    private Button button;
20
+
21
+    public ButtonOptionsPresenter(Toolbar toolbar, Button button) {
22
+        this.toolbar = toolbar;
23
+        this.button = button;
24
+    }
25
+
26
+    public void tint(Drawable drawable, int tint) {
27
+        drawable.setColorFilter(new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN));
28
+    }
29
+
30
+    public void setTypeFace(Typeface typeface) {
31
+        UiUtils.runOnPreDrawOnce(toolbar, () -> {
32
+            ArrayList<View> buttons = findActualTextViewInMenu();
33
+            for (View btn : buttons) {
34
+                ((TextView) btn).setTypeface(typeface);
35
+            }
36
+        });
37
+    }
38
+
39
+    public void setTextColor() {
40
+        UiUtils.runOnPreDrawOnce(toolbar, () -> {
41
+            ArrayList<View> buttons = findActualTextViewInMenu();
42
+            for (View btn : buttons) {
43
+                if (button.enabled.isTrueOrUndefined() && button.color.hasValue()) {
44
+                    setEnabledColor((TextView) btn);
45
+                } else if (button.enabled.isFalse()) {
46
+                    setDisabledColor((TextView) btn, button.disabledColor.get(Color.LTGRAY));
47
+                }
48
+            }
49
+        });
50
+    }
51
+
52
+    private void setDisabledColor(TextView btn, int color) {
53
+        btn.setTextColor(color);
54
+    }
55
+
56
+    private void setEnabledColor(TextView btn) {
57
+        btn.setTextColor(button.color.get());
58
+    }
59
+
60
+    @NonNull
61
+    private ArrayList<View> findActualTextViewInMenu() {
62
+        ArrayList<View> outViews = new ArrayList<>();
63
+        toolbar.findViewsWithText(outViews, button.title.get(), View.FIND_VIEWS_WITH_TEXT);
64
+        return outViews;
65
+    }
66
+
67
+}

+ 0
- 11
lib/android/app/src/main/java/com/reactnativenavigation/utils/DrawableTinter.java Datei anzeigen

@@ -1,11 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import android.graphics.PorterDuff;
4
-import android.graphics.PorterDuffColorFilter;
5
-import android.graphics.drawable.Drawable;
6
-
7
-public class DrawableTinter {
8
-    public void tint(Drawable drawable, int tint) {
9
-        drawable.setColorFilter(new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN));
10
-    }
11
-}

+ 15
- 36
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TopBarButtonController.java Datei anzeigen

@@ -11,7 +11,6 @@ import android.text.SpannableString;
11 11
 import android.text.style.AbsoluteSizeSpan;
12 12
 import android.util.Log;
13 13
 import android.view.MenuItem;
14
-import android.view.View;
15 14
 import android.widget.ImageButton;
16 15
 import android.widget.TextView;
17 16
 
@@ -19,14 +18,13 @@ import com.reactnativenavigation.parse.Options;
19 18
 import com.reactnativenavigation.parse.params.Button;
20 19
 import com.reactnativenavigation.parse.params.Text;
21 20
 import com.reactnativenavigation.utils.ArrayUtils;
22
-import com.reactnativenavigation.utils.DrawableTinter;
21
+import com.reactnativenavigation.utils.ButtonOptionsPresenter;
23 22
 import com.reactnativenavigation.utils.ImageLoader;
24 23
 import com.reactnativenavigation.utils.ImageLoadingListenerAdapter;
25 24
 import com.reactnativenavigation.utils.UiUtils;
26 25
 import com.reactnativenavigation.utils.ViewUtils;
27 26
 import com.reactnativenavigation.views.titlebar.TitleBarReactButtonView;
28 27
 
29
-import java.util.ArrayList;
30 28
 import java.util.List;
31 29
 
32 30
 public class TopBarButtonController extends ViewController<TitleBarReactButtonView> implements MenuItem.OnMenuItemClickListener {
@@ -35,16 +33,16 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
35 33
     }
36 34
 
37 35
     private final ImageLoader imageLoader;
38
-    private DrawableTinter drawableTinter;
36
+    private ButtonOptionsPresenter optionsPresenter;
39 37
     private final Button button;
40 38
     private final ReactViewCreator viewCreator;
41 39
     private TopBarButtonController.OnClickListener onPressListener;
42 40
     private Drawable icon;
43 41
 
44
-    public TopBarButtonController(Activity activity, ImageLoader imageLoader, DrawableTinter drawableTinter, Button button, ReactViewCreator viewCreator, OnClickListener onClickListener) {
42
+    public TopBarButtonController(Activity activity, ImageLoader imageLoader, ButtonOptionsPresenter optionsPresenter, Button button, ReactViewCreator viewCreator, OnClickListener onClickListener) {
45 43
         super(activity, button.id, new Options());
46 44
         this.imageLoader = imageLoader;
47
-        this.drawableTinter = drawableTinter;
45
+        this.optionsPresenter = optionsPresenter;
48 46
         this.button = button;
49 47
         this.viewCreator = viewCreator;
50 48
         this.onPressListener = onClickListener;
@@ -129,8 +127,9 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
129 127
                     }
130 128
                 });
131 129
             } else {
132
-                setTextColor(toolbar);
130
+                optionsPresenter.setTextColor();
133 131
                 setFontSize(menuItem);
132
+                optionsPresenter.setTypeFace(button.fontFamily);
134 133
             }
135 134
         }
136 135
         setTestId(toolbar, button.testId);
@@ -143,41 +142,21 @@ public class TopBarButtonController extends ViewController<TitleBarReactButtonVi
143 142
     private void setIconColor(Drawable icon) {
144 143
         if (button.disableIconTint.isTrue()) return;
145 144
         if (button.enabled.isTrueOrUndefined() && button.color.hasValue()) {
146
-            drawableTinter.tint(icon, button.color.get());
145
+            optionsPresenter.tint(icon, button.color.get());
147 146
         } else if (button.enabled.isFalse()) {
148
-            drawableTinter.tint(icon, button.disabledColor.get(Color.LTGRAY));
149
-        }
150
-    }
151
-
152
-    private void setTextColor(Toolbar toolbar) {
153
-        UiUtils.runOnPreDrawOnce(toolbar, () -> {
154
-            ArrayList<View> outViews = findActualTextViewInMenuByText(toolbar);
155
-            setTextColorForFoundButtonViews(outViews);
156
-        });
157
-    }
158
-
159
-    @NonNull
160
-    private ArrayList<View> findActualTextViewInMenuByText(Toolbar toolbar) {
161
-        ArrayList<View> outViews = new ArrayList<>();
162
-        toolbar.findViewsWithText(outViews, button.title.get(), View.FIND_VIEWS_WITH_TEXT);
163
-        return outViews;
164
-    }
165
-
166
-    private void setTextColorForFoundButtonViews(ArrayList<View> buttons) {
167
-        for (View btn : buttons) {
168
-            if (button.enabled.isTrueOrUndefined() && button.color.hasValue()) {
169
-                ((TextView) btn).setTextColor(this.button.color.get());
170
-            } else if (button.enabled.isFalse()) {
171
-                ((TextView) btn).setTextColor(button.disabledColor.get(Color.LTGRAY));
172
-            }
147
+            optionsPresenter.tint(icon, button.disabledColor.get(Color.LTGRAY));
173 148
         }
174 149
     }
175 150
 
176 151
     private void setFontSize(MenuItem menuItem) {
177 152
         SpannableString spanString = new SpannableString(button.title.get());
178
-        if (this.button.buttonFontSize.hasValue())
179
-            spanString.setSpan(new AbsoluteSizeSpan(button.buttonFontSize.get(), true),
180
-                    0, button.title.get().length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
153
+        if (this.button.fontSize.hasValue())
154
+            spanString.setSpan(
155
+                    new AbsoluteSizeSpan(button.fontSize.get(), true),
156
+                    0,
157
+                    button.title.get().length(),
158
+                    Spannable.SPAN_INCLUSIVE_INCLUSIVE
159
+            );
181 160
         menuItem.setTitleCondensed(spanString);
182 161
     }
183 162
 

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java Datei anzeigen

@@ -13,7 +13,7 @@ import com.reactnativenavigation.parse.Alignment;
13 13
 import com.reactnativenavigation.parse.Component;
14 14
 import com.reactnativenavigation.parse.params.Button;
15 15
 import com.reactnativenavigation.parse.params.Color;
16
-import com.reactnativenavigation.utils.DrawableTinter;
16
+import com.reactnativenavigation.utils.ButtonOptionsPresenter;
17 17
 import com.reactnativenavigation.utils.ImageLoader;
18 18
 import com.reactnativenavigation.utils.UiUtils;
19 19
 import com.reactnativenavigation.utils.ViewUtils;
@@ -192,7 +192,7 @@ public class TitleBar extends Toolbar {
192 192
     }
193 193
 
194 194
     public TopBarButtonController createButtonController(Button button) {
195
-        return new TopBarButtonController((Activity) getContext(), new ImageLoader(), new DrawableTinter(), button, buttonCreator, onClickListener);
195
+        return new TopBarButtonController((Activity) getContext(), new ImageLoader(), new ButtonOptionsPresenter(this, button), button, buttonCreator, onClickListener);
196 196
     }
197 197
 
198 198
     public Toolbar.LayoutParams getComponentLayoutParams(Component component) {

+ 2
- 3
lib/android/app/src/test/java/com/reactnativenavigation/utils/TitleBarHelper.java Datei anzeigen

@@ -2,10 +2,9 @@ package com.reactnativenavigation.utils;
2 2
 
3 3
 import android.support.v7.view.menu.ActionMenuItemView;
4 4
 import android.support.v7.widget.Toolbar;
5
-import android.view.View;
6 5
 
7 6
 public class TitleBarHelper {
8
-    public static View getRightButton(Toolbar toolbar, int index) {
9
-        return (View) ViewUtils.findChildrenByClassRecursive(toolbar, ActionMenuItemView.class).get(toolbar.getMenu().size() - index - 1);
7
+    public static ActionMenuItemView getRightButton(Toolbar toolbar, int index) {
8
+        return (ActionMenuItemView) ViewUtils.findChildrenByClassRecursive(toolbar, ActionMenuItemView.class).get(toolbar.getMenu().size() - index - 1);
10 9
     }
11 10
 }

+ 37
- 23
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopBarButtonControllerTest.java Datei anzeigen

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.graphics.Typeface;
4 5
 import android.support.v7.widget.Toolbar;
5 6
 import android.view.MenuItem;
6 7
 
@@ -15,7 +16,7 @@ import com.reactnativenavigation.parse.params.Button;
15 16
 import com.reactnativenavigation.parse.params.Color;
16 17
 import com.reactnativenavigation.parse.params.NullText;
17 18
 import com.reactnativenavigation.parse.params.Text;
18
-import com.reactnativenavigation.utils.DrawableTinter;
19
+import com.reactnativenavigation.utils.ButtonOptionsPresenter;
19 20
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
20 21
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
21 22
 
@@ -27,14 +28,13 @@ import static org.mockito.ArgumentMatchers.eq;
27 28
 import static org.mockito.Mockito.spy;
28 29
 import static org.mockito.Mockito.times;
29 30
 import static org.mockito.Mockito.verify;
30
-import static org.mockito.Mockito.verifyZeroInteractions;
31 31
 
32 32
 public class TopBarButtonControllerTest extends BaseTest {
33 33
 
34 34
     private TopBarButtonController uut;
35 35
     private StackController stackController;
36 36
     private Button button;
37
-    private DrawableTinter drawableTinter = spy(new DrawableTinter());
37
+    private ButtonOptionsPresenter optionsPresenter;
38 38
 
39 39
     @Override
40 40
     public void beforeEach() {
@@ -42,7 +42,6 @@ public class TopBarButtonControllerTest extends BaseTest {
42 42
         final Activity activity = newActivity();
43 43
 
44 44
         TopBarButtonCreatorMock buttonCreatorMock = new TopBarButtonCreatorMock();
45
-        uut = new TopBarButtonController(activity, ImageLoaderMock.mock(), drawableTinter, button, buttonCreatorMock, (buttonId) -> {});
46 45
         stackController = spy(new StackController(activity,
47 46
                 buttonCreatorMock,
48 47
                 new TitleBarReactViewCreatorMock(),
@@ -53,7 +52,12 @@ public class TopBarButtonControllerTest extends BaseTest {
53 52
         );
54 53
         stackController.getView().layout(0, 0, 1080, 1920);
55 54
         stackController.getTopBar().layout(0, 0, 1080, 200);
56
-        stackController.getTopBar().getTitleBar().layout(0, 0, 1080, 200);
55
+        getTitleBar().layout(0, 0, 1080, 200);
56
+
57
+        optionsPresenter = spy(new ButtonOptionsPresenter(getTitleBar(), button));
58
+        uut = new TopBarButtonController(activity, ImageLoaderMock.mock(), optionsPresenter, button, buttonCreatorMock, (buttonId) -> {});
59
+
60
+        stackController.ensureViewIsCreated();
57 61
     }
58 62
 
59 63
     @Test
@@ -66,45 +70,55 @@ public class TopBarButtonControllerTest extends BaseTest {
66 70
 
67 71
     @Test
68 72
     public void setIconColor_enabled() {
69
-        stackController.ensureViewIsCreated();
70
-
71 73
         setIconButton(true);
72
-        Toolbar titleBar = stackController.getTopBar().getTitleBar();
73
-        uut.addToMenu(titleBar, 0);
74
+        uut.addToMenu(getTitleBar(), 0);
74 75
 
75
-        assertThat(titleBar.getMenu().size()).isOne();
76
-        verify(drawableTinter, times(1)).tint(any(), eq(android.graphics.Color.RED));
76
+        assertThat(getTitleBar().getMenu().size()).isOne();
77
+        verify(optionsPresenter, times(1)).tint(any(), eq(android.graphics.Color.RED));
77 78
     }
78 79
 
79 80
     @Test
80 81
     public void setIconColor_disabled() {
81
-        stackController.ensureViewIsCreated();
82
-
83 82
         setIconButton(false);
84
-        uut.addToMenu(stackController.getTopBar().getTitleBar(), 0);
83
+        uut.addToMenu(getTitleBar(), 0);
85 84
 
86
-        verify(drawableTinter, times(1)).tint(any(), eq(android.graphics.Color.LTGRAY));
85
+        verify(optionsPresenter, times(1)).tint(any(), eq(android.graphics.Color.LTGRAY));
87 86
     }
88 87
 
89 88
     @Test
90 89
     public void setIconColor_disabledColor() {
91
-        stackController.ensureViewIsCreated();
92
-
93 90
         setIconButton(false);
94 91
         button.disabledColor = new Color(android.graphics.Color.BLACK);
95
-        uut.addToMenu(stackController.getTopBar().getTitleBar(), 0);
92
+        uut.addToMenu(getTitleBar(), 0);
96 93
 
97
-        verify(drawableTinter, times(1)).tint(any(), eq(android.graphics.Color.BLACK));
94
+        verify(optionsPresenter, times(1)).tint(any(), eq(android.graphics.Color.BLACK));
98 95
     }
99 96
 
100 97
     @Test
101 98
     public void disableIconTint() {
102
-        stackController.ensureViewIsCreated();
103
-
104 99
         setIconButton(false);
105 100
         button.disableIconTint = new Bool(true);
106
-        uut.addToMenu(stackController.getTopBar().getTitleBar(), 0);
107
-        verifyZeroInteractions(drawableTinter);
101
+        uut.addToMenu(getTitleBar(), 0);
102
+        verify(optionsPresenter, times(0)).setTextColor();
103
+    }
104
+
105
+    @Test
106
+    public void fontFamily() {
107
+        setTextButton();
108
+        uut.addToMenu(getTitleBar(), 0);
109
+        verify(optionsPresenter, times(1)).setTypeFace(Typeface.MONOSPACE);
110
+    }
111
+
112
+    private Toolbar getTitleBar() {
113
+        return stackController.getTopBar().getTitleBar();
114
+    }
115
+
116
+    private void setTextButton() {
117
+        button.id = "btn1";
118
+        button.color = new Color(android.graphics.Color.RED);
119
+        button.title = new Text("Button");
120
+        button.fontFamily = Typeface.MONOSPACE;
121
+        button.showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS;
108 122
     }
109 123
 
110 124
     private void setIconButton(boolean enabled) {