Change FeatureFlagService.isFeatureEnabled return value from Boolean to Flow<Boolean>

This commit is contained in:
Benoit Marty 2023-10-31 16:48:24 +01:00 committed by Benoit Marty
parent 5f85707235
commit f554fb8dcc
10 changed files with 62 additions and 25 deletions

View file

@ -23,5 +23,6 @@ android {
dependencies {
api(projects.libraries.featureflag.api)
implementation(libs.coroutines.core)
}
}

View file

@ -18,19 +18,27 @@ package io.element.android.libraries.featureflag.test
import io.element.android.libraries.featureflag.api.Feature
import io.element.android.libraries.featureflag.api.FeatureFlagService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
class FakeFeatureFlagService(
initialState: Map<String, Boolean> = emptyMap()
) : FeatureFlagService {
private val enabledFeatures = HashMap(initialState)
private val enabledFeatures = initialState
.map {
it.key to MutableStateFlow(it.value)
}
.toMap()
.toMutableMap()
override suspend fun setFeatureEnabled(feature: Feature, enabled: Boolean): Boolean {
enabledFeatures[feature.key] = enabled
val flow = enabledFeatures.getOrPut(feature.key) { MutableStateFlow(enabled) }
flow.emit(enabled)
return true
}
override suspend fun isFeatureEnabled(feature: Feature): Boolean {
return enabledFeatures[feature.key] ?: false
override fun isFeatureEnabledFlow(feature: Feature): Flow<Boolean> {
return enabledFeatures.getOrPut(feature.key) { MutableStateFlow(feature.defaultValue) }
}
}