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
1 changed files with 19 additions and 16 deletions

View File

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