From b14c588a34df8076a8dbf125cf67b9e37ad804cf Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 11 Nov 2025 16:01:19 -0500 Subject: [PATCH] Moves the dispatcher of the save event for server models from each of the viewmodels to the account view model since the scope gets cancelled when the screen closes. --- .../actions/mediaServers/AllMediaServersScreen.kt | 9 ++++++--- .../mediaServers/BlossomServersViewModel.kt | 15 ++++++++++----- .../actions/mediaServers/NIP96ServersViewModel.kt | 15 ++++++++++----- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/AllMediaServersScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/AllMediaServersScreen.kt index 587fab25f..5639688de 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/AllMediaServersScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/AllMediaServersScreen.kt @@ -52,9 +52,12 @@ fun AllMediaServersScreen( val nip96ServersViewModel: NIP96ServersViewModel = viewModel() val blossomServersViewModel: BlossomServersViewModel = viewModel() - LaunchedEffect(key1 = Unit) { - nip96ServersViewModel.load(accountViewModel.account) - blossomServersViewModel.load(accountViewModel.account) + nip96ServersViewModel.init(accountViewModel) + blossomServersViewModel.init(accountViewModel) + + LaunchedEffect(key1 = accountViewModel) { + nip96ServersViewModel.load() + blossomServersViewModel.load() } MediaServersScaffold(nip96ServersViewModel, blossomServersViewModel) { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/BlossomServersViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/BlossomServersViewModel.kt index cde4dc856..8d1d601b9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/BlossomServersViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/BlossomServersViewModel.kt @@ -23,23 +23,28 @@ package com.vitorpamplona.amethyst.ui.actions.mediaServers import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.model.Account +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.Rfc3986 -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch class BlossomServersViewModel : ViewModel() { - lateinit var account: Account + private lateinit var accountViewModel: AccountViewModel + private lateinit var account: Account private val _fileServers = MutableStateFlow>(emptyList()) val fileServers = _fileServers.asStateFlow() private var isModified = false - fun load(account: Account) { - this.account = account + fun init(accountViewModel: AccountViewModel) { + this.accountViewModel = accountViewModel + this.account = accountViewModel.account + } + + fun load() { refresh() } @@ -119,7 +124,7 @@ class BlossomServersViewModel : ViewModel() { fun saveFileServers() { if (isModified) { - viewModelScope.launch(Dispatchers.IO) { + accountViewModel.runIOCatching { val serverList = _fileServers.value.map { it.baseUrl } account.sendBlossomServersList(serverList) refresh() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/NIP96ServersViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/NIP96ServersViewModel.kt index 641b7be15..c09bd7eb9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/NIP96ServersViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/mediaServers/NIP96ServersViewModel.kt @@ -23,23 +23,28 @@ package com.vitorpamplona.amethyst.ui.actions.mediaServers import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.model.Account +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.Rfc3986 -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch class NIP96ServersViewModel : ViewModel() { - lateinit var account: Account + private lateinit var accountViewModel: AccountViewModel + private lateinit var account: Account private val _fileServers = MutableStateFlow>(emptyList()) val fileServers = _fileServers.asStateFlow() private var isModified = false - fun load(account: Account) { - this.account = account + fun init(accountViewModel: AccountViewModel) { + this.accountViewModel = accountViewModel + this.account = accountViewModel.account + } + + fun load() { refresh() } @@ -114,7 +119,7 @@ class NIP96ServersViewModel : ViewModel() { fun saveFileServers() { if (isModified) { - viewModelScope.launch(Dispatchers.IO) { + accountViewModel.runIOCatching { val serverList = _fileServers.value.map { it.baseUrl } account.sendFileServersList(serverList) refresh()