diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt deleted file mode 100644 index 3f07d2da6..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateResult.kt +++ /dev/null @@ -1,7 +0,0 @@ -package eu.kanade.tachiyomi.data.updater - -sealed class GithubUpdateResult { - - class NewUpdate(val release: GithubRelease): GithubUpdateResult() - class NoNewUpdate : GithubUpdateResult() -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt new file mode 100644 index 000000000..9ac138e98 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/Release.kt @@ -0,0 +1,13 @@ +package eu.kanade.tachiyomi.data.updater + +interface Release { + + val info: String + + /** + * Get download link of latest release. + * @return download link of latest release. + */ + val downloadLink: String + +} 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 new file mode 100644 index 000000000..4d2a1de66 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateChecker.kt @@ -0,0 +1,25 @@ +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 +import rx.Observable + +abstract class UpdateChecker { + + companion object { + fun getUpdateChecker(): UpdateChecker { + return if (BuildConfig.DEBUG) { + DevRepoUpdateChecker() + } else { + GithubUpdateChecker() + } + } + } + + /** + * Returns observable containing release information + */ + abstract fun checkForUpdate(): Observable + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt new file mode 100644 index 000000000..a59864f55 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateResult.kt @@ -0,0 +1,8 @@ +package eu.kanade.tachiyomi.data.updater + +abstract class UpdateResult { + + open class NewUpdate(val release: T): UpdateResult() + open class NoNewUpdate: 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 59832bd2d..594ecd31b 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,10 +13,10 @@ import eu.kanade.tachiyomi.util.notificationManager class UpdaterJob : Job() { override fun onRunJob(params: Params): Result { - return GithubUpdateChecker() + return UpdateChecker.getUpdateChecker() .checkForUpdate() .map { result -> - if (result is GithubUpdateResult.NewUpdate) { + if (result is UpdateResult.NewUpdate<*>) { val url = result.release.downloadLink val intent = Intent(context, UpdaterService::class.java).apply { @@ -33,9 +33,9 @@ class UpdaterJob : Job() { PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) } } - Job.Result.SUCCESS + Result.SUCCESS } - .onErrorReturn { Job.Result.FAILURE } + .onErrorReturn { Result.FAILURE } // Sadly, the task needs to be synchronous. .toBlocking() .single() @@ -64,4 +64,4 @@ class UpdaterJob : Job() { } } -} \ No newline at end of file +} 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 new file mode 100644 index 000000000..ea8a79a18 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoRelease.kt @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..a24036830 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateChecker.kt @@ -0,0 +1,40 @@ +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.asObservable +import okhttp3.OkHttpClient +import rx.Observable +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 fun checkForUpdate(): Observable { + return client.newCall(GET(DevRepoRelease.LATEST_URL)).asObservable() + .map { response -> + // Get latest repo version number from header in format "Location: tachiyomi-r1512.apk" + val latestVersionNumber: String = versionRegex.find(response.header("Location")!!)!!.groupValues[1] + + 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 new file mode 100644 index 000000000..1bda48b9c --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/devrepo/DevRepoUpdateResult.kt @@ -0,0 +1,10 @@ +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/GithubRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt similarity index 72% rename from app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt index 8c20690e2..f65bf39ba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt @@ -1,24 +1,25 @@ -package eu.kanade.tachiyomi.data.updater +package eu.kanade.tachiyomi.data.updater.github import com.google.gson.annotations.SerializedName +import eu.kanade.tachiyomi.data.updater.Release /** * Release object. * Contains information about the latest release from Github. * * @param version version of latest release. - * @param changeLog log of latest release. + * @param info log of latest release. * @param assets assets of latest release. */ class GithubRelease(@SerializedName("tag_name") val version: String, - @SerializedName("body") val changeLog: String, - @SerializedName("assets") private val assets: List) { + @SerializedName("body") override val info: String, + @SerializedName("assets") private val assets: List): Release { /** * Get download link of latest release from the assets. * @return download link of latest release. */ - val downloadLink: String + override val downloadLink: String get() = assets[0].downloadLink /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt similarity index 95% rename from app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubService.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt index 82befdc72..e19e3528d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.data.updater +package eu.kanade.tachiyomi.data.updater.github import eu.kanade.tachiyomi.network.NetworkHelper import retrofit2.Retrofit @@ -30,4 +30,4 @@ interface GithubService { @GET("/repos/inorichi/tachiyomi/releases/latest") fun getLatestVersion(): Observable -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt similarity index 67% rename from app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateChecker.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt index 7d321dc71..6fc629740 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt @@ -1,16 +1,15 @@ -package eu.kanade.tachiyomi.data.updater +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 import rx.Observable -class GithubUpdateChecker { +class GithubUpdateChecker : UpdateChecker() { private val service: GithubService = GithubService.create() - /** - * Returns observable containing release information - */ - fun checkForUpdate(): Observable { + override fun checkForUpdate(): Observable { return service.getLatestVersion().map { release -> val newVersion = release.version.replace("[^\\d.]".toRegex(), "") @@ -22,4 +21,5 @@ class GithubUpdateChecker { } } } -} \ No newline at end of file + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt new file mode 100644 index 000000000..fcb304604 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateResult.kt @@ -0,0 +1,10 @@ +package eu.kanade.tachiyomi.data.updater.github + +import eu.kanade.tachiyomi.data.updater.UpdateResult + +sealed class GithubUpdateResult : UpdateResult() { + + class NewUpdate(release: GithubRelease): UpdateResult.NewUpdate(release) + class NoNewUpdate : UpdateResult.NoNewUpdate() + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt index a736da2a5..bbd23da2c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt @@ -9,8 +9,8 @@ import android.view.View import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.updater.GithubUpdateChecker -import eu.kanade.tachiyomi.data.updater.GithubUpdateResult +import eu.kanade.tachiyomi.data.updater.UpdateChecker +import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.data.updater.UpdaterJob import eu.kanade.tachiyomi.data.updater.UpdaterService import eu.kanade.tachiyomi.ui.base.controller.DialogController @@ -26,20 +26,19 @@ import java.util.Locale import java.util.TimeZone import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys - class SettingsAboutController : SettingsController() { /** * Checks for new releases */ - private val updateChecker by lazy { GithubUpdateChecker() } + private val updateChecker by lazy { UpdateChecker.getUpdateChecker() } /** * The subscribtion service of the obtained release object */ private var releaseSubscription: Subscription? = null - private val isUpdaterEnabled = !BuildConfig.DEBUG && BuildConfig.INCLUDE_UPDATER + private val isUpdaterEnabled = BuildConfig.INCLUDE_UPDATER override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { titleRes = R.string.pref_category_about @@ -124,14 +123,14 @@ class SettingsAboutController : SettingsController() { .observeOn(AndroidSchedulers.mainThread()) .subscribe({ result -> when (result) { - is GithubUpdateResult.NewUpdate -> { - val body = result.release.changeLog + is UpdateResult.NewUpdate<*> -> { + val body = result.release.info val url = result.release.downloadLink // Create confirmation window NewUpdateDialogController(body, url).showDialog(router) } - is GithubUpdateResult.NoNewUpdate -> { + is UpdateResult.NoNewUpdate -> { activity?.toast(R.string.update_check_no_new_updates) } }