-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81283aa
commit 94d250f
Showing
1 changed file
with
35 additions
and
10 deletions.
There are no files selected for viewing
45 changes: 35 additions & 10 deletions
45
libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ApiFakerConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,47 @@ | ||
package com.woocommerce.android.apifaker | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import com.woocommerce.android.apifaker.db.EndpointDao | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.stateIn | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
private const val PREF_FILE_NAME = "api_faker" | ||
private const val PREFERENCE_KEY = "api_faker_enabled" | ||
|
||
@Singleton | ||
class ApiFakerConfig @Inject constructor() { | ||
@Inject | ||
internal lateinit var endpointDao: EndpointDao | ||
internal class ApiFakerConfig @Inject constructor( | ||
context: Context, | ||
private val 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) | ||
|
||
private val _enabled: MutableStateFlow<Boolean> = MutableStateFlow(false) | ||
val enabled = _enabled.asStateFlow() | ||
.map { it && endpointDao.endpointsCount() > 0 } | ||
val enabled = prefFlow.map { | ||
it && !endpointDao.isEmpty() | ||
}.stateIn(configScope, SharingStarted.WhileSubscribed(), false) | ||
|
||
fun setStatus(enabled: Boolean) { | ||
preferences.edit().putBoolean(PREFERENCE_KEY, enabled).apply() | ||
} | ||
|
||
internal fun setStatus(enabled: Boolean) { | ||
_enabled.value = enabled | ||
private fun SharedPreferences.prefFlow(key: String, defaultValue: Boolean) = callbackFlow<Boolean> { | ||
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, listenerKey -> | ||
if (listenerKey == key) { | ||
trySend(getBoolean(key, defaultValue)) | ||
} | ||
} | ||
registerOnSharedPreferenceChangeListener(listener) | ||
trySend(getBoolean(key, defaultValue)) | ||
awaitClose { unregisterOnSharedPreferenceChangeListener(listener) } | ||
} | ||
} |