Thursday, April 11, 2024

SharedPref



import android.content.Context
import android.content.SharedPreferences

class SharedPref(context: Context) {

private val preferences: SharedPreferences = context.getSharedPreferences("APP_PREF", Context.MODE_PRIVATE)

fun saveString(key: String, value: String) {
preferences.edit().putString(key, value).apply()
}

fun getString(key: String, defaultValue: String): String {
return preferences.getString(key, defaultValue) ?: defaultValue
}

fun saveInt(key: String, value: Int) {
preferences.edit().putInt(key, value).apply()
}

fun getInt(key: String, defaultValue: Int): Int {
return preferences.getInt(key, defaultValue)
}

fun saveBoolean(key: String, value: Boolean) {
preferences.edit().putBoolean(key, value).apply()
}

fun getBoolean(key: String, defaultValue: Boolean): Boolean {
return preferences.getBoolean(key, defaultValue)
}

fun saveFloat(key: String, value: Float) {
preferences.edit().putFloat(key, value).apply()
}

fun getFloat(key: String, defaultValue: Float): Float {
return preferences.getFloat(key, defaultValue)
}

fun saveLong(key: String, value: Long) {
preferences.edit().putLong(key, value).apply()
}

fun getLong(key: String, defaultValue: Long): Long {
return preferences.getLong(key, defaultValue)
}

fun contains(key: String): Boolean {
return preferences.contains(key)
}

fun remove(key: String) {
preferences.edit().remove(key).apply()
}

fun clear() {
preferences.edit().clear().apply()
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home