Merge pull request #7005 from thundernest/add_account_setup_state_propagation

Add account setup state propagation
This commit is contained in:
Wolf-Martell Montwé 2023-06-20 14:57:29 +02:00 committed by GitHub
commit f72bd24fbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 438 additions and 11 deletions

View file

@ -0,0 +1,11 @@
package app.k9mail.feature.account.setup.domain.entity
typealias AutoDiscoveryAuthenticationType = app.k9mail.autodiscovery.api.AuthenticationType
internal fun AutoDiscoveryAuthenticationType.toAuthenticationType(): AuthenticationType {
return when (this) {
AutoDiscoveryAuthenticationType.PasswordCleartext -> AuthenticationType.PasswordCleartext
AutoDiscoveryAuthenticationType.PasswordEncrypted -> AuthenticationType.PasswordEncrypted
AutoDiscoveryAuthenticationType.OAuth2 -> AuthenticationType.OAuth2
}
}

View file

@ -1,3 +1,10 @@
package app.k9mail.feature.account.setup.domain.entity
internal typealias AutoDiscoveryConnectionSecurity = app.k9mail.autodiscovery.api.ConnectionSecurity
internal fun AutoDiscoveryConnectionSecurity.toConnectionSecurity(): ConnectionSecurity {
return when (this) {
AutoDiscoveryConnectionSecurity.StartTLS -> ConnectionSecurity.StartTLS
AutoDiscoveryConnectionSecurity.TLS -> ConnectionSecurity.TLS
}
}

View file

@ -0,0 +1,11 @@
package app.k9mail.feature.account.setup.domain.entity
import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.IncomingServerSettings
internal fun IncomingServerSettings.toIncomingProtocolType(): IncomingProtocolType {
when (this) {
is ImapServerSettings -> return IncomingProtocolType.IMAP
else -> throw IllegalArgumentException("Unsupported incoming server settings type: $this")
}
}

View file

@ -1,6 +1,10 @@
package app.k9mail.feature.account.setup.ui
import app.k9mail.core.ui.compose.common.mvi.UnidirectionalViewModel
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract
import app.k9mail.feature.account.setup.ui.incoming.AccountIncomingConfigContract
import app.k9mail.feature.account.setup.ui.options.AccountOptionsContract
import app.k9mail.feature.account.setup.ui.outgoing.AccountOutgoingConfigContract
interface AccountSetupContract {
@ -19,10 +23,28 @@ interface AccountSetupContract {
sealed interface Event {
object OnNext : Event
data class OnAutoDiscoveryFinished(
val state: AccountAutoDiscoveryContract.State,
) : Event
object OnBack : Event
}
sealed interface Effect {
data class UpdateIncomingConfig(
val state: AccountIncomingConfigContract.State,
) : Effect
data class UpdateOutgoingConfig(
val state: AccountOutgoingConfigContract.State,
) : Effect
data class UpdateOptions(
val state: AccountOptionsContract.State,
) : Effect
object NavigateNext : Effect
object NavigateBack : Effect
}

View file

@ -33,15 +33,18 @@ fun AccountSetupScreen(
) {
val (state, dispatch) = viewModel.observe { effect ->
when (effect) {
Effect.NavigateBack -> onBack()
is Effect.UpdateIncomingConfig -> incomingViewModel.initState(effect.state)
is Effect.UpdateOutgoingConfig -> outgoingViewModel.initState(effect.state)
is Effect.UpdateOptions -> optionsViewModel.initState(effect.state)
Effect.NavigateNext -> onFinish()
Effect.NavigateBack -> onBack()
}
}
when (state.value.setupStep) {
SetupStep.AUTO_CONFIG -> {
AccountAutoDiscoveryScreen(
onNext = { dispatch(Event.OnNext) },
onNext = { dispatch(Event.OnAutoDiscoveryFinished(it)) },
onBack = { dispatch(Event.OnBack) },
viewModel = autoDiscoveryViewModel,
)

View file

@ -6,6 +6,10 @@ import app.k9mail.feature.account.setup.ui.AccountSetupContract.Event
import app.k9mail.feature.account.setup.ui.AccountSetupContract.SetupStep
import app.k9mail.feature.account.setup.ui.AccountSetupContract.State
import app.k9mail.feature.account.setup.ui.AccountSetupContract.ViewModel
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract
import app.k9mail.feature.account.setup.ui.common.mapper.toIncomingConfigState
import app.k9mail.feature.account.setup.ui.common.mapper.toOptionsState
import app.k9mail.feature.account.setup.ui.common.mapper.toOutgoingConfigState
class AccountSetupViewModel(
initialState: State = State(),
@ -13,11 +17,19 @@ class AccountSetupViewModel(
override fun event(event: Event) {
when (event) {
is Event.OnAutoDiscoveryFinished -> handleAutoDiscoveryFinished(event.state)
Event.OnBack -> onBack()
Event.OnNext -> onNext()
}
}
private fun handleAutoDiscoveryFinished(autoDiscoveryState: AccountAutoDiscoveryContract.State) {
emitEffect(Effect.UpdateIncomingConfig(autoDiscoveryState.toIncomingConfigState()))
emitEffect(Effect.UpdateOutgoingConfig(autoDiscoveryState.toOutgoingConfigState()))
emitEffect(Effect.UpdateOptions(autoDiscoveryState.toOptionsState()))
onNext()
}
private fun onBack() {
when (state.value.setupStep) {
SetupStep.AUTO_CONFIG -> navigateBack()

View file

@ -13,13 +13,14 @@ import app.k9mail.core.ui.compose.theme.ThunderbirdTheme
import app.k9mail.feature.account.setup.R
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract.Effect
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract.Event
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract.State
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract.ViewModel
import app.k9mail.feature.account.setup.ui.common.AccountSetupBottomBar
import app.k9mail.feature.account.setup.ui.common.AccountSetupTopHeader
@Composable
internal fun AccountAutoDiscoveryScreen(
onNext: () -> Unit,
onNext: (State) -> Unit,
onBack: () -> Unit,
viewModel: ViewModel,
modifier: Modifier = Modifier,
@ -27,7 +28,7 @@ internal fun AccountAutoDiscoveryScreen(
val (state, dispatch) = viewModel.observe { effect ->
when (effect) {
Effect.NavigateBack -> onBack()
Effect.NavigateNext -> onNext()
Effect.NavigateNext -> onNext(viewModel.state.value)
}
}

View file

@ -0,0 +1,76 @@
package app.k9mail.feature.account.setup.ui.common.mapper
import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.SmtpServerSettings
import app.k9mail.feature.account.setup.domain.entity.ConnectionSecurity
import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType
import app.k9mail.feature.account.setup.domain.entity.toConnectionSecurity
import app.k9mail.feature.account.setup.domain.entity.toImapDefaultPort
import app.k9mail.feature.account.setup.domain.entity.toIncomingProtocolType
import app.k9mail.feature.account.setup.domain.entity.toSmtpDefaultPort
import app.k9mail.feature.account.setup.domain.input.NumberInputField
import app.k9mail.feature.account.setup.domain.input.StringInputField
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract
import app.k9mail.feature.account.setup.ui.incoming.AccountIncomingConfigContract
import app.k9mail.feature.account.setup.ui.options.AccountOptionsContract
import app.k9mail.feature.account.setup.ui.outgoing.AccountOutgoingConfigContract
internal fun AccountAutoDiscoveryContract.State.toIncomingConfigState(): AccountIncomingConfigContract.State {
val incomingSettings = autoDiscoverySettings?.incomingServerSettings as? ImapServerSettings?
return AccountIncomingConfigContract.State(
protocolType = incomingSettings?.toIncomingProtocolType() ?: IncomingProtocolType.DEFAULT,
server = StringInputField(
value = prefillServer(
hostname = incomingSettings?.hostname?.value,
),
),
security = incomingSettings?.connectionSecurity?.toConnectionSecurity() ?: ConnectionSecurity.DEFAULT,
port = NumberInputField(
value = incomingSettings?.port?.value?.toLong() ?: ConnectionSecurity.DEFAULT.toImapDefaultPort(),
),
username = StringInputField(
value = prefillUserName(
emailAddress = emailAddress.value,
username = incomingSettings?.username,
),
),
password = StringInputField(value = password.value),
)
}
internal fun AccountAutoDiscoveryContract.State.toOutgoingConfigState(): AccountOutgoingConfigContract.State {
val outgoingSettings = autoDiscoverySettings?.outgoingServerSettings as? SmtpServerSettings?
return AccountOutgoingConfigContract.State(
server = StringInputField(
value = prefillServer(
hostname = outgoingSettings?.hostname?.value,
),
),
security = outgoingSettings?.connectionSecurity?.toConnectionSecurity() ?: ConnectionSecurity.DEFAULT,
port = NumberInputField(
value = outgoingSettings?.port?.value?.toLong() ?: ConnectionSecurity.DEFAULT.toSmtpDefaultPort(),
),
username = StringInputField(
value = prefillUserName(
emailAddress = emailAddress.value,
username = outgoingSettings?.username,
),
),
password = StringInputField(value = password.value),
)
}
private fun prefillServer(hostname: String?) = hostname.orEmpty()
internal fun prefillUserName(
emailAddress: String,
username: String?,
): String {
return username.takeUnless { it.isNullOrEmpty() } ?: emailAddress
}
internal fun AccountAutoDiscoveryContract.State.toOptionsState(): AccountOptionsContract.State {
return AccountOptionsContract.State(
accountName = StringInputField(value = emailAddress.value),
)
}

View file

@ -0,0 +1,25 @@
package app.k9mail.feature.account.setup.domain.entity
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
class AutoDiscoveryAuthenticationTypeKtTest {
@Test
fun `should map all AutoDiscoveryAuthenticationTypes`() {
val types = AutoDiscoveryAuthenticationType.values()
for (type in types) {
val authenticationType = type.toAuthenticationType()
assertThat(authenticationType).isEqualTo(
when (type) {
AutoDiscoveryAuthenticationType.PasswordCleartext -> AuthenticationType.PasswordCleartext
AutoDiscoveryAuthenticationType.PasswordEncrypted -> AuthenticationType.PasswordEncrypted
AutoDiscoveryAuthenticationType.OAuth2 -> AuthenticationType.OAuth2
},
)
}
}
}

View file

@ -0,0 +1,24 @@
package app.k9mail.feature.account.setup.domain.entity
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
class AutoDiscoveryConnectionSecurityKtTest {
@Test
fun `should map all AutoDiscoveryConnectionSecurities`() {
val securities = AutoDiscoveryConnectionSecurity.values()
for (security in securities) {
val connectionSecurity = security.toConnectionSecurity()
assertThat(connectionSecurity).isEqualTo(
when (security) {
AutoDiscoveryConnectionSecurity.StartTLS -> ConnectionSecurity.StartTLS
AutoDiscoveryConnectionSecurity.TLS -> ConnectionSecurity.TLS
},
)
}
}
}

View file

@ -0,0 +1,25 @@
package app.k9mail.feature.account.setup.domain.entity
import app.k9mail.autodiscovery.api.AuthenticationType
import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.core.common.net.toHostname
import app.k9mail.core.common.net.toPort
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
class IncomingServerSettingsExtensionKtTest {
@Test
fun `should map all ImapServerSettings to IncomingProtocolType IMAP`() {
val imapServerSettings = ImapServerSettings(
hostname = "example.com".toHostname(),
port = 993.toPort(),
connectionSecurity = AutoDiscoveryConnectionSecurity.TLS,
authenticationType = AuthenticationType.PasswordCleartext,
username = "username",
)
assertThat(imapServerSettings.toIncomingProtocolType()).isEqualTo(IncomingProtocolType.IMAP)
}
}

View file

@ -2,19 +2,21 @@ package app.k9mail.feature.account.setup.ui
import app.cash.turbine.testIn
import app.k9mail.core.ui.compose.testing.MainDispatcherRule
import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect.NavigateBack
import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect.NavigateNext
import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect
import app.k9mail.feature.account.setup.ui.AccountSetupContract.SetupStep
import app.k9mail.feature.account.setup.ui.AccountSetupContract.State
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract
import app.k9mail.feature.account.setup.ui.incoming.AccountIncomingConfigContract
import app.k9mail.feature.account.setup.ui.options.AccountOptionsContract
import app.k9mail.feature.account.setup.ui.outgoing.AccountOutgoingConfigContract
import assertk.assertThat
import assertk.assertions.assertThatAndTurbinesConsumed
import assertk.assertions.isEqualTo
import assertk.assertions.prop
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class)
class AccountSetupViewModelTest {
@get:Rule
@ -35,7 +37,16 @@ class AccountSetupViewModelTest {
prop(State::setupStep).isEqualTo(SetupStep.AUTO_CONFIG)
}
viewModel.event(AccountSetupContract.Event.OnNext)
viewModel.event(AccountSetupContract.Event.OnAutoDiscoveryFinished(AccountAutoDiscoveryContract.State()))
assertThat(effectTurbine.awaitItem())
.isEqualTo(Effect.UpdateIncomingConfig(AccountIncomingConfigContract.State()))
assertThat(effectTurbine.awaitItem())
.isEqualTo(Effect.UpdateOutgoingConfig(AccountOutgoingConfigContract.State()))
assertThat(effectTurbine.awaitItem())
.isEqualTo(Effect.UpdateOptions(AccountOptionsContract.State()))
assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(),
@ -68,7 +79,7 @@ class AccountSetupViewModelTest {
actual = effectTurbine.awaitItem(),
turbines = turbines,
) {
isEqualTo(NavigateNext)
isEqualTo(Effect.NavigateNext)
}
}
@ -121,7 +132,7 @@ class AccountSetupViewModelTest {
actual = effectTurbine.awaitItem(),
turbines = turbines,
) {
isEqualTo(NavigateBack)
isEqualTo(Effect.NavigateBack)
}
}
}

View file

@ -0,0 +1,199 @@
package app.k9mail.feature.account.setup.ui.common.mapper
import app.k9mail.autodiscovery.api.AutoDiscoveryResult
import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.SmtpServerSettings
import app.k9mail.core.common.net.toHostname
import app.k9mail.core.common.net.toPort
import app.k9mail.feature.account.setup.domain.entity.AutoDiscoveryAuthenticationType
import app.k9mail.feature.account.setup.domain.entity.AutoDiscoveryConnectionSecurity
import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType
import app.k9mail.feature.account.setup.domain.entity.toConnectionSecurity
import app.k9mail.feature.account.setup.domain.input.NumberInputField
import app.k9mail.feature.account.setup.domain.input.StringInputField
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract
import app.k9mail.feature.account.setup.ui.incoming.AccountIncomingConfigContract
import app.k9mail.feature.account.setup.ui.options.AccountOptionsContract
import app.k9mail.feature.account.setup.ui.outgoing.AccountOutgoingConfigContract
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
class AccountAutoDiscoveryStateMapperKtTest {
@Test
fun `should map to default IncomingConfigState when empty`() {
val incomingConfigState = EMPTY_STATE.toIncomingConfigState()
assertThat(incomingConfigState).isEqualTo(AccountIncomingConfigContract.State())
}
@Test
fun `should map to IncomingConfigState when no AutoDiscovery`() {
val incomingConfigState = EMAIL_PASSWORD_STATE.toIncomingConfigState()
assertThat(incomingConfigState).isEqualTo(
AccountIncomingConfigContract.State(
username = StringInputField(value = EMAIL_ADDRESS),
password = StringInputField(value = PASSWORD),
),
)
}
@Test
fun `should map to IncomingConfigState when AutoDiscovery`() {
val incomingConfigState = AUTO_DISCOVERY_STATE.toIncomingConfigState()
assertThat(incomingConfigState).isEqualTo(
AccountIncomingConfigContract.State(
protocolType = IncomingProtocolType.IMAP,
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_IMAP.value.toLong()),
username = StringInputField(value = AUTO_DISCOVERY_USERNAME),
password = StringInputField(value = PASSWORD),
),
)
}
@Test
fun `should map to email username IncomingConfigState when AutoDiscovery empty username`() {
val incomingConfigState = AUTO_DISCOVERY_STATE_USERNAME_EMPTY.toIncomingConfigState()
assertThat(incomingConfigState).isEqualTo(
AccountIncomingConfigContract.State(
protocolType = IncomingProtocolType.IMAP,
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_IMAP.value.toLong()),
username = StringInputField(value = EMAIL_ADDRESS),
password = StringInputField(value = PASSWORD),
),
)
}
@Test
fun `should map to OutgoingConfigState when empty`() {
val outgoingConfigState = EMPTY_STATE.toOutgoingConfigState()
assertThat(outgoingConfigState).isEqualTo(AccountOutgoingConfigContract.State())
}
@Test
fun `should map to OutgoingConfigState when no AutoDiscovery`() {
val outgoingConfigState = EMAIL_PASSWORD_STATE.toOutgoingConfigState()
assertThat(outgoingConfigState).isEqualTo(
AccountOutgoingConfigContract.State(
username = StringInputField(value = EMAIL_ADDRESS),
password = StringInputField(value = PASSWORD),
),
)
}
@Test
fun `should map to OutgoingConfigState when AutoDiscovery`() {
val outgoingConfigState = AUTO_DISCOVERY_STATE.toOutgoingConfigState()
assertThat(outgoingConfigState).isEqualTo(
AccountOutgoingConfigContract.State(
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_SMTP.value.toLong()),
username = StringInputField(value = AUTO_DISCOVERY_USERNAME),
password = StringInputField(value = PASSWORD),
),
)
}
@Test
fun `should map to email username OutgoingConfigState when AutoDiscovery empty username`() {
val outgoingConfigState = AUTO_DISCOVERY_STATE_USERNAME_EMPTY.toOutgoingConfigState()
assertThat(outgoingConfigState).isEqualTo(
AccountOutgoingConfigContract.State(
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_SMTP.value.toLong()),
username = StringInputField(value = EMAIL_ADDRESS),
password = StringInputField(value = PASSWORD),
),
)
}
@Test
fun `should map to OptionsState when empty`() {
val optionsState = EMPTY_STATE.toOptionsState()
assertThat(optionsState).isEqualTo(AccountOptionsContract.State())
}
@Test
fun `should map to OptionsState when email and password set`() {
val optionsState = EMAIL_PASSWORD_STATE.toOptionsState()
assertThat(optionsState).isEqualTo(
AccountOptionsContract.State(
accountName = StringInputField(value = EMAIL_ADDRESS),
),
)
}
private companion object {
const val EMAIL_ADDRESS = "test@example.com"
const val PASSWORD = "password"
const val SERVER_IMAP = "imap.example.com"
const val SERVER_SMTP = "smtp.example.com"
val AUTO_DISCOVERY_HOSTNAME = "incoming.example.com".toHostname()
val AUTO_DISCOVERY_PORT_IMAP = 143.toPort()
val AUTO_DISCOVERY_PORT_SMTP = 587.toPort()
val AUTO_DISCOVERY_SECURITY = AutoDiscoveryConnectionSecurity.StartTLS
val AUTO_DISCOVERY_AUTHENTICATION = AutoDiscoveryAuthenticationType.PasswordEncrypted
const val AUTO_DISCOVERY_USERNAME = "username"
val EMPTY_STATE = AccountAutoDiscoveryContract.State()
val EMAIL_PASSWORD_STATE = AccountAutoDiscoveryContract.State(
emailAddress = StringInputField(value = EMAIL_ADDRESS),
password = StringInputField(value = PASSWORD),
)
val AUTO_DISCOVERY_STATE = EMAIL_PASSWORD_STATE.copy(
autoDiscoverySettings = AutoDiscoveryResult.Settings(
incomingServerSettings = ImapServerSettings(
hostname = AUTO_DISCOVERY_HOSTNAME,
port = AUTO_DISCOVERY_PORT_IMAP,
connectionSecurity = AUTO_DISCOVERY_SECURITY,
authenticationType = AUTO_DISCOVERY_AUTHENTICATION,
username = AUTO_DISCOVERY_USERNAME,
),
outgoingServerSettings = SmtpServerSettings(
hostname = AUTO_DISCOVERY_HOSTNAME,
port = AUTO_DISCOVERY_PORT_SMTP,
connectionSecurity = AUTO_DISCOVERY_SECURITY,
authenticationType = AUTO_DISCOVERY_AUTHENTICATION,
username = AUTO_DISCOVERY_USERNAME,
),
isTrusted = true,
),
)
val AUTO_DISCOVERY_STATE_USERNAME_EMPTY = AUTO_DISCOVERY_STATE.copy(
autoDiscoverySettings = AUTO_DISCOVERY_STATE.autoDiscoverySettings?.copy(
incomingServerSettings = (
AUTO_DISCOVERY_STATE.autoDiscoverySettings
?.incomingServerSettings as ImapServerSettings
).copy(
username = "",
),
outgoingServerSettings = (
AUTO_DISCOVERY_STATE.autoDiscoverySettings
?.outgoingServerSettings as SmtpServerSettings
).copy(
username = "",
),
),
)
}
}