mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Add automatic updates for dev builds (#2128)
This commit is contained in:
		| @@ -1,7 +0,0 @@ | ||||
| package eu.kanade.tachiyomi.data.updater | ||||
|  | ||||
| sealed class GithubUpdateResult { | ||||
|  | ||||
|     class NewUpdate(val release: GithubRelease): GithubUpdateResult() | ||||
|     class NoNewUpdate : GithubUpdateResult() | ||||
| } | ||||
| @@ -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 | ||||
|  | ||||
| } | ||||
| @@ -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<UpdateResult> | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,8 @@ | ||||
| package eu.kanade.tachiyomi.data.updater | ||||
|  | ||||
| abstract class UpdateResult { | ||||
|  | ||||
|     open class NewUpdate<T : Release>(val release: T): UpdateResult() | ||||
|     open class NoNewUpdate: UpdateResult() | ||||
|  | ||||
| } | ||||
| @@ -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() { | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -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" | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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<NetworkHelper>().client.newBuilder() | ||||
|                 .followRedirects(false) | ||||
|                 .build() | ||||
|     } | ||||
|  | ||||
|     private val versionRegex: Regex by lazy { | ||||
|         Regex("tachiyomi-r(\\d+).apk") | ||||
|     } | ||||
|  | ||||
|     override fun checkForUpdate(): Observable<UpdateResult> { | ||||
|         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() | ||||
|                     } | ||||
|                 } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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<DevRepoRelease>(release) | ||||
|     class NoNewUpdate: UpdateResult.NoNewUpdate() | ||||
|  | ||||
| } | ||||
| @@ -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<Assets>) { | ||||
|                     @SerializedName("body") override val info: String, | ||||
|                     @SerializedName("assets") private val assets: List<Assets>): 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 | ||||
| 
 | ||||
|     /** | ||||
| @@ -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<GithubRelease> | ||||
| 
 | ||||
| } | ||||
| } | ||||
| @@ -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<GithubUpdateResult> { | ||||
|     override fun checkForUpdate(): Observable<UpdateResult> { | ||||
|         return service.getLatestVersion().map { release -> | ||||
|             val newVersion = release.version.replace("[^\\d.]".toRegex(), "") | ||||
| 
 | ||||
| @@ -22,4 +21,5 @@ class GithubUpdateChecker { | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| } | ||||
| @@ -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<GithubRelease>(release) | ||||
|     class NoNewUpdate : UpdateResult.NoNewUpdate() | ||||
|  | ||||
| } | ||||
| @@ -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) | ||||
|                         } | ||||
|                     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user