Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Last active March 7, 2018 08:50
Show Gist options
  • Save rezaiyan/25465ead2ef103b7af8c321f46da320b to your computer and use it in GitHub Desktop.
Save rezaiyan/25465ead2ef103b7af8c321f46da320b to your computer and use it in GitHub Desktop.
package ir.parvazyab.android.common.cache;
import android.content.SharedPreferences;
import java.util.Set;
import javax.inject.Inject;
/**
* Created by rezaiyan on 24/11/17.
*/
public class SharedPrefsHelper {
static final String PREF_KEY_ACCESS_USER_NAME = "access-username";
static final String PREF_KEY_ACCESS_TOKEN = "access-token";
private static final String PREF_FILE_NAME= "mySharefPref.xml";
static final String PREF_KEY_ACCESS_USER_NAME_SUGGESTION = "access-username_suggestion";
private SharedPreferences mSharedPreferences;
SharedPrefsHelper(Context context) {
mSharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
}
public void put(String key, String value) {
mSharedPreferences.edit().putString(key, value).apply();
}
public void put(String key, int value) {
mSharedPreferences.edit().putInt(key, value).apply();
}
public void put(String key, float value) {
mSharedPreferences.edit().putFloat(key, value).apply();
}
public void put(String key, boolean value) {
mSharedPreferences.edit().putBoolean(key, value).apply();
}
public String get(String key, String defaultValue) {
return mSharedPreferences.getString(key, defaultValue);
}
public Integer get(String key, int defaultValue) {
return mSharedPreferences.getInt(key, defaultValue);
}
public Float get(String key, float defaultValue) {
return mSharedPreferences.getFloat(key, defaultValue);
}
public Boolean get(String key, boolean defaultValue) {
return mSharedPreferences.getBoolean(key, defaultValue);
}
public void put(String key, Set<String> value) {
mSharedPreferences.edit().putStringSet(key, value).apply();
}
public Set<String> get(String key, Set<String> defaultValue) {
return mSharedPreferences.getStringSet(key, defaultValue);
}
public void deleteSavedData() {
mSharedPreferences.edit().clear().apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment