Correctly sorts by online status when the app has already cached the status of a streaming

This commit is contained in:
Vitor Pamplona
2025-09-02 18:34:21 -04:00
parent eef0adc6f9
commit c46cf79501
2 changed files with 21 additions and 4 deletions

View File

@@ -41,6 +41,12 @@ import kotlin.coroutines.cancellation.CancellationException
object OnlineChecker { object OnlineChecker {
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100) val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
fun isCachedAndOffline(url: String?): Boolean {
if (url.isNullOrBlank()) return false
val cached = checkOnlineCache.get(url)
return cached != null && !cached.online && cached.timeInSecs > TimeUtils.fiveMinutesAgo()
}
fun isOnlineCached(url: String?): Boolean { fun isOnlineCached(url: String?): Boolean {
if (url.isNullOrBlank()) return false if (url.isNullOrBlank()) return false
val cached = checkOnlineCache.get(url) val cached = checkOnlineCache.get(url)

View File

@@ -31,6 +31,7 @@ import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.author.AuthorsByPr
import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.community.SingleCommunityTopNavFilter import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.community.SingleCommunityTopNavFilter
import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.muted.MutedAuthorsByOutboxTopNavFilter import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.muted.MutedAuthorsByOutboxTopNavFilter
import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.muted.MutedAuthorsByProxyTopNavFilter import com.vitorpamplona.amethyst.model.topNavFeeds.noteBased.muted.MutedAuthorsByProxyTopNavFilter
import com.vitorpamplona.amethyst.service.OnlineChecker
import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter
import com.vitorpamplona.amethyst.ui.dal.FilterByListParams import com.vitorpamplona.amethyst.ui.dal.FilterByListParams
import com.vitorpamplona.quartz.nip51Lists.muteList.MuteListEvent import com.vitorpamplona.quartz.nip51Lists.muteList.MuteListEvent
@@ -102,7 +103,7 @@ open class DiscoverLiveFeedFilter(
return items return items
.sortedWith( .sortedWith(
compareBy( compareBy(
{ convertStatusToOrder((it.event as? LiveActivitiesEvent)?.status()) }, { convertStatusToOrder((it.event as? LiveActivitiesEvent)) },
{ participantCounts[it] }, { participantCounts[it] },
{ allParticipants[it] }, { allParticipants[it] },
{ (it.event as? LiveActivitiesEvent)?.starts() ?: it.createdAt() }, { (it.event as? LiveActivitiesEvent)?.starts() ?: it.createdAt() },
@@ -111,11 +112,21 @@ open class DiscoverLiveFeedFilter(
).reversed() ).reversed()
} }
fun convertStatusToOrder(status: StatusTag.STATUS?): Int = fun convertStatusToOrder(event: LiveActivitiesEvent?): Int {
when (status) { if (event == null) return 0
StatusTag.STATUS.LIVE -> 2 val url = event.streaming()
if (url == null) return 0
return when (event.status()) {
StatusTag.STATUS.LIVE -> {
if (OnlineChecker.isCachedAndOffline(url)) {
0
} else {
2
}
}
StatusTag.STATUS.PLANNED -> 1 StatusTag.STATUS.PLANNED -> 1
StatusTag.STATUS.ENDED -> 0 StatusTag.STATUS.ENDED -> 0
else -> 0 else -> 0
} }
}
} }