feat: warnings
This commit is contained in:
parent
6e6429db82
commit
a401ab89f3
3 changed files with 92 additions and 22 deletions
|
@ -408,7 +408,11 @@ data class Notification(
|
||||||
@SerialName("media_id")
|
@SerialName("media_id")
|
||||||
val mediaId: Int,
|
val mediaId: Int,
|
||||||
@SerialName("comment_id")
|
@SerialName("comment_id")
|
||||||
val commentId: Int
|
val commentId: Int,
|
||||||
|
@SerialName("type")
|
||||||
|
val type: Int? = null,
|
||||||
|
@SerialName("content")
|
||||||
|
val content: String? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -449,6 +453,8 @@ data class User(
|
||||||
val isMod: Boolean? = null,
|
val isMod: Boolean? = null,
|
||||||
@SerialName("total_votes")
|
@SerialName("total_votes")
|
||||||
val totalVotes: Int,
|
val totalVotes: Int,
|
||||||
|
@SerialName("warnings")
|
||||||
|
val warnings: Int
|
||||||
) : java.io.Serializable {
|
) : java.io.Serializable {
|
||||||
companion object {
|
companion object {
|
||||||
private const val serialVersionUID: Long = 1
|
private const val serialVersionUID: Long = 1
|
||||||
|
|
|
@ -18,35 +18,54 @@ import androidx.work.WorkerParameters
|
||||||
import ani.dantotsu.MainActivity
|
import ani.dantotsu.MainActivity
|
||||||
import ani.dantotsu.R
|
import ani.dantotsu.R
|
||||||
import ani.dantotsu.connections.comments.CommentsAPI
|
import ani.dantotsu.connections.comments.CommentsAPI
|
||||||
import ani.dantotsu.media.MediaDetailsActivity
|
|
||||||
import ani.dantotsu.settings.saving.PrefManager
|
import ani.dantotsu.settings.saving.PrefManager
|
||||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import java.io.File
|
import java.util.Locale
|
||||||
import java.text.DateFormat
|
|
||||||
|
|
||||||
|
|
||||||
class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||||
Worker(appContext, workerParams) {
|
Worker(appContext, workerParams) {
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
val scope = CoroutineScope(Dispatchers.IO)
|
val scope = CoroutineScope(Dispatchers.IO)
|
||||||
//time in human readable format
|
|
||||||
val formattedTime = DateFormat.getDateTimeInstance().format(System.currentTimeMillis())
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
PrefManager.init(applicationContext) //make sure prefs are initialized
|
PrefManager.init(applicationContext) //make sure prefs are initialized
|
||||||
val client = OkHttpClient()
|
val client = OkHttpClient()
|
||||||
CommentsAPI.fetchAuthToken(client)
|
CommentsAPI.fetchAuthToken(client)
|
||||||
val notifications = CommentsAPI.getNotifications(client)
|
val notifications = CommentsAPI.getNotifications(client)
|
||||||
val mediaIds = notifications?.notifications?.map { it.mediaId }
|
//if we have at least one reply notification, we need to fetch the media titles
|
||||||
val names = MediaNameFetch.fetchMediaTitles(mediaIds ?: emptyList())
|
var names = emptyMap<Int, MediaNameFetch.Companion.ReturnedData>()
|
||||||
|
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 }
|
||||||
|
names = MediaNameFetch.fetchMediaTitles(mediaIds)
|
||||||
|
}
|
||||||
|
|
||||||
notifications?.notifications?.forEach {
|
notifications?.notifications?.forEach {
|
||||||
|
val type: NotificationType = when (it.type) {
|
||||||
|
1 -> NotificationType.COMMENT_REPLY
|
||||||
|
2 -> NotificationType.COMMENT_WARNING
|
||||||
|
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 Comment Reply"
|
val title = "New Comment Reply"
|
||||||
val mediaName = names[it.mediaId]?.title ?: "Unknown"
|
val mediaName = names[it.mediaId]?.title ?: "Unknown"
|
||||||
val message = "${it.username} replied to your comment in $mediaName"
|
val message = "${it.username} replied to your comment in $mediaName"
|
||||||
val notification = createNotification(
|
createNotification(
|
||||||
NotificationType.COMMENT_REPLY,
|
NotificationType.COMMENT_REPLY,
|
||||||
message,
|
message,
|
||||||
title,
|
title,
|
||||||
|
@ -55,6 +74,7 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||||
names[it.mediaId]?.color ?: "#222222",
|
names[it.mediaId]?.color ?: "#222222",
|
||||||
names[it.mediaId]?.coverImage ?: ""
|
names[it.mediaId]?.coverImage ?: ""
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (ActivityCompat.checkSelfPermission(
|
if (ActivityCompat.checkSelfPermission(
|
||||||
applicationContext,
|
applicationContext,
|
||||||
|
@ -63,8 +83,8 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||||
) {
|
) {
|
||||||
NotificationManagerCompat.from(applicationContext)
|
NotificationManagerCompat.from(applicationContext)
|
||||||
.notify(
|
.notify(
|
||||||
NotificationType.COMMENT_REPLY.id,
|
type.id,
|
||||||
it.commentId,
|
System.currentTimeMillis().toInt(),
|
||||||
notification
|
notification
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -83,6 +103,28 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||||
imageUrl: String
|
imageUrl: String
|
||||||
): android.app.Notification {
|
): android.app.Notification {
|
||||||
val notification = when (notificationType) {
|
val notification = when (notificationType) {
|
||||||
|
NotificationType.COMMENT_WARNING -> {
|
||||||
|
val intent = Intent(applicationContext, MainActivity::class.java).apply {
|
||||||
|
putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
|
||||||
|
putExtra("mediaId", mediaId)
|
||||||
|
putExtra("commentId", commentId)
|
||||||
|
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()
|
||||||
|
}
|
||||||
NotificationType.COMMENT_REPLY -> {
|
NotificationType.COMMENT_REPLY -> {
|
||||||
val intent = Intent(applicationContext, MainActivity::class.java).apply {
|
val intent = Intent(applicationContext, MainActivity::class.java).apply {
|
||||||
putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
|
putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
|
||||||
|
@ -141,6 +183,7 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters) :
|
||||||
|
|
||||||
enum class NotificationType(val id: String) {
|
enum class NotificationType(val id: String) {
|
||||||
COMMENT_REPLY(Notifications.CHANNEL_COMMENTS),
|
COMMENT_REPLY(Notifications.CHANNEL_COMMENTS),
|
||||||
|
COMMENT_WARNING(Notifications.CHANNEL_COMMENT_WARING)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
|
@ -48,7 +48,9 @@ object Notifications {
|
||||||
/**
|
/**
|
||||||
* Notification channel used for comment notifications
|
* Notification channel used for comment notifications
|
||||||
*/
|
*/
|
||||||
|
private const val GROUP_COMMENTS = "group_comments"
|
||||||
const val CHANNEL_COMMENTS = "comments_channel"
|
const val CHANNEL_COMMENTS = "comments_channel"
|
||||||
|
const val CHANNEL_COMMENT_WARING = "comment_warning_channel"
|
||||||
const val ID_COMMENT_REPLY = -801
|
const val ID_COMMENT_REPLY = -801
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,6 +94,20 @@ object Notifications {
|
||||||
// Delete old notification channels
|
// Delete old notification channels
|
||||||
deprecatedChannels.forEach(notificationManager::deleteNotificationChannel)
|
deprecatedChannels.forEach(notificationManager::deleteNotificationChannel)
|
||||||
|
|
||||||
|
notificationManager.createNotificationChannelGroupsCompat(
|
||||||
|
listOf(
|
||||||
|
buildNotificationChannelGroup(GROUP_DOWNLOADER) {
|
||||||
|
setName("Downloader")
|
||||||
|
},
|
||||||
|
buildNotificationChannelGroup(GROUP_APK_UPDATES) {
|
||||||
|
setName("App & Extension Updates")
|
||||||
|
},
|
||||||
|
buildNotificationChannelGroup(GROUP_COMMENTS) {
|
||||||
|
setName("Comments")
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
notificationManager.createNotificationChannelsCompat(
|
notificationManager.createNotificationChannelsCompat(
|
||||||
listOf(
|
listOf(
|
||||||
buildNotificationChannel(CHANNEL_COMMON, IMPORTANCE_LOW) {
|
buildNotificationChannel(CHANNEL_COMMON, IMPORTANCE_LOW) {
|
||||||
|
@ -115,6 +131,11 @@ object Notifications {
|
||||||
},
|
},
|
||||||
buildNotificationChannel(CHANNEL_COMMENTS, IMPORTANCE_HIGH) {
|
buildNotificationChannel(CHANNEL_COMMENTS, IMPORTANCE_HIGH) {
|
||||||
setName("Comments")
|
setName("Comments")
|
||||||
|
setGroup(GROUP_COMMENTS)
|
||||||
|
},
|
||||||
|
buildNotificationChannel(CHANNEL_COMMENT_WARING, IMPORTANCE_HIGH) {
|
||||||
|
setName("Comment Warnings")
|
||||||
|
setGroup(GROUP_COMMENTS)
|
||||||
},
|
},
|
||||||
buildNotificationChannel(CHANNEL_APP_UPDATE, IMPORTANCE_DEFAULT) {
|
buildNotificationChannel(CHANNEL_APP_UPDATE, IMPORTANCE_DEFAULT) {
|
||||||
setGroup(GROUP_APK_UPDATES)
|
setGroup(GROUP_APK_UPDATES)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue