Move recipient into state

This commit is contained in:
andrew 2023-08-24 01:00:31 +09:30
parent 0ed9731622
commit 5142c45643
2 changed files with 25 additions and 32 deletions

View File

@ -112,6 +112,11 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { uiState -> viewModel.uiState.collect { uiState ->
binding.textViewDeleteType.isVisible = uiState.showExpirationTypeSelector
binding.layoutDeleteTypes.isVisible = uiState.showExpirationTypeSelector
binding.textViewFooter.isVisible = uiState.recipient?.isClosedGroupRecipient == true
binding.textViewFooter.text = HtmlCompat.fromHtml(getString(R.string.activity_expiration_settings_group_footer), HtmlCompat.FROM_HTML_MODE_COMPACT)
when (uiState.settingsSaved) { when (uiState.settingsSaved) {
true -> { true -> {
ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(this@ExpirationSettingsActivity) ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(this@ExpirationSettingsActivity)
@ -150,17 +155,6 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
} }
} }
} }
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.recipient.collect {
binding.textViewDeleteType.isVisible = viewModel.uiState.value.showExpirationTypeSelector
binding.layoutDeleteTypes.isVisible = viewModel.uiState.value.showExpirationTypeSelector
binding.textViewFooter.isVisible = it?.isClosedGroupRecipient == true
binding.textViewFooter.text = HtmlCompat.fromHtml(getString(R.string.activity_expiration_settings_group_footer), HtmlCompat.FROM_HTML_MODE_COMPACT)
}
}
}
} }
private fun RecyclerView.addDividers() { private fun RecyclerView.addDividers() {

View File

@ -49,9 +49,6 @@ class ExpirationSettingsViewModel(
private var expirationConfig: ExpirationConfiguration? = null private var expirationConfig: ExpirationConfiguration? = null
private val _recipient = MutableStateFlow<Recipient?>(null)
val recipient: StateFlow<Recipient?> = _recipient
private val _selectedExpirationType: MutableStateFlow<ExpiryMode> = MutableStateFlow(ExpiryMode.NONE) private val _selectedExpirationType: MutableStateFlow<ExpiryMode> = MutableStateFlow(ExpiryMode.NONE)
val selectedExpirationType: StateFlow<ExpiryMode> = _selectedExpirationType val selectedExpirationType: StateFlow<ExpiryMode> = _selectedExpirationType
@ -67,13 +64,13 @@ class ExpirationSettingsViewModel(
expirationConfig = storage.getExpirationConfiguration(threadId) expirationConfig = storage.getExpirationConfiguration(threadId)
val expirationType = expirationConfig?.expiryMode val expirationType = expirationConfig?.expiryMode
val recipient = threadDb.getRecipientForThreadId(threadId) val recipient = threadDb.getRecipientForThreadId(threadId)
_recipient.value = recipient
val groupInfo = recipient?.takeIf { it.isClosedGroupRecipient } val groupInfo = recipient?.takeIf { it.isClosedGroupRecipient }
?.run { address.toGroupString().let(groupDb::getGroup).orNull() } ?.run { address.toGroupString().let(groupDb::getGroup).orNull() }
_uiState.update { currentUiState -> _uiState.update { currentUiState ->
currentUiState.copy( currentUiState.copy(
isSelfAdmin = groupInfo == null || groupInfo.admins.any{ it.serialize() == textSecurePreferences.getLocalNumber() }, isSelfAdmin = groupInfo == null || groupInfo.admins.any{ it.serialize() == textSecurePreferences.getLocalNumber() },
showExpirationTypeSelector = true showExpirationTypeSelector = true,
recipient = recipient
) )
} }
_selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) { _selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) {
@ -89,18 +86,18 @@ class ExpirationSettingsViewModel(
else -> afterSendOptions.firstOrNull() else -> afterSendOptions.firstOrNull()
} }
} }
selectedExpirationType.mapLatest { // selectedExpirationType.mapLatest {
when (it) { // when (it) {
is ExpiryMode.Legacy, is ExpiryMode.AfterSend -> afterSendOptions // is ExpiryMode.Legacy, is ExpiryMode.AfterSend -> afterSendOptions
is ExpiryMode.AfterRead -> afterReadOptions // is ExpiryMode.AfterRead -> afterReadOptions
else -> emptyList() // else -> emptyList()
} // }
}.onEach { options -> // }.onEach { options ->
val enabled = _uiState.value.isSelfAdmin || recipient.value?.isClosedGroupRecipient == true // val enabled = _uiState.value.isSelfAdmin || recipient.value?.isClosedGroupRecipient == true
_expirationTimerOptions.value = options.let { // _expirationTimerOptions.value = options.let {
if (ExpirationConfiguration.isNewConfigEnabled && recipient.value?.run { isLocalNumber || isClosedGroupRecipient } == true) it.drop(1) else it // if (ExpirationConfiguration.isNewConfigEnabled && recipient.value?.run { isLocalNumber || isClosedGroupRecipient } == true) it.drop(1) else it
}.map { it.copy(enabled = enabled) } // }.map { it.copy(enabled = enabled) }
}.launchIn(viewModelScope) // }.launchIn(viewModelScope)
} }
fun onExpirationTypeSelected(option: RadioOption<ExpiryMode>) { fun onExpirationTypeSelected(option: RadioOption<ExpiryMode>) {
@ -119,13 +116,14 @@ class ExpirationSettingsViewModel(
} }
fun onSetClick() = viewModelScope.launch { fun onSetClick() = viewModelScope.launch {
val state = uiState.value
val expiryMode = _selectedExpirationTimer.value?.value ?: ExpiryMode.NONE val expiryMode = _selectedExpirationTimer.value?.value ?: ExpiryMode.NONE
val typeValue = expiryMode.let { val typeValue = expiryMode.let {
if (it is ExpiryMode.Legacy) ExpiryMode.AfterRead(it.expirySeconds) if (it is ExpiryMode.Legacy) ExpiryMode.AfterRead(it.expirySeconds)
else it else it
} }
val address = recipient.value?.address val address = state.recipient?.address
if (address == null || (expirationConfig?.expiryMode == typeValue)) { if (address == null || expirationConfig?.expiryMode == typeValue) {
_uiState.update { _uiState.update {
it.copy(settingsSaved = false) it.copy(settingsSaved = false)
} }
@ -150,7 +148,7 @@ class ExpirationSettingsViewModel(
fun getDeleteOptions(): List<ExpirationRadioOption> { fun getDeleteOptions(): List<ExpirationRadioOption> {
if (!uiState.value.showExpirationTypeSelector) return emptyList() if (!uiState.value.showExpirationTypeSelector) return emptyList()
val recipient = recipient.value ?: return emptyList() val recipient = uiState.value.recipient ?: return emptyList()
return if (ExpirationConfiguration.isNewConfigEnabled) when { return if (ExpirationConfiguration.isNewConfigEnabled) when {
recipient.isLocalNumber -> noteToSelfOptions() recipient.isLocalNumber -> noteToSelfOptions()
@ -267,5 +265,6 @@ class ExpirationSettingsViewModel(
data class ExpirationSettingsUiState( data class ExpirationSettingsUiState(
val isSelfAdmin: Boolean = false, val isSelfAdmin: Boolean = false,
val showExpirationTypeSelector: Boolean = false, val showExpirationTypeSelector: Boolean = false,
val settingsSaved: Boolean? = null val settingsSaved: Boolean? = null,
val recipient: Recipient? = null
) )