feat: better notification
This commit is contained in:
parent
2f7c6e734e
commit
e256fb1560
8 changed files with 131 additions and 27 deletions
|
@ -13,6 +13,10 @@ class MediaNameFetch {
|
|||
mediaIds.forEachIndexed { index, mediaId ->
|
||||
query += """
|
||||
media$index: Media(id: $mediaId) {
|
||||
coverImage {
|
||||
medium
|
||||
color
|
||||
}
|
||||
id
|
||||
title {
|
||||
romaji
|
||||
|
@ -24,7 +28,7 @@ class MediaNameFetch {
|
|||
return query
|
||||
}
|
||||
|
||||
suspend fun fetchMediaTitles(ids: List<Int>): Map<Int, String> {
|
||||
suspend fun fetchMediaTitles(ids: List<Int>): Map<Int, ReturnedData> {
|
||||
return try {
|
||||
val url = "https://graphql.anilist.co/"
|
||||
val data = mapOf(
|
||||
|
@ -40,15 +44,19 @@ class MediaNameFetch {
|
|||
data = data
|
||||
)
|
||||
val mediaResponse = parseMediaResponseWithGson(response.text)
|
||||
val mediaMap = mutableMapOf<Int, String>()
|
||||
val mediaMap = mutableMapOf<Int, ReturnedData>()
|
||||
mediaResponse.data.forEach { (_, mediaItem) ->
|
||||
mediaMap[mediaItem.id] = mediaItem.title.romaji
|
||||
mediaMap[mediaItem.id] = ReturnedData(
|
||||
mediaItem.title.romaji,
|
||||
mediaItem.coverImage.medium,
|
||||
mediaItem.coverImage.color
|
||||
)
|
||||
}
|
||||
mediaMap
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
val errorMap = mutableMapOf<Int, String>()
|
||||
ids.forEach { errorMap[it] = "Unknown" }
|
||||
val errorMap = mutableMapOf<Int, ReturnedData>()
|
||||
ids.forEach { errorMap[it] = ReturnedData("Unknown", "", "#222222") }
|
||||
errorMap
|
||||
}
|
||||
}
|
||||
|
@ -58,14 +66,17 @@ class MediaNameFetch {
|
|||
val type = object : TypeToken<MediaResponse>() {}.type
|
||||
return gson.fromJson(response, type)
|
||||
}
|
||||
data class ReturnedData(val title: String, val coverImage: String, val color: String)
|
||||
|
||||
data class MediaResponse(val data: Map<String, MediaItem>)
|
||||
data class MediaItem(
|
||||
val coverImage: MediaCoverImage,
|
||||
val id: Int,
|
||||
val title: MediaTitle
|
||||
)
|
||||
|
||||
data class MediaTitle(val romaji: String)
|
||||
data class MediaCoverImage(val medium: String, val color: String)
|
||||
|
||||
}
|
||||
}
|
|
@ -5,37 +5,55 @@ import android.app.PendingIntent
|
|||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import ani.dantotsu.MainActivity
|
||||
import ani.dantotsu.R
|
||||
import ani.dantotsu.connections.comments.CommentsAPI
|
||||
import ani.dantotsu.media.MediaDetailsActivity
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import okhttp3.OkHttpClient
|
||||
import java.io.File
|
||||
import java.text.DateFormat
|
||||
|
||||
|
||||
class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||
Worker(appContext, workerParams) {
|
||||
override fun doWork(): Result {
|
||||
val scope = CoroutineScope(Dispatchers.IO)
|
||||
//time in human readable format
|
||||
val formattedTime = DateFormat.getDateTimeInstance().format(System.currentTimeMillis())
|
||||
scope.launch {
|
||||
val notifications = CommentsAPI.getNotifications()
|
||||
PrefManager.init(applicationContext) //make sure prefs are initialized
|
||||
val client = OkHttpClient()
|
||||
CommentsAPI.fetchAuthToken(client)
|
||||
val notifications = CommentsAPI.getNotifications(client)
|
||||
val mediaIds = notifications?.notifications?.map { it.mediaId }
|
||||
val names = MediaNameFetch.fetchMediaTitles(mediaIds ?: emptyList())
|
||||
notifications?.notifications?.forEach {
|
||||
val title = "New Comment Reply"
|
||||
val mediaName = names[it.mediaId] ?: "Unknown"
|
||||
val mediaName = names[it.mediaId]?.title ?: "Unknown"
|
||||
val message = "${it.username} replied to your comment in $mediaName"
|
||||
val notification = createNotification(
|
||||
NotificationType.COMMENT_REPLY,
|
||||
message,
|
||||
title,
|
||||
it.mediaId,
|
||||
it.commentId
|
||||
it.commentId,
|
||||
names[it.mediaId]?.color ?: "#222222",
|
||||
names[it.mediaId]?.coverImage ?: ""
|
||||
)
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(
|
||||
|
@ -46,11 +64,10 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
|||
NotificationManagerCompat.from(applicationContext)
|
||||
.notify(
|
||||
NotificationType.COMMENT_REPLY.id,
|
||||
Notifications.ID_COMMENT_REPLY,
|
||||
it.commentId,
|
||||
notification
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return Result.success()
|
||||
|
@ -61,11 +78,13 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
|||
message: String,
|
||||
title: String,
|
||||
mediaId: Int,
|
||||
commentId: Int
|
||||
commentId: Int,
|
||||
color: String,
|
||||
imageUrl: String
|
||||
): android.app.Notification {
|
||||
val notification = when (notificationType) {
|
||||
NotificationType.COMMENT_REPLY -> {
|
||||
val intent = Intent(applicationContext, MediaDetailsActivity::class.java).apply {
|
||||
val intent = Intent(applicationContext, MainActivity::class.java).apply {
|
||||
putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
|
||||
putExtra("mediaId", mediaId)
|
||||
putExtra("commentId", commentId)
|
||||
|
@ -80,16 +99,46 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
|||
val builder = NotificationCompat.Builder(applicationContext, notificationType.id)
|
||||
.setContentTitle(title)
|
||||
.setContentText(message)
|
||||
.setSmallIcon(R.drawable.ic_round_comment_24)
|
||||
.setSmallIcon(R.drawable.notification_icon)
|
||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
if (imageUrl.isNotEmpty()) {
|
||||
val bitmap = getBitmapFromUrl(imageUrl)
|
||||
if (bitmap != null) {
|
||||
builder.setLargeIcon(bitmap)
|
||||
}
|
||||
}
|
||||
if (color.isNotEmpty()) {
|
||||
builder.color = Color.parseColor(color)
|
||||
}
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
return notification
|
||||
}
|
||||
|
||||
private fun getBitmapFromVectorDrawable(context: Context, drawableId: Int): Bitmap? {
|
||||
val drawable = ContextCompat.getDrawable(context, drawableId) ?: return null
|
||||
val bitmap = Bitmap.createBitmap(
|
||||
drawable.intrinsicWidth,
|
||||
drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
|
||||
)
|
||||
val canvas = Canvas(bitmap)
|
||||
drawable.setBounds(0, 0, canvas.width, canvas.height)
|
||||
drawable.draw(canvas)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
private fun getBitmapFromUrl(url: String): Bitmap? {
|
||||
return try {
|
||||
val inputStream = java.net.URL(url).openStream()
|
||||
BitmapFactory.decodeStream(inputStream)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
enum class NotificationType(val id: String) {
|
||||
COMMENT_REPLY(Notifications.CHANNEL_COMMENTS),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue