From 37ea82bd8fb7e823e433fd3d36760a0f90ffdaeb Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 3 Jul 2023 17:16:00 +0200 Subject: [PATCH] Don't show OAuth 2.0 option for POP3 accounts --- .../setup/domain/entity/AuthenticationType.kt | 7 ---- .../incoming/AccountIncomingConfigContent.kt | 3 +- .../AccountIncomingConfigStateExtensions.kt | 32 +++++++++++++++++++ .../AccountIncomingConfigViewModel.kt | 8 +++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/domain/entity/AuthenticationType.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/domain/entity/AuthenticationType.kt index 6d7d968df..5463562cd 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/domain/entity/AuthenticationType.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/domain/entity/AuthenticationType.kt @@ -27,13 +27,6 @@ enum class AuthenticationType( val DEFAULT = PasswordCleartext fun all() = values().toList().toImmutableList() - fun incoming() = listOf( - PasswordCleartext, - PasswordEncrypted, - ClientCertificate, - OAuth2, - ).toImmutableList() - fun outgoing() = all() } } diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigContent.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigContent.kt index 5665704be..78625352c 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigContent.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigContent.kt @@ -26,7 +26,6 @@ import app.k9mail.core.ui.compose.theme.K9Theme import app.k9mail.core.ui.compose.theme.MainTheme import app.k9mail.core.ui.compose.theme.ThunderbirdTheme import app.k9mail.feature.account.setup.R -import app.k9mail.feature.account.setup.domain.entity.AuthenticationType import app.k9mail.feature.account.setup.domain.entity.ConnectionSecurity import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType import app.k9mail.feature.account.setup.ui.common.item.ErrorItem @@ -136,7 +135,7 @@ internal fun AccountIncomingConfigContent( item { SelectInput( - options = AuthenticationType.incoming(), + options = state.allowedAuthenticationTypes, optionToStringTransformation = { it.toResourceString(resources) }, selectedOption = state.authenticationType, onOptionChange = { onEvent(Event.AuthenticationTypeChanged(it)) }, diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigStateExtensions.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigStateExtensions.kt index 55491d2d3..9956324bf 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigStateExtensions.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigStateExtensions.kt @@ -1,4 +1,36 @@ package app.k9mail.feature.account.setup.ui.incoming +import app.k9mail.feature.account.setup.domain.entity.AuthenticationType +import app.k9mail.feature.account.setup.domain.entity.AuthenticationType.ClientCertificate +import app.k9mail.feature.account.setup.domain.entity.AuthenticationType.OAuth2 +import app.k9mail.feature.account.setup.domain.entity.AuthenticationType.PasswordCleartext +import app.k9mail.feature.account.setup.domain.entity.AuthenticationType.PasswordEncrypted +import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType +import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.toImmutableList + internal val AccountIncomingConfigContract.State.isPasswordFieldVisible: Boolean get() = authenticationType.isPasswordRequired + +internal val AccountIncomingConfigContract.State.allowedAuthenticationTypes: ImmutableList + get() = protocolType.allowedAuthenticationTypes.toImmutableList() + +internal val IncomingProtocolType.allowedAuthenticationTypes: List + get() = when (this) { + IncomingProtocolType.IMAP -> { + listOf( + PasswordCleartext, + PasswordEncrypted, + ClientCertificate, + OAuth2, + ) + } + + IncomingProtocolType.POP3 -> { + listOf( + PasswordCleartext, + PasswordEncrypted, + ClientCertificate, + ) + } + } diff --git a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigViewModel.kt b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigViewModel.kt index b452716fe..8ed1f8f54 100644 --- a/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigViewModel.kt +++ b/feature/account/setup/src/main/kotlin/app/k9mail/feature/account/setup/ui/incoming/AccountIncomingConfigViewModel.kt @@ -69,12 +69,20 @@ internal class AccountIncomingConfigViewModel( private fun updateProtocolType(protocolType: IncomingProtocolType) { updateState { + val allowedAuthenticationTypesForNewProtocol = protocolType.allowedAuthenticationTypes + val newAuthenticationType = if (it.authenticationType in allowedAuthenticationTypesForNewProtocol) { + it.authenticationType + } else { + allowedAuthenticationTypesForNewProtocol.first() + } + it.copy( protocolType = protocolType, security = protocolType.defaultConnectionSecurity, port = it.port.updateValue( protocolType.toDefaultPort(protocolType.defaultConnectionSecurity), ), + authenticationType = newAuthenticationType, ) } }