Skip to content

Commit

Permalink
Fix issue with updating the faker config
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Dec 4, 2024
1 parent 319c00f commit 7dd5408
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal interface EndpointDao {
fun observeEndpoints(): Flow<List<MockedEndpoint>>

@Query("Select COUNT(*) FROM Request")
suspend fun endpointsCount(): Int
fun observeEndpointsCount(): Flow<Int>

@Transaction
@Query(
Expand Down Expand Up @@ -51,6 +51,4 @@ internal interface EndpointDao {
val id = insertRequest(request)
insertResponse(response.copy(endpointId = id))
}

suspend fun isEmpty() = endpointsCount() == 0
}

0 comments on commit 7dd5408

Please sign in to comment.