Created
May 20, 2014 05:56
-
-
Save neilbantoc/89eeebf9b2175c50bc65 to your computer and use it in GitHub Desktop.
Base class for data manager pattern
This file contains 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
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.content.SharedPreferences.Editor; | |
import android.util.Log; | |
import com.stratpoint.starstrak.StarstrakApplication; | |
public class BaseDataManager { | |
private static final String TAG = BaseDataManager.class.getSimpleName(); | |
protected static Context context = ; | |
protected static HashMap<String, SharedPreferences> preferenceCahe = new HashMap<String, SharedPreferences>(); | |
protected static SharedPreferences getSharedPreferences(String name){ | |
if (!preferenceCahe.containsKey(name)) { | |
preferenceCahe.put(name, context.getSharedPreferences(name, Context.MODE_PRIVATE)); | |
} | |
return preferenceCahe.get(name); | |
} | |
protected static Editor editSharedPreferences(String name){ | |
return getSharedPreferences(name).edit(); | |
} | |
/** | |
* Convenience method for determining if shared preference file contains a specific key | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @return true or false | |
*/ | |
protected static boolean hasKey(String name, String key){ | |
return getSharedPreferences(name).contains(key); | |
} | |
/** | |
* Convenience method for getting a string value from a specified shared preference file | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @param defValue value to be used in case mapping does not exist | |
* @return value for key or null if does not exist | |
*/ | |
protected static String getString(String name, String key, String defValue){ | |
return getSharedPreferences(name).getString(key, defValue); | |
} | |
/** | |
* Convenience method for saving a string to a specific shared preference file using specified key. | |
* | |
* Useful for quick commits where you are to save only one value. For saving multiple values, use | |
* an editor instead: | |
* | |
* Editor editor = editSharedPreferences(name); | |
* editor.putString(key, value); | |
* editor.putBoolean(key, value); | |
* editor.putInt(key, value); | |
* editor.commit(); | |
* | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @param value value to be saved | |
*/ | |
protected static void saveString(String name, String key, String value){ | |
getSharedPreferences(name).edit().putString(key, value).commit(); | |
} | |
/** | |
* Convenience method for getting a boolean value from a specified shared preference file | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @return boolean value, or false if doesn't exist | |
*/ | |
protected static boolean getBoolean(String name, String key, boolean defValue){ | |
return getSharedPreferences(name).getBoolean(key, defValue); | |
} | |
/** | |
* Convenience method for saving a boolean to a specific shared preference file using specified key. | |
* | |
* Useful for quick commits where you are to save only one value. For saving multiple values, use | |
* an editor instead: | |
* | |
* Editor editor = editSharedPreferences(name); | |
* editor.putString(key, value); | |
* editor.putBoolean(key, value); | |
* editor.putInt(key, value); | |
* editor.commit(); | |
* | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @param value value to be saved | |
*/ | |
protected static void saveBoolean(String name, String key, boolean value){ | |
getSharedPreferences(name).edit().putBoolean(key, value).commit(); | |
} | |
/** | |
* Convenience method for getting an integer value from a specified shared preference file | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @return boolean value, or false if doesn't exist | |
*/ | |
protected static int getInt(String name, String key, int defValue){ | |
return getSharedPreferences(name).getInt(key, defValue); | |
} | |
/** | |
* Convenience method for saving an integer to a specific shared preference file using specified key. | |
* | |
* Useful for quick commits where you are to save only one value. For saving multiple values, use | |
* an editor instead: | |
* | |
* Editor editor = editSharedPreferences(name); | |
* editor.putString(key, value); | |
* editor.putBoolean(key, value); | |
* editor.putInt(key, value); | |
* editor.commit(); | |
* | |
* @param name name of the shared preference file | |
* @param key key used to map string in shared preference file | |
* @param value value to be saved | |
*/ | |
protected static void saveInt(String name, String key, int value){ | |
getSharedPreferences(name).edit().putInt(key, value).commit(); | |
} | |
/** | |
* Print out the contents of the specified preferences file to the logcat. Useful for debugging. | |
* @param name of preference file | |
*/ | |
protected static void printContents(String name){ | |
SharedPreferences preferences = getSharedPreferences(name); | |
Map<String, ?> mappings = preferences.getAll(); | |
Set<String> keys = mappings.keySet(); | |
Log.i(TAG, "Printing contents of shared preference: " + name); | |
for (String key: keys){ | |
Log.d(TAG, "[" + key + "]" + ": " + mappings.get(key).toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment