From 6122eb3669ede809e4053b14c38b7cfbd13a1938 Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:16:25 -0500 Subject: [PATCH] feat: global notification --- app/src/main/java/ani/dantotsu/App.kt | 2 +- .../connections/comments/CommentsAPI.kt | 4 +- .../CommentNotificationWorker.kt | 113 +++++++++++++----- .../dantotsu/settings/saving/Preferences.kt | 1 + .../data/notification/Notifications.kt | 6 + 5 files changed, 93 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/App.kt b/app/src/main/java/ani/dantotsu/App.kt index bb1a7cb8..ecc2addd 100644 --- a/app/src/main/java/ani/dantotsu/App.kt +++ b/app/src/main/java/ani/dantotsu/App.kt @@ -167,7 +167,7 @@ class App : MultiDexApplication() { //run once androidx.work.WorkManager.getInstance(this).enqueue(OneTimeWorkRequest.Companion.from(AnilistNotificationWorker::class.java)) } - androidx.work.WorkManager.getInstance(this).cancelUniqueWork("ani.dantotsu.notifications.NotificationWorker") + androidx.work.WorkManager.getInstance(this).cancelUniqueWork("ani.dantotsu.notifications.NotificationWorker") //legacy worker } diff --git a/app/src/main/java/ani/dantotsu/connections/comments/CommentsAPI.kt b/app/src/main/java/ani/dantotsu/connections/comments/CommentsAPI.kt index c226631d..e06af9e4 100644 --- a/app/src/main/java/ani/dantotsu/connections/comments/CommentsAPI.kt +++ b/app/src/main/java/ani/dantotsu/connections/comments/CommentsAPI.kt @@ -412,7 +412,9 @@ data class Notification( @SerialName("type") val type: Int? = null, @SerialName("content") - val content: String? = null + val content: String? = null, + @SerialName("notification_id") + val notificationId: Int ) diff --git a/app/src/main/java/ani/dantotsu/notifications/CommentNotificationWorker.kt b/app/src/main/java/ani/dantotsu/notifications/CommentNotificationWorker.kt index 5aeb75c3..9f6a6da8 100644 --- a/app/src/main/java/ani/dantotsu/notifications/CommentNotificationWorker.kt +++ b/app/src/main/java/ani/dantotsu/notifications/CommentNotificationWorker.kt @@ -19,6 +19,7 @@ import ani.dantotsu.MainActivity import ani.dantotsu.R import ani.dantotsu.connections.comments.CommentsAPI import ani.dantotsu.settings.saving.PrefManager +import ani.dantotsu.settings.saving.PrefName import eu.kanade.tachiyomi.data.notification.Notifications import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -34,45 +35,75 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet PrefManager.init(applicationContext) //make sure prefs are initialized val client = OkHttpClient() CommentsAPI.fetchAuthToken(client) - val notifications = CommentsAPI.getNotifications(client) + val notificationResponse = CommentsAPI.getNotifications(client) + var notifications = notificationResponse?.notifications?.toMutableList() //if we have at least one reply notification, we need to fetch the media titles var names = emptyMap() - if (notifications?.notifications?.any { it.type == 1 || it.type == null } == true) { - val mediaIds = notifications.notifications.filter { it.type == 1 || it.type == null }.map { it.mediaId } + if (notifications?.any { it.type == 1 || it.type == null } == true) { + val mediaIds = notifications.filter { it.type == 1 || it.type == null }.map { it.mediaId } names = MediaNameFetch.fetchMediaTitles(mediaIds) } - notifications?.notifications?.forEach { + val recentGlobal = PrefManager.getVal( + PrefName.RecentGlobalNotification) + + notifications = notifications?.filter { it.type != 3 || it.notificationId > recentGlobal }?.toMutableList() + + val newRecentGlobal = notifications?.filter { it.type == 3 }?.maxOfOrNull { it.notificationId } + if (newRecentGlobal != null) { + PrefManager.setVal(PrefName.RecentGlobalNotification, newRecentGlobal) + } + + notifications?.forEach { val type: NotificationType = when (it.type) { 1 -> NotificationType.COMMENT_REPLY 2 -> NotificationType.COMMENT_WARNING + 3 -> NotificationType.APP_GLOBAL else -> NotificationType.COMMENT_REPLY } - val notification = if (it.type == 2) { - val title = "You received a warning" - val message = it.content ?: "Be more thoughtful with your comments" - createNotification( - NotificationType.COMMENT_WARNING, - message, - title, - it.mediaId, - it.commentId, - "", - "" - ) - } else { - val title = "New CommentNotificationWorker Reply" - val mediaName = names[it.mediaId]?.title ?: "Unknown" - val message = "${it.username} replied to your comment in $mediaName" - createNotification( - NotificationType.COMMENT_REPLY, - message, - title, - it.mediaId, - it.commentId, - names[it.mediaId]?.color ?: "#222222", - names[it.mediaId]?.coverImage ?: "" - ) + val notification = when (type) { + NotificationType.COMMENT_WARNING -> { + val title = "You received a warning" + val message = it.content ?: "Be more thoughtful with your comments" + createNotification( + NotificationType.COMMENT_WARNING, + message, + title, + it.mediaId, + it.commentId, + "", + "" + ) + } + + NotificationType.COMMENT_REPLY -> { + val title = "New CommentNotificationWorker Reply" + val mediaName = names[it.mediaId]?.title ?: "Unknown" + val message = "${it.username} replied to your comment in $mediaName" + createNotification( + NotificationType.COMMENT_REPLY, + message, + title, + it.mediaId, + it.commentId, + names[it.mediaId]?.color ?: "#222222", + names[it.mediaId]?.coverImage ?: "" + ) + } + + NotificationType.APP_GLOBAL -> { + val title = "Update from Dantotsu" + val message = it.content ?: "New feature available" + createNotification( + NotificationType.APP_GLOBAL, + message, + title, + 0, + 0, + "", + "" + ) + } } if (ActivityCompat.checkSelfPermission( @@ -141,7 +172,7 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet .setContentTitle(title) .setContentText(message) .setSmallIcon(R.drawable.notification_icon) - .setPriority(NotificationCompat.PRIORITY_HIGH) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .setAutoCancel(true) if (imageUrl.isNotEmpty()) { @@ -155,6 +186,25 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet } builder.build() } + NotificationType.APP_GLOBAL -> { + val intent = Intent(applicationContext, MainActivity::class.java).apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + val pendingIntent = PendingIntent.getActivity( + applicationContext, + 0, + intent, + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + ) + val builder = NotificationCompat.Builder(applicationContext, notificationType.id) + .setContentTitle(title) + .setContentText(message) + .setSmallIcon(R.drawable.notification_icon) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + builder.build() + } } return notification } @@ -182,7 +232,8 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet enum class NotificationType(val id: String) { COMMENT_REPLY(Notifications.CHANNEL_COMMENTS), - COMMENT_WARNING(Notifications.CHANNEL_COMMENT_WARING) + COMMENT_WARNING(Notifications.CHANNEL_COMMENT_WARING), + APP_GLOBAL(Notifications.CHANNEL_APP_GLOBAL) } companion object { diff --git a/app/src/main/java/ani/dantotsu/settings/saving/Preferences.kt b/app/src/main/java/ani/dantotsu/settings/saving/Preferences.kt index ccfb99f1..a95175d5 100644 --- a/app/src/main/java/ani/dantotsu/settings/saving/Preferences.kt +++ b/app/src/main/java/ani/dantotsu/settings/saving/Preferences.kt @@ -168,6 +168,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files CommentAuthResponse(Pref(Location.Irrelevant, AuthResponse::class, "")), CommentTokenExpiry(Pref(Location.Irrelevant, Long::class, 0L)), LogToFile(Pref(Location.Irrelevant, Boolean::class, false)), + RecentGlobalNotification(Pref(Location.Irrelevant, Int::class, 0)), //Protected DiscordToken(Pref(Location.Protected, String::class, "")), diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt index 11491edf..5db7ff8f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt @@ -53,6 +53,9 @@ object Notifications { const val CHANNEL_COMMENT_WARING = "comment_warning_channel" const val ID_COMMENT_REPLY = -801 + + const val CHANNEL_APP_GLOBAL = "app_global" + /** * Notification channel and ids used for anilist updates. */ @@ -151,6 +154,9 @@ object Notifications { setName("Anilist") setGroup(GROUP_ANILIST) }, + buildNotificationChannel(CHANNEL_APP_GLOBAL, IMPORTANCE_HIGH) { + setName("Global Updates") + }, buildNotificationChannel(CHANNEL_APP_UPDATE, IMPORTANCE_DEFAULT) { setGroup(GROUP_APK_UPDATES) setName("App Updates")