refactor: use generic option adapters, fix compile issues

This commit is contained in:
0x330a 2023-08-10 17:16:46 +10:00
parent 317ecf6cee
commit 9faa400a5b
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
3 changed files with 25 additions and 13 deletions

View File

@ -24,12 +24,13 @@ import org.thoughtcrime.securesms.database.GroupDatabase
import org.thoughtcrime.securesms.database.Storage
import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.preferences.ExpirationRadioOption
import org.thoughtcrime.securesms.preferences.RadioOption
import kotlin.reflect.KClass
class ExpirationSettingsViewModel(
private val threadId: Long,
private val afterReadOptions: List<ExpirationRadioOption>,
private val afterSendOptions: List<ExpirationRadioOption>,
private val afterReadOptions: List<RadioOption<ExpiryMode>>,
private val afterSendOptions: List<RadioOption<ExpiryMode>>,
private val textSecurePreferences: TextSecurePreferences,
private val messageExpirationManager: MessageExpirationManagerProtocol,
private val threadDb: ThreadDatabase,
@ -49,10 +50,10 @@ class ExpirationSettingsViewModel(
val selectedExpirationType: StateFlow<ExpiryMode> = _selectedExpirationType
private val _selectedExpirationTimer = MutableStateFlow(afterSendOptions.firstOrNull())
val selectedExpirationTimer: StateFlow<ExpirationRadioOption?> = _selectedExpirationTimer
val selectedExpirationTimer: StateFlow<RadioOption<ExpiryMode>?> = _selectedExpirationTimer
private val _expirationTimerOptions = MutableStateFlow<List<ExpirationRadioOption>>(emptyList())
val expirationTimerOptions: StateFlow<List<ExpirationRadioOption>> = _expirationTimerOptions
private val _expirationTimerOptions = MutableStateFlow<List<RadioOption<ExpiryMode>>>(emptyList())
val expirationTimerOptions: StateFlow<List<RadioOption<ExpiryMode>>> = _expirationTimerOptions
init {
// SETUP
@ -92,19 +93,23 @@ class ExpirationSettingsViewModel(
}.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 = enabled) }
options.filterIsInstance<ExpirationRadioOption>().map {
it.copy(enabled = enabled)
}
} else {
options.slice(1 until options.size).map { it.copy(enabled = enabled) }
options.slice(1 until options.size).filterIsInstance<ExpirationRadioOption>().map {
it.copy(enabled = enabled)
}
}
}.launchIn(viewModelScope)
}
fun onExpirationTypeSelected(option: ExpirationRadioOption) {
fun onExpirationTypeSelected(option: RadioOption<ExpiryMode>) {
_selectedExpirationType.value = option.value
_selectedExpirationTimer.value = _expirationTimerOptions.value.firstOrNull()
}
fun onExpirationTimerSelected(option: ExpirationRadioOption) {
fun onExpirationTimerSelected(option: RadioOption<ExpiryMode>) {
_selectedExpirationTimer.value = option
}

View File

@ -46,9 +46,9 @@ class ClearAllDataDialog : DialogFragment() {
private fun createView(): View {
binding = DialogClearAllDataBinding.inflate(LayoutInflater.from(requireContext()))
val device = RadioOption("deviceOnly", requireContext().getString(R.string.dialog_clear_all_data_clear_device_only))
val network = RadioOption("deviceAndNetwork", requireContext().getString(R.string.dialog_clear_all_data_clear_device_and_network))
var selectedOption = device
val device = StringRadioOption("deviceOnly", requireContext().getString(R.string.dialog_clear_all_data_clear_device_only))
val network = StringRadioOption("deviceAndNetwork", requireContext().getString(R.string.dialog_clear_all_data_clear_device_and_network))
var selectedOption: RadioOption<String> = device
val optionAdapter = RadioOptionAdapter { selectedOption = it }
binding.recyclerView.apply {
itemAnimator = null

View File

@ -100,4 +100,11 @@ class ExpirationRadioOption(
subtitle,
enabled,
contentDescription
)
) {
fun copy(value: ExpiryMode = this.value,
title: String = this.title,
subtitle: String? = this.subtitle,
enabled: Boolean = this.enabled,
contentDescription: String = this.contentDescription) =
ExpirationRadioOption(value, title, subtitle, enabled, contentDescription)
}