-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureFlippingManager.kt
More file actions
82 lines (71 loc) · 3.02 KB
/
FeatureFlippingManager.kt
File metadata and controls
82 lines (71 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.xpeho.xpeapp.domain
import androidx.annotation.VisibleForTesting
import com.google.firebase.FirebaseException
import com.xpeho.xpeapp.BuildConfig
import com.xpeho.xpeapp.data.FeatureFlippingEnum
import com.xpeho.xpeapp.data.service.FirebaseService
import com.xpeho.xpeapp.utils.CrashlyticsUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.io.IOException
class FeatureFlippingManager(
val firebaseService: FirebaseService
) {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
private val _featuresState: MutableStateFlow<FeatureFlippingState> = MutableStateFlow(FeatureFlippingState.LOADING)
init {
CoroutineScope(Dispatchers.IO).launch {
fetchData()
}
}
fun getState() = _featuresState
fun isFeatureEnabled(feature: FeatureFlippingEnum): Boolean {
val currentState = _featuresState.value
return if (currentState is FeatureFlippingState.SUCCESS) {
currentState.featureEnabled[feature] ?: false
} else {
false
}
}
fun update() {
CoroutineScope(Dispatchers.IO).launch {
fetchData()
}
}
private suspend fun fetchData() {
CrashlyticsUtils.logEvent("FeatureFlipping: " +
"Récupération des feature flags")
val featureFlippingList = try {
firebaseService.fetchFeatureFlipping()
} catch (e: IOException) {
CrashlyticsUtils.logEvent("FeatureFlipping: Erreur réseau")
CrashlyticsUtils.recordException(e)
_featuresState.value = FeatureFlippingState.ERROR("Network error: ${e.message}")
return
} catch (e: FirebaseException) {
CrashlyticsUtils.logEvent("FeatureFlipping: Erreur Firebase")
CrashlyticsUtils.recordException(e)
_featuresState.value = FeatureFlippingState.ERROR("Firebase error: ${e.message}")
return
}
val featureEnabled = mutableMapOf<FeatureFlippingEnum, Boolean>()
for (feature in featureFlippingList) {
val enumOfFeature = FeatureFlippingEnum.entries.find { it.value == feature.id }
if (enumOfFeature != null) {
featureEnabled[enumOfFeature] =
if (BuildConfig.ENVIRONMENT == "prod") feature.prodEnabled else feature.uatEnabled
}
}
CrashlyticsUtils.logEvent("FeatureFlipping: Configuration " +
"chargée avec succès (${featureEnabled.size} features)")
CrashlyticsUtils.setCustomKey("feature_flags_loaded", featureEnabled.size.toString())
_featuresState.value = FeatureFlippingState.SUCCESS(featureEnabled.toMap())
}
}
sealed interface FeatureFlippingState {
object LOADING : FeatureFlippingState
data class ERROR(val error: String) : FeatureFlippingState
data class SUCCESS(val featureEnabled: Map<FeatureFlippingEnum, Boolean>) : FeatureFlippingState
}