diff --git a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt index 322745b..6ea00b8 100644 --- a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt +++ b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt @@ -29,21 +29,31 @@ import com.telefonica.tweaks.domain.TweaksGraph import com.telefonica.tweaks.ui.TweaksCategoryScreen import com.telefonica.tweaks.ui.TweaksScreen import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.map import javax.inject.Inject -open class Tweaks { +open class Tweaks : TweaksContract { @Inject internal lateinit var tweaksBusinessLogic: TweaksBusinessLogic - open fun getTweakValue(key: String): Flow = tweaksBusinessLogic.getValue(key) + override fun getTweakValue(key: String): Flow = tweaksBusinessLogic.getValue(key) - open suspend fun setTweakValue(key: String, value: T) { + override fun getTweakValue(key: String, defaultValue: T): Flow = + getTweakValue(key).map { it ?: defaultValue } + + override suspend fun getTweak(key: String): T? = getTweakValue(key).firstOrNull() + + override suspend fun getTweak(key: String, defaultValue: T): T = + getTweak(key) ?: defaultValue + + override suspend fun setTweakValue(key: String, value: T) { tweaksBusinessLogic.setValue(key, value) } - open suspend fun clearValue(key: String) { + override suspend fun clearValue(key: String) { tweaksBusinessLogic.clearValue(key) } diff --git a/library/src/main/java/com/telefonica/tweaks/TweaksContract.kt b/library/src/main/java/com/telefonica/tweaks/TweaksContract.kt new file mode 100644 index 0000000..4e0ad17 --- /dev/null +++ b/library/src/main/java/com/telefonica/tweaks/TweaksContract.kt @@ -0,0 +1,21 @@ +package com.telefonica.tweaks + +import kotlinx.coroutines.flow.Flow + + +interface TweaksContract { + + fun getTweakValue(key: String): Flow + + fun getTweakValue(key: String, defaultValue: T): Flow + + suspend fun getTweak(key: String): T? + + suspend fun getTweak(key: String, defaultValue: T): T + + suspend fun setTweakValue(key: String, value: T) + + suspend fun clearValue(key: String) + + +} \ No newline at end of file diff --git a/library/src/noop/java/com/telefonica/tweaks/Tweaks.kt b/library/src/noop/java/com/telefonica/tweaks/Tweaks.kt index 7f102c5..accdb28 100644 --- a/library/src/noop/java/com/telefonica/tweaks/Tweaks.kt +++ b/library/src/noop/java/com/telefonica/tweaks/Tweaks.kt @@ -6,18 +6,29 @@ import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import com.telefonica.tweaks.domain.* import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map -open class Tweaks { +open class Tweaks : TweaksContract { private val keyToEntryValueMap: MutableMap> = mutableMapOf() @Suppress("UNCHECKED_CAST") - open fun getTweakValue(key: String): Flow { + override fun getTweakValue(key: String): Flow { val entry= keyToEntryValueMap[key] as TweakEntry return getTweakValue(entry) } + override fun getTweakValue(key: String, defaultValue: T): Flow = + getTweakValue(key).map { it ?: defaultValue } + + override suspend fun getTweak(key: String): T? = + getTweakValue(key).firstOrNull() + + override suspend fun getTweak(key: String, defaultValue: T): T = + getTweak(key) ?: defaultValue + @Suppress("UNCHECKED_CAST") private fun getTweakValue(entry: TweakEntry): Flow = when (entry) { is ReadOnly<*> -> (entry as ReadOnly).value @@ -25,9 +36,9 @@ open class Tweaks { else -> flowOf() } - open suspend fun setTweakValue(key: String, value: T) {} + override suspend fun setTweakValue(key: String, value: T) {} - open suspend fun clearValue(key: String) {} + override suspend fun clearValue(key: String) {} private fun initialize(tweaksGraph: TweaksGraph) { val allEntries: List> = tweaksGraph.categories