Browse Source

Load typface when parsing options (#2416)

* Load typface when parsing options

* fix test
Guy Carmeli 7 years ago
parent
commit
13e2e903e1
No account linked to committer's email address

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/NavigationOptions.java View File

33
 		NavigationOptions result = new NavigationOptions();
33
 		NavigationOptions result = new NavigationOptions();
34
 		if (json == null) return result;
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
 		result.topTabsOptions = TopTabsOptions.parse(json.optJSONObject("topTabs"));
37
 		result.topTabsOptions = TopTabsOptions.parse(json.optJSONObject("topTabs"));
38
         result.topTabOptions = TopTabOptions.parse(typefaceManager, json.optJSONObject("topTab"));
38
         result.topTabOptions = TopTabOptions.parse(typefaceManager, json.optJSONObject("topTab"));
39
 		result.bottomTabsOptions = BottomTabsOptions.parse(json.optJSONObject("bottomTabs"));
39
 		result.bottomTabsOptions = BottomTabsOptions.parse(json.optJSONObject("bottomTabs"));

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

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

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

7
 import com.reactnativenavigation.parse.NavigationOptions;
7
 import com.reactnativenavigation.parse.NavigationOptions;
8
 import com.reactnativenavigation.parse.TopBarOptions;
8
 import com.reactnativenavigation.parse.TopBarOptions;
9
 import com.reactnativenavigation.parse.TopTabOptions;
9
 import com.reactnativenavigation.parse.TopTabOptions;
10
-import com.reactnativenavigation.utils.TypefaceLoader;
11
 import com.reactnativenavigation.views.TopBar;
10
 import com.reactnativenavigation.views.TopBar;
12
 
11
 
13
 import java.util.ArrayList;
12
 import java.util.ArrayList;
36
         topBar.setTitleTextColor(options.textColor);
35
         topBar.setTitleTextColor(options.textColor);
37
         topBar.setTitleFontSize(options.textFontSize);
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
         if (options.hidden == NavigationOptions.BooleanOptions.True) {
39
         if (options.hidden == NavigationOptions.BooleanOptions.True) {
42
             hideTopBar(options.animateHide);
40
             hideTopBar(options.animateHide);
43
         }
41
         }

+ 4
- 4
lib/android/app/src/main/java/com/reactnativenavigation/utils/TypefaceLoader.java View File

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

+ 10
- 0
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TypefaceLoaderMock.java View File

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 View File

1
 package com.reactnativenavigation.parse;
1
 package com.reactnativenavigation.parse;
2
 
2
 
3
+import android.graphics.Typeface;
3
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
4
-import android.test.mock.MockContext;
5
 
5
 
6
 import com.reactnativenavigation.BaseTest;
6
 import com.reactnativenavigation.BaseTest;
7
+import com.reactnativenavigation.mocks.TypefaceLoaderMock;
7
 import com.reactnativenavigation.utils.TypefaceLoader;
8
 import com.reactnativenavigation.utils.TypefaceLoader;
8
 
9
 
9
 import org.json.JSONException;
10
 import org.json.JSONException;
10
 import org.json.JSONObject;
11
 import org.json.JSONObject;
11
 import org.junit.Test;
12
 import org.junit.Test;
13
+import org.mockito.Mockito;
12
 
14
 
13
 import static com.reactnativenavigation.parse.NavigationOptions.BooleanOptions.True;
15
 import static com.reactnativenavigation.parse.NavigationOptions.BooleanOptions.True;
14
 import static org.assertj.core.api.Java6Assertions.assertThat;
16
 import static org.assertj.core.api.Java6Assertions.assertThat;
20
     private static final int TOP_BAR_TEXT_COLOR = 0xff123456;
22
     private static final int TOP_BAR_TEXT_COLOR = 0xff123456;
21
     private static final int TOP_BAR_FONT_SIZE = 18;
23
     private static final int TOP_BAR_FONT_SIZE = 18;
22
     private static final String TOP_BAR_FONT_FAMILY = "HelveticaNeue-CondensedBold";
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
     private static final NavigationOptions.BooleanOptions TOP_BAR_HIDDEN = True;
26
     private static final NavigationOptions.BooleanOptions TOP_BAR_HIDDEN = True;
24
     private static final NavigationOptions.BooleanOptions BOTTOM_TABS_ANIMATE_HIDE = True;
27
     private static final NavigationOptions.BooleanOptions BOTTOM_TABS_ANIMATE_HIDE = True;
25
     private static final NavigationOptions.BooleanOptions BOTTOM_TABS_HIDDEN = True;
28
     private static final NavigationOptions.BooleanOptions BOTTOM_TABS_HIDDEN = True;
26
     private static final int BOTTOM_TABS_BADGE = 3;
29
     private static final int BOTTOM_TABS_BADGE = 3;
27
     private static final String BOTTOM_TABS_CURRENT_TAB_ID = "ContainerId";
30
     private static final String BOTTOM_TABS_CURRENT_TAB_ID = "ContainerId";
28
     private static final int BOTTOM_TABS_CURRENT_TAB_INDEX = 1;
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
     @Test
40
     @Test
32
 	public void parsesNullAsDefaultEmptyOptions() throws Exception {
41
 	public void parsesNullAsDefaultEmptyOptions() throws Exception {
47
         assertThat(result.topBarOptions.backgroundColor).isEqualTo(TOP_BAR_BACKGROUND_COLOR);
56
         assertThat(result.topBarOptions.backgroundColor).isEqualTo(TOP_BAR_BACKGROUND_COLOR);
48
         assertThat(result.topBarOptions.textColor).isEqualTo(TOP_BAR_TEXT_COLOR);
57
         assertThat(result.topBarOptions.textColor).isEqualTo(TOP_BAR_TEXT_COLOR);
49
         assertThat(result.topBarOptions.textFontSize).isEqualTo(TOP_BAR_FONT_SIZE);
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
         assertThat(result.topBarOptions.hidden).isEqualTo(TOP_BAR_HIDDEN);
60
         assertThat(result.topBarOptions.hidden).isEqualTo(TOP_BAR_HIDDEN);
52
         assertThat(result.bottomTabsOptions.animateHide).isEqualTo(BOTTOM_TABS_ANIMATE_HIDE);
61
         assertThat(result.bottomTabsOptions.animateHide).isEqualTo(BOTTOM_TABS_ANIMATE_HIDE);
53
         assertThat(result.bottomTabsOptions.hidden).isEqualTo(BOTTOM_TABS_HIDDEN);
62
         assertThat(result.bottomTabsOptions.hidden).isEqualTo(BOTTOM_TABS_HIDDEN);

+ 4
- 4
lib/android/app/src/test/java/com/reactnativenavigation/utils/TypefaceLoaderTest.java View File

17
 	public void loadTypefaceNoAssets() {
17
 	public void loadTypefaceNoAssets() {
18
 		Context context = new MockContext();
18
 		Context context = new MockContext();
19
 		TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
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
 		Typeface typeface = mockedLoader.getTypeFace("Helvetica-Bold");
22
 		Typeface typeface = mockedLoader.getTypeFace("Helvetica-Bold");
23
 		assertThat(typeface).isNotNull();
23
 		assertThat(typeface).isNotNull();
28
 	public void loadTypefaceWithAssets() {
28
 	public void loadTypefaceWithAssets() {
29
         Context context = new MockContext();
29
         Context context = new MockContext();
30
         TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
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
 		Typeface typeface = mockedLoader.getTypeFace("Helvetica-Italic");
33
 		Typeface typeface = mockedLoader.getTypeFace("Helvetica-Italic");
34
 		assertThat(typeface).isNotNull();
34
 		assertThat(typeface).isNotNull();
39
 	public void loadTypefaceWrongName() {
39
 	public void loadTypefaceWrongName() {
40
         Context context = new MockContext();
40
         Context context = new MockContext();
41
         TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
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
 		Typeface typeface = mockedLoader.getTypeFace("Some-name");
44
 		Typeface typeface = mockedLoader.getTypeFace("Some-name");
45
 		assertThat(typeface).isNotNull();
45
 		assertThat(typeface).isNotNull();
50
 	public void loadTypefaceNull() {
50
 	public void loadTypefaceNull() {
51
         Context context = new MockContext();
51
         Context context = new MockContext();
52
         TypefaceLoader mockedLoader = Mockito.spy(new TypefaceLoader(context));
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
 		Typeface typeface = mockedLoader.getTypeFace(null);
55
 		Typeface typeface = mockedLoader.getTypeFace(null);
56
 		assertThat(typeface).isNull();
56
 		assertThat(typeface).isNull();