SPImplementation cleanup

This commit is contained in:
Milos Kozak 2019-12-26 15:12:12 +01:00
parent ec2a0212f5
commit 895b32d8f3
3 changed files with 31 additions and 30 deletions

View file

@ -7,7 +7,7 @@ import dagger.Binds
import dagger.Module
import dagger.Provides
import info.nightscout.androidaps.utils.sharedPreferences.SP
import info.nightscout.androidaps.utils.sharedPreferences.SPImpl
import info.nightscout.androidaps.utils.sharedPreferences.SPImplementation
import javax.inject.Singleton
@Module(includes = [AppModule.AppBindings::class])
@ -16,7 +16,7 @@ class AppModule {
@Provides
@Singleton
fun provideSharedPreferences(context: Context): SP {
return SPImpl(PreferenceManager.getDefaultSharedPreferences(context))
return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context))
}
@Module

View file

@ -1,34 +1,37 @@
package info.nightscout.androidaps.utils.sharedPreferences
import androidx.annotation.StringRes
/**
* Created by adrian on 2019-12-23.
*/
interface SP {
fun getAll(): Map<String, *>
fun clear()
fun contains(key: String): Boolean
fun contains(resourceId: Int): Boolean
fun remove(resourceID: Int)
fun remove(@StringRes resourceID: Int)
fun remove(key: String)
fun getString(resourceID: Int, defaultValue: String): String
fun getString(@StringRes resourceID: Int, defaultValue: String): String
fun getString(key: String, defaultValue: String): String
fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean
fun getBoolean(@StringRes resourceID: Int, defaultValue: Boolean): Boolean
fun getBoolean(key: String, defaultValue: Boolean): Boolean
fun getDouble(resourceID: Int, defaultValue: Double): Double
fun getDouble(@StringRes resourceID: Int, defaultValue: Double): Double
fun getDouble(key: String, defaultValue: Double): Double
fun getInt(resourceID: Int, defaultValue: Int): Int
fun getInt(@StringRes resourceID: Int, defaultValue: Int): Int
fun getInt(key: String, defaultValue: Int): Int
fun getLong(resourceID: Int, defaultValue: Long): Long
fun getLong(@StringRes resourceID: Int, defaultValue: Long): Long
fun getLong(key: String, defaultValue: Long): Long
fun putBoolean(key: String, value: Boolean)
fun putBoolean(resourceID: Int, value: Boolean)
fun putBoolean(@StringRes resourceID: Int, value: Boolean)
fun putDouble(key: String, value: Double)
fun putLong(key: String, value: Long)
fun putLong(resourceID: Int, value: Long)
fun putLong(@StringRes resourceID: Int, value: Long)
fun putInt(key: String, value: Int)
fun putInt(resourceID: Int, value: Int)
fun incInt(resourceID: Int)
fun putString(resourceID: Int, value: String)
fun putInt(@StringRes resourceID: Int, value: Int)
fun incInt(@StringRes resourceID: Int)
fun putString(@StringRes resourceID: Int, value: String)
fun putString(key: String, value: String)
}

View file

@ -10,7 +10,7 @@ import javax.inject.Singleton
* Created by mike on 17.02.2017.
*/
@Singleton
class SPImpl @Inject constructor(private val sharedPreferences: SharedPreferences) : SP {
class SPImplementation @Inject constructor(private val sharedPreferences: SharedPreferences) : SP {
override fun getAll(): Map<String, *> = sharedPreferences.all
@ -21,16 +21,16 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
override fun contains(resourceId: Int): Boolean = sharedPreferences.contains(MainApp.gs(resourceId))
override fun remove(resourceID: Int) =
sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply()
sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply()
override fun remove(key: String) =
sharedPreferences.edit().remove(key).apply()
sharedPreferences.edit().remove(key).apply()
override fun getString(resourceID: Int, defaultValue: String): String =
sharedPreferences.getString(MainApp.gs(resourceID), defaultValue)
sharedPreferences.getString(MainApp.gs(resourceID), defaultValue) ?: defaultValue
override fun getString(key: String, defaultValue: String): String =
sharedPreferences.getString(key, defaultValue)
sharedPreferences.getString(key, defaultValue) ?: defaultValue
override fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean {
return try {
@ -49,10 +49,10 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
}
override fun getDouble(resourceID: Int, defaultValue: Double): Double =
SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()))
SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()))
override fun getDouble(key: String, defaultValue: Double): Double =
SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString()))
SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString()))
override fun getInt(resourceID: Int, defaultValue: Int): Int {
return try {
@ -89,24 +89,22 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
override fun putBoolean(key: String, value: Boolean) = sharedPreferences.edit().putBoolean(key, value).apply()
override fun putBoolean(resourceID: Int, value: Boolean) =
sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply()
sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply()
override fun putDouble(key: String, value: Double) =
sharedPreferences.edit().putString(key, java.lang.Double.toString(value)).apply()
sharedPreferences.edit().putString(key, java.lang.Double.toString(value)).apply()
override fun putLong(key: String, value: Long) =
sharedPreferences.edit().putLong(key, value).apply()
sharedPreferences.edit().putLong(key, value).apply()
override fun putLong(resourceID: Int, value: Long) =
sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply()
sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply()
override fun putInt(key: String, value: Int) =
sharedPreferences.edit().putInt(key, value).apply()
sharedPreferences.edit().putInt(key, value).apply()
override fun putInt(resourceID: Int, value: Int) =
sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply()
sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply()
override fun incInt(resourceID: Int) {
val value = getInt(resourceID, 0) + 1
@ -114,9 +112,9 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
}
override fun putString(resourceID: Int, value: String) =
sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply()
sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply()
override fun putString(key: String, value: String) =
sharedPreferences.edit().putString(key, value).apply()
sharedPreferences.edit().putString(key, value).apply()
}