Add notification action to open GitHub release page

Co-authored-by: Jays2Kings <Jays2Kings@users.noreply.github.com>
This commit is contained in:
arkon 2021-10-16 10:42:14 -04:00
parent a8c8f15e07
commit 3d8f123e05
6 changed files with 32 additions and 22 deletions

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.updater package eu.kanade.tachiyomi.data.updater
import android.content.Context
import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
@ -23,9 +24,9 @@ class AppUpdateChecker {
} }
} }
suspend fun checkForUpdate(): AppUpdateResult { suspend fun checkForUpdate(context: Context): AppUpdateResult {
return withIOContext { return withIOContext {
networkService.client val result = networkService.client
.newCall(GET("https://api.github.com/repos/$repo/releases/latest")) .newCall(GET("https://api.github.com/repos/$repo/releases/latest"))
.await() .await()
.parseAs<GithubRelease>() .parseAs<GithubRelease>()
@ -39,6 +40,12 @@ class AppUpdateChecker {
AppUpdateResult.NoNewUpdate AppUpdateResult.NoNewUpdate
} }
} }
if (result is AppUpdateResult.NewUpdate) {
AppUpdateNotifier(context).promptUpdate(result.release)
}
result
} }
} }

View file

@ -17,10 +17,7 @@ class AppUpdateJob(private val context: Context, workerParams: WorkerParameters)
override suspend fun doWork() = coroutineScope { override suspend fun doWork() = coroutineScope {
try { try {
val result = AppUpdateChecker().checkForUpdate() AppUpdateChecker().checkForUpdate(context)
if (result is AppUpdateResult.NewUpdate) {
AppUpdateNotifier(context).promptUpdate(result.release.getDownloadLink())
}
Result.success() Result.success()
} catch (e: Exception) { } catch (e: Exception) {
Result.failure() Result.failure()

View file

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
@ -25,22 +26,32 @@ internal class AppUpdateNotifier(private val context: Context) {
context.notificationManager.notify(id, build()) context.notificationManager.notify(id, build())
} }
fun promptUpdate(url: String) { fun promptUpdate(release: GithubRelease) {
val intent = Intent(context, AppUpdateService::class.java).apply { 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) { with(notificationBuilder) {
setContentTitle(context.getString(R.string.app_name)) setContentTitle(context.getString(R.string.update_check_notification_update_available))
setContentText(context.getString(R.string.update_check_notification_update_available))
setSmallIcon(android.R.drawable.stat_sys_download_done) setSmallIcon(android.R.drawable.stat_sys_download_done)
setContentIntent(pendingIntent) setContentIntent(updateIntent)
clearActions() clearActions()
addAction( addAction(
android.R.drawable.stat_sys_download_done, android.R.drawable.stat_sys_download_done,
context.getString(R.string.action_download), context.getString(R.string.action_download),
pendingIntent updateIntent,
)
addAction(
R.drawable.ic_info_24dp,
context.getString(R.string.whats_new),
releaseInfoIntent,
) )
} }
notificationBuilder.show() notificationBuilder.show()

View file

@ -5,17 +5,13 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
/** /**
* Release object.
* Contains information about the latest release from GitHub. * 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 @Serializable
data class GithubRelease( data class GithubRelease(
@SerialName("tag_name") val version: String, @SerialName("tag_name") val version: String,
@SerialName("body") val info: String, @SerialName("body") val info: String,
@SerialName("html_url") val releaseLink: String,
@SerialName("assets") private val assets: List<Assets> @SerialName("assets") private val assets: List<Assets>
) { ) {
@ -37,7 +33,6 @@ data class GithubRelease(
/** /**
* Assets class containing download url. * Assets class containing download url.
* @param downloadLink download url.
*/ */
@Serializable @Serializable
data class Assets(@SerialName("browser_download_url") val downloadLink: String) data class Assets(@SerialName("browser_download_url") val downloadLink: String)

View file

@ -336,7 +336,7 @@ class MainActivity : BaseViewBindingActivity<MainActivityBinding>() {
lifecycleScope.launchIO { lifecycleScope.launchIO {
try { try {
val result = AppUpdateChecker().checkForUpdate() val result = AppUpdateChecker().checkForUpdate(this@MainActivity)
if (result is AppUpdateResult.NewUpdate) { if (result is AppUpdateResult.NewUpdate) {
NewUpdateDialogController(result).showDialog(router) NewUpdateDialogController(result).showDialog(router)
} }

View file

@ -107,11 +107,11 @@ class AboutController : SettingsController(), NoAppBarElevationController {
private fun checkVersion() { private fun checkVersion() {
if (activity == null) return if (activity == null) return
activity?.toast(R.string.update_check_look_for_updates) activity!!.toast(R.string.update_check_look_for_updates)
launchNow { launchNow {
try { try {
when (val result = updateChecker.checkForUpdate()) { when (val result = updateChecker.checkForUpdate(activity!!)) {
is AppUpdateResult.NewUpdate -> { is AppUpdateResult.NewUpdate -> {
NewUpdateDialogController(result).showDialog(router) NewUpdateDialogController(result).showDialog(router)
} }