From e2dde71cdb109285fa7b40929aa149baa18fb154 Mon Sep 17 00:00:00 2001 From: Kayra Uylar Date: Sat, 30 Sep 2023 19:34:31 +0300 Subject: [PATCH] fix: optimize update checking process --- app/src/main/java/dev/kuylar/lighttube/Utils.kt | 15 ++++++--------- .../kuylar/lighttube/ui/activity/MainActivity.kt | 8 ++++++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/dev/kuylar/lighttube/Utils.kt b/app/src/main/java/dev/kuylar/lighttube/Utils.kt index d02e195..8fc14bc 100644 --- a/app/src/main/java/dev/kuylar/lighttube/Utils.kt +++ b/app/src/main/java/dev/kuylar/lighttube/Utils.kt @@ -1,6 +1,5 @@ package dev.kuylar.lighttube -import android.content.Context import android.util.Log import android.view.LayoutInflater import android.view.ViewGroup @@ -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() @@ -123,11 +122,9 @@ class Utils { .create() .fromJson( response.body!!.string(), - object : TypeToken>() {}) - 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) { @@ -135,7 +132,7 @@ class Utils { updateInfo = UpdateInfo( version, latestVer, - res.first().assets.first().browserDownloadUrl + res.assets.first().browserDownloadUrl ) } } diff --git a/app/src/main/java/dev/kuylar/lighttube/ui/activity/MainActivity.kt b/app/src/main/java/dev/kuylar/lighttube/ui/activity/MainActivity.kt index 427825f..e04326d 100644 --- a/app/src/main/java/dev/kuylar/lighttube/ui/activity/MainActivity.kt +++ b/app/src/main/java/dev/kuylar/lighttube/ui/activity/MainActivity.kt @@ -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 @@ -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) { @@ -240,7 +242,9 @@ class MainActivity : AppCompatActivity() { searchAutoComplete.showDropDown() loadingSuggestions = false } - } catch (e: Exception) { + } catch (e: LightTubeException) { + loadingSuggestions = false + } catch (e: IOException) { loadingSuggestions = false } }