Remove a compose recomposition between the started state and the isOnline state that is already cached.

This commit is contained in:
Vitor Pamplona 2023-11-03 17:14:10 -04:00
parent ef90b82396
commit e0d2a71b47
2 changed files with 18 additions and 2 deletions

View File

@ -13,6 +13,14 @@ object OnlineChecker {
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
val fiveMinutes = 1000 * 60 * 5
fun isOnlineCached(url: String?): Boolean {
if (url.isNullOrBlank()) return false
if ((checkOnlineCache.get(url)?.timeInMs ?: 0) > System.currentTimeMillis() - fiveMinutes) {
return checkOnlineCache.get(url).online
}
return false
}
fun isOnline(url: String?): Boolean {
checkNotInMainThread()

View File

@ -27,6 +27,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.service.NostrHomeDataSource
import com.vitorpamplona.amethyst.service.OnlineChecker
import com.vitorpamplona.amethyst.ui.navigation.Route
import com.vitorpamplona.amethyst.ui.note.UpdateZapAmountDialog
import com.vitorpamplona.amethyst.ui.screen.FeedViewModel
@ -166,7 +167,11 @@ private fun HomePages(
@Composable
fun CheckIfUrlIsOnline(url: String, accountViewModel: AccountViewModel, whenOnline: @Composable () -> Unit) {
var online by remember { mutableStateOf(false) }
var online by remember {
mutableStateOf(
OnlineChecker.isOnlineCached(url)
)
}
LaunchedEffect(key1 = url) {
accountViewModel.checkIsOnline(url) { isOnline ->
@ -176,7 +181,10 @@ fun CheckIfUrlIsOnline(url: String, accountViewModel: AccountViewModel, whenOnli
}
}
Crossfade(targetState = online) {
Crossfade(
targetState = online,
label = "CheckIfUrlIsOnline"
) {
if (it) {
whenOnline()
}