Add AccountCreator as external dependency for the account setup
This commit is contained in:
parent
88a4b7913f
commit
5384350689
7 changed files with 154 additions and 0 deletions
|
@ -2,6 +2,7 @@ package app.k9mail.feature.preview
|
|||
|
||||
import app.k9mail.core.common.oauth.OAuthConfigurationFactory
|
||||
import app.k9mail.feature.account.setup.featureAccountSetupModule
|
||||
import app.k9mail.feature.preview.account.AccountCreator
|
||||
import app.k9mail.feature.preview.account.AccountOwnerNameProvider
|
||||
import app.k9mail.feature.preview.account.AccountSetupFinishedLauncher
|
||||
import app.k9mail.feature.preview.auth.AndroidKeyStoreDirectoryProvider
|
||||
|
@ -18,6 +19,7 @@ import org.koin.dsl.module
|
|||
|
||||
val accountModule: Module = module {
|
||||
factory { AccountOwnerNameProvider() }
|
||||
factory { AccountCreator() }
|
||||
factory {
|
||||
AccountSetupFinishedLauncher(
|
||||
context = androidContext(),
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package app.k9mail.feature.preview.account
|
||||
|
||||
import app.k9mail.feature.account.setup.domain.ExternalContract
|
||||
import app.k9mail.feature.account.setup.domain.entity.Account
|
||||
import java.util.UUID
|
||||
|
||||
class AccountCreator : ExternalContract.AccountCreator {
|
||||
|
||||
override suspend fun createAccount(account: Account): String {
|
||||
return UUID.randomUUID().toString()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.fsck.k9.account
|
||||
|
||||
import android.content.Context
|
||||
import app.k9mail.feature.account.setup.domain.ExternalContract
|
||||
import app.k9mail.feature.account.setup.domain.entity.Account
|
||||
import com.fsck.k9.Core
|
||||
import com.fsck.k9.Preferences
|
||||
import com.fsck.k9.mailstore.SpecialLocalFoldersCreator
|
||||
|
||||
class AccountCreator(
|
||||
private val accountCreatorHelper: AccountCreatorHelper,
|
||||
private val localFoldersCreator: SpecialLocalFoldersCreator,
|
||||
private val preferences: Preferences,
|
||||
private val context: Context,
|
||||
) : ExternalContract.AccountCreator {
|
||||
|
||||
override suspend fun createAccount(account: Account): String {
|
||||
val newAccount = preferences.newAccount()
|
||||
|
||||
newAccount.email = account.emailAddress
|
||||
newAccount.senderName = account.senderName
|
||||
|
||||
newAccount.incomingServerSettings = account.incomingServerSettings
|
||||
newAccount.outgoingServerSettings = account.outgoingServerSettings
|
||||
|
||||
newAccount.deletePolicy = accountCreatorHelper.getDefaultDeletePolicy(newAccount.incomingServerSettings.type)
|
||||
newAccount.chipColor = accountCreatorHelper.pickColor()
|
||||
|
||||
localFoldersCreator.createSpecialLocalFolders(newAccount)
|
||||
|
||||
newAccount.markSetupFinished()
|
||||
|
||||
preferences.saveAccount(newAccount)
|
||||
|
||||
Core.setServicesEnabled(context)
|
||||
|
||||
return newAccount.uuid
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.fsck.k9.account
|
||||
|
||||
import org.koin.android.ext.koin.androidApplication
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.dsl.module
|
||||
|
||||
|
@ -9,6 +10,16 @@ val newAccountModule = module {
|
|||
preferences = get(),
|
||||
)
|
||||
}
|
||||
|
||||
factory {
|
||||
AccountCreator(
|
||||
accountCreatorHelper = get(),
|
||||
localFoldersCreator = get(),
|
||||
preferences = get(),
|
||||
context = androidApplication()
|
||||
)
|
||||
}
|
||||
|
||||
factory {
|
||||
AccountSetupFinishedLauncher(
|
||||
context = androidContext(),
|
||||
|
|
|
@ -1,6 +1,18 @@
|
|||
package app.k9mail.feature.account.setup.domain
|
||||
|
||||
import app.k9mail.feature.account.setup.domain.entity.Account
|
||||
|
||||
interface ExternalContract {
|
||||
|
||||
fun interface AccountCreator {
|
||||
suspend fun createAccount(account: Account): String
|
||||
|
||||
sealed interface AccountCreatorResult {
|
||||
object Success : AccountCreatorResult
|
||||
data class Error(val message: String) : AccountCreatorResult
|
||||
}
|
||||
}
|
||||
|
||||
fun interface AccountOwnerNameProvider {
|
||||
fun getOwnerName(): String?
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package app.k9mail.feature.account.setup.domain.entity
|
||||
|
||||
import com.fsck.k9.mail.ServerSettings
|
||||
|
||||
data class Account(
|
||||
val emailAddress: String,
|
||||
val incomingServerSettings: ServerSettings,
|
||||
val outgoingServerSettings: ServerSettings,
|
||||
val senderName: String,
|
||||
)
|
|
@ -0,0 +1,68 @@
|
|||
package app.k9mail.feature.account.setup.domain.usecase
|
||||
|
||||
import app.k9mail.feature.account.setup.AccountSetupExternalContract.AccountCreator.AccountCreatorResult
|
||||
import app.k9mail.feature.account.setup.domain.entity.Account
|
||||
import app.k9mail.feature.account.setup.domain.entity.AccountOptions
|
||||
import app.k9mail.feature.account.setup.domain.entity.MailConnectionSecurity
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isEqualTo
|
||||
import com.fsck.k9.mail.AuthType
|
||||
import com.fsck.k9.mail.ServerSettings
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
class CreateAccountTest {
|
||||
|
||||
@Test
|
||||
fun `should successfully create account`() = runTest {
|
||||
var recordedAccount: Account? = null
|
||||
val createAccount = CreateAccount(
|
||||
accountCreator = { account ->
|
||||
recordedAccount = account
|
||||
AccountCreatorResult.Success(accountUuid = "uuid")
|
||||
},
|
||||
)
|
||||
|
||||
val emailAddress = "user@example.com"
|
||||
val incomingServerSettings = ServerSettings(
|
||||
type = "imap",
|
||||
host = "imap.example.com",
|
||||
port = 993,
|
||||
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
|
||||
authenticationType = AuthType.PLAIN,
|
||||
username = "user",
|
||||
password = "password",
|
||||
clientCertificateAlias = null,
|
||||
)
|
||||
val outgoingServerSettings = ServerSettings(
|
||||
type = "smtp",
|
||||
host = "smtp.example.com",
|
||||
port = 465,
|
||||
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
|
||||
authenticationType = AuthType.PLAIN,
|
||||
username = "user",
|
||||
password = "password",
|
||||
clientCertificateAlias = null,
|
||||
)
|
||||
val options = AccountOptions(
|
||||
accountName = "accountName",
|
||||
displayName = "displayName",
|
||||
emailSignature = null,
|
||||
checkFrequencyInMinutes = 15,
|
||||
messageDisplayCount = 25,
|
||||
showNotification = true,
|
||||
)
|
||||
|
||||
val result = createAccount.execute(emailAddress, incomingServerSettings, outgoingServerSettings, options)
|
||||
|
||||
assertThat(result).isEqualTo("uuid")
|
||||
assertThat(recordedAccount).isEqualTo(
|
||||
Account(
|
||||
emailAddress = emailAddress,
|
||||
incomingServerSettings = incomingServerSettings,
|
||||
outgoingServerSettings = outgoingServerSettings,
|
||||
options = options,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue