Preference util for Android
매번 빈번하게 사용되는 저장소인 android Preference 유틸리티 이다. 프로젝트에 종속적이지 않으며 단독 사용가능한 유틸리티 클래스
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
object PreferenceHelper {
fun defaultPrefs(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
fun customPrefs(context: Context, name: String): SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)
private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
fun SharedPreferences.remove(key: String) {
edit() { it.remove(key) }
}
/**
* puts a value for the given [key].
*/
operator fun SharedPreferences.set(key: String, value: Any?)
= when (value) {
is String? -> edit { it.putString(key, value) }
is Int -> edit { it.putInt(key, value) }
is Boolean -> edit { it.putBoolean(key, value) }
is Float -> edit { it.putFloat(key, value) }
is Long -> edit { it.putLong(key, value) }
else -> throw UnsupportedOperationException("Not yet implemented")
}
/**
* finds a preference based on the given [key].
* [T] is the type of value
* @param defaultValue optional defaultValue - will take a default defaultValue if it is not specified
*/
inline operator fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T
= when (T::class) {
String::class -> getString(key, defaultValue as? String ?: "") as T
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
Preference에 넣기
PreferenceHelper.defaultPrefs(context)["PREF_NEVER_ASK_PUSH_AGREEMENT"] = true
Preference에 빼기
val isNaverAskPush = PreferenceHelper.defaultPrefs(context)["PREF_NEVER_ASK_PUSH_AGREEMENT",false]
Read other posts