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 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) {
@@ -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<List<ServerName>>(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()
@@ -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<List<ServerName>>(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()