Removes duplicated Loading of Relay Info

This commit is contained in:
Vitor Pamplona
2025-09-03 15:44:23 -04:00
parent c64e65ddb4
commit c9cdc74c91
2 changed files with 30 additions and 27 deletions
@@ -56,6 +56,8 @@ object Nip11CachedRetriever {
class Empty( class Empty(
data: Nip11RelayInformation, data: Nip11RelayInformation,
) : RetrieveResult(data, TimeUtils.now()) ) : RetrieveResult(data, TimeUtils.now())
fun isValid() = time > TimeUtils.oneHourAgo()
} }
private val relayInformationEmptyCache = LruCache<NormalizedRelayUrl, Nip11RelayInformation>(1000) private val relayInformationEmptyCache = LruCache<NormalizedRelayUrl, Nip11RelayInformation>(1000)
@@ -79,16 +81,18 @@ object Nip11CachedRetriever {
fun getFromCache(relay: NormalizedRelayUrl): Nip11RelayInformation { fun getFromCache(relay: NormalizedRelayUrl): Nip11RelayInformation {
val result = relayInformationDocumentCache.get(relay) val result = relayInformationDocumentCache.get(relay)
if (result == null) {
// resets the clock
val empty = getEmpty(relay)
relayInformationDocumentCache.put(relay, RetrieveResult.Empty(empty))
return empty
}
return when (result) { return when (result) {
is RetrieveResult.Success -> return result.data is RetrieveResult.Success -> result.data
is RetrieveResult.Error -> return result.data is RetrieveResult.Error -> result.data
is RetrieveResult.Empty -> return result.data is RetrieveResult.Empty -> result.data
is RetrieveResult.Loading -> return result.data is RetrieveResult.Loading -> result.data
else -> {
val empty = getEmpty(relay)
relayInformationDocumentCache.put(relay, RetrieveResult.Empty(empty))
empty
}
} }
} }
@@ -100,23 +104,23 @@ object Nip11CachedRetriever {
) { ) {
val doc = relayInformationDocumentCache.get(relay) val doc = relayInformationDocumentCache.get(relay)
if (doc != null) { if (doc != null) {
if (doc is RetrieveResult.Success) { when (doc) {
onInfo(doc.data) is RetrieveResult.Success -> onInfo(doc.data)
} else if (doc is RetrieveResult.Loading) { is RetrieveResult.Loading -> {
if (TimeUtils.now() - doc.time < TimeUtils.ONE_MINUTE) { if (doc.isValid()) {
// just wait. // just wait.
} else { } else {
retrieve(relay, okHttpClient, onInfo, onError) retrieve(relay, okHttpClient, onInfo, onError)
}
} }
} else if (doc is RetrieveResult.Error) { is RetrieveResult.Error -> {
if (TimeUtils.now() - doc.time < TimeUtils.ONE_HOUR) { if (doc.isValid()) {
onError(relay, doc.error, null) onError(relay, doc.error, null)
} else { } else {
retrieve(relay, okHttpClient, onInfo, onError) retrieve(relay, okHttpClient, onInfo, onError)
}
} }
} else { is RetrieveResult.Empty -> retrieve(relay, okHttpClient, onInfo, onError)
// Empty
retrieve(relay, okHttpClient, onInfo, onError)
} }
} else { } else {
retrieve(relay, okHttpClient, onInfo, onError) retrieve(relay, okHttpClient, onInfo, onError)
@@ -135,10 +139,12 @@ object Nip11CachedRetriever {
okHttpClient = okHttpClient, okHttpClient = okHttpClient,
onInfo = { onInfo = {
relayInformationDocumentCache.put(relay, RetrieveResult.Success(it)) relayInformationDocumentCache.put(relay, RetrieveResult.Success(it))
relayInformationEmptyCache.remove(relay)
onInfo(it) onInfo(it)
}, },
onError = { relay, code, errorMsg -> onError = { relay, code, errorMsg ->
relayInformationDocumentCache.put(relay, RetrieveResult.Error(getEmpty(relay), code, errorMsg)) relayInformationDocumentCache.put(relay, RetrieveResult.Error(getEmpty(relay), code, errorMsg))
relayInformationEmptyCache.remove(relay)
onError(relay, code, errorMsg) onError(relay, code, errorMsg)
}, },
) )
@@ -26,7 +26,6 @@ import com.vitorpamplona.amethyst.model.FeatureSetType
import com.vitorpamplona.amethyst.ui.navigation.navs.INav import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.navigation.routes.Route import com.vitorpamplona.amethyst.ui.navigation.routes.Route
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.publicChannels.ephemChat.header.loadRelayInfo
@Composable @Composable
fun BasicRelaySetupInfoDialog( fun BasicRelaySetupInfoDialog(
@@ -35,8 +34,6 @@ fun BasicRelaySetupInfoDialog(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: INav, nav: INav,
) { ) {
val relayInfo by loadRelayInfo(item.relay, accountViewModel)
BasicRelaySetupInfoClickableRow( BasicRelaySetupInfoClickableRow(
item = item, item = item,
loadProfilePicture = accountViewModel.settings.showProfilePictures.value, loadProfilePicture = accountViewModel.settings.showProfilePictures.value,