Skip to content

Commit

Permalink
Add isPrefSet and setPref to make I18nUtil cleaner
Browse files Browse the repository at this point in the history
Summary: Add `isPrefSet` and `setPref` function to make I18nUtil cleaner

Reviewed By: fkgozali

Differential Revision: D3684958

fbshipit-source-id: 96f51d495d700d46096dc162c7599bc51a0b37cd
  • Loading branch information
MengjueW authored and Facebook Github Bot 5 committed Aug 10, 2016
1 parent f89f09f commit 380830e
Showing 1 changed file with 19 additions and 16 deletions.
Expand Up @@ -20,7 +20,7 @@
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";
Expand Down Expand Up @@ -57,33 +57,23 @@ public boolean isRTL(Context context) {
* 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();
} }


/** /**
* Could be used to test RTL layout with English * Could be used to test RTL layout with English
* 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
Expand All @@ -92,4 +82,17 @@ private boolean isDevicePreferredLanguageRTL() {
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();
}
} }

0 comments on commit 380830e

Please sign in to comment.