This commit is contained in:
andrew 2023-08-28 01:50:20 +09:30
parent c22860665e
commit 9f6dd728d5
2 changed files with 16 additions and 25 deletions

View File

@ -114,7 +114,7 @@ class ExpirationSettingsActivity: PassphraseRequiredActionBarActivity() {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state.collect { state -> viewModel.state.collect { state ->
actionBar?.subtitle = state.subtitle(this@ExpirationSettingsActivity) supportActionBar?.subtitle = state.subtitle(this@ExpirationSettingsActivity)
// val position = deleteTypeOptions.indexOfFirst { it.value == state.selectedExpirationType } // val position = deleteTypeOptions.indexOfFirst { it.value == state.selectedExpirationType }
// deleteTypeOptionAdapter.setSelectedPosition(max(0, position)) // deleteTypeOptionAdapter.setSelectedPosition(max(0, position))

View File

@ -42,7 +42,8 @@ class ExpirationSettingsViewModel(
private val messageExpirationManager: MessageExpirationManagerProtocol, private val messageExpirationManager: MessageExpirationManagerProtocol,
private val threadDb: ThreadDatabase, private val threadDb: ThreadDatabase,
private val groupDb: GroupDatabase, private val groupDb: GroupDatabase,
private val storage: Storage private val storage: Storage,
private val isNewConfigEnabled: Boolean
) : ViewModel() { ) : ViewModel() {
private val _event = Channel<Event>() private val _event = Channel<Event>()
@ -64,7 +65,6 @@ class ExpirationSettingsViewModel(
private var expirationConfig: ExpirationConfiguration? = null private var expirationConfig: ExpirationConfiguration? = null
init { init {
// SETUP
viewModelScope.launch { viewModelScope.launch {
expirationConfig = storage.getExpirationConfiguration(threadId) expirationConfig = storage.getExpirationConfiguration(threadId)
val expiryMode = expirationConfig?.expiryMode ?: ExpiryMode.NONE val expiryMode = expirationConfig?.expiryMode ?: ExpiryMode.NONE
@ -80,18 +80,6 @@ class ExpirationSettingsViewModel(
) )
} }
} }
// selectedExpirationType.mapLatest {
// when (it) {
// is ExpiryMode.Legacy, is ExpiryMode.AfterSend -> afterSendOptions
// is ExpiryMode.AfterRead -> afterReadOptions
// else -> emptyList()
// }
// }.onEach { options ->
// val enabled = _uiState.value.isSelfAdmin || recipient.value?.isClosedGroupRecipient == true
// _expirationTimerOptions.value = options.let {
// if (ExpirationConfiguration.isNewConfigEnabled && recipient.value?.run { isLocalNumber || isClosedGroupRecipient } == true) it.drop(1) else it
// }.map { it.copy(enabled = enabled) }
// }.launchIn(viewModelScope)
} }
private fun typeOption( private fun typeOption(
@ -103,7 +91,7 @@ class ExpirationSettingsViewModel(
) = OptionModel(GetString(title), subtitle?.let(::GetString), selected = state.expiryType == type) { setType(type) } ) = OptionModel(GetString(title), subtitle?.let(::GetString), selected = state.expiryType == type) { setType(type) }
private fun typeOptions(state: State) = private fun typeOptions(state: State) =
if (state.isSelf) emptyList() if (state.isSelf || state.isGroup) emptyList()
else listOf( else listOf(
typeOption(ExpiryType.NONE, state, R.string.expiration_off, contentDescription = R.string.AccessibilityId_disable_disappearing_messages), typeOption(ExpiryType.NONE, state, R.string.expiration_off, contentDescription = R.string.AccessibilityId_disable_disappearing_messages),
typeOption(ExpiryType.LEGACY, state, R.string.expiration_type_disappear_legacy, contentDescription = R.string.expiration_type_disappear_legacy_description), typeOption(ExpiryType.LEGACY, state, R.string.expiration_type_disappear_legacy, contentDescription = R.string.expiration_type_disappear_legacy_description),
@ -132,29 +120,31 @@ class ExpirationSettingsViewModel(
// private fun timeOptions(state: State) = timeOptions(state.types.isEmpty(), state.expiryType == ExpiryType.AFTER_SEND) // private fun timeOptions(state: State) = timeOptions(state.types.isEmpty(), state.expiryType == ExpiryType.AFTER_SEND)
private fun timeOptions(state: State): List<OptionModel> = private fun timeOptions(state: State): List<OptionModel> =
if (state.isSelf) noteToSelfOptions(state) if (state.isSelf || state.isGroup) timeOptionsOnly(state)
else when (state.expiryMode) { else when (state.expiryMode) {
is ExpiryMode.Legacy -> afterReadTimes is ExpiryMode.Legacy -> afterReadTimes
is ExpiryMode.AfterRead -> afterReadTimes is ExpiryMode.AfterRead -> afterReadTimes
is ExpiryMode.AfterSend -> afterSendTimes is ExpiryMode.AfterSend -> afterSendTimes
else -> emptyList() else -> emptyList()
}.map(::option) }.map { timeOption(it, state) }
private val afterReadTimes = listOf(12.hours, 1.days, 7.days, 14.days) private val afterReadTimes = listOf(12.hours, 1.days, 7.days, 14.days)
private val afterSendTimes = listOf(5.minutes, 1.hours) + afterReadTimes private val afterSendTimes = listOf(5.minutes, 1.hours) + afterReadTimes
private fun noteToSelfOptions(state: State) = listOfNotNull( private fun timeOptionsOnly(state: State) = listOfNotNull(
typeOption(ExpiryType.NONE, state, R.string.arrays__off), typeOption(ExpiryType.NONE, state, R.string.arrays__off),
noteToSelfOption(1.minutes, state, subtitle = "for testing purposes").takeIf { BuildConfig.DEBUG }, noteToSelfOption(1.minutes, state, subtitle = "for testing purposes").takeIf { BuildConfig.DEBUG },
) + afterSendTimes.map { noteToSelfOption(it, state) } ) + afterSendTimes.map { noteToSelfOption(it, state) }
private fun option( private fun timeOption(
duration: Duration, duration: Duration,
state: State,
title: GetString = GetString { ExpirationUtil.getExpirationDisplayValue(it, duration.inWholeSeconds.toInt()) }, title: GetString = GetString { ExpirationUtil.getExpirationDisplayValue(it, duration.inWholeSeconds.toInt()) },
) = OptionModel( ) = OptionModel(
title = title, title = title,
selected = false selected = state.expiryMode?.duration == duration
) ) { setTime(duration.inWholeSeconds) }
private fun noteToSelfOption( private fun noteToSelfOption(
duration: Duration, duration: Duration,
state: State, state: State,
@ -214,7 +204,8 @@ class ExpirationSettingsViewModel(
messageExpirationManager, messageExpirationManager,
threadDb, threadDb,
groupDb, groupDb,
storage storage,
ExpirationConfiguration.isNewConfigEnabled
) as T ) as T
} }
} }
@ -230,8 +221,8 @@ data class State(
val expiryMode: ExpiryMode? = null, val expiryMode: ExpiryMode? = null,
val types: List<ExpiryType> = emptyList() val types: List<ExpiryType> = emptyList()
) { ) {
val subtitle get() = when (expiryType) { val subtitle get() = when {
ExpiryType.AFTER_SEND -> GetString(R.string.activity_expiration_settings_subtitle_sent) isGroup || isSelf -> GetString(R.string.activity_expiration_settings_subtitle_sent)
else -> GetString(R.string.activity_expiration_settings_subtitle) else -> GetString(R.string.activity_expiration_settings_subtitle)
} }
val duration get() = expiryMode?.duration val duration get() = expiryMode?.duration