From 3d8f123e053c4612cda5feebca3c18f93fff7ae4 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 16 Oct 2021 10:42:14 -0400 Subject: [PATCH] Add notification action to open GitHub release page Co-authored-by: Jays2Kings --- .../data/updater/AppUpdateChecker.kt | 11 ++++++-- .../tachiyomi/data/updater/AppUpdateJob.kt | 5 +--- .../data/updater/AppUpdateNotifier.kt | 25 +++++++++++++------ .../tachiyomi/data/updater/GithubRelease.kt | 7 +----- .../kanade/tachiyomi/ui/main/MainActivity.kt | 2 +- .../tachiyomi/ui/more/AboutController.kt | 4 +-- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt index 7aac67ab65..37488a7533 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateChecker.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.data.updater +import android.content.Context import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.network.GET @@ -23,9 +24,9 @@ class AppUpdateChecker { } } - suspend fun checkForUpdate(): AppUpdateResult { + suspend fun checkForUpdate(context: Context): AppUpdateResult { return withIOContext { - networkService.client + val result = networkService.client .newCall(GET("https://api.github.com/repos/$repo/releases/latest")) .await() .parseAs() @@ -39,6 +40,12 @@ class AppUpdateChecker { AppUpdateResult.NoNewUpdate } } + + if (result is AppUpdateResult.NewUpdate) { + AppUpdateNotifier(context).promptUpdate(result.release) + } + + result } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt index 70288453b6..bb3d2a76ca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt @@ -17,10 +17,7 @@ class AppUpdateJob(private val context: Context, workerParams: WorkerParameters) override suspend fun doWork() = coroutineScope { try { - val result = AppUpdateChecker().checkForUpdate() - if (result is AppUpdateResult.NewUpdate) { - AppUpdateNotifier(context).promptUpdate(result.release.getDownloadLink()) - } + AppUpdateChecker().checkForUpdate(context) Result.success() } catch (e: Exception) { Result.failure() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateNotifier.kt index fc914f7597..9701be1668 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateNotifier.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.net.Uri import androidx.core.app.NotificationCompat +import androidx.core.net.toUri import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationReceiver @@ -25,22 +26,32 @@ internal class AppUpdateNotifier(private val context: Context) { context.notificationManager.notify(id, build()) } - fun promptUpdate(url: String) { + fun promptUpdate(release: GithubRelease) { val intent = Intent(context, AppUpdateService::class.java).apply { - putExtra(AppUpdateService.EXTRA_DOWNLOAD_URL, url) + putExtra(AppUpdateService.EXTRA_DOWNLOAD_URL, release.getDownloadLink()) } - val pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + val updateIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + + val releaseIntent = Intent(Intent.ACTION_VIEW, release.releaseLink.toUri()).apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP + } + val releaseInfoIntent = PendingIntent.getActivity(context, release.hashCode(), releaseIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) + with(notificationBuilder) { - setContentTitle(context.getString(R.string.app_name)) - setContentText(context.getString(R.string.update_check_notification_update_available)) + setContentTitle(context.getString(R.string.update_check_notification_update_available)) setSmallIcon(android.R.drawable.stat_sys_download_done) - setContentIntent(pendingIntent) + setContentIntent(updateIntent) clearActions() addAction( android.R.drawable.stat_sys_download_done, context.getString(R.string.action_download), - pendingIntent + updateIntent, + ) + addAction( + R.drawable.ic_info_24dp, + context.getString(R.string.whats_new), + releaseInfoIntent, ) } notificationBuilder.show() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt index 5f0e1ff5f5..068fac3f6e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/GithubRelease.kt @@ -5,17 +5,13 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable /** - * Release object. * Contains information about the latest release from GitHub. - * - * @param version version of latest release. - * @param info log of latest release. - * @param assets assets of latest release. */ @Serializable data class GithubRelease( @SerialName("tag_name") val version: String, @SerialName("body") val info: String, + @SerialName("html_url") val releaseLink: String, @SerialName("assets") private val assets: List ) { @@ -37,7 +33,6 @@ data class GithubRelease( /** * Assets class containing download url. - * @param downloadLink download url. */ @Serializable data class Assets(@SerialName("browser_download_url") val downloadLink: String) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index f194e22b1f..19d9e21263 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -336,7 +336,7 @@ class MainActivity : BaseViewBindingActivity() { lifecycleScope.launchIO { try { - val result = AppUpdateChecker().checkForUpdate() + val result = AppUpdateChecker().checkForUpdate(this@MainActivity) if (result is AppUpdateResult.NewUpdate) { NewUpdateDialogController(result).showDialog(router) } 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 eac9be7a37..162cb90d46 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 @@ -107,11 +107,11 @@ class AboutController : SettingsController(), NoAppBarElevationController { private fun checkVersion() { if (activity == null) return - activity?.toast(R.string.update_check_look_for_updates) + activity!!.toast(R.string.update_check_look_for_updates) launchNow { try { - when (val result = updateChecker.checkForUpdate()) { + when (val result = updateChecker.checkForUpdate(activity!!)) { is AppUpdateResult.NewUpdate -> { NewUpdateDialogController(result).showDialog(router) }