Using a globalscope instead of current scope for showing new chapters notification

Since it may get called after a cancelled job
This commit is contained in:
Jay 2020-06-01 20:25:14 -04:00
parent 7d5fa5a603
commit 3a74c7918d

View file

@ -26,6 +26,8 @@ import eu.kanade.tachiyomi.util.lang.chop
import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.notification
import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationBuilder
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.util.ArrayList import java.util.ArrayList
@ -119,99 +121,98 @@ class LibraryUpdateNotifier(private val context: Context) {
* *
* @param updates a list of manga with new updates. * @param updates a list of manga with new updates.
*/ */
suspend fun showResultNotification(updates: Map<LibraryManga, Array<Chapter>>) { fun showResultNotification(updates: Map<LibraryManga, Array<Chapter>>) {
val notifications = ArrayList<Pair<Notification, Int>>() GlobalScope.launch {
updates.forEach { val notifications = ArrayList<Pair<Notification, Int>>()
val manga = it.key updates.forEach {
val chapters = it.value val manga = it.key
val chapterNames = chapters.map { chapter -> chapter.name } val chapters = it.value
notifications.add(Pair(context.notification(Notifications.CHANNEL_NEW_CHAPTERS) { val chapterNames = chapters.map { chapter -> chapter.name }
setSmallIcon(R.drawable.ic_tachi) notifications.add(Pair(context.notification(Notifications.CHANNEL_NEW_CHAPTERS) {
try {
val request = GetRequest.Builder(context).data(manga)
.networkCachePolicy(CachePolicy.DISABLED)
.transformations(CircleCropTransformation()).size(width = ICON_SIZE, height = ICON_SIZE)
.build()
Coil.imageLoader(context)
.execute(request).drawable?.let { drawable ->
setLargeIcon((drawable as BitmapDrawable).bitmap)
}
} catch (e: Exception) { }
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
setContentTitle(manga.title)
color = ContextCompat.getColor(context, R.color.colorAccent)
val chaptersNames = if (chapterNames.size > MAX_CHAPTERS) {
"${chapterNames.take(MAX_CHAPTERS - 1)
.joinToString(", ")}, " + context.resources.getQuantityString(
R.plurals.notification_and_n_more,
(chapterNames.size - (MAX_CHAPTERS - 1)),
(chapterNames.size - (MAX_CHAPTERS - 1))
)
} else chapterNames.joinToString(", ")
setContentText(chaptersNames)
setStyle(NotificationCompat.BigTextStyle().bigText(chaptersNames))
priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS)
setContentIntent(
NotificationReceiver.openChapterPendingActivity(
context, manga, chapters.first()
)
)
addAction(
R.drawable.ic_eye_24dp, context.getString(R.string.mark_as_read),
NotificationReceiver.markAsReadPendingBroadcast(
context,
manga, chapters, Notifications.ID_NEW_CHAPTERS
)
)
addAction(
R.drawable.ic_book_24dp, context.getString(R.string.view_chapters),
NotificationReceiver.openChapterPendingActivity(
context,
manga, Notifications.ID_NEW_CHAPTERS
)
)
setAutoCancel(true)
}, manga.id.hashCode()))
}
NotificationManagerCompat.from(context).apply {
notify(
Notifications.ID_NEW_CHAPTERS,
context.notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachi) setSmallIcon(R.drawable.ic_tachi)
setLargeIcon(notificationBitmap) try {
setContentTitle(context.getString(R.string.new_chapters_found)) val request = GetRequest.Builder(context).data(manga)
color = ContextCompat.getColor(context, R.color.colorAccent) .networkCachePolicy(CachePolicy.DISABLED)
if (updates.size > 1) { .transformations(CircleCropTransformation())
setContentText( .size(width = ICON_SIZE, height = ICON_SIZE).build()
context.resources.getQuantityString(
R.plurals Coil.imageLoader(context).execute(request).drawable?.let { drawable ->
.for_n_titles, setLargeIcon((drawable as BitmapDrawable).bitmap)
updates.size, updates.size }
) } catch (e: Exception) {
)
setStyle(
NotificationCompat.BigTextStyle()
.bigText(updates.keys.joinToString("\n") {
it.title.chop(45)
})
)
} else {
setContentText(updates.keys.first().title.chop(45))
} }
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
setContentTitle(manga.title)
color = ContextCompat.getColor(context, R.color.colorAccent)
val chaptersNames = if (chapterNames.size > MAX_CHAPTERS) {
"${chapterNames.take(MAX_CHAPTERS - 1)
.joinToString(", ")}, " + context.resources.getQuantityString(
R.plurals.notification_and_n_more,
(chapterNames.size - (MAX_CHAPTERS - 1)),
(chapterNames.size - (MAX_CHAPTERS - 1))
)
} else chapterNames.joinToString(", ")
setContentText(chaptersNames)
setStyle(NotificationCompat.BigTextStyle().bigText(chaptersNames))
priority = NotificationCompat.PRIORITY_HIGH priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS) setGroup(Notifications.GROUP_NEW_CHAPTERS)
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) setContentIntent(
setGroupSummary(true) NotificationReceiver.openChapterPendingActivity(
setContentIntent(getNotificationIntent()) context, manga, chapters.first()
)
)
addAction(
R.drawable.ic_eye_24dp,
context.getString(R.string.mark_as_read),
NotificationReceiver.markAsReadPendingBroadcast(
context, manga, chapters, Notifications.ID_NEW_CHAPTERS
)
)
addAction(
R.drawable.ic_book_24dp,
context.getString(R.string.view_chapters),
NotificationReceiver.openChapterPendingActivity(
context, manga, Notifications.ID_NEW_CHAPTERS
)
)
setAutoCancel(true) setAutoCancel(true)
}) }, manga.id.hashCode()))
}
notifications.forEach { NotificationManagerCompat.from(context).apply {
notify(it.second, it.first)
notify(Notifications.ID_NEW_CHAPTERS,
context.notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachi)
setLargeIcon(notificationBitmap)
setContentTitle(context.getString(R.string.new_chapters_found))
color = ContextCompat.getColor(context, R.color.colorAccent)
if (updates.size > 1) {
setContentText(
context.resources.getQuantityString(
R.plurals.for_n_titles, updates.size, updates.size
)
)
setStyle(
NotificationCompat.BigTextStyle()
.bigText(updates.keys.joinToString("\n") {
it.title.chop(45)
})
)
} else {
setContentText(updates.keys.first().title.chop(45))
}
priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS)
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
setGroupSummary(true)
setContentIntent(getNotificationIntent())
setAutoCancel(true)
})
notifications.forEach {
notify(it.second, it.first)
}
} }
} }
} }