SPImplementation cleanup
This commit is contained in:
parent
ec2a0212f5
commit
895b32d8f3
|
@ -7,7 +7,7 @@ import dagger.Binds
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import info.nightscout.androidaps.utils.sharedPreferences.SP
|
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
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module(includes = [AppModule.AppBindings::class])
|
@Module(includes = [AppModule.AppBindings::class])
|
||||||
|
@ -16,7 +16,7 @@ class AppModule {
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideSharedPreferences(context: Context): SP {
|
fun provideSharedPreferences(context: Context): SP {
|
||||||
return SPImpl(PreferenceManager.getDefaultSharedPreferences(context))
|
return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
|
|
|
@ -1,34 +1,37 @@
|
||||||
package info.nightscout.androidaps.utils.sharedPreferences
|
package info.nightscout.androidaps.utils.sharedPreferences
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by adrian on 2019-12-23.
|
* Created by adrian on 2019-12-23.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
interface SP {
|
interface SP {
|
||||||
|
|
||||||
fun getAll(): Map<String, *>
|
fun getAll(): Map<String, *>
|
||||||
fun clear()
|
fun clear()
|
||||||
fun contains(key: String): Boolean
|
fun contains(key: String): Boolean
|
||||||
fun contains(resourceId: Int): Boolean
|
fun contains(resourceId: Int): Boolean
|
||||||
fun remove(resourceID: Int)
|
fun remove(@StringRes resourceID: Int)
|
||||||
fun remove(key: String)
|
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 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 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 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 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 getLong(key: String, defaultValue: Long): Long
|
||||||
fun putBoolean(key: String, value: Boolean)
|
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 putDouble(key: String, value: Double)
|
||||||
fun putLong(key: String, value: Long)
|
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(key: String, value: Int)
|
||||||
fun putInt(resourceID: Int, value: Int)
|
fun putInt(@StringRes resourceID: Int, value: Int)
|
||||||
fun incInt(resourceID: Int)
|
fun incInt(@StringRes resourceID: Int)
|
||||||
fun putString(resourceID: Int, value: String)
|
fun putString(@StringRes resourceID: Int, value: String)
|
||||||
fun putString(key: String, value: String)
|
fun putString(key: String, value: String)
|
||||||
}
|
}
|
|
@ -10,7 +10,7 @@ import javax.inject.Singleton
|
||||||
* Created by mike on 17.02.2017.
|
* Created by mike on 17.02.2017.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@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
|
override fun getAll(): Map<String, *> = sharedPreferences.all
|
||||||
|
|
||||||
|
@ -27,10 +27,10 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
|
||||||
sharedPreferences.edit().remove(key).apply()
|
sharedPreferences.edit().remove(key).apply()
|
||||||
|
|
||||||
override fun getString(resourceID: Int, defaultValue: String): String =
|
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 =
|
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 {
|
override fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean {
|
||||||
return try {
|
return try {
|
||||||
|
@ -91,11 +91,9 @@ class SPImpl @Inject constructor(private val sharedPreferences: SharedPreference
|
||||||
override fun putBoolean(resourceID: Int, value: Boolean) =
|
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) =
|
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) =
|
override fun putLong(key: String, value: Long) =
|
||||||
sharedPreferences.edit().putLong(key, value).apply()
|
sharedPreferences.edit().putLong(key, value).apply()
|
||||||
|
|
Loading…
Reference in a new issue