Created
August 9, 2018 13:34
-
-
Save hamen/cbae1762106d29ac4a6c9cd8f8e00d24 to your computer and use it in GitHub Desktop.
KeyValueStorage - How to decouple SharedPreferences on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface KeyValueStorage { | |
fun getAll(): Map<String, *> | |
fun contains(key: String): Boolean | |
fun remove(key: String): Boolean | |
fun getBoolean(key: String, defaultValue: Boolean): Boolean | |
fun putBoolean(key: String, value: Boolean): Boolean | |
fun getFloat(key: String, defaultValue: Float): Float | |
fun putFloat(key: String, value: Float): Boolean | |
fun getLong(key: String, defaultValue: Long): Long | |
fun putLong(key: String, value: Long): Boolean | |
fun getInt(key: String, defaultValue: Int): Int | |
fun putInt(key: String, value: Int): Boolean | |
fun getStringSet(key: String, defaultValue: Set<String>): Set<String> | |
fun putStringSet(key: String, value: Set<String>): Boolean | |
fun getString(key: String, defaultValue: String): String | |
fun putString(key: String, value: String): Boolean | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KeyValueStorageFactory { | |
companion object { | |
fun build( | |
context: Context, | |
storeName: String | |
): KeyValueStorage { | |
val sp = context.getSharedPreferences(storeName, Context.MODE_PRIVATE) | |
return KeyValueStorageSpImpl(sp) | |
} | |
fun build(sharedPreferences: SharedPreferences): KeyValueStorage = KeyValueStorageSpImpl(sharedPreferences) | |
} | |
class KeyValueStorageSpImpl(private val sp: SharedPreferences) : KeyValueStorage { | |
override fun getAll(): Map<String, *> = sp.all | |
override fun contains(key: String) = sp.contains(key) | |
override fun remove(key: String) = sp.edit().remove(key).commit() | |
override fun getBoolean(key: String, defaultValue: Boolean) = sp.getBoolean(key, defaultValue) | |
override fun putBoolean(key: String, value: Boolean) = sp.edit().putBoolean(key, value).commit() | |
override fun getFloat(key: String, defaultValue: Float) = sp.getFloat(key, defaultValue) | |
override fun putFloat(key: String, value: Float) = sp.edit().putFloat(key, value).commit() | |
override fun getLong(key: String, defaultValue: Long) = sp.getLong(key, defaultValue) | |
override fun putLong(key: String, value: Long) = sp.edit().putLong(key, value).commit() | |
override fun getInt(key: String, defaultValue: Int) = sp.getInt(key, defaultValue) | |
override fun putInt(key: String, value: Int) = sp.edit().putInt(key, value).commit() | |
override fun getStringSet(key: String, defaultValue: Set<String>): Set<String> = sp.getStringSet(key, defaultValue) | |
override fun putStringSet(key: String, value: Set<String>) = sp.edit().putStringSet(key, value).commit() | |
override fun getString(key: String, defaultValue: String): String = sp.getString(key, defaultValue) | |
override fun putString(key: String, value: String) = sp.edit().putString(key, value).commit() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment