Updated E hentai notifications to new system
This commit is contained in:
parent
321eb38f2d
commit
3a79f8fb50
@ -1,78 +0,0 @@
|
|||||||
package eu.kanade.tachiyomi.data.library
|
|
||||||
|
|
||||||
import android.app.Notification
|
|
||||||
import android.app.PendingIntent
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import android.os.Build
|
|
||||||
import androidx.core.app.NotificationCompat
|
|
||||||
import eu.kanade.tachiyomi.R
|
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
|
||||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
|
||||||
import eu.kanade.tachiyomi.ui.main.MainActivity
|
|
||||||
import eu.kanade.tachiyomi.util.lang.chop
|
|
||||||
import eu.kanade.tachiyomi.util.system.notification
|
|
||||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
|
||||||
|
|
||||||
class LibraryUpdateNotifierEH(private val context: Context) {
|
|
||||||
/**
|
|
||||||
* Bitmap of the app for notifications.
|
|
||||||
*/
|
|
||||||
val notificationBitmap by lazy {
|
|
||||||
BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shows the notification containing the result of the update done by the service.
|
|
||||||
*
|
|
||||||
* @param updates a list of manga with new updates.
|
|
||||||
*/
|
|
||||||
fun showResultNotification(updates: List<Manga>) {
|
|
||||||
val newUpdates = updates.map { it.title.chop(45) }.toMutableSet()
|
|
||||||
|
|
||||||
// Append new chapters from a previous, existing notification
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
val previousNotification = context.notificationManager.activeNotifications
|
|
||||||
.find { it.id == Notifications.ID_OLD_LIBRARY_RESULT }
|
|
||||||
|
|
||||||
if (previousNotification != null) {
|
|
||||||
val oldUpdates = previousNotification.notification.extras
|
|
||||||
.getString(Notification.EXTRA_BIG_TEXT)
|
|
||||||
|
|
||||||
if (!oldUpdates.isNullOrEmpty()) {
|
|
||||||
newUpdates += oldUpdates.split("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.notificationManager.notify(
|
|
||||||
Notifications.ID_OLD_LIBRARY_RESULT,
|
|
||||||
context.notification(Notifications.CHANNEL_LIBRARY) {
|
|
||||||
setSmallIcon(R.drawable.ic_book_24dp)
|
|
||||||
setLargeIcon(notificationBitmap)
|
|
||||||
setContentTitle(context.getString(R.string.notification_new_chapters))
|
|
||||||
if (newUpdates.size > 1) {
|
|
||||||
setContentText(context.getString(R.string.notification_new_chapters_text_old, newUpdates.size))
|
|
||||||
setStyle(NotificationCompat.BigTextStyle().bigText(newUpdates.joinToString("\n")))
|
|
||||||
setNumber(newUpdates.size)
|
|
||||||
} else {
|
|
||||||
setContentText(newUpdates.first())
|
|
||||||
}
|
|
||||||
priority = NotificationCompat.PRIORITY_HIGH
|
|
||||||
setContentIntent(getNotificationIntent(context))
|
|
||||||
setAutoCancel(true)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an intent to open the main activity.
|
|
||||||
*/
|
|
||||||
private fun getNotificationIntent(context: Context): PendingIntent {
|
|
||||||
val intent = Intent(context, MainActivity::class.java)
|
|
||||||
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
|
|
||||||
intent.action = MainActivity.SHORTCUT_RECENTLY_UPDATED
|
|
||||||
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,7 +14,7 @@ import com.kizitonwose.time.hours
|
|||||||
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
||||||
import eu.kanade.tachiyomi.data.database.models.Chapter
|
import eu.kanade.tachiyomi.data.database.models.Chapter
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||||
import eu.kanade.tachiyomi.data.library.LibraryUpdateNotifierEH
|
import eu.kanade.tachiyomi.data.library.LibraryUpdateNotifier
|
||||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||||
import eu.kanade.tachiyomi.source.SourceManager
|
import eu.kanade.tachiyomi.source.SourceManager
|
||||||
import eu.kanade.tachiyomi.source.online.all.EHentai
|
import eu.kanade.tachiyomi.source.online.all.EHentai
|
||||||
@ -29,6 +29,7 @@ import exh.metadata.metadata.base.insertFlatMetadata
|
|||||||
import exh.util.await
|
import exh.util.await
|
||||||
import exh.util.cancellable
|
import exh.util.cancellable
|
||||||
import exh.util.jobScheduler
|
import exh.util.jobScheduler
|
||||||
|
import java.util.ArrayList
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@ -55,7 +56,7 @@ class EHentaiUpdateWorker : JobService(), CoroutineScope {
|
|||||||
private val updateHelper: EHentaiUpdateHelper by injectLazy()
|
private val updateHelper: EHentaiUpdateHelper by injectLazy()
|
||||||
private val logger = XLog.tag("EHUpdater")
|
private val logger = XLog.tag("EHUpdater")
|
||||||
|
|
||||||
private val updateNotifier by lazy { LibraryUpdateNotifierEH(this) }
|
private val updateNotifier by lazy { LibraryUpdateNotifier(this) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called if the system has determined that you must stop execution of your job
|
* This method is called if the system has determined that you must stop execution of your job
|
||||||
@ -162,7 +163,7 @@ class EHentaiUpdateWorker : JobService(), CoroutineScope {
|
|||||||
|
|
||||||
var failuresThisIteration = 0
|
var failuresThisIteration = 0
|
||||||
var updatedThisIteration = 0
|
var updatedThisIteration = 0
|
||||||
val updatedManga = mutableListOf<Manga>()
|
val updatedManga = ArrayList<Pair<Manga, Array<Chapter>>>()
|
||||||
val modifiedThisIteration = mutableSetOf<Long>()
|
val modifiedThisIteration = mutableSetOf<Long>()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -226,9 +227,9 @@ class EHentaiUpdateWorker : JobService(), CoroutineScope {
|
|||||||
updateHelper.findAcceptedRootAndDiscardOthers(manga.source, chapters).await()
|
updateHelper.findAcceptedRootAndDiscardOthers(manga.source, chapters).await()
|
||||||
|
|
||||||
if ((new.isNotEmpty() && manga.id == acceptedRoot.manga.id) ||
|
if ((new.isNotEmpty() && manga.id == acceptedRoot.manga.id) ||
|
||||||
(hasNew && updatedManga.none { it.id == acceptedRoot.manga.id })
|
(hasNew && updatedManga.none { it.first.id == acceptedRoot.manga.id })
|
||||||
) {
|
) {
|
||||||
updatedManga += acceptedRoot.manga
|
updatedManga += Pair(acceptedRoot.manga, new.toTypedArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
modifiedThisIteration += acceptedRoot.manga.id!!
|
modifiedThisIteration += acceptedRoot.manga.id!!
|
||||||
@ -247,7 +248,7 @@ class EHentaiUpdateWorker : JobService(), CoroutineScope {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (updatedManga.isNotEmpty()) {
|
if (updatedManga.isNotEmpty()) {
|
||||||
updateNotifier.showResultNotification(updatedManga)
|
updateNotifier.showUpdateNotifications(updatedManga)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user