Emit LoadingVM events on main thread

This commit is contained in:
Andrew 2024-06-19 16:55:19 +09:30
parent 01064717b2
commit b0a939291d

View File

@ -12,9 +12,11 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.timeout import kotlinx.coroutines.flow.timeout
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import javax.inject.Inject import javax.inject.Inject
import kotlin.time.Duration import kotlin.time.Duration
@ -44,13 +46,13 @@ internal class LoadingViewModel @Inject constructor(
try { try {
TextSecurePreferences.events TextSecurePreferences.events
.filter { it == TextSecurePreferences.CONFIGURATION_SYNCED } .filter { it == TextSecurePreferences.CONFIGURATION_SYNCED }
.timeout(TIMEOUT_TIME)
.onStart { emit(TextSecurePreferences.CONFIGURATION_SYNCED) } .onStart { emit(TextSecurePreferences.CONFIGURATION_SYNCED) }
.collectLatest { .filter { prefs.getConfigurationMessageSynced() }
if (prefs.getConfigurationMessageSynced()) onSuccess() .timeout(TIMEOUT_TIME)
} .flowOn(Dispatchers.Main)
.collectLatest { onSuccess() }
} catch (e: Exception) { } catch (e: Exception) {
onFail() withContext(Dispatchers.Main) { onFail() }
} }
} }
} }
@ -61,8 +63,8 @@ internal class LoadingViewModel @Inject constructor(
_events.emit(Event.SUCCESS) _events.emit(Event.SUCCESS)
} }
private fun onFail() { private suspend fun onFail() {
_events.tryEmit(Event.TIMEOUT) _events.emit(Event.TIMEOUT)
} }
} }