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.

This commit is contained in:
Vitor Pamplona
2025-11-11 16:01:19 -05:00
parent 1589b646a8
commit b14c588a34
3 changed files with 26 additions and 13 deletions
@@ -52,9 +52,12 @@ fun AllMediaServersScreen(
val nip96ServersViewModel: NIP96ServersViewModel = viewModel() val nip96ServersViewModel: NIP96ServersViewModel = viewModel()
val blossomServersViewModel: BlossomServersViewModel = viewModel() val blossomServersViewModel: BlossomServersViewModel = viewModel()
LaunchedEffect(key1 = Unit) { nip96ServersViewModel.init(accountViewModel)
nip96ServersViewModel.load(accountViewModel.account) blossomServersViewModel.init(accountViewModel)
blossomServersViewModel.load(accountViewModel.account)
LaunchedEffect(key1 = accountViewModel) {
nip96ServersViewModel.load()
blossomServersViewModel.load()
} }
MediaServersScaffold(nip96ServersViewModel, blossomServersViewModel) { MediaServersScaffold(nip96ServersViewModel, blossomServersViewModel) {
@@ -23,23 +23,28 @@ package com.vitorpamplona.amethyst.ui.actions.mediaServers
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.Account 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.Log
import com.vitorpamplona.quartz.utils.Rfc3986 import com.vitorpamplona.quartz.utils.Rfc3986
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class BlossomServersViewModel : ViewModel() { class BlossomServersViewModel : ViewModel() {
lateinit var account: Account private lateinit var accountViewModel: AccountViewModel
private lateinit var account: Account
private val _fileServers = MutableStateFlow<List<ServerName>>(emptyList()) private val _fileServers = MutableStateFlow<List<ServerName>>(emptyList())
val fileServers = _fileServers.asStateFlow() val fileServers = _fileServers.asStateFlow()
private var isModified = false private var isModified = false
fun load(account: Account) { fun init(accountViewModel: AccountViewModel) {
this.account = account this.accountViewModel = accountViewModel
this.account = accountViewModel.account
}
fun load() {
refresh() refresh()
} }
@@ -119,7 +124,7 @@ class BlossomServersViewModel : ViewModel() {
fun saveFileServers() { fun saveFileServers() {
if (isModified) { if (isModified) {
viewModelScope.launch(Dispatchers.IO) { accountViewModel.runIOCatching {
val serverList = _fileServers.value.map { it.baseUrl } val serverList = _fileServers.value.map { it.baseUrl }
account.sendBlossomServersList(serverList) account.sendBlossomServersList(serverList)
refresh() refresh()
@@ -23,23 +23,28 @@ package com.vitorpamplona.amethyst.ui.actions.mediaServers
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.Account 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.Log
import com.vitorpamplona.quartz.utils.Rfc3986 import com.vitorpamplona.quartz.utils.Rfc3986
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class NIP96ServersViewModel : ViewModel() { class NIP96ServersViewModel : ViewModel() {
lateinit var account: Account private lateinit var accountViewModel: AccountViewModel
private lateinit var account: Account
private val _fileServers = MutableStateFlow<List<ServerName>>(emptyList()) private val _fileServers = MutableStateFlow<List<ServerName>>(emptyList())
val fileServers = _fileServers.asStateFlow() val fileServers = _fileServers.asStateFlow()
private var isModified = false private var isModified = false
fun load(account: Account) { fun init(accountViewModel: AccountViewModel) {
this.account = account this.accountViewModel = accountViewModel
this.account = accountViewModel.account
}
fun load() {
refresh() refresh()
} }
@@ -114,7 +119,7 @@ class NIP96ServersViewModel : ViewModel() {
fun saveFileServers() { fun saveFileServers() {
if (isModified) { if (isModified) {
viewModelScope.launch(Dispatchers.IO) { accountViewModel.runIOCatching {
val serverList = _fileServers.value.map { it.baseUrl } val serverList = _fileServers.value.map { it.baseUrl }
account.sendFileServersList(serverList) account.sendFileServersList(serverList)
refresh() refresh()