Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class PreferenceHelper(private val mContext: Context = App.applicationContext()) {
fun putInt(key: String?, value: Int): PreferenceHelper {
val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext)
val edit = preferences.edit()
edit.putInt(key, value)
edit.apply()
return this
}
fun putBoolean(key: String?, `val`: Boolean): PreferenceHelper {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
val edit = preferences.edit()
edit.putBoolean(key, `val`)
edit.apply()
return this
}
fun putString(key: String?, `val`: String?): PreferenceHelper {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
val edit = preferences.edit()
edit.putString(key, `val`)
edit.apply()
return this
}
fun putFloat(key: String?, `val`: Float): PreferenceHelper {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
val edit = preferences.edit()
edit.putFloat(key, `val`)
edit.apply()
return this
}
fun putLong(key: String?, `val`: Long): PreferenceHelper {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
val edit = preferences.edit()
edit.putLong(key, `val`)
edit.apply()
return this
}
fun getLong(key: String?, _default: Long): Long {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.getLong(key, _default)
}
fun getFloat(key: String?, _default: Float): Float {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.getFloat(key, _default)
}
fun getString(key: String?, _default: String): String {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.getString(key, _default)?:_default
}
fun getString(key: String?): String {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.getString(key, "0")?:"0"
}
fun getInt(key: String?, _default: Int): Int {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.getInt(key, _default)
}
fun getBoolean(key: String?, _default: Boolean): Boolean {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.getBoolean(key, _default)
}
fun hasKey(key: String?): Boolean {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
return preferences.contains(key)
}
fun clearPreferences(): PreferenceHelper {
val preferences: SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(mContext)
preferences.edit().clear().apply()
return this
}
companion object {
const val OTP = "OTP"
const val PERMISSIONS_REQUEST_STORAGE = 0
const val PERMISSIONS_REQUEST_CAMERA = 1
const val USER_PHONE_STRING = "mobile"
const val USER_SESSION_STRING = "session"
const val USER_ID_STRING = "userId"
const val COUNTRY_CODE_STRING = "countryCode"
const val USER_NAME_STRING = "userName"
const val USER_TABLE_STRING = "device_tble_id"
const val USER_IMG_STRING = "userIMG"
const val IS_LOGGED_IN_BOOL = "isloggeding"
const val IS_FROM_TO_DASHBOARD = "isfromdashboard"
fun with(context: Context): PreferenceHelper {
return PreferenceHelper(context)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment