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

@ -102,17 +102,19 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state.collect { state ->
actionBar?.subtitle = state.subtitle(this@ExpirationSettingsActivity)
when (state.settingsSaved) {
true -> {
viewModel.event.collect {
if (it.saveSuccess) {
ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(this@ExpirationSettingsActivity)
finish()
} else showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated))
}
false -> showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated))
else -> {}
}
}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state.collect { state ->
actionBar?.subtitle = state.subtitle(this@ExpirationSettingsActivity)
// val position = deleteTypeOptions.indexOfFirst { it.value == state.selectedExpirationType }
// deleteTypeOptionAdapter.setSelectedPosition(max(0, position))

View File

@ -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<Event>()
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<ExpiryType> = emptyList()