Moves account loading to the IO thread

This commit is contained in:
Vitor Pamplona
2025-11-21 16:37:48 -05:00
parent c5fae65f2d
commit 1f3bd7614e
@@ -65,7 +65,6 @@ import com.vitorpamplona.quartz.utils.Log
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@@ -85,17 +84,14 @@ class AccountStateViewModel : ViewModel() {
fun loginWithDefaultAccountIfLoggedOff() { fun loginWithDefaultAccountIfLoggedOff() {
// pulls account from storage. // pulls account from storage.
if (_accountContent.value !is AccountState.LoggedIn) { if (_accountContent.value !is AccountState.LoggedIn) {
viewModelScope.launch { viewModelScope.launch(Dispatchers.IO) {
loginWithDefaultAccount() loginWithDefaultAccount()
} }
} }
} }
private suspend fun loginWithDefaultAccount(route: Route? = null) { private suspend fun loginWithDefaultAccount(route: Route? = null) {
val accountSettings = val accountSettings = LocalPreferences.loadAccountConfigFromEncryptedStorage()
withContext(Dispatchers.IO) {
LocalPreferences.loadAccountConfigFromEncryptedStorage()
}
if (accountSettings != null) { if (accountSettings != null) {
startUI(accountSettings, route) startUI(accountSettings, route)
@@ -135,7 +131,7 @@ class AccountStateViewModel : ViewModel() {
throw Exception("Invalid key while trying to login with external signer") throw Exception("Invalid key while trying to login with external signer")
} }
val account = val accountSettings =
if (loginWithExternalSigner) { if (loginWithExternalSigner) {
AccountSettings( AccountSettings(
keyPair = KeyPair(pubKey = pubKeyParsed), keyPair = KeyPair(pubKey = pubKeyParsed),
@@ -164,18 +160,18 @@ class AccountStateViewModel : ViewModel() {
) )
} }
LocalPreferences.setDefaultAccount(account) LocalPreferences.setDefaultAccount(accountSettings)
startUI(account) startUI(accountSettings)
} }
@OptIn(FlowPreview::class) fun startUI(
suspend fun startUI(
accountSettings: AccountSettings, accountSettings: AccountSettings,
route: Route? = null, route: Route? = null,
) = withContext(Dispatchers.Main) { ) {
val account = Amethyst.instance.accountsCache.loadAccount(accountSettings)
_accountContent.update { _accountContent.update {
AccountState.LoggedIn(Amethyst.instance.accountsCache.loadAccount(accountSettings), route) AccountState.LoggedIn(account, route)
} }
} }