SPImplementation cleanup
This commit is contained in:
parent
ec2a0212f5
commit
895b32d8f3
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
||||
|
@ -27,10 +27,10 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
|
|||
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 {
|
||||
|
@ -91,11 +91,9 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
|
|||
override fun putBoolean(resourceID: Int, value: Boolean) =
|
||||
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()
|
||||
|
||||
|
||||
override fun putLong(key: String, value: Long) =
|
||||
sharedPreferences.edit().putLong(key, value).apply()
|
||||
|
Loading…
Reference in a new issue