Remove a compose recomposition between the started state and the isOnline state that is already cached.
This commit is contained in:
@@ -13,6 +13,14 @@ object OnlineChecker {
|
|||||||
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
|
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
|
||||||
val fiveMinutes = 1000 * 60 * 5
|
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 {
|
fun isOnline(url: String?): Boolean {
|
||||||
checkNotInMainThread()
|
checkNotInMainThread()
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import androidx.lifecycle.Lifecycle
|
|||||||
import androidx.lifecycle.LifecycleEventObserver
|
import androidx.lifecycle.LifecycleEventObserver
|
||||||
import com.vitorpamplona.amethyst.R
|
import com.vitorpamplona.amethyst.R
|
||||||
import com.vitorpamplona.amethyst.service.NostrHomeDataSource
|
import com.vitorpamplona.amethyst.service.NostrHomeDataSource
|
||||||
|
import com.vitorpamplona.amethyst.service.OnlineChecker
|
||||||
import com.vitorpamplona.amethyst.ui.navigation.Route
|
import com.vitorpamplona.amethyst.ui.navigation.Route
|
||||||
import com.vitorpamplona.amethyst.ui.note.UpdateZapAmountDialog
|
import com.vitorpamplona.amethyst.ui.note.UpdateZapAmountDialog
|
||||||
import com.vitorpamplona.amethyst.ui.screen.FeedViewModel
|
import com.vitorpamplona.amethyst.ui.screen.FeedViewModel
|
||||||
@@ -166,7 +167,11 @@ private fun HomePages(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CheckIfUrlIsOnline(url: String, accountViewModel: AccountViewModel, whenOnline: @Composable () -> Unit) {
|
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) {
|
LaunchedEffect(key1 = url) {
|
||||||
accountViewModel.checkIsOnline(url) { isOnline ->
|
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) {
|
if (it) {
|
||||||
whenOnline()
|
whenOnline()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user