Просмотр исходного кода

Load typface when parsing options (#2416)

* Load typface when parsing options

* fix test
Guy Carmeli 6 лет назад
Родитель
Сommit
13e2e903e1
No account linked to committer's email address

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/NavigationOptions.java Просмотреть файл

@@ -33,7 +33,7 @@ public class NavigationOptions implements DEFAULT_VALUES {
33 33
 		NavigationOptions result = new NavigationOptions();
34 34
 		if (json == null) return result;
35 35
 
36
-		result.topBarOptions = TopBarOptions.parse(json.optJSONObject("topBar"));
36
+		result.topBarOptions = TopBarOptions.parse(typefaceManager, json.optJSONObject("topBar"));
37 37
 		result.topTabsOptions = TopTabsOptions.parse(json.optJSONObject("topTabs"));
38 38
         result.topTabOptions = TopTabOptions.parse(typefaceManager, json.optJSONObject("topTab"));
39 39
 		result.bottomTabsOptions = BottomTabsOptions.parse(json.optJSONObject("bottomTabs"));

+ 11
- 9
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java Просмотреть файл

@@ -1,13 +1,17 @@
1 1
 package com.reactnativenavigation.parse;
2 2
 
3 3
 
4
+import android.graphics.Typeface;
4 5
 import android.support.annotation.ColorInt;
6
+import android.support.annotation.Nullable;
7
+
8
+import com.reactnativenavigation.utils.TypefaceLoader;
5 9
 
6 10
 import org.json.JSONObject;
7 11
 
8 12
 public class TopBarOptions implements DEFAULT_VALUES {
9 13
 
10
-	public static TopBarOptions parse(JSONObject json) {
14
+	public static TopBarOptions parse(TypefaceLoader typefaceManager, JSONObject json) {
11 15
 		TopBarOptions options = new TopBarOptions();
12 16
 		if (json == null) return options;
13 17
 
@@ -15,7 +19,7 @@ public class TopBarOptions implements DEFAULT_VALUES {
15 19
 		options.backgroundColor = json.optInt("backgroundColor", NO_COLOR_VALUE);
16 20
 		options.textColor = json.optInt("textColor", NO_COLOR_VALUE);
17 21
 		options.textFontSize = (float) json.optDouble("textFontSize", NO_FLOAT_VALUE);
18
-		options.textFontFamily = json.optString("textFontFamily", NO_VALUE);
22
+		options.textFontFamily = typefaceManager.getTypeFace(json.optString("textFontFamily", NO_VALUE));
19 23
 		options.hidden = NavigationOptions.BooleanOptions.parse(json.optString("hidden"));
20 24
 		options.animateHide = NavigationOptions.BooleanOptions.parse(json.optString("animateHide"));
21 25
 
@@ -23,12 +27,10 @@ public class TopBarOptions implements DEFAULT_VALUES {
23 27
 	}
24 28
 
25 29
 	public String title = NO_VALUE;
26
-	@ColorInt
27
-	public int backgroundColor = NO_COLOR_VALUE;
28
-	@ColorInt
29
-	public int textColor = NO_COLOR_VALUE;
30
+	@ColorInt public int backgroundColor = NO_COLOR_VALUE;
31
+	@ColorInt public int textColor = NO_COLOR_VALUE;
30 32
 	public float textFontSize = NO_FLOAT_VALUE;
31
-	public String textFontFamily = NO_VALUE;
33
+	@Nullable public Typeface textFontFamily;
32 34
 	public NavigationOptions.BooleanOptions hidden = NavigationOptions.BooleanOptions.False;
33 35
 	public NavigationOptions.BooleanOptions animateHide = NavigationOptions.BooleanOptions.False;
34 36
 
@@ -40,7 +42,7 @@ public class TopBarOptions implements DEFAULT_VALUES {
40 42
 			textColor = other.textColor;
41 43
 		if (other.textFontSize != NO_FLOAT_VALUE)
42 44
 			textFontSize = other.textFontSize;
43
-		if (!NO_VALUE.equals(other.textFontFamily))
45
+		if (other.textFontFamily != null)
44 46
 			textFontFamily = other.textFontFamily;
45 47
 		if (other.hidden != NavigationOptions.BooleanOptions.NoValue) {
46 48
 			hidden = other.hidden;
@@ -59,7 +61,7 @@ public class TopBarOptions implements DEFAULT_VALUES {
59 61
             textColor = defaultOptions.textColor;
60 62
         if (textFontSize == NO_FLOAT_VALUE)
61 63
             textFontSize = defaultOptions.textFontSize;
62
-        if (NO_VALUE.equals(textFontFamily))
64
+        if (textFontFamily == null)
63 65
             textFontFamily = defaultOptions.textFontFamily;
64 66
         if (hidden == NavigationOptions.BooleanOptions.NoValue)
65 67
             hidden = defaultOptions.hidden;

+ 1
- 3
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java Просмотреть файл

@@ -7,7 +7,6 @@ import com.reactnativenavigation.parse.Button;
7 7
 import com.reactnativenavigation.parse.NavigationOptions;
8 8
 import com.reactnativenavigation.parse.TopBarOptions;
9 9
 import com.reactnativenavigation.parse.TopTabOptions;
10
-import com.reactnativenavigation.utils.TypefaceLoader;
11 10
 import com.reactnativenavigation.views.TopBar;
12 11
 
13 12
 import java.util.ArrayList;
@@ -36,8 +35,7 @@ public class OptionsPresenter {
36 35
         topBar.setTitleTextColor(options.textColor);
37 36
         topBar.setTitleFontSize(options.textFontSize);
38 37
 
39
-        TypefaceLoader typefaceLoader = new TypefaceLoader(topBar.getContext());
40
-        topBar.setTitleTypeface(typefaceLoader.getTypeFace(options.textFontFamily));
38
+        topBar.setTitleTypeface(options.textFontFamily);
41 39
         if (options.hidden == NavigationOptions.BooleanOptions.True) {
42 40
             hideTopBar(options.animateHide);
43 41
         }

+ 4
- 4
lib/android/app/src/main/java/com/reactnativenavigation/utils/TypefaceLoader.java Просмотреть файл

@@ -25,13 +25,13 @@ public class TypefaceLoader {
25 25
 		if (TextUtils.isEmpty(fontFamilyName)) return null;
26 26
 		if (typefaceCache.containsKey(fontFamilyName)) return typefaceCache.get(fontFamilyName);
27 27
 
28
-		Typeface result = load(context, fontFamilyName);
28
+		Typeface result = load(fontFamilyName);
29 29
 		typefaceCache.put(fontFamilyName, result);
30 30
 		return result;
31 31
 	}
32 32
 
33
-	private Typeface load(Context context, String fontFamilyName) {
34
-		Typeface typeface = getTypefaceFromAssets(context, fontFamilyName);
33
+	private Typeface load(String fontFamilyName) {
34
+		Typeface typeface = getTypefaceFromAssets(fontFamilyName);
35 35
 		if (typeface != null) return typeface;
36 36
 
37 37
 		int style = getStyle(fontFamilyName);
@@ -49,7 +49,7 @@ public class TypefaceLoader {
49 49
 	}
50 50
 
51 51
 	@Nullable
52
-	Typeface getTypefaceFromAssets(Context context, String fontFamilyName) {
52
+    public Typeface getTypefaceFromAssets(String fontFamilyName) {
53 53
 		try {
54 54
 			if (context != null) {
55 55
 				AssetManager assets = context.getAssets();

+ 10
- 0
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TypefaceLoaderMock.java Просмотреть файл

@@ -0,0 +1,10 @@
1
+package com.reactnativenavigation.mocks;
2
+
3
+import com.reactnativenavigation.utils.TypefaceLoader;
4
+
5
+public class TypefaceLoaderMock extends TypefaceLoader {
6
+
7
+    public TypefaceLoaderMock() {
8
+        super(null);
9
+    }
10
+}

+ 12
- 3
lib/android/app/src/test/java/com/reactnativenavigation/parse/NavigationOptionsTest.java Просмотреть файл

@@ -1,14 +1,16 @@
1 1
 package com.reactnativenavigation.parse;
2 2
 
3
+import android.graphics.Typeface;
3 4
 import android.support.annotation.NonNull;
4
-import android.test.mock.MockContext;
5 5
 
6 6
 import com.reactnativenavigation.BaseTest;
7
+import com.reactnativenavigation.mocks.TypefaceLoaderMock;
7 8
 import com.reactnativenavigation.utils.TypefaceLoader;
8 9
 
9 10
 import org.json.JSONException;
10 11
 import org.json.JSONObject;
11 12
 import org.junit.Test;
13
+import org.mockito.Mockito;
12 14
 
13 15
 import static com.reactnativenavigation.parse.NavigationOptions.BooleanOptions.True;
14 16
 import static org.assertj.core.api.Java6Assertions.assertThat;
@@ -20,13 +22,20 @@ public class NavigationOptionsTest extends BaseTest {
20 22
     private static final int TOP_BAR_TEXT_COLOR = 0xff123456;
21 23
     private static final int TOP_BAR_FONT_SIZE = 18;
22 24
     private static final String TOP_BAR_FONT_FAMILY = "HelveticaNeue-CondensedBold";
25
+    private static final Typeface TOP_BAR_TYPEFACE = Typeface.create("HelveticaNeue-CondensedBold", Typeface.BOLD);
23 26
     private static final NavigationOptions.BooleanOptions TOP_BAR_HIDDEN = True;
24 27
     private static final NavigationOptions.BooleanOptions BOTTOM_TABS_ANIMATE_HIDE = True;
25 28
     private static final NavigationOptions.BooleanOptions BOTTOM_TABS_HIDDEN = True;
26 29
     private static final int BOTTOM_TABS_BADGE = 3;
27 30
     private static final String BOTTOM_TABS_CURRENT_TAB_ID = "ContainerId";
28 31
     private static final int BOTTOM_TABS_CURRENT_TAB_INDEX = 1;
29
-    private TypefaceLoader mockLoader = new TypefaceLoader(new MockContext());
32
+    private TypefaceLoader mockLoader;
33
+
34
+    @Override
35
+    public void beforeEach() {
36
+        mockLoader = Mockito.mock(TypefaceLoaderMock.class);
37
+        Mockito.doReturn(TOP_BAR_TYPEFACE).when(mockLoader).getTypeFace(TOP_BAR_FONT_FAMILY);
38
+    }
30 39
 
31 40
     @Test
32 41
 	public void parsesNullAsDefaultEmptyOptions() throws Exception {
@@ -47,7 +56,7 @@ public class NavigationOptionsTest extends BaseTest {
47 56
         assertThat(result.topBarOptions.backgroundColor).isEqualTo(TOP_BAR_BACKGROUND_COLOR);
48 57
         assertThat(result.topBarOptions.textColor).isEqualTo(TOP_BAR_TEXT_COLOR);
49 58
         assertThat(result.topBarOptions.textFontSize).isEqualTo(TOP_BAR_FONT_SIZE);
50
-        assertThat(result.topBarOptions.textFontFamily).isEqualTo(TOP_BAR_FONT_FAMILY);
59
+        assertThat(result.topBarOptions.textFontFamily).isEqualTo(TOP_BAR_TYPEFACE);
51 60
         assertThat(result.topBarOptions.hidden).isEqualTo(TOP_BAR_HIDDEN);
52 61
         assertThat(result.bottomTabsOptions.animateHide).isEqualTo(BOTTOM_TABS_ANIMATE_HIDE);
53 62
         assertThat(result.bottomTabsOptions.hidden).isEqualTo(BOTTOM_TABS_HIDDEN);

+ 4
- 4
lib/android/app/src/test/java/com/reactnativenavigation/utils/TypefaceLoaderTest.java Просмотреть файл

@@ -17,7 +17,7 @@ public class TypefaceLoaderTest extends BaseTest {
17 17
 	public void loadTypefaceNoAssets() {
18 18
 		Context context = new MockContext();
19 19
 		TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
20
-		Mockito.doReturn(null).when(mockedLoader).getTypefaceFromAssets(context, "Helvetica-Bold");
20
+		Mockito.doReturn(null).when(mockedLoader).getTypefaceFromAssets("Helvetica-Bold");
21 21
 
22 22
 		Typeface typeface = mockedLoader.getTypeFace("Helvetica-Bold");
23 23
 		assertThat(typeface).isNotNull();
@@ -28,7 +28,7 @@ public class TypefaceLoaderTest extends BaseTest {
28 28
 	public void loadTypefaceWithAssets() {
29 29
         Context context = new MockContext();
30 30
         TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
31
-		Mockito.doReturn(Typeface.create("Helvetica-Italic", Typeface.ITALIC)).when(mockedLoader).getTypefaceFromAssets(context, "Helvetica-Italic");
31
+		Mockito.doReturn(Typeface.create("Helvetica-Italic", Typeface.ITALIC)).when(mockedLoader).getTypefaceFromAssets("Helvetica-Italic");
32 32
 
33 33
 		Typeface typeface = mockedLoader.getTypeFace("Helvetica-Italic");
34 34
 		assertThat(typeface).isNotNull();
@@ -39,7 +39,7 @@ public class TypefaceLoaderTest extends BaseTest {
39 39
 	public void loadTypefaceWrongName() {
40 40
         Context context = new MockContext();
41 41
         TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
42
-		Mockito.doReturn(null).when(mockedLoader).getTypefaceFromAssets(context, "Some-name");
42
+		Mockito.doReturn(null).when(mockedLoader).getTypefaceFromAssets("Some-name");
43 43
 
44 44
 		Typeface typeface = mockedLoader.getTypeFace("Some-name");
45 45
 		assertThat(typeface).isNotNull();
@@ -50,7 +50,7 @@ public class TypefaceLoaderTest extends BaseTest {
50 50
 	public void loadTypefaceNull() {
51 51
         Context context = new MockContext();
52 52
         TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
53
-		Mockito.doReturn(null).when(mockedLoader).getTypefaceFromAssets(context, null);
53
+		Mockito.doReturn(null).when(mockedLoader).getTypefaceFromAssets(null);
54 54
 
55 55
 		Typeface typeface = mockedLoader.getTypeFace(null);
56 56
 		assertThat(typeface).isNull();