Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANDROID-14297 Get Tweaks as suspend functions and non null #39

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading