From ca1373f36bb067a86a52ccd12867345d061dba3a Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 13 Sep 2020 10:53:51 -0400 Subject: [PATCH] Check GitHub for preview release updates instead of inorichi's server --- .../tachiyomi/data/updater/UpdateChecker.kt | 23 ----------- .../tachiyomi/data/updater/UpdaterJob.kt | 3 +- .../data/updater/devrepo/DevRepoRelease.kt | 13 ------ .../updater/devrepo/DevRepoUpdateChecker.kt | 41 ------------------- .../updater/devrepo/DevRepoUpdateResult.kt | 9 ---- .../data/updater/github/GithubRelease.kt | 2 +- .../data/updater/github/GithubService.kt | 7 ++-- .../updater/github/GithubUpdateChecker.kt | 32 ++++++++++++--- .../tachiyomi/ui/more/AboutController.kt | 4 +- 9 files changed, 35 insertions(+), 99 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt deleted file mode 100644 index 9f1e8eac1b..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt +++ /dev/null @@ -1,23 +0,0 @@ -package eu.kanade.tachiyomi.data.updater - -import eu.kanade.tachiyomi.BuildConfig -import eu.kanade.tachiyomi.data.updater.devrepo.DevRepoUpdateChecker -import eu.kanade.tachiyomi.data.updater.github.GithubUpdateChecker - -abstract class UpdateChecker { - - companion object { - fun getUpdateChecker(): UpdateChecker { - return if (BuildConfig.DEBUG) { - DevRepoUpdateChecker() - } else { - GithubUpdateChecker() - } - } - } - - /** - * Returns observable containing release information - */ - abstract suspend fun checkForUpdate(): UpdateResult -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt index f786afc101..524060c329 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt @@ -13,6 +13,7 @@ import androidx.work.Worker import androidx.work.WorkerParameters import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.Notifications +import eu.kanade.tachiyomi.data.updater.github.GithubUpdateChecker import eu.kanade.tachiyomi.util.system.notificationManager import java.util.concurrent.TimeUnit import kotlinx.coroutines.runBlocking @@ -23,7 +24,7 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) : override fun doWork(): Result { return runBlocking { try { - val result = UpdateChecker.getUpdateChecker().checkForUpdate() + val result = GithubUpdateChecker().checkForUpdate() if (result is UpdateResult.NewUpdate<*>) { val url = result.release.downloadLink diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt deleted file mode 100644 index 0f2b1ac751..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt +++ /dev/null @@ -1,13 +0,0 @@ -package eu.kanade.tachiyomi.data.updater.devrepo - -import eu.kanade.tachiyomi.data.updater.Release - -class DevRepoRelease(override val info: String) : Release { - - override val downloadLink: String - get() = LATEST_URL - - companion object { - const val LATEST_URL = "https://tachiyomi.kanade.eu/latest" - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt deleted file mode 100644 index c224ba4578..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt +++ /dev/null @@ -1,41 +0,0 @@ -package eu.kanade.tachiyomi.data.updater.devrepo - -import eu.kanade.tachiyomi.BuildConfig -import eu.kanade.tachiyomi.data.updater.UpdateChecker -import eu.kanade.tachiyomi.data.updater.UpdateResult -import eu.kanade.tachiyomi.network.GET -import eu.kanade.tachiyomi.network.NetworkHelper -import eu.kanade.tachiyomi.network.await -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import okhttp3.OkHttpClient -import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.get - -class DevRepoUpdateChecker : UpdateChecker() { - - private val client: OkHttpClient by lazy { - Injekt.get().client.newBuilder() - .followRedirects(false) - .build() - } - - private val versionRegex: Regex by lazy { - Regex("tachiyomi-r(\\d+).apk") - } - - override suspend fun checkForUpdate(): UpdateResult { - val response = withContext(Dispatchers.IO) { - client.newCall(GET(DevRepoRelease.LATEST_URL)).await() - } - - // Get latest repo version number from header in format "Location: tachiyomi-r1512.apk" - val latestVersionNumber: String = versionRegex.find(response.header("Location")!!)!!.groupValues[1] - - return if (latestVersionNumber.toInt() > BuildConfig.COMMIT_COUNT.toInt()) { - DevRepoUpdateResult.NewUpdate(DevRepoRelease("v$latestVersionNumber")) - } else { - DevRepoUpdateResult.NoNewUpdate() - } - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt deleted file mode 100644 index 1b62201a85..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt +++ /dev/null @@ -1,9 +0,0 @@ -package eu.kanade.tachiyomi.data.updater.devrepo - -import eu.kanade.tachiyomi.data.updater.UpdateResult - -sealed class DevRepoUpdateResult : UpdateResult() { - - class NewUpdate(release: DevRepoRelease) : UpdateResult.NewUpdate(release) - class NoNewUpdate : UpdateResult.NoNewUpdate() -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt index 09f1b37d01..ec20699afb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt @@ -28,5 +28,5 @@ class GithubRelease( * Assets class containing download url. * @param downloadLink download url. */ - inner class Assets(@SerializedName("browser_download_url") val downloadLink: String) + class Assets(@SerializedName("browser_download_url") val downloadLink: String) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt index 88a9eb37cb..e64a19d5ec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt @@ -4,11 +4,12 @@ import eu.kanade.tachiyomi.network.NetworkHelper import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET +import retrofit2.http.Path import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get /** - * Used to connect with the GitHub API. + * Used to connect with the GitHub API to get the latest release version from a repo. */ interface GithubService { @@ -24,6 +25,6 @@ interface GithubService { } } - @GET("/repos/inorichi/tachiyomi/releases/latest") - suspend fun getLatestVersion(): GithubRelease + @GET("/repos/{repo}/releases/latest") + suspend fun getLatestVersion(@Path("repo", encoded = true) repo: String): GithubRelease } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt index 20ab5de5c3..aa01b68e36 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt @@ -1,23 +1,43 @@ package eu.kanade.tachiyomi.data.updater.github import eu.kanade.tachiyomi.BuildConfig -import eu.kanade.tachiyomi.data.updater.UpdateChecker import eu.kanade.tachiyomi.data.updater.UpdateResult -class GithubUpdateChecker : UpdateChecker() { +class GithubUpdateChecker { private val service: GithubService = GithubService.create() - override suspend fun checkForUpdate(): UpdateResult { - val release = service.getLatestVersion() + private val repo: String by lazy { + if (BuildConfig.DEBUG) { + "tachiyomiorg/android-app-preview" + } else { + "inorichi/tachiyomi" + } + } - val newVersion = release.version.replace("[^\\d.]".toRegex(), "") + suspend fun checkForUpdate(): UpdateResult { + val release = service.getLatestVersion(repo) // Check if latest version is different from current version - return if (newVersion != BuildConfig.VERSION_NAME) { + return if (isNewVersion(release.version)) { GithubUpdateResult.NewUpdate(release) } else { GithubUpdateResult.NoNewUpdate() } } + + private fun isNewVersion(versionTag: String): Boolean { + // Removes prefixes like "r" or "v" + val newVersion = versionTag.replace("[^\\d.]".toRegex(), "") + + return if (BuildConfig.DEBUG) { + // Preview builds: based on releases in "tachiyomiorg/android-app-preview" repo + // tagged as something like "r1234" + newVersion.toInt() > BuildConfig.COMMIT_COUNT.toInt() + } else { + // Release builds: based on releases in "inorichi/tachiyomi" repo + // tagged as something like "v0.1.2" + newVersion != BuildConfig.VERSION_NAME + } + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt index 1abef4141e..33e9c497c9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt @@ -10,9 +10,9 @@ import com.afollestad.materialdialogs.MaterialDialog import com.mikepenz.aboutlibraries.LibsBuilder import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.updater.UpdateChecker import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.data.updater.UpdaterService +import eu.kanade.tachiyomi.data.updater.github.GithubUpdateChecker import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.setting.SettingsController import eu.kanade.tachiyomi.util.lang.launchNow @@ -35,7 +35,7 @@ class AboutController : SettingsController() { /** * Checks for new releases */ - private val updateChecker by lazy { UpdateChecker.getUpdateChecker() } + private val updateChecker by lazy { GithubUpdateChecker() } private val dateFormat: DateFormat = preferences.dateFormat()