Skip to content

Instantly share code, notes, and snippets.

@recoverrelax
Last active January 4, 2021 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recoverrelax/9a3284150cfbd3ac08be4a204d6b60b8 to your computer and use it in GitHub Desktop.
Save recoverrelax/9a3284150cfbd3ac08be4a204d6b60b8 to your computer and use it in GitHub Desktop.
Base SharedPreferences usage for Kotlin (with dagger2 bonus)
abstract class BaseSharedPreferences(
val preferences: SharedPreferences,
val moshi: Moshi
) {
protected fun get(key: String, default: String): String = preferences.getString(key, default)
protected fun get(key: String, default: Int): Int = preferences.getInt(key, default)
protected fun get(key: String, default: Float): Float = preferences.getFloat(key, default)
protected fun get(key: String, default: Long): Long = preferences.getLong(key, default)
protected fun get(key: String, default: Boolean): Boolean = preferences.getBoolean(key, default)
protected fun put(key: String, value: String): Unit = preferences.editor { putString(key, value) }
protected fun put(key: String, value: Int): Unit = preferences.editor { putInt(key, value) }
protected fun put(key: String, value: Float): Unit = preferences.editor { putFloat(key, value) }
protected fun put(key: String, value: Long): Unit = preferences.editor { putLong(key, value) }
protected fun put(key: String, value: Boolean): Unit = preferences.editor { putBoolean(key, value) }
protected inline fun SharedPreferences.editor(editor: SharedPreferences.Editor.() -> Unit) {
edit().apply { editor(this) }.apply()
}
@Suppress("IMPLICIT_CAST_TO_ANY")
protected inline fun <reified T> getObject(key: String): T? {
val prefs = preferences
val json = prefs.getString(key, Prefs.PREF_DEF_STRING)
return if(json != EMPTY_STRING) moshi.fromJson(T::class.java, json) else null
}
protected inline fun <reified T> putObject(key: String, value: T) {
val prefs = preferences
prefs.edit().apply {
val jsonValue = if(value == null) Prefs.PREF_DEF_STRING else moshi.toJson(T::class.java, value)
putString(key, jsonValue)
}.apply()
}
protected inline fun editMultiple(doStuff: () -> Unit) {
val prefs = preferences
prefs.edit().apply {
doStuff()
}.apply()
}
protected fun hasPreference(key: String) = preferences.contains(key)
}
class PrefsApp(
preferences: SharedPreferences,
moshi: Moshi
): BaseSharedPreferences(preferences, moshi) {
private val FIRST_RUN = "FIRST_RUN"
private val USER_DEVICE_ID = "USER_DEVICE_ID"
private val REALM_KEY = "REALM_KEY"
private val PREFS_KEY = "PREFS_KEY"
var firstRun: Boolean
get() = get(FIRST_RUN, true)
set(value) = put(FIRST_RUN, value)
var realmKey: String
get() = get(REALM_KEY, Prefs.PREF_DEF_STRING)
set(value) = put(REALM_KEY, value)
var prefsKey: String
get() = get(PREFS_KEY, Prefs.PREF_DEF_STRING)
set(value) = put(PREFS_KEY, value)
var deviceId: String
get() = get(USER_DEVICE_ID, Prefs.PREF_DEF_STRING)
set(value) = put(USER_DEVICE_ID, value)
}
@Module
class SharedPreferencesModule {
@Provides
@AppScope
fun providePreferences(appContext: Application): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(appContext)
}
@Provides
@AppScope
fun provideAppPreds(
preferences: SharedPreferences,
moshi: Moshi
): PrefsApp = PrefsApp(preferences, moshi)
@yudikarma
Copy link

where is moshi.toJson ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment