From ff79919d2a9981d7c73bd3beb94c1a3ed354d58a Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Fri, 3 Feb 2023 19:21:33 +0100 Subject: [PATCH] prevent the app from getting into an invalid state when old shortcuts are used (#3252) --- .../com/keylesspalace/tusky/db/AccountManager.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt b/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt index c4956b814..bf8d414fc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt @@ -45,9 +45,8 @@ class AccountManager @Inject constructor(db: AppDatabase) { init { accounts = accountDao.loadAll().toMutableList() - activeAccount = accounts.find { acc -> - acc.isActive - } + activeAccount = accounts.find { acc -> acc.isActive } + ?: accounts.firstOrNull()?.also { acc -> acc.isActive = true } } /** @@ -169,15 +168,17 @@ class AccountManager @Inject constructor(db: AppDatabase) { */ fun setActiveAccount(accountId: Long) { + val newActiveAccount = accounts.find { (id) -> + id == accountId + } ?: return // invalid accountId passed, do nothing + activeAccount?.let { Log.d(TAG, "setActiveAccount: saving account with id " + it.id) it.isActive = false saveAccount(it) } - activeAccount = accounts.find { (id) -> - id == accountId - } + activeAccount = newActiveAccount activeAccount?.let { it.isActive = true