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()