From a8a97b68347543725d3c98dacf2bc4855ef1642b Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Tue, 5 Jul 2022 18:18:12 +0200 Subject: [PATCH] fix relogin bug (#2609) * fix relogin bug * fix ktlint --- .../keylesspalace/tusky/db/AccountManager.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 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 04f8e6f5..9c5e118b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt @@ -72,14 +72,18 @@ class AccountManager @Inject constructor(db: AppDatabase) { accountDao.insertOrReplace(it) } // check if this is a relogin with an existing account, if yes update it, otherwise create a new one - val newAccountEntity = accounts.find { account -> + val existingAccountIndex = accounts.indexOfFirst { account -> domain == account.domain && newAccount.id == account.accountId - }?.copy( - accessToken = accessToken, - clientId = clientId, - clientSecret = clientSecret, - oauthScopes = oauthScopes - ) ?: run { + } + val newAccountEntity = if (existingAccountIndex != -1) { + accounts[existingAccountIndex].copy( + accessToken = accessToken, + clientId = clientId, + clientSecret = clientSecret, + oauthScopes = oauthScopes, + isActive = true + ).also { accounts[existingAccountIndex] = it } + } else { val maxAccountId = accounts.maxByOrNull { it.id }?.id ?: 0 val newAccountId = maxAccountId + 1 AccountEntity(