|
| 1 | +package land.fx.fula; |
| 2 | + |
| 3 | +import static android.content.Context.MODE_PRIVATE; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +public class SharedPreferenceHelper { |
| 10 | + private static SharedPreferenceHelper me; |
| 11 | + private static String sharedPrefName; |
| 12 | + private static Context context; |
| 13 | + |
| 14 | + private SharedPreferenceHelper() { |
| 15 | + } |
| 16 | + |
| 17 | + public static SharedPreferenceHelper getInstance(Context cntx) { |
| 18 | + if (me == null) { |
| 19 | + me = new SharedPreferenceHelper(); |
| 20 | + } |
| 21 | + context = cntx; |
| 22 | + sharedPrefName = "APP_KEY_PAIR_VALUE"; |
| 23 | + return me; |
| 24 | + } |
| 25 | + |
| 26 | + public String getValue(String key) { |
| 27 | + SharedPreferences prefs = context.getSharedPreferences(sharedPrefName, MODE_PRIVATE); |
| 28 | + return prefs.getString(key, null); |
| 29 | + } |
| 30 | + |
| 31 | + public boolean getBooleanValue(String key) { |
| 32 | + SharedPreferences prefs = context.getSharedPreferences(sharedPrefName, MODE_PRIVATE); |
| 33 | + return prefs.getBoolean(key, false); |
| 34 | + } |
| 35 | + |
| 36 | + public SharedPreferenceHelper add(String key, String value) { |
| 37 | + try { |
| 38 | + context.getSharedPreferences(sharedPrefName, MODE_PRIVATE).edit().putString(key, value).apply(); |
| 39 | + return me; |
| 40 | + } catch (Exception ex) { |
| 41 | + Log.e("React-Native-Fula", "SharedPrefHandler: AddSharedPref: Exception: " + ex.getMessage(), ex); |
| 42 | + throw ex; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public SharedPreferenceHelper add(String key, boolean value) { |
| 47 | + try { |
| 48 | + context.getSharedPreferences(sharedPrefName, MODE_PRIVATE).edit().putBoolean(key, value).apply(); |
| 49 | + return me; |
| 50 | + } catch (Exception e) { |
| 51 | + Log.e("React-Native-Fula", "SharedPrefHandler: AddSharedPref: Exception: " + e.getMessage(), e); |
| 52 | + throw e; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public SharedPreferenceHelper remove(String key) { |
| 57 | + try { |
| 58 | + context.getSharedPreferences(sharedPrefName, MODE_PRIVATE).edit().remove(key).apply(); |
| 59 | + return me; |
| 60 | + } catch (Exception ex) { |
| 61 | + Log.e("React-Native-Fula", "SharedPrefHandler: AddSharedPref: Exception: " + ex.getMessage(), ex); |
| 62 | + throw ex; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments