From 06c0ab30800d79251de8dd5de6427b8878fa9255 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Thu, 3 Aug 2023 08:54:39 +1000 Subject: [PATCH] refactor: let frontend use the normal types and enabled options properly set --- .../expiration/ExpirationSettingsActivity.kt | 29 ++++++++++++++----- .../expiration/ExpirationSettingsViewModel.kt | 14 ++++----- .../securesms/database/Storage.kt | 8 +++++ .../layout/activity_expiration_settings.xml | 2 ++ .../libsession/database/StorageProtocol.kt | 1 - .../sending_receiving/MessageSender.kt | 2 +- 6 files changed, 38 insertions(+), 18 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 823ad87b6e..ebde6583fc 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 @@ -8,7 +8,9 @@ import androidx.activity.viewModels import androidx.core.content.ContextCompat import androidx.core.text.HtmlCompat import androidx.core.view.isVisible +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.RecyclerView import dagger.hilt.android.AndroidEntryPoint @@ -109,39 +111,50 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() { binding.buttonSet.setOnClickListener { viewModel.onSetClick() } - lifecycleScope.launchWhenStarted { - launch { + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.uiState.collect { uiState -> when (uiState.settingsSaved) { true -> { showToast(getString(R.string.ExpirationSettingsActivity_settings_updated)) finish() } + false -> showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated)) else -> {} } } } - launch { + } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.selectedExpirationType.collect { type -> val position = deleteTypeOptions.indexOfFirst { it.value.toIntOrNull() == type } deleteTypeOptionAdapter.setSelectedPosition(max(0, position)) } } - launch { + } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.selectedExpirationTimer.collect { option -> - val position = viewModel.expirationTimerOptions.value.indexOfFirst { it.value == option?.value } + val position = + viewModel.expirationTimerOptions.value.indexOfFirst { it.value == option?.value } timerOptionAdapter.setSelectedPosition(max(0, position)) } } - launch { + } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.expirationTimerOptions.collect { options -> - binding.textViewTimer.isVisible = options.isNotEmpty() && viewModel.uiState.value.showExpirationTypeSelector + binding.textViewTimer.isVisible = + options.isNotEmpty() && viewModel.uiState.value.showExpirationTypeSelector binding.layoutTimer.isVisible = options.isNotEmpty() timerOptionAdapter.submitList(options) } } - launch { + } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.recipient.collect { binding.textViewDeleteType.isVisible = viewModel.uiState.value.showExpirationTypeSelector binding.layoutDeleteTypes.isVisible = viewModel.uiState.value.showExpirationTypeSelector 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 f169e2c708..b887ea839c 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 @@ -58,6 +58,7 @@ class ExpirationSettingsViewModel( val expirationTimerOptions: StateFlow> = _expirationTimerOptions init { + // SETUP viewModelScope.launch { expirationConfig = storage.getExpirationConfiguration(threadId) val expirationType = expirationConfig?.expiryMode @@ -73,13 +74,9 @@ class ExpirationSettingsViewModel( ) } _selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) { - if (recipient?.isLocalNumber == true || recipient?.isClosedGroupRecipient == true) { - ExpirationType.DELETE_AFTER_SEND.number - } else { - expirationType?.typeRadioIndex() ?: -1 - } + expirationType.typeRadioIndex() } else { - if (expirationType != null) 0 else -1 + if (expirationType != null && expirationType != ExpiryMode.NONE) 0 else -1 } _selectedExpirationTimer.value = when(expirationType) { is ExpiryMode.AfterSend -> afterSendOptions.find { it.value.toIntOrNull() == expirationType.expirySeconds.toInt() } @@ -94,10 +91,11 @@ class ExpirationSettingsViewModel( else -> emptyList() } }.onEach { options -> + val enabled = _uiState.value.isSelfAdmin || recipient.value?.isClosedGroupRecipient == true _expirationTimerOptions.value = if (ExpirationConfiguration.isNewConfigEnabled && (recipient.value?.isLocalNumber == true || recipient.value?.isClosedGroupRecipient == true)) { - options.map { it.copy(enabled = _uiState.value.isSelfAdmin) } + options.map { it.copy(enabled = enabled) } } else { - options.slice(1 until options.size).map { it.copy(enabled = _uiState.value.isSelfAdmin) } + options.slice(1 until options.size).map { it.copy(enabled = enabled) } } }.launchIn(viewModelScope) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index aa7254c90b..b8b480d553 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -1701,6 +1701,14 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co } override fun setExpirationConfiguration(config: ExpirationConfiguration) { + val recipient = getRecipientForThread(config.threadId) ?: return + if (recipient.isClosedGroupRecipient) { + configFactory.userGroups?.getLegacyGroupInfo() + } else if (recipient.isLocalNumber) { + + } else if (recipient.isContactRecipient) { + + } DatabaseComponent.get(context).expirationConfigurationDatabase().setExpirationConfiguration(config) } diff --git a/app/src/main/res/layout/activity_expiration_settings.xml b/app/src/main/res/layout/activity_expiration_settings.xml index 2e26bae1e1..8a9668b87e 100644 --- a/app/src/main/res/layout/activity_expiration_settings.xml +++ b/app/src/main/res/layout/activity_expiration_settings.xml @@ -50,6 +50,7 @@ android:paddingVertical="@dimen/medium_spacing">