From c22860665e26f73f904aa35db41dcc9662e872d7 Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 27 Aug 2023 23:20:14 +0930 Subject: [PATCH] Add event flow --- .../expiration/ExpirationSettingsActivity.kt | 20 ++++++++++--------- .../expiration/ExpirationSettingsViewModel.kt | 18 ++++++++++------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsActivity.kt index e3403c181f..2804878e3f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsActivity.kt @@ -100,20 +100,22 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() { binding.container.setContent { DisappearingMessagesScreen() } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + viewModel.event.collect { + if (it.saveSuccess) { + ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(this@ExpirationSettingsActivity) + finish() + } else showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated)) + } + } + } + lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.state.collect { state -> actionBar?.subtitle = state.subtitle(this@ExpirationSettingsActivity) - when (state.settingsSaved) { - true -> { - ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(this@ExpirationSettingsActivity) - finish() - } - false -> showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated)) - else -> {} - } - // val position = deleteTypeOptions.indexOfFirst { it.value == state.selectedExpirationType } // deleteTypeOptionAdapter.setSelectedPosition(max(0, position)) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt index 19b7f92387..d9bd236547 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/expiration/ExpirationSettingsViewModel.kt @@ -7,9 +7,11 @@ import androidx.lifecycle.viewModelScope import dagger.assisted.Assisted import dagger.assisted.AssistedInject import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import network.loki.messenger.BuildConfig @@ -43,6 +45,9 @@ class ExpirationSettingsViewModel( private val storage: Storage ) : ViewModel() { + private val _event = Channel() + val event = _event.receiveAsFlow() + private val _state = MutableStateFlow(State()) val state = _state.asStateFlow() @@ -171,9 +176,7 @@ class ExpirationSettingsViewModel( } val address = state.recipient?.address if (address == null || expirationConfig?.expiryMode == typeValue) { - _state.update { - it.copy(settingsSaved = false) - } + _event.send(Event(false)) return@launch } @@ -187,9 +190,7 @@ class ExpirationSettingsViewModel( messageExpirationManager.setExpirationTimer(message, typeValue) MessageSender.send(message, address) - _state.update { - it.copy(settingsSaved = true) - } + _event.send(Event(true)) } @dagger.assisted.AssistedFactory @@ -218,10 +219,13 @@ class ExpirationSettingsViewModel( } } +data class Event( + val saveSuccess: Boolean +) + data class State( val isGroup: Boolean = false, val isSelfAdmin: Boolean = false, - val settingsSaved: Boolean? = null, val recipient: Recipient? = null, val expiryMode: ExpiryMode? = null, val types: List = emptyList()