Last active
          March 23, 2020 08:09 
        
      - 
      
- 
        Save KalpeshTalkar/eebb69a3fee4fc8ffcf4eac25757b435 to your computer and use it in GitHub Desktop. 
    A wrapper over shared preferences. *Dependency required: A custom application class which provides global app context. (https://gist.github.com/KalpeshTalkar/c4c7ccf8003c6214c11226b86c565bb7)
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // | |
| // Copyright © 2017 Kalpesh Talkar. All rights reserved. | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| // | |
| // For support: https://gist.github.com/KalpeshTalkar/eebb69a3fee4fc8ffcf4eac25757b435 | |
| // | |
| import android.content.Context; | |
| import android.content.SharedPreferences; | |
| import android.util.Log; | |
| /** | |
| * Created by Kalpesh on 17/08/17. | |
| */ | |
| /// A wrapper/abstraction layer over SharedPreferences which features storage/retrieving data from/to SharedPreferences | |
| public class LocalStorage { | |
| public static String AUTH_TOKEN_KEY = "X-AUTH-TOKEN"; | |
| /// Shared preferences name | |
| private static String APP_PREFS = "AppPrefs"; | |
| private static String KEY_COUPON_CODE = "couponCode"; | |
| private static String KEY_COUPON_AMT = "couponAmt"; | |
| /// Get instance of shared preferences in private mode | |
| private static SharedPreferences getSharedPrefs() { | |
| return App.sharedContext().getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); | |
| } | |
| /// Get instance of Editor | |
| private static SharedPreferences.Editor getEditor() { | |
| return getSharedPrefs().edit(); | |
| } | |
| /// Get locally stored value for specific key in the desired data type | |
| /// The value can be null if it does not exist or cannot be casted to the desired data type | |
| public static <T extends Object> T getValue(String key, Object defaultValue, Class<T> type) { | |
| Object value = null; | |
| SharedPreferences sharedPrefs = getSharedPrefs(); | |
| if (type.equals(String.class)) { // String | |
| String defVal = ""; | |
| if (defaultValue != null && defaultValue instanceof String) { | |
| defVal = (String) defaultValue; | |
| } | |
| try { | |
| value = sharedPrefs.getString(key, defVal); | |
| } catch (Exception e) { | |
| Log.e("LocalStorage", "Error getting value for key: " + key + "\nError: " + e.getMessage()); | |
| } | |
| } else if (type.equals(Integer.class)) { // Integer | |
| int defVal = 0; | |
| if (defaultValue != null && defaultValue instanceof Integer) { | |
| defVal = (int) defaultValue; | |
| } | |
| try { | |
| value = sharedPrefs.getInt(key, defVal); | |
| } catch (Exception e) { | |
| Log.e("LocalStorage", "Error getting value for key: " + key + "\nError: " + e.getMessage()); | |
| } | |
| } else if (type.equals(Float.class)) { // Float | |
| float defVal = 0; | |
| if (defaultValue != null && defaultValue instanceof Float) { | |
| defVal = (float) defaultValue; | |
| } | |
| try { | |
| value = sharedPrefs.getFloat(key, defVal); | |
| } catch (Exception e) { | |
| Log.e("LocalStorage", "Error getting value for key: " + key + "\nError: " + e.getMessage()); | |
| } | |
| } else if (type.equals(Boolean.class)) { // Boolean | |
| boolean defVal = false; | |
| if (defaultValue != null && defaultValue instanceof Boolean) { | |
| defVal = (boolean) defaultValue; | |
| } | |
| try { | |
| value = sharedPrefs.getBoolean(key, defVal); | |
| } catch (Exception e) { | |
| Log.e("LocalStorage", "Error getting value for key: " + key + "\nError: " + e.getMessage()); | |
| } | |
| } else if (type.equals(Long.class)) { // Long | |
| long defVal = 0; | |
| if (defaultValue != null && defaultValue instanceof Long) { | |
| defVal = (long) defaultValue; | |
| } | |
| try { | |
| value = sharedPrefs.getLong(key, defVal); | |
| } catch (Exception e) { | |
| Log.e("LocalStorage", "Error getting value for key: " + key + "\nError: " + e.getMessage()); | |
| } | |
| } else if (type.equals(Float.class)) { // Float | |
| float defVal = 0; | |
| if (defaultValue != null && defaultValue instanceof Float) { | |
| defVal = (float) defaultValue; | |
| } | |
| try { | |
| value = sharedPrefs.getFloat(key, defVal); | |
| } catch (Exception e) { | |
| Log.e("LocalStorage", "Error getting value for key: " + key + "\nError: " + e.getMessage()); | |
| } | |
| } | |
| return type.cast(value); | |
| } | |
| /// Store data locally for specific key | |
| public static void putValue(Object value, String key) { | |
| SharedPreferences.Editor editor = getEditor(); | |
| if (value instanceof String) { // String | |
| editor.putString(key, (String) value); | |
| } else if (value instanceof Integer) { // Integer | |
| editor.putInt(key, (int) value); | |
| } else if (value instanceof Float) { // Float | |
| editor.putFloat(key, (float) value); | |
| } else if (value instanceof Boolean) { // Boolean | |
| editor.putBoolean(key, (boolean) value); | |
| } else if (value instanceof Long) { // Long | |
| editor.putLong(key, (long) value); | |
| } else if (value instanceof Float) { // Float | |
| editor.putFloat(key, (float) value); | |
| } | |
| editor.commit(); | |
| } | |
| public static void saveToken(String token) { | |
| if (token == null) { | |
| token = ""; | |
| } | |
| putValue(token, AUTH_TOKEN_KEY); | |
| } | |
| public static String getToken() { | |
| return getValue(AUTH_TOKEN_KEY, "", String.class); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Usage:
Storing a value:
LocalStorage.putValue(true, "isLoggedIn");Fetching value:
boolean isLoggedIn = LocalStorage.getValue("isLoggedIn", false, Boolean.class);