mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-10 22:53:42 +02:00
Abandoning the precision of Mills to seconds on the isOnline Check
This commit is contained in:
@@ -24,6 +24,7 @@ import android.util.Log
|
|||||||
import android.util.LruCache
|
import android.util.LruCache
|
||||||
import androidx.compose.runtime.Immutable
|
import androidx.compose.runtime.Immutable
|
||||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||||
|
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||||
import okhttp3.EventListener
|
import okhttp3.EventListener
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Protocol
|
import okhttp3.Protocol
|
||||||
@@ -33,29 +34,37 @@ import okio.ByteString.Companion.toByteString
|
|||||||
import kotlin.coroutines.cancellation.CancellationException
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
|
|
||||||
@Immutable data class OnlineCheckResult(
|
@Immutable data class OnlineCheckResult(
|
||||||
val timeInMs: Long,
|
val timeInSecs: Long,
|
||||||
val online: Boolean,
|
val online: Boolean,
|
||||||
)
|
)
|
||||||
|
|
||||||
object OnlineChecker {
|
object OnlineChecker {
|
||||||
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
|
val checkOnlineCache = LruCache<String, OnlineCheckResult>(100)
|
||||||
val fiveMinutes = 1000 * 60 * 5
|
|
||||||
|
|
||||||
fun isOnlineCached(url: String?): Boolean {
|
fun isOnlineCached(url: String?): Boolean {
|
||||||
if (url.isNullOrBlank()) return false
|
if (url.isNullOrBlank()) return false
|
||||||
if ((checkOnlineCache.get(url)?.timeInMs ?: 0) > System.currentTimeMillis() - fiveMinutes) {
|
val cached = checkOnlineCache.get(url)
|
||||||
return checkOnlineCache.get(url).online
|
if (cached != null && cached.timeInSecs > TimeUtils.fiveMinutesAgo()) {
|
||||||
|
return cached.online
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun resetIfOfflineToRetry(url: String) {
|
||||||
|
val cached = checkOnlineCache.get(url)
|
||||||
|
if (cached != null && !cached.online) {
|
||||||
|
checkOnlineCache.remove(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun isOnline(
|
suspend fun isOnline(
|
||||||
url: String?,
|
url: String?,
|
||||||
okHttpClient: (String) -> OkHttpClient,
|
okHttpClient: (String) -> OkHttpClient,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
if (url.isNullOrBlank()) return false
|
if (url.isNullOrBlank()) return false
|
||||||
if ((checkOnlineCache.get(url)?.timeInMs ?: 0) > System.currentTimeMillis() - fiveMinutes) {
|
val cached = checkOnlineCache.get(url)
|
||||||
return checkOnlineCache.get(url).online
|
if (cached != null && cached.timeInSecs > TimeUtils.fiveMinutesAgo()) {
|
||||||
|
return cached.online
|
||||||
}
|
}
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
@@ -89,14 +98,16 @@ object OnlineChecker {
|
|||||||
.build()
|
.build()
|
||||||
val client = okHttpClient(url)
|
val client = okHttpClient(url)
|
||||||
|
|
||||||
client.newCall(request).executeAsync().use { it.isSuccessful }
|
client.newCall(request).executeAsync().use {
|
||||||
|
it.isSuccessful
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkOnlineCache.put(url, OnlineCheckResult(System.currentTimeMillis(), result))
|
checkOnlineCache.put(url, OnlineCheckResult(TimeUtils.now(), result))
|
||||||
result
|
result
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
checkOnlineCache.put(url, OnlineCheckResult(System.currentTimeMillis(), false))
|
checkOnlineCache.put(url, OnlineCheckResult(TimeUtils.now(), false))
|
||||||
Log.e("LiveActivities", "Failed to check streaming url $url", e)
|
Log.e("LiveActivities", "Failed to check streaming url $url", e)
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user