Add isPrefSet and setPref to make I18nUtil cleaner

Summary: Add `isPrefSet` and `setPref` function to make I18nUtil cleaner

Reviewed By: fkgozali

Differential Revision: D3684958

fbshipit-source-id: 96f51d495d700d46096dc162c7599bc51a0b37cd
This commit is contained in:
Mengjue Wang 2016-08-09 18:52:47 -07:00 committed by Facebook Github Bot 5
parent f89f09f14c
commit 380830e4aa

View File

@ -20,7 +20,7 @@ import java.util.Locale;
public class I18nUtil { public class I18nUtil {
private static I18nUtil sharedI18nUtilInstance = null; private static I18nUtil sharedI18nUtilInstance = null;
private static final String MY_PREFS_NAME = private static final String SHARED_PREFS_NAME =
"com.facebook.react.modules.i18nmanager.I18nUtil"; "com.facebook.react.modules.i18nmanager.I18nUtil";
private static final String KEY_FOR_PREFS_ALLOWRTL = private static final String KEY_FOR_PREFS_ALLOWRTL =
"RCTI18nUtil_allowRTL"; "RCTI18nUtil_allowRTL";
@ -57,16 +57,11 @@ public class I18nUtil {
* Before the bridge is initialized * Before the bridge is initialized
*/ */
private boolean isRTLAllowed(Context context) { private boolean isRTLAllowed(Context context) {
SharedPreferences prefs = return isPrefSet(context, KEY_FOR_PREFS_ALLOWRTL, false);
context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_FOR_PREFS_ALLOWRTL, false);
} }
public void allowRTL(Context context, boolean allowRTL) { public void allowRTL(Context context, boolean allowRTL) {
SharedPreferences.Editor editor = setPref(context, KEY_FOR_PREFS_ALLOWRTL, allowRTL);
context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
editor.putBoolean(KEY_FOR_PREFS_ALLOWRTL, allowRTL);
editor.apply();
} }
/** /**
@ -74,16 +69,11 @@ public class I18nUtil {
* Used for development and testing purpose * Used for development and testing purpose
*/ */
private boolean isRTLForced(Context context) { private boolean isRTLForced(Context context) {
SharedPreferences prefs = return isPrefSet(context, KEY_FOR_PREFS_FORCERTL, false);
context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_FOR_PREFS_FORCERTL, false);
} }
public void forceRTL(Context context, boolean allowRTL) { public void forceRTL(Context context, boolean forceRTL) {
SharedPreferences.Editor editor = setPref(context, KEY_FOR_PREFS_FORCERTL, forceRTL);
context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
editor.putBoolean(KEY_FOR_PREFS_FORCERTL, allowRTL);
editor.apply();
} }
// Check if the current device language is RTL // Check if the current device language is RTL
@ -92,4 +82,17 @@ public class I18nUtil {
TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()); TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault());
return directionality == ViewCompat.LAYOUT_DIRECTION_RTL; return directionality == ViewCompat.LAYOUT_DIRECTION_RTL;
} }
private boolean isPrefSet(Context context, String key, boolean defaultValue) {
SharedPreferences prefs =
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(key, defaultValue);
}
private void setPref(Context context, String key, boolean value) {
SharedPreferences.Editor editor =
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit();
editor.putBoolean(key, value);
editor.apply();
}
} }