Skip to content

Commit

Permalink
fix: optimize update checking process
Browse files Browse the repository at this point in the history
  • Loading branch information
kuylar committed Sep 30, 2023
1 parent f0e4800 commit e2dde71
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
15 changes: 6 additions & 9 deletions app/src/main/java/dev/kuylar/lighttube/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kuylar.lighttube

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
Expand Down Expand Up @@ -109,11 +108,11 @@ class Utils {
}
}

fun checkForUpdates(context: Context): UpdateInfo? {
fun checkForUpdates(): UpdateInfo? {
var updateInfo: UpdateInfo? = null
try {
val req = Request.Builder().apply {
url("https://api.github.com/repos/kuylar/lighttube-android/releases")
url("https://api.github.com/repos/kuylar/lighttube-android/releases/latest")
header("User-Agent", getUserAgent())
}.build()

Expand All @@ -123,19 +122,17 @@ class Utils {
.create()
.fromJson(
response.body!!.string(),
object : TypeToken<List<GithubRelease>>() {})
val latestVer = res.first().tagName.substring(1)
@Suppress("DEPRECATION")
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
val version = pInfo.versionName.split(" ").first()
GithubRelease::class.java)
val latestVer = res.tagName.substring(1)
val version = BuildConfig.VERSION_NAME.split(" ").first()
val latestVersionCode = latestVer.replace(".", "").toInt()
val currentVersionCode = version.replace(".", "").toInt()
if (latestVersionCode > currentVersionCode) {
Log.i("UpdateChecker", "Update available! (${version} -> ${latestVer})")
updateInfo = UpdateInfo(
version,
latestVer,
res.first().assets.first().browserDownloadUrl
res.assets.first().browserDownloadUrl
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ import com.google.android.material.elevation.SurfaceColors
import dev.kuylar.lighttube.R
import dev.kuylar.lighttube.Utils
import dev.kuylar.lighttube.api.LightTubeApi
import dev.kuylar.lighttube.api.models.LightTubeException
import dev.kuylar.lighttube.databinding.ActivityMainBinding
import dev.kuylar.lighttube.ui.VideoPlayerManager
import dev.kuylar.lighttube.ui.fragment.UpdateFragment
import java.io.IOException
import kotlin.concurrent.thread
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -140,7 +142,7 @@ class MainActivity : AppCompatActivity() {

// check for updates
thread {
val update = Utils.checkForUpdates(this)
val update = Utils.checkForUpdates()
if (update != null) {
val skippedUpdate = sp.getString("skippedUpdate", null)
if (skippedUpdate == update.latestVersion) {
Expand Down Expand Up @@ -240,7 +242,9 @@ class MainActivity : AppCompatActivity() {
searchAutoComplete.showDropDown()
loadingSuggestions = false
}
} catch (e: Exception) {
} catch (e: LightTubeException) {
loadingSuggestions = false
} catch (e: IOException) {
loadingSuggestions = false
}
}
Expand Down

0 comments on commit e2dde71

Please sign in to comment.