diff --git a/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ApiFakerConfig.kt b/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ApiFakerConfig.kt index 541ccf5f937..7506b7cd938 100644 --- a/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ApiFakerConfig.kt +++ b/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ApiFakerConfig.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import javax.inject.Inject @@ -19,15 +20,18 @@ private const val PREFERENCE_KEY = "api_faker_enabled" @Singleton internal class ApiFakerConfig @Inject constructor( context: Context, - private val endpointDao: EndpointDao + endpointDao: EndpointDao ) { private val configScope = CoroutineScope(Dispatchers.Main) private val preferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE) private val prefFlow = preferences.prefFlow(PREFERENCE_KEY, false) - val enabled = prefFlow.map { - it && !endpointDao.isEmpty() + val enabled = combine( + prefFlow, + endpointDao.observeEndpointsCount().map { it == 0 } + ) { pref, isEmpty -> + pref && !isEmpty }.stateIn(configScope, SharingStarted.Eagerly, false) fun setStatus(enabled: Boolean) { diff --git a/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/db/EndpointDao.kt b/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/db/EndpointDao.kt index 1c1c47d0c99..e3efbd08124 100644 --- a/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/db/EndpointDao.kt +++ b/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/db/EndpointDao.kt @@ -20,7 +20,7 @@ internal interface EndpointDao { fun observeEndpoints(): Flow> @Query("Select COUNT(*) FROM Request") - suspend fun endpointsCount(): Int + fun observeEndpointsCount(): Flow @Transaction @Query( @@ -51,6 +51,4 @@ internal interface EndpointDao { val id = insertRequest(request) insertResponse(response.copy(endpointId = id)) } - - suspend fun isEmpty() = endpointsCount() == 0 }