Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 496a64c0 authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

Change name to AccountStateLoader

parent f0353eb7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ val accountModule: Module = module {
    single { InMemoryAccountStore() }
        .binds(
            arrayOf(
                AccountCommonExternalContract.AccountLoader::class,
                AccountCommonExternalContract.AccountStateLoader::class,
                AccountSetupExternalContract.AccountCreator::class,
                AccountEditExternalContract.AccountUpdater::class,
            ),
+3 −3
Original line number Diff line number Diff line
package app.k9mail.feature.preview.account

import app.k9mail.feature.account.common.AccountCommonExternalContract.AccountLoader
import app.k9mail.feature.account.common.AccountCommonExternalContract.AccountStateLoader
import app.k9mail.feature.account.common.domain.entity.Account
import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthorizationState
@@ -11,13 +11,13 @@ import app.k9mail.feature.account.setup.AccountSetupExternalContract.AccountCrea

class InMemoryAccountStore(
    private val accountMap: MutableMap<String, Account> = mutableMapOf(),
) : AccountCreator, AccountUpdater, AccountLoader {
) : AccountCreator, AccountUpdater, AccountStateLoader {

    suspend fun load(accountUuid: String): Account? {
        return accountMap[accountUuid]
    }

    override suspend fun loadAccount(accountUuid: String): AccountState? {
    override suspend fun loadAccountState(accountUuid: String): AccountState? {
        return accountMap[accountUuid]?.let { mapToAccountState(it) }
    }

+2 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ val newAccountModule = module {
        )
    }

    factory<AccountCommonExternalContract.AccountLoader> {
        AccountLoader(
    factory<AccountCommonExternalContract.AccountStateLoader> {
        AccountStateLoader(
            accountManager = get(),
        )
    }
+5 −5
Original line number Diff line number Diff line
@@ -10,13 +10,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.fsck.k9.Account as K9Account

class AccountLoader(
class AccountStateLoader(
    private val accountManager: AccountManager,
    private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : AccountCommonExternalContract.AccountLoader {
) : AccountCommonExternalContract.AccountStateLoader {

    @Suppress("TooGenericExceptionCaught")
    override suspend fun loadAccount(accountUuid: String): AccountState? {
    override suspend fun loadAccountState(accountUuid: String): AccountState? {
        return try {
            withContext(coroutineDispatcher) {
                load(accountUuid)
@@ -29,10 +29,10 @@ class AccountLoader(
    }

    private fun load(accountUuid: String): AccountState? {
        return accountManager.getAccount(accountUuid)?.let { mapToAccount(it) }
        return accountManager.getAccount(accountUuid)?.let { mapToAccountState(it) }
    }

    private fun mapToAccount(account: K9Account): AccountState {
    private fun mapToAccountState(account: K9Account): AccountState {
        return AccountState(
            uuid = account.uuid,
            emailAddress = account.email,
+14 −16
Original line number Diff line number Diff line
@@ -5,43 +5,41 @@ import app.k9mail.feature.account.common.domain.entity.AuthorizationState
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import com.fsck.k9.Account
import com.fsck.k9.Identity
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import kotlinx.coroutines.test.runTest
import org.junit.Test
import com.fsck.k9.Account as K9Account

class AccountLoaderTest {
class AccountStateLoaderTest {

    @Test
    fun `loadAccount() should return null when accountManager returns null`() = runTest {
    fun `loadAccountState() should return null when accountManager returns null`() = runTest {
        val accountManager = FakeAccountManager()
        val accountLoader = AccountLoader(accountManager)
        val accountLoader = AccountStateLoader(accountManager)

        val result = accountLoader.loadAccount("accountUuid")
        val result = accountLoader.loadAccountState("accountUuid")

        assertThat(result).isNull()
    }

    @Test
    fun `loadAccount() should return account when present in accountManager`() = runTest {
    fun `loadAccountState() should return account when present in accountManager`() = runTest {
        val accounts = mapOf(
            "accountUuid" to K9Account(
                uuid = "accountUuid",
            ).also {
                it.identities = mutableListOf(Identity())
                it.email = "emailAddress"
                it.incomingServerSettings = INCOMING_SERVER_SETTINGS
                it.outgoingServerSettings = OUTGOING_SERVER_SETTINGS
                it.oAuthState = "oAuthState"
            "accountUuid" to Account(uuid = "accountUuid").apply {
                identities = mutableListOf(Identity())
                email = "emailAddress"
                incomingServerSettings = INCOMING_SERVER_SETTINGS
                outgoingServerSettings = OUTGOING_SERVER_SETTINGS
                oAuthState = "oAuthState"
            },
        )
        val accountManager = FakeAccountManager(accounts = accounts)
        val accountLoader = AccountLoader(accountManager)
        val accountLoader = AccountStateLoader(accountManager)

        val result = accountLoader.loadAccount("accountUuid")
        val result = accountLoader.loadAccountState("accountUuid")

        assertThat(result).isEqualTo(
            AccountState(
Loading