From cfbf113910a248d08aaf531da31e14117ea32797 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 3 Jul 2025 13:08:27 -0400 Subject: [PATCH] Fixes crash on lazy flows when uploading images before the relay list is loaded. --- .../vitorpamplona/amethyst/model/Account.kt | 4 +--- .../FileStorageServerListState.kt | 2 +- .../nipB7Blossom/BlossomServerListState.kt | 2 +- .../model/serverList/MergedServerListState.kt | 20 +++++++++---------- .../mediaServers/BlossomServersViewModel.kt | 2 +- .../mediaServers/NIP96ServersViewModel.kt | 2 +- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 8a20110a82..1a2b26af04 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -102,8 +102,6 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer -import com.vitorpamplona.quartz.nip01Core.relay.normalizer.isLocalHost -import com.vitorpamplona.quartz.nip01Core.relay.normalizer.isOnion import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal @@ -260,7 +258,7 @@ class Account( val blossomServers = BlossomServerListState(signer, cache, scope, settings) val fileStorageServers = FileStorageServerListState(signer, cache, scope, settings) - val serverLists = MergedServerListState(fileStorageServers.fileServers, blossomServers.fileServers, scope) + val serverLists = MergedServerListState(fileStorageServers, blossomServers, scope) // Relay settings val outboxRelays = AccountOutboxRelayState(nip65RelayList, privateStorageRelayList, localRelayList, scope) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt index 079e096bf5..bccbc05725 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt @@ -55,7 +55,7 @@ class FileStorageServerListState( return event?.servers() ?: emptyList() } - val fileServers = + val flow = getFileServersListFlow() .map { normalizeServers(it.note) } .onStart { emit(normalizeServers(getFileServersNote())) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt index 8cf1196f16..a85941bade 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt @@ -56,7 +56,7 @@ class BlossomServerListState( return event?.servers() ?: emptyList() } - val fileServers = + val flow = getBlossomServersListFlow() .map { normalizeServers(it.note) } .onStart { emit(normalizeServers(getBlossomServersNote())) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/serverList/MergedServerListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/serverList/MergedServerListState.kt index 599487b36f..578e912ed7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/serverList/MergedServerListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/serverList/MergedServerListState.kt @@ -20,7 +20,8 @@ */ package com.vitorpamplona.amethyst.model.serverList -import android.R.attr.host +import com.vitorpamplona.amethyst.model.nip96FileStorage.FileStorageServerListState +import com.vitorpamplona.amethyst.model.nipB7Blossom.BlossomServerListState import com.vitorpamplona.amethyst.ui.actions.mediaServers.DEFAULT_MEDIA_SERVERS import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerName import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerType @@ -30,14 +31,13 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flowOn -import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import org.czeal.rfc3986.URIReference class MergedServerListState( - val fileServers: StateFlow>, - val blossomServers: StateFlow>, + val fileServers: FileStorageServerListState, + val blossomServers: BlossomServerListState, val scope: CoroutineScope, ) { fun host(url: String): String = @@ -59,15 +59,15 @@ class MergedServerListState( return result + ServerName("NIP95", "", ServerType.NIP95) } - val liveServerList: StateFlow> by lazy { - combine(fileServers, blossomServers) { nip96s, blossoms -> + val liveServerList: StateFlow> = + combine(fileServers.flow, blossomServers.flow) { nip96s, blossoms -> mergeServerList(nip96s, blossoms) - }.onStart { emit(mergeServerList(fileServers.value, blossomServers.value)) } - .flowOn(Dispatchers.Default) + }.onStart { + emit(mergeServerList(fileServers.flow.value, blossomServers.flow.value)) + }.flowOn(Dispatchers.Default) .stateIn( scope, SharingStarted.Eagerly, - emptyList(), + DEFAULT_MEDIA_SERVERS, ) - } } 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 8172f7fea8..6eab5aadb2 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 @@ -127,5 +127,5 @@ class BlossomServersViewModel : ViewModel() { } } - private fun obtainFileServers(): List? = account.blossomServers.fileServers.value + private fun obtainFileServers(): List? = account.blossomServers.flow.value } 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 bd50c97e97..0d8c32b19a 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 @@ -122,5 +122,5 @@ class NIP96ServersViewModel : ViewModel() { } } - private fun obtainFileServers(): List? = account.fileStorageServers.fileServers.value + private fun obtainFileServers(): List? = account.fileStorageServers.flow.value }