More fixes for update checker

This commit is contained in:
Mygod
2021-10-31 17:00:43 -04:00
parent 1f8fd4d853
commit 4e639e5c17
2 changed files with 33 additions and 20 deletions

View File

@@ -16,6 +16,7 @@ import java.time.Instant
import java.util.concurrent.CancellationException
import java.util.concurrent.TimeUnit
import kotlin.math.max
import kotlin.math.min
object UpdateChecker {
private const val KEY_LAST_FETCHED = "update.lastFetched"
@@ -49,18 +50,26 @@ object UpdateChecker {
private val myVer = BuildConfig.VERSION_NAME.toSemVer()
private fun findUpdate(response: JSONArray): GitHubUpdate? {
var latest: String? = null
var latestVer = myVer
var earliest = Long.MAX_VALUE
for (i in 0 until response.length()) {
val obj = response.getJSONObject(i)
val name = obj.getString("name")
try {
if (name.toSemVer() <= myVer) continue
val semver = try {
name.toSemVer()
} catch (e: IllegalArgumentException) {
Timber.w(e)
continue
}
return GitHubUpdate(name, Instant.parse(obj.getString("published_at")).toEpochMilli())
if (semver <= myVer) continue
if (semver > latestVer) {
latest = name
latestVer = semver
}
return null
earliest = min(earliest, Instant.parse(obj.getString("published_at")).toEpochMilli())
}
return latest?.let { GitHubUpdate(it, earliest) }
}
fun check() = flow<AppUpdate?> {
emit(app.pref.getString(KEY_VERSION, null)?.let {

View File

@@ -10,6 +10,7 @@ import com.google.android.play.core.install.model.InstallStatus
import com.google.android.play.core.ktx.AppUpdateResult
import com.google.android.play.core.ktx.requestUpdateFlow
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import timber.log.Timber
@@ -44,8 +45,19 @@ object UpdateChecker {
private val manager by lazy { AppUpdateManagerFactory.create(app) }
fun check() = try {
manager.requestUpdateFlow().map { result ->
fun check() = manager.requestUpdateFlow().catch { e ->
when (e) {
is InstallException -> {
app.logEvent("InstallErrorCode") { param("errorCode", e.errorCode.toLong()) }
throw AppUpdate.IgnoredException(e)
}
is RuntimeException -> if (e.message == "Failed to bind to the service.") {
app.logEvent("UpdateBindFailure")
throw AppUpdate.IgnoredException(e)
}
}
throw e
}.map { result ->
when (result) {
is AppUpdateResult.NotAvailable -> null
is AppUpdateResult.Available -> UpdateAvailable(result)
@@ -55,12 +67,4 @@ object UpdateChecker {
is AppUpdateResult.Downloaded -> UpdateDownloaded(result)
}
}
} catch (e: InstallException) {
app.logEvent("InstallErrorCode") { param("errorCode", e.errorCode.toLong()) }
throw AppUpdate.IgnoredException(e)
} catch (e: RuntimeException) {
if (e.message != "Failed to bind to the service.") throw e
app.logEvent("UpdateBindFailure")
throw AppUpdate.IgnoredException(e)
}
}