refactor: let frontend use the normal types and enabled options properly set

This commit is contained in:
0x330a 2023-08-03 08:54:39 +10:00
parent bcb2071f44
commit 06c0ab3080
6 changed files with 38 additions and 18 deletions

View File

@ -8,7 +8,9 @@ import androidx.activity.viewModels
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
@ -109,39 +111,50 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
binding.buttonSet.setOnClickListener { binding.buttonSet.setOnClickListener {
viewModel.onSetClick() viewModel.onSetClick()
} }
lifecycleScope.launchWhenStarted { lifecycleScope.launch {
launch { repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { uiState -> viewModel.uiState.collect { uiState ->
when (uiState.settingsSaved) { when (uiState.settingsSaved) {
true -> { true -> {
showToast(getString(R.string.ExpirationSettingsActivity_settings_updated)) showToast(getString(R.string.ExpirationSettingsActivity_settings_updated))
finish() finish()
} }
false -> showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated)) false -> showToast(getString(R.string.ExpirationSettingsActivity_settings_not_updated))
else -> {} else -> {}
} }
} }
} }
launch { }
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.selectedExpirationType.collect { type -> viewModel.selectedExpirationType.collect { type ->
val position = deleteTypeOptions.indexOfFirst { it.value.toIntOrNull() == type } val position = deleteTypeOptions.indexOfFirst { it.value.toIntOrNull() == type }
deleteTypeOptionAdapter.setSelectedPosition(max(0, position)) deleteTypeOptionAdapter.setSelectedPosition(max(0, position))
} }
} }
launch { }
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.selectedExpirationTimer.collect { option -> 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)) timerOptionAdapter.setSelectedPosition(max(0, position))
} }
} }
launch { }
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.expirationTimerOptions.collect { options -> 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() binding.layoutTimer.isVisible = options.isNotEmpty()
timerOptionAdapter.submitList(options) timerOptionAdapter.submitList(options)
} }
} }
launch { }
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.recipient.collect { viewModel.recipient.collect {
binding.textViewDeleteType.isVisible = viewModel.uiState.value.showExpirationTypeSelector binding.textViewDeleteType.isVisible = viewModel.uiState.value.showExpirationTypeSelector
binding.layoutDeleteTypes.isVisible = viewModel.uiState.value.showExpirationTypeSelector binding.layoutDeleteTypes.isVisible = viewModel.uiState.value.showExpirationTypeSelector

View File

@ -58,6 +58,7 @@ class ExpirationSettingsViewModel(
val expirationTimerOptions: StateFlow<List<RadioOption>> = _expirationTimerOptions val expirationTimerOptions: StateFlow<List<RadioOption>> = _expirationTimerOptions
init { init {
// SETUP
viewModelScope.launch { viewModelScope.launch {
expirationConfig = storage.getExpirationConfiguration(threadId) expirationConfig = storage.getExpirationConfiguration(threadId)
val expirationType = expirationConfig?.expiryMode val expirationType = expirationConfig?.expiryMode
@ -73,13 +74,9 @@ class ExpirationSettingsViewModel(
) )
} }
_selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) { _selectedExpirationType.value = if (ExpirationConfiguration.isNewConfigEnabled) {
if (recipient?.isLocalNumber == true || recipient?.isClosedGroupRecipient == true) { expirationType.typeRadioIndex()
ExpirationType.DELETE_AFTER_SEND.number
} else {
expirationType?.typeRadioIndex() ?: -1
}
} else { } else {
if (expirationType != null) 0 else -1 if (expirationType != null && expirationType != ExpiryMode.NONE) 0 else -1
} }
_selectedExpirationTimer.value = when(expirationType) { _selectedExpirationTimer.value = when(expirationType) {
is ExpiryMode.AfterSend -> afterSendOptions.find { it.value.toIntOrNull() == expirationType.expirySeconds.toInt() } is ExpiryMode.AfterSend -> afterSendOptions.find { it.value.toIntOrNull() == expirationType.expirySeconds.toInt() }
@ -94,10 +91,11 @@ class ExpirationSettingsViewModel(
else -> emptyList() else -> emptyList()
} }
}.onEach { options -> }.onEach { options ->
val enabled = _uiState.value.isSelfAdmin || recipient.value?.isClosedGroupRecipient == true
_expirationTimerOptions.value = if (ExpirationConfiguration.isNewConfigEnabled && (recipient.value?.isLocalNumber == true || 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 { } 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) }.launchIn(viewModelScope)
} }

View File

@ -1701,6 +1701,14 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co
} }
override fun setExpirationConfiguration(config: ExpirationConfiguration) { 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) DatabaseComponent.get(context).expirationConfigurationDatabase().setExpirationConfiguration(config)
} }

View File

@ -50,6 +50,7 @@
android:paddingVertical="@dimen/medium_spacing"> android:paddingVertical="@dimen/medium_spacing">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:overScrollMode="never"
android:id="@+id/recycler_view_delete_types" android:id="@+id/recycler_view_delete_types"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -80,6 +81,7 @@
android:paddingVertical="@dimen/medium_spacing"> android:paddingVertical="@dimen/medium_spacing">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:overScrollMode="never"
android:id="@+id/recycler_view_timer_options" android:id="@+id/recycler_view_timer_options"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -159,7 +159,6 @@ interface StorageProtocol {
// Settings // Settings
fun setProfileSharing(address: Address, value: Boolean) fun setProfileSharing(address: Address, value: Boolean)
// Thread // Thread
fun getOrCreateThreadIdFor(address: Address): Long fun getOrCreateThreadIdFor(address: Address): Long
fun getThreadIdFor(publicKey: String, groupPublicKey: String?, openGroupID: String?, createThread: Boolean): Long? fun getThreadIdFor(publicKey: String, groupPublicKey: String?, openGroupID: String?, createThread: Boolean): Long?

View File

@ -253,7 +253,7 @@ object MessageSender {
val threadId = message.threadID val threadId = message.threadID
?: run { ?: run {
val address = if (isSyncMessage && message is VisibleMessage) message.syncTarget else message.recipient val address = if (isSyncMessage && message is VisibleMessage) message.syncTarget else message.recipient
storage.getOrCreateThreadIdFor(Address.fromSerialized(address!!)) storage.getThreadId(Address.fromSerialized(address!!)) ?: return null
} }
val config = storage.getExpirationConfiguration(threadId) ?: return null val config = storage.getExpirationConfiguration(threadId) ?: return null
val expiryMode = config.expiryMode val expiryMode = config.expiryMode