Moves account saving from StateViewModel to Account

This commit is contained in:
Vitor Pamplona
2025-09-08 17:22:54 -04:00
parent ba0ce3366c
commit 2455a34987
2 changed files with 10 additions and 15 deletions

View File

@@ -24,6 +24,7 @@ import android.util.Log
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.BuildConfig
import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
import com.vitorpamplona.amethyst.logTime
import com.vitorpamplona.amethyst.model.edits.PrivateStorageRelayListDecryptionCache
@@ -209,6 +210,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
@@ -1797,5 +1799,13 @@ class Account(
}
}
}
scope.launch(Dispatchers.IO) {
settings.saveable.debounce(1000).collect {
if (it.accountSettings != null) {
LocalPreferences.saveToEncryptedStorage(it.accountSettings)
}
}
}
}
}

View File

@@ -67,11 +67,9 @@ import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -84,8 +82,6 @@ class AccountStateViewModel : ViewModel() {
private val _accountContent = MutableStateFlow<AccountState>(AccountState.Loading)
val accountContent = _accountContent.asStateFlow()
private var collectorJob: Job? = null
fun loginWithDefaultAccountIfLoggedOff() {
// pulls account from storage.
if (_accountContent.value !is AccountState.LoggedIn) {
@@ -187,22 +183,11 @@ class AccountStateViewModel : ViewModel() {
_accountContent.update {
AccountState.LoggedIn(Amethyst.instance.accountsCache.loadAccount(accountSettings), route)
}
collectorJob?.cancel()
collectorJob =
viewModelScope.launch(Dispatchers.IO) {
accountSettings.saveable.debounce(1000).collect {
if (it.accountSettings != null) {
LocalPreferences.saveToEncryptedStorage(it.accountSettings)
}
}
}
}
private fun prepareLogoutOrSwitch() =
when (val state = _accountContent.value) {
is AccountState.LoggedIn -> {
collectorJob?.cancel()
state.currentViewModelStore.viewModelStore.clear()
}