avoiding run blocking

This commit is contained in:
Adam Brown 2022-08-18 21:29:20 +01:00
parent 0c35481bda
commit c6d68b7583
4 changed files with 8 additions and 7 deletions

View File

@ -4,7 +4,6 @@ import android.content.Context
import app.dapk.st.domain.push.PushTokenRegistrarPreferences import app.dapk.st.domain.push.PushTokenRegistrarPreferences
import app.dapk.st.push.firebase.FirebasePushTokenRegistrar import app.dapk.st.push.firebase.FirebasePushTokenRegistrar
import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar
import kotlinx.coroutines.runBlocking
import org.unifiedpush.android.connector.UnifiedPush import org.unifiedpush.android.connector.UnifiedPush
private val FIREBASE_OPTION = Registrar("Google - Firebase (FCM)") private val FIREBASE_OPTION = Registrar("Google - Firebase (FCM)")
@ -17,13 +16,13 @@ class PushTokenRegistrars(
private val pushTokenStore: PushTokenRegistrarPreferences, private val pushTokenStore: PushTokenRegistrarPreferences,
) : PushTokenRegistrar { ) : PushTokenRegistrar {
private var selection: Registrar = runBlocking { pushTokenStore.currentSelection()?.let { Registrar(it) } } ?: FIREBASE_OPTION private var selection: Registrar? = null
fun options(): List<Registrar> { fun options(): List<Registrar> {
return listOf(NONE, FIREBASE_OPTION) + UnifiedPush.getDistributors(context).map { Registrar(it) } return listOf(NONE, FIREBASE_OPTION) + UnifiedPush.getDistributors(context).map { Registrar(it) }
} }
fun currentSelection() = selection suspend fun currentSelection() = selection ?: (pushTokenStore.currentSelection()?.let { Registrar(it) } ?: FIREBASE_OPTION).also { selection = it }
suspend fun makeSelection(option: Registrar) { suspend fun makeSelection(option: Registrar) {
selection = option selection = option

View File

@ -8,7 +8,7 @@ internal class SettingsItemFactory(
private val pushTokenRegistrars: PushTokenRegistrars, private val pushTokenRegistrars: PushTokenRegistrars,
) { ) {
fun root() = listOf( suspend fun root() = listOf(
SettingItem.Header("General"), SettingItem.Header("General"),
SettingItem.Text(SettingItem.Id.Encryption, "Encryption"), SettingItem.Text(SettingItem.Id.Encryption, "Encryption"),
SettingItem.Text(SettingItem.Id.EventLog, "Event log"), SettingItem.Text(SettingItem.Id.EventLog, "Event log"),

View File

@ -101,10 +101,12 @@ internal class SettingsViewModel(
fun fetchPushProviders() { fun fetchPushProviders() {
updatePageState<Page.PushProviders> { copy(options = Lce.Loading()) } updatePageState<Page.PushProviders> { copy(options = Lce.Loading()) }
viewModelScope.launch { viewModelScope.launch {
val currentSelection = pushTokenRegistrars.currentSelection()
val options = pushTokenRegistrars.options()
updatePageState<Page.PushProviders> { updatePageState<Page.PushProviders> {
copy( copy(
selection = pushTokenRegistrars.currentSelection(), selection = currentSelection,
options = Lce.Content(pushTokenRegistrars.options()) options = Lce.Content(options)
) )
} }
} }

View File

@ -10,7 +10,7 @@ class SettingsItemFactoryTest {
private val buildMeta = BuildMeta(versionName = "a-version-name", versionCode = 100) private val buildMeta = BuildMeta(versionName = "a-version-name", versionCode = 100)
private val settingsItemFactory = SettingsItemFactory(buildMeta) private val settingsItemFactory = SettingsItemFactory(buildMeta, )
@Test @Test
fun `when creating root items, then is expected`() { fun `when creating root items, then is expected`() {