@@ -19,7 +19,8 @@ import com.hadilq.guidomia.database.api.CarDataEntityCommand
19
19
import com.hadilq.guidomia.database.api.GetCarEntityCommand
20
20
import com.hadilq.guidomia.database.api.GetCarEntityCommandResult
21
21
import com.hadilq.guidomia.featureflags.api.CommandExecutor
22
- import com.hadilq.guidomia.featureflags.api.available
22
+ import com.hadilq.guidomia.featureflags.api.FeatureFlag
23
+ import com.hadilq.guidomia.featureflags.api.featureFlag
23
24
import com.hadilq.guidomia.guidomia.impl.data.mapper.CarDatabaseMapper
24
25
import com.hadilq.guidomia.guidomia.impl.domain.entity.CarEntity
25
26
import javax.inject.Inject
@@ -29,22 +30,32 @@ class CarDatabaseDataSource @Inject constructor(
29
30
private val mapper : CarDatabaseMapper ,
30
31
) {
31
32
32
- private var command: CarDataEntityCommand ? = null
33
+ private var command: FeatureFlag < CarDataEntityCommand > ? = null
33
34
34
- suspend fun availableCommand (): Boolean =
35
- if (command != null ) {
36
- true
37
- } else {
38
- command = executor.available(GetCarEntityCommand (), GetCarEntityCommandResult ::class )?.result
39
- command != null
40
- }
35
+ suspend fun featureFlag (): Boolean = when (fetchFlag()) {
36
+ is FeatureFlag .On -> true
37
+ is FeatureFlag .Off -> false
38
+ }
41
39
42
- suspend fun isEmpty (): Boolean = command?.isEmpty() ? : true
40
+ suspend fun isEmpty (): Boolean = when (val flag = fetchFlag()) {
41
+ is FeatureFlag .On -> flag.value.isEmpty()
42
+ is FeatureFlag .Off -> true
43
+ }
43
44
44
- suspend fun fetchCars (): List <CarEntity > =
45
- command?.getAll()?.map { mapper.map(it) } ? : emptyList()
45
+ suspend fun fetchCars (): List <CarEntity > = when (val flag = fetchFlag()) {
46
+ is FeatureFlag .On -> flag.value.getAll().map { mapper.map(it) }
47
+ is FeatureFlag .Off -> emptyList()
48
+ }
46
49
47
50
suspend fun save (cars : List <CarEntity >) {
48
- command?.insertAll(cars.map { mapper.map(it) })
51
+ when (val flag = fetchFlag()) {
52
+ is FeatureFlag .On -> flag.value.insertAll(cars.map { mapper.map(it) })
53
+ }
54
+ }
55
+
56
+ private suspend fun fetchFlag (): FeatureFlag <CarDataEntityCommand > = command ? : run {
57
+ executor.featureFlag(GetCarEntityCommand (), GetCarEntityCommandResult ::class )
58
+ .to { result }
59
+ .also { command = it }
49
60
}
50
61
}
0 commit comments