Add event flow

This commit is contained in:
andrew 2023-08-27 23:20:14 +09:30
parent b65a383ac5
commit c22860665e
2 changed files with 22 additions and 16 deletions

View File

@ -100,20 +100,22 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
binding.container.setContent { DisappearingMessagesScreen() } 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 { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state.collect { state -> viewModel.state.collect { state ->
actionBar?.subtitle = state.subtitle(this@ExpirationSettingsActivity) 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 } // val position = deleteTypeOptions.indexOfFirst { it.value == state.selectedExpirationType }
// deleteTypeOptionAdapter.setSelectedPosition(max(0, position)) // deleteTypeOptionAdapter.setSelectedPosition(max(0, position))
} }

View File

@ -7,9 +7,11 @@ import androidx.lifecycle.viewModelScope
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import network.loki.messenger.BuildConfig import network.loki.messenger.BuildConfig
@ -43,6 +45,9 @@ class ExpirationSettingsViewModel(
private val storage: Storage private val storage: Storage
) : ViewModel() { ) : ViewModel() {
private val _event = Channel<Event>()
val event = _event.receiveAsFlow()
private val _state = MutableStateFlow(State()) private val _state = MutableStateFlow(State())
val state = _state.asStateFlow() val state = _state.asStateFlow()
@ -171,9 +176,7 @@ class ExpirationSettingsViewModel(
} }
val address = state.recipient?.address val address = state.recipient?.address
if (address == null || expirationConfig?.expiryMode == typeValue) { if (address == null || expirationConfig?.expiryMode == typeValue) {
_state.update { _event.send(Event(false))
it.copy(settingsSaved = false)
}
return@launch return@launch
} }
@ -187,9 +190,7 @@ class ExpirationSettingsViewModel(
messageExpirationManager.setExpirationTimer(message, typeValue) messageExpirationManager.setExpirationTimer(message, typeValue)
MessageSender.send(message, address) MessageSender.send(message, address)
_state.update { _event.send(Event(true))
it.copy(settingsSaved = true)
}
} }
@dagger.assisted.AssistedFactory @dagger.assisted.AssistedFactory
@ -218,10 +219,13 @@ class ExpirationSettingsViewModel(
} }
} }
data class Event(
val saveSuccess: Boolean
)
data class State( data class State(
val isGroup: Boolean = false, val isGroup: Boolean = false,
val isSelfAdmin: Boolean = false, val isSelfAdmin: Boolean = false,
val settingsSaved: Boolean? = null,
val recipient: Recipient? = null, val recipient: Recipient? = null,
val expiryMode: ExpiryMode? = null, val expiryMode: ExpiryMode? = null,
val types: List<ExpiryType> = emptyList() val types: List<ExpiryType> = emptyList()