Fix shareFlow#tryEmit in pick display name

This commit is contained in:
bemusementpark 2024-07-02 14:28:54 +09:30
parent 508547a013
commit d4c433a926
2 changed files with 9 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
@ -40,7 +41,7 @@ class PickDisplayNameActivity : BaseActionBarActivity() {
setComposeContent { DisplayNameScreen(viewModel) } setComposeContent { DisplayNameScreen(viewModel) }
lifecycleScope.launch { lifecycleScope.launch(Dispatchers.Main) {
viewModel.events.collect { viewModel.events.collect {
when (it) { when (it) {
is Event.CreateAccount -> startMessageNotificationsActivity(it.profileName) is Event.CreateAccount -> startMessageNotificationsActivity(it.profileName)

View File

@ -3,13 +3,16 @@ package org.thoughtcrime.securesms.onboarding.pickname
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import network.loki.messenger.R import network.loki.messenger.R
import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol.Companion.NAME_PADDED_LENGTH import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol.Companion.NAME_PADDED_LENGTH
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
@ -39,14 +42,13 @@ internal class PickDisplayNameViewModel(
// next screen. // next screen.
_states.update { it.copy(isTextErrorColor = false, error = null) } _states.update { it.copy(isTextErrorColor = false, error = null) }
when { viewModelScope.launch(Dispatchers.IO) {
loadFailed -> { if (loadFailed) {
prefs.setProfileName(displayName) prefs.setProfileName(displayName)
configFactory.user?.setName(displayName) configFactory.user?.setName(displayName)
_events.tryEmit(Event.LoadAccountComplete) _events.emit(Event.LoadAccountComplete)
} } else _events.emit(Event.CreateAccount(displayName))
else -> _events.tryEmit(Event.CreateAccount(displayName))
} }
} }
} }