From 97f28fd2e54ddc16441979205be9bcb7e8d6bedc Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 27 Aug 2023 01:24:41 +0930 Subject: [PATCH] Respect option selected in note to self --- .../expiration/ExpirationSettingsViewModel.kt | 26 ++++++++++--------- .../libsession_util/util/ExpiryMode.kt | 6 ++++- 2 files changed, 19 insertions(+), 13 deletions(-) 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 791e55bb38..d981ac3a22 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 @@ -86,19 +86,19 @@ class ExpirationSettingsViewModel( private fun typeOption( type: ExpiryType, - selected: ExpiryType?, + state: State, @StringRes title: Int, @StringRes subtitle: Int? = null, @StringRes contentDescription: Int = title - ) = OptionModel(GetString(title), subtitle?.let(::GetString), selected = selected == type) { setType(type) } + ) = OptionModel(GetString(title), subtitle?.let(::GetString), selected = state.expiryType == type) { setType(type) } private fun typeOptions(state: State) = if (state.isSelf) emptyList() else listOf( - typeOption(ExpiryType.NONE, state.expiryType, R.string.expiration_off, contentDescription = R.string.AccessibilityId_disable_disappearing_messages), - typeOption(ExpiryType.LEGACY, state.expiryType, R.string.expiration_type_disappear_legacy, contentDescription = R.string.expiration_type_disappear_legacy_description), - typeOption(ExpiryType.AFTER_READ, state.expiryType, R.string.expiration_type_disappear_after_read, contentDescription = R.string.expiration_type_disappear_after_read_description), - typeOption(ExpiryType.AFTER_SEND, state.expiryType, R.string.expiration_type_disappear_after_send, contentDescription = R.string.expiration_type_disappear_after_send_description), + 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.AFTER_READ, state, R.string.expiration_type_disappear_after_read, contentDescription = R.string.expiration_type_disappear_after_read_description), + typeOption(ExpiryType.AFTER_SEND, state, R.string.expiration_type_disappear_after_send, contentDescription = R.string.expiration_type_disappear_after_send_description), ) private fun setType(type: ExpiryType) { @@ -122,7 +122,7 @@ class ExpirationSettingsViewModel( // private fun timeOptions(state: State) = timeOptions(state.types.isEmpty(), state.expiryType == ExpiryType.AFTER_SEND) private fun timeOptions(state: State): List = - if (state.isSelf) noteToSelfOptions(state.expiryType) + if (state.isSelf) noteToSelfOptions(state) else when (state.expiryMode) { is ExpiryMode.Legacy -> afterReadTimes is ExpiryMode.AfterRead -> afterReadTimes @@ -133,10 +133,10 @@ class ExpirationSettingsViewModel( private val afterReadTimes = listOf(12.hours, 1.days, 7.days, 14.days) private val afterSendTimes = listOf(5.minutes, 1.hours) + afterReadTimes - private fun noteToSelfOptions(selected: ExpiryType?) = listOfNotNull( - typeOption(ExpiryType.NONE, selected, R.string.arrays__off), - noteToSelfOption(1.minutes, subtitle = "for testing purposes").takeIf { BuildConfig.DEBUG }, - ) + afterSendTimes.map(::noteToSelfOption) + private fun noteToSelfOptions(state: State) = listOfNotNull( + typeOption(ExpiryType.NONE, state, R.string.arrays__off), + noteToSelfOption(1.minutes, state, subtitle = "for testing purposes").takeIf { BuildConfig.DEBUG }, + ) + afterSendTimes.map { noteToSelfOption(it, state) } private fun option( duration: Duration, @@ -147,12 +147,13 @@ class ExpirationSettingsViewModel( ) private fun noteToSelfOption( duration: Duration, + state: State, title: GetString = GetString { ExpirationUtil.getExpirationDisplayValue(it, duration.inWholeSeconds.toInt()) }, subtitle: String? = null ) = OptionModel( title = title, subtitle = subtitle?.let(::GetString), - selected = false, + selected = state.duration == duration, onClick = { setMode(ExpiryMode.AfterSend(duration.inWholeSeconds)) } ) @@ -219,6 +220,7 @@ data class State( val expiryMode: ExpiryMode? = null, val types: List = emptyList() ) { + val duration get() = expiryMode?.duration val isSelf = recipient?.isLocalNumber == true val expiryType get() = expiryMode?.type } diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt index aa1d9a6cfc..f93a97dd58 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/ExpiryMode.kt @@ -1,8 +1,12 @@ package network.loki.messenger.libsession_util.util +import kotlin.time.Duration.Companion.seconds + sealed class ExpiryMode(val expirySeconds: Long) { object NONE: ExpiryMode(0) class Legacy(seconds: Long): ExpiryMode(seconds) // after read class AfterSend(seconds: Long): ExpiryMode(seconds) class AfterRead(seconds: Long): ExpiryMode(seconds) -} \ No newline at end of file + + val duration get() = expirySeconds.seconds +}