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

Commit eb30b5f2 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

mail: Remove unused accounts on start

parent 1754a3ce
Loading
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -100,18 +100,27 @@ internal class EeloAccountCreator(context: Context) : KoinComponent {
        return true
    }

    private fun deleteIncompleteAccounts() {
        val accounts = preferences.getAccounts()
    private fun deleteIncompleteAccounts(accounts: List<Account>, accountManager: AccountManager) {
        accounts.filter { !it.isFinishedSetup }.forEach {
            accountRemover.removeAccountAsync(it.uuid)
        }

        // Fetch all accounts from the system's AccountManager
        AccountManagerConstants.ACCOUNT_TYPES.forEach { accountType ->
            val systemAccounts = accountManager.getAccountsByType(accountType).map { it.name }

            // Remove accounts from preferences if their name is not in system accounts
            accounts.filter { account -> account.name !in systemAccounts }.forEach {
                accountRemover.removeAccountAsync(it.uuid)
            }
        }
    }

    fun loadAccountsFromAccountManager(): Boolean {
        try {
            val accountManager = AccountManager.get(mContext)
            val accounts = preferences.getAccounts()
            deleteIncompleteAccounts()
            deleteIncompleteAccounts(accounts, accountManager)
            AccountManagerConstants.ACCOUNT_TYPES
                .forEach { accountType ->
                    loadAccounts(