Skip to content

Commit

Permalink
ANDROID-14297 Get Tweaks as suspend functions and non null (#39)
Browse files Browse the repository at this point in the history
* Get Tweaks as suspend functions and non null

* Provide no-op implementation

* Add Tweaks Contract

* Format file
  • Loading branch information
jdelga authored Feb 9, 2024
1 parent dcf63c6 commit 4647e78
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
18 changes: 14 additions & 4 deletions library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> getTweakValue(key: String): Flow<T?> = tweaksBusinessLogic.getValue(key)
override fun <T> getTweakValue(key: String): Flow<T?> = tweaksBusinessLogic.getValue(key)

open suspend fun <T> setTweakValue(key: String, value: T) {
override fun <T> getTweakValue(key: String, defaultValue: T): Flow<T> =
getTweakValue<T>(key).map { it ?: defaultValue }

override suspend fun <T> getTweak(key: String): T? = getTweakValue<T>(key).firstOrNull()

override suspend fun <T> getTweak(key: String, defaultValue: T): T =
getTweak(key) ?: defaultValue

override suspend fun <T> setTweakValue(key: String, value: T) {
tweaksBusinessLogic.setValue(key, value)
}

open suspend fun clearValue(key: String) {
override suspend fun clearValue(key: String) {
tweaksBusinessLogic.clearValue(key)
}

Expand Down
21 changes: 21 additions & 0 deletions library/src/main/java/com/telefonica/tweaks/TweaksContract.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.telefonica.tweaks

import kotlinx.coroutines.flow.Flow


interface TweaksContract {

fun <T> getTweakValue(key: String): Flow<T?>

fun <T> getTweakValue(key: String, defaultValue: T): Flow<T>

suspend fun <T> getTweak(key: String): T?

suspend fun <T> getTweak(key: String, defaultValue: T): T

suspend fun <T> setTweakValue(key: String, value: T)

suspend fun clearValue(key: String)


}
19 changes: 15 additions & 4 deletions library/src/noop/java/com/telefonica/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,39 @@ 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<String, Editable<*>> = mutableMapOf()

@Suppress("UNCHECKED_CAST")
open fun <T> getTweakValue(key: String): Flow<T?> {
override fun <T> getTweakValue(key: String): Flow<T?> {
val entry= keyToEntryValueMap[key] as TweakEntry
return getTweakValue(entry)
}

override fun <T> getTweakValue(key: String, defaultValue: T): Flow<T> =
getTweakValue<T>(key).map { it ?: defaultValue }

override suspend fun <T> getTweak(key: String): T? =
getTweakValue<T>(key).firstOrNull()

override suspend fun <T> getTweak(key: String, defaultValue: T): T =
getTweak(key) ?: defaultValue

@Suppress("UNCHECKED_CAST")
private fun <T> getTweakValue(entry: TweakEntry): Flow<T?> = when (entry) {
is ReadOnly<*> -> (entry as ReadOnly<T>).value
is Editable<*> -> (entry as Editable<T>).defaultValue ?: flowOf()
else -> flowOf()
}

open suspend fun <T> setTweakValue(key: String, value: T) {}
override suspend fun <T> 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<Editable<*>> = tweaksGraph.categories
Expand Down

0 comments on commit 4647e78

Please sign in to comment.