Repository: thundernest/k-9
Issue: Canceled account setup leads to additional account in drawer

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

6.311-SNAPSHOT

### Where did you get the app from?

_No response_

### Android version

doesn't matter

### Device model

_No response_

### Steps to reproduce

1. Have at least one account set up already.
2. Go to *Settings*.
3. Tap *Add account*.
4. Enter a syntactically valid email address, e.g. `test@domain.example`. Tap *Next*.
5. Enter a password. Tap *Next*.
6. Press the back button until you're back in the message list.
7. Expand the drawer. See an entry for the account we started to set up.

### Expected behavior

Account should only be displayed in the drawer after account setup was completed.

### Actual behavior

Entry for incomplete account is included in the account list in the drawer.

### Logs

_No response_

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt, app/core/src/main/java/com/fsck/k9/Preferences.kt, app/ui/legacy/src/main/java/com/fsck/k9/account/AccountRemover.kt

diff --git a/app/core/src/main/java/com/fsck/k9/Preferences.kt b/app/core/src/main/java/com/fsck/k9/Preferences.kt
index 7e66d18f8..84517c4cf 100644
--- a/app/core/src/main/java/com/fsck/k9/Preferences.kt
+++ b/app/core/src/main/java/com/fsck/k9/Preferences.kt
@@ -110,6 +110,9 @@ class Preferences internal constructor(
             }
         }
 
+    private val completeAccounts: List<Account>
+        get() = accounts.filter { it.isFinishedSetup }
+
     override fun getAccount(accountUuid: String): Account? {
         synchronized(accountLock) {
             if (accountsMap == null) {
@@ -151,10 +154,10 @@ class Preferences internal constructor(
     @OptIn(ExperimentalCoroutinesApi::class)
     override fun getAccountsFlow(): Flow<List<Account>> {
         return callbackFlow {
-            send(accounts)
+            send(completeAccounts)
 
             val listener = AccountsChangeListener {
-                trySendBlocking(accounts)
+                trySendBlocking(completeAccounts)
             }
             addOnAccountsChangeListener(listener)
 
diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountRemover.kt b/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountRemover.kt
index 483cba3df..7d8ab4513 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountRemover.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/account/AccountRemover.kt
@@ -36,7 +36,7 @@ class AccountRemover(
 
         preferences.deleteAccount(account)
 
-        localKeyStoreManager.deleteCertificates(account)
+        removeCertificates(account)
         Core.setServicesEnabled()
 
         Timber.v("Finished removing account '%s'.", accountName)
@@ -62,4 +62,12 @@ class AccountRemover(
             Timber.e(e, "Failed to reset remote store for account %s", account)
         }
     }
+
+    private fun removeCertificates(account: Account) {
+        try {
+            localKeyStoreManager.deleteCertificates(account)
+        } catch (e: Exception) {
+            Timber.e(e, "Failed to remove certificates for account %s", account)
+        }
+    }
 }
diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt
index b1aca7316..121883d90 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/SettingsListFragment.kt
@@ -77,11 +77,10 @@ class SettingsListFragment : Fragment(), ItemTouchCallback {
 
     private fun populateSettingsList() {
         viewModel.accounts.observeNotNull(this) { accounts ->
-            val accountsFinishedSetup = accounts.filter { it.isFinishedSetup }
-            if (accountsFinishedSetup.isEmpty()) {
+            if (accounts.isEmpty()) {
                 launchOnboarding()
             } else {
-                populateSettingsList(accountsFinishedSetup)
+                populateSettingsList(accounts)
             }
         }
     }