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.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

View File

@ -58,6 +58,7 @@ class ExpirationSettingsViewModel(
val expirationTimerOptions: StateFlow<List<RadioOption>> = _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)
}

View File

@ -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)
}

View File

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

View File

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

View File

@ -253,7 +253,7 @@ object MessageSender {
val threadId = message.threadID
?: run {
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 expiryMode = config.expiryMode