updating the debug features to use the datastore api
- we're using the blocking api due to only needing to suspend in the debug variant, we may look to change this in the future
This commit is contained in:
parent
90d00b96b7
commit
05ce1414b9
@ -17,50 +17,62 @@
|
|||||||
package im.vector.app.features.debug.features
|
package im.vector.app.features.debug.features
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import androidx.datastore.core.DataStore
|
||||||
|
import androidx.datastore.preferences.core.MutablePreferences
|
||||||
|
import androidx.datastore.preferences.core.Preferences
|
||||||
|
import androidx.datastore.preferences.core.edit
|
||||||
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
import im.vector.app.features.DefaultVectorFeatures
|
import im.vector.app.features.DefaultVectorFeatures
|
||||||
import im.vector.app.features.VectorFeatures
|
import im.vector.app.features.VectorFeatures
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "debug_features")
|
||||||
|
|
||||||
class DebugVectorFeatures(
|
class DebugVectorFeatures(
|
||||||
context: Context,
|
context: Context,
|
||||||
private val vectorFeatures: DefaultVectorFeatures
|
private val vectorFeatures: DefaultVectorFeatures
|
||||||
) : VectorFeatures {
|
) : VectorFeatures {
|
||||||
|
|
||||||
private val featurePrefs = context.getSharedPreferences("debug-features", Context.MODE_PRIVATE)
|
private val dataStore = context.dataStore
|
||||||
|
|
||||||
override fun loginVersion(): VectorFeatures.LoginVersion {
|
override fun loginVersion(): VectorFeatures.LoginVersion {
|
||||||
return featurePrefs.readEnum<VectorFeatures.LoginVersion>() ?: vectorFeatures.loginVersion()
|
return readPreferences().getEnum<VectorFeatures.LoginVersion>() ?: vectorFeatures.loginVersion()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Enum<T>> hasEnumOverride(type: KClass<T>): Boolean {
|
fun <T : Enum<T>> hasEnumOverride(type: KClass<T>) = readPreferences().containsEnum(type)
|
||||||
return featurePrefs.containsEnum(type)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <T : Enum<T>> overrideEnum(value: T?, type: KClass<T>) {
|
fun <T : Enum<T>> overrideEnum(value: T?, type: KClass<T>) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
featurePrefs.removeEnum(type)
|
updatePreferences { it.removeEnum(type) }
|
||||||
} else {
|
} else {
|
||||||
featurePrefs.putEnum(value, type)
|
updatePreferences { it.putEnum(value, type) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun readPreferences() = runBlocking { dataStore.data.first() }
|
||||||
|
|
||||||
|
private fun updatePreferences(block: (MutablePreferences) -> Unit) = runBlocking {
|
||||||
|
dataStore.edit { block(it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Enum<T>> SharedPreferences.removeEnum(type: KClass<T>) {
|
private fun <T : Enum<T>> MutablePreferences.removeEnum(type: KClass<T>) {
|
||||||
edit().remove("enum-${type.simpleName}").apply()
|
remove(enumPreferencesKey(type))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Enum<T>> SharedPreferences.containsEnum(type: KClass<T>): Boolean {
|
private fun <T : Enum<T>> Preferences.containsEnum(type: KClass<T>) = contains(enumPreferencesKey(type))
|
||||||
return contains("enum-${type.simpleName}")
|
|
||||||
|
private fun <T : Enum<T>> MutablePreferences.putEnum(value: T, type: KClass<T>) {
|
||||||
|
this[enumPreferencesKey(type)] = value.name
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified T : Enum<T>> SharedPreferences.readEnum(): T? {
|
private inline fun <reified T : Enum<T>> Preferences.getEnum(): T? {
|
||||||
val value = T::class.simpleName
|
return get(enumPreferencesKey<T>())?.let { enumValueOf<T>(it) }
|
||||||
return getString("enum-$value", null)?.let { enumValueOf<T>(it) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Enum<T>> SharedPreferences.putEnum(value: T, type: KClass<T>) {
|
private inline fun <reified T : Enum<T>> enumPreferencesKey() = enumPreferencesKey(T::class)
|
||||||
edit()
|
|
||||||
.putString("enum-${type.simpleName}", value.name)
|
private fun <T : Enum<T>> enumPreferencesKey(type: KClass<T>) = stringPreferencesKey("enum-${type.simpleName}")
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user