Add "show entry" action to download notifications (#1159)

* Add 'show entry' to download notifications

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>

* fixup! Add 'show entry' to download notifications

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>

* fixup! Add 'show entry' to download notifications

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>

* spotless! Add 'show entry' to download notifications

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>

* fixup! spotless- Apply suggestions from code review

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>

---------

Signed-off-by: Catting <5874051+mm12@users.noreply.github.com>
Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
This commit is contained in:
Catting 2024-08-26 08:31:02 -05:00 committed by GitHub
parent 45628b14db
commit 952a98c180
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 4 deletions

View file

@ -83,6 +83,11 @@ 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 +165,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 +176,13 @@ 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 +200,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 the error occurred 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 +212,13 @@ 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

@ -324,6 +324,7 @@ class Downloader(
context.stringResource(MR.strings.download_insufficient_space),
download.chapter.name,
download.manga.title,
download.manga.id,
)
return
}
@ -407,7 +408,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 +458,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

@ -248,6 +248,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 +486,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
*