Add 'show entry' to download notifications

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>
This commit is contained in:
Catting 2024-08-25 10:41:03 -05:00
parent 5dc6569a68
commit 77aec29aaf
3 changed files with 73 additions and 4 deletions

View File

@ -15,6 +15,7 @@ import eu.kanade.tachiyomi.util.system.cancelNotification
import eu.kanade.tachiyomi.util.system.notificationBuilder
import eu.kanade.tachiyomi.util.system.notify
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.domain.manga.model.Manga
import tachiyomi.i18n.MR
import uy.kohesive.injekt.injectLazy
import java.util.regex.Pattern
@ -83,6 +84,12 @@ internal class DownloadNotifier(private val context: Context) {
context.stringResource(MR.strings.action_pause),
NotificationReceiver.pauseDownloadsPendingBroadcast(context),
)
addAction(
R.drawable.ic_book_24dp,
context.stringResource(MR.strings.action_show_manga),
NotificationReceiver.openEntryPendingActivity(context, download.manga.id),
)
}
val downloadingProgressText = context.stringResource(
@ -160,9 +167,10 @@ internal class DownloadNotifier(private val context: Context) {
*
* @param reason the text to show.
* @param timeout duration after which to automatically dismiss the notification.
* @param mangaId the id of the entry being warned about
* Only works on Android 8+.
*/
fun onWarning(reason: String, timeout: Long? = null, contentIntent: PendingIntent? = null) {
fun onWarning(reason: String, timeout: Long? = null, contentIntent: PendingIntent? = null, mangaId: Long? = null) {
with(errorNotificationBuilder) {
setContentTitle(context.stringResource(MR.strings.download_notifier_downloader_title))
setStyle(NotificationCompat.BigTextStyle().bigText(reason))
@ -170,6 +178,14 @@ internal class DownloadNotifier(private val context: Context) {
setAutoCancel(true)
clearActions()
setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context))
if (mangaId != null) {
addAction(
R.drawable.ic_book_24dp,
context.stringResource(MR.strings.action_show_manga),
NotificationReceiver.openEntryPendingActivity(context, mangaId),
)
}
setProgress(0, 0, false)
timeout?.let { setTimeoutAfter(it) }
contentIntent?.let { setContentIntent(it) }
@ -187,8 +203,9 @@ internal class DownloadNotifier(private val context: Context) {
*
* @param error string containing error information.
* @param chapter string containing chapter title.
* @param mangaId the id of the entry that was errored on
*/
fun onError(error: String? = null, chapter: String? = null, mangaTitle: String? = null) {
fun onError(error: String? = null, chapter: String? = null, mangaTitle: String? = null, mangaId: Long? = null) {
// Create notification
with(errorNotificationBuilder) {
setContentTitle(
@ -198,6 +215,14 @@ internal class DownloadNotifier(private val context: Context) {
setSmallIcon(R.drawable.ic_warning_white_24dp)
clearActions()
setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context))
if (mangaId != null) {
addAction(
R.drawable.ic_book_24dp,
context.stringResource(MR.strings.action_show_manga),
NotificationReceiver.openEntryPendingActivity(context, mangaId),
)
}
setProgress(0, 0, false)
show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR)

View File

@ -302,6 +302,7 @@ class Downloader(
context.stringResource(MR.strings.download_queue_size_warning),
WARNING_NOTIF_TIMEOUT_MS,
NotificationHandler.openUrl(context, LibraryUpdateNotifier.HELP_WARNING_URL),
manga.id,
)
}
DownloadJob.start(context)
@ -324,6 +325,7 @@ class Downloader(
context.stringResource(MR.strings.download_insufficient_space),
download.chapter.name,
download.manga.title,
download.manga.id,
)
return
}
@ -407,7 +409,7 @@ class Downloader(
// If the page list threw, it will resume here
logcat(LogPriority.ERROR, error)
download.status = Download.State.ERROR
notifier.onError(error.message, download.chapter.name, download.manga.title)
notifier.onError(error.message, download.chapter.name, download.manga.title, download.manga.id)
}
}
@ -457,7 +459,7 @@ class Downloader(
// Mark this page as error and allow to download the remaining
page.progress = 0
page.status = Page.State.ERROR
notifier.onError(e.message, download.chapter.name, download.manga.title)
notifier.onError(e.message, download.chapter.name, download.manga.title, download.manga.id)
}
}

View File

@ -18,8 +18,10 @@ import eu.kanade.tachiyomi.util.system.notificationManager
import eu.kanade.tachiyomi.util.system.toShareIntent
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.runBlocking
import logcat.LogPriority
import tachiyomi.core.common.Constants
import tachiyomi.core.common.util.lang.launchIO
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.chapter.interactor.GetChapter
import tachiyomi.domain.chapter.interactor.UpdateChapter
import tachiyomi.domain.chapter.model.Chapter
@ -108,6 +110,11 @@ class NotificationReceiver : BroadcastReceiver() {
downloadChapters(urls, mangaId)
}
}
// Open an entry's page
// ACTION_OPEN_ENTRY -> {
// val mangaId = intent.getLongExtra(EXTRA_MANGA_ID, -1)
// openEntry(context, mangaId)
// }
}
}
@ -160,6 +167,19 @@ class NotificationReceiver : BroadcastReceiver() {
}
}
// private fun openEntry(context: Context, mangaId: Long){ // TODO: open manga
// val manga = runBlocking { getManga.await(mangaId) }
// if (manga != null){
// val intent = Intent(context,MainActivity::class.java).apply {
// flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
// }
// context.startActivity(intent)
// }
// else{
// context.toast(MR.strings.error_no_match)
// }
// }
/**
* Method called when user wants to stop a backup restore job.
*
@ -248,6 +268,8 @@ class NotificationReceiver : BroadcastReceiver() {
private const val ACTION_OPEN_CHAPTER = "$ID.$NAME.ACTION_OPEN_CHAPTER"
private const val ACTION_DOWNLOAD_CHAPTER = "$ID.$NAME.ACTION_DOWNLOAD_CHAPTER"
private const val ACTION_OPEN_ENTRY = "$ID.$NAME.ACTION_OPEN_ENTRY"
private const val ACTION_RESUME_DOWNLOADS = "$ID.$NAME.ACTION_RESUME_DOWNLOADS"
private const val ACTION_PAUSE_DOWNLOADS = "$ID.$NAME.ACTION_PAUSE_DOWNLOADS"
private const val ACTION_CLEAR_DOWNLOADS = "$ID.$NAME.ACTION_CLEAR_DOWNLOADS"
@ -484,6 +506,26 @@ class NotificationReceiver : BroadcastReceiver() {
)
}
/**
* Returns [PendingIntent] that opens the manga info controller
*
* @param context context of application
* @param mangaId id of the entry to open
*/
internal fun openEntryPendingActivity(context: Context, mangaId: Long): PendingIntent {
val newIntent =
Intent(context, MainActivity::class.java).setAction(Constants.SHORTCUT_MANGA)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(Constants.MANGA_EXTRA, mangaId)
.putExtra("notificationId", mangaId.hashCode())
return PendingIntent.getActivity(
context,
mangaId.hashCode(),
newIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
}
/**
* Returns [PendingIntent] that starts a service which stops the library update
*