interfaces: Extend SP with edit block like androidx.core.content.edit

Signed-off-by: Carlos Rafael Giani <crg7475@mailbox.org>
This commit is contained in:
Carlos Rafael Giani 2021-11-28 22:00:22 +01:00
parent 8a3679a3c7
commit 6ef9b0c135
2 changed files with 97 additions and 0 deletions

View file

@ -7,6 +7,45 @@ import androidx.annotation.StringRes
*/ */
interface SP { interface SP {
// Using a helper Editor interface to distinguish its
// methods from SP's. The latter always run apply().
// The whole point of the edit() function below is to
// _avoid_ unnecessary apply() / commit() calls, so
// we cannot use SP's put* methods in edit().
interface Editor {
fun clear()
fun remove(@StringRes resourceID: Int)
fun remove(key: String)
fun putBoolean(key: String, value: Boolean)
fun putBoolean(@StringRes resourceID: Int, value: Boolean)
fun putDouble(key: String, value: Double)
fun putDouble(@StringRes resourceID: Int, value: Double)
fun putLong(key: String, value: Long)
fun putLong(@StringRes resourceID: Int, value: Long)
fun putInt(key: String, value: Int)
fun putInt(@StringRes resourceID: Int, value: Int)
fun putString(key: String, value: String)
fun putString(@StringRes resourceID: Int, value: String)
}
/**
* Allows for editing shared preferences in a scoped manner.
*
* This works just the same way as the androidx.core.content.edit
* extension does. An [Editor] instance is created and used as
* the receiver of [block]. When the block is done, either
* the shared preferences commit or apply functions are called,
* depending on the value of [commit].
*
* Example:
*
* sp.edit(commit = false) {
* putString("my-key", "abc123")
* }
*/
fun edit(commit: Boolean = false, block: Editor.() -> Unit)
fun getAll(): Map<String, *> fun getAll(): Map<String, *>
fun clear() fun clear()

View file

@ -1,7 +1,9 @@
package info.nightscout.shared.sharedPreferences package info.nightscout.shared.sharedPreferences
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.annotation.StringRes
import info.nightscout.shared.SafeParse import info.nightscout.shared.SafeParse
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -12,6 +14,62 @@ class SPImplementation @Inject constructor(
private val context: Context private val context: Context
) : SP { ) : SP {
@SuppressLint("ApplySharedPref")
override fun edit(commit: Boolean, block: SP.Editor.() -> Unit) {
val spEdit = sharedPreferences.edit()
val edit = object : SP.Editor {
override fun clear() {
spEdit.clear()
}
override fun remove(@StringRes resourceID: Int) {
spEdit.remove(context.getString(resourceID))
}
override fun remove(key: String) {
spEdit.remove(key)
}
override fun putBoolean(key: String, value: Boolean) {
spEdit.putBoolean(key, value)
}
override fun putBoolean(@StringRes resourceID: Int, value: Boolean) {
spEdit.putBoolean(context.getString(resourceID), value)
}
override fun putDouble(key: String, value: Double) {
spEdit.putString(key, value.toString())
}
override fun putDouble(@StringRes resourceID: Int, value: Double) {
spEdit.putString(context.getString(resourceID), value.toString())
}
override fun putLong(key: String, value: Long) {
spEdit.putLong(key, value)
}
override fun putLong(@StringRes resourceID: Int, value: Long) {
spEdit.putLong(context.getString(resourceID), value)
}
override fun putInt(key: String, value: Int) {
spEdit.putInt(key, value)
}
override fun putInt(@StringRes resourceID: Int, value: Int) {
spEdit.putInt(context.getString(resourceID), value)
}
override fun putString(key: String, value: String) {
spEdit.putString(key, value)
}
override fun putString(@StringRes resourceID: Int, value: String) {
spEdit.putString(context.getString(resourceID), value)
}
}
block(edit)
if (commit)
spEdit.commit()
else
spEdit.apply()
}
override fun getAll(): Map<String, *> = sharedPreferences.all override fun getAll(): Map<String, *> = sharedPreferences.all
override fun clear() = sharedPreferences.edit().clear().apply() override fun clear() = sharedPreferences.edit().clear().apply()