Permalink
Browse files

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
  • Loading branch information...
1 parent f89f09f commit 380830e4aa212fb934d3fe82d971cb8a316671f8 @MengjueW MengjueW committed with Facebook Github Bot 5 Aug 10, 2016
Showing with 19 additions and 16 deletions.
  1. +19 −16 ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java
@@ -20,7 +20,7 @@
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,33 +57,23 @@ public boolean isRTL(Context context) {
* 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);
}
/**
* Could be used to test RTL layout with English
* 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 @@ private boolean isDevicePreferredLanguageRTL() {
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();
+ }
}

0 comments on commit 380830e

Please sign in to comment.