Skip to content

Commit

Permalink
feat: subscriptions in notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed May 17, 2024
1 parent 6c1176a commit df2867c
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum class NotificationType(val value: String) {
//custom
COMMENT_REPLY("COMMENT_REPLY"),
COMMENT_WARNING("COMMENT_WARNING"),
DANTOTSU_UPDATE("DANTOTSU_UPDATE");
DANTOTSU_UPDATE("DANTOTSU_UPDATE"),
SUBSCRIPTION("SUBSCRIPTION");

fun toFormattedString(): String {
return this.value.replace("_", " ").lowercase(Locale.ROOT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class CommentNotificationTask : Task {
null
) ?: listOf()
val newStore = notificationStore.toMutableList()
if (newStore.size > 10) {
if (newStore.size > 30) {
newStore.remove(newStore.minByOrNull { it.time })
}
if (newStore.any { it.content == notification.content }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ data class CommentStore(
val time: Long = System.currentTimeMillis(),
) : java.io.Serializable {
companion object {

@Suppress("INAPPROPRIATE_CONST_NAME")
private const val serialVersionUID = 2L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ class SubscriptionNotificationTask : Task {
if (ep != null) ep.number + " " + context.getString(R.string.just_released) to null
else null
} ?: return@map
addSubscriptionToStore(
SubscriptionStore(
media.name,
text.first,
media.id
)
)
PrefManager.setVal(PrefName.UnreadCommentNotifications,
PrefManager.getVal<Int>(PrefName.UnreadCommentNotifications) + 1)
val notification = createNotification(
context.applicationContext,
media,
Expand Down Expand Up @@ -219,4 +228,17 @@ class SubscriptionNotificationTask : Task {
}
)
}

private fun addSubscriptionToStore(notification: SubscriptionStore) {
val notificationStore = PrefManager.getNullableVal<List<SubscriptionStore>>(
PrefName.SubscriptionNotificationStore,
null
) ?: listOf()
val newStore = notificationStore.toMutableList()
if (newStore.size >= 100) {
newStore.remove(newStore.minByOrNull { it.time })
}
newStore.add(notification)
PrefManager.setVal(PrefName.SubscriptionNotificationStore, newStore)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ani.dantotsu.notifications.subscription

import kotlinx.serialization.Serializable

@Serializable
data class SubscriptionStore(
val title: String,
val content: String,
val mediaId: Int,
val type: String = "SUBSCRIPTION",
val time: Long = System.currentTimeMillis(),
) : java.io.Serializable {
companion object {
private const val serialVersionUID = 1L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class ActivityItemBuilder {
NotificationType.DANTOTSU_UPDATE -> {
notification.context ?: "You should not see this"
}

NotificationType.SUBSCRIPTION -> {
notification.context ?: "You should not see this"
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import ani.dantotsu.initActivity
import ani.dantotsu.media.MediaDetailsActivity
import ani.dantotsu.navBarHeight
import ani.dantotsu.notifications.comment.CommentStore
import ani.dantotsu.notifications.subscription.SubscriptionStore
import ani.dantotsu.profile.ProfileActivity
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
Expand All @@ -41,6 +42,8 @@ import kotlinx.coroutines.withContext

class NotificationActivity : AppCompatActivity() {
private lateinit var binding: ActivityFollowBinding
private lateinit var commentStore: List<CommentStore>
private lateinit var subscriptionStore: List<SubscriptionStore>
private var adapter: GroupieAdapter = GroupieAdapter()
private var notificationList: List<Notification> = emptyList()
val filters = ArrayList<String>()
Expand Down Expand Up @@ -70,6 +73,15 @@ class NotificationActivity : AppCompatActivity() {
onBackPressedDispatcher.onBackPressed()
}
binding.listProgressBar.visibility = ViewGroup.VISIBLE
commentStore = PrefManager.getNullableVal<List<CommentStore>>(
PrefName.CommentNotificationStore,
null
) ?: listOf()
subscriptionStore = PrefManager.getNullableVal<List<SubscriptionStore>>(
PrefName.SubscriptionNotificationStore,
null
) ?: listOf()

binding.followFilterButton.setOnClickListener {
val dialogView = LayoutInflater.from(currContext()).inflate(R.layout.custom_dialog_layout, null)
val checkboxContainer = dialogView.findViewById<LinearLayout>(R.id.checkboxContainer)
Expand Down Expand Up @@ -193,22 +205,39 @@ class NotificationActivity : AppCompatActivity() {
notifications
}.toMutableList()
}
if (activityId == -1 && currentPage == 1) {
val commentStore = PrefManager.getNullableVal<List<CommentStore>>(
PrefName.CommentNotificationStore,
null
) ?: listOf()
if (activityId == -1) {
val furthestTime = newNotifications.minOfOrNull { it.createdAt } ?: 0
commentStore.forEach {
val notification = Notification(
it.type.toString(),
System.currentTimeMillis().toInt(),
commentId = it.commentId,
notificationType = it.type.toString(),
mediaId = it.mediaId,
context = it.title + "\n" + it.content,
createdAt = (it.time / 1000L).toInt(),
)
newNotifications += notification
if ((it.time > furthestTime * 1000L || !hasNextPage) && notificationList.none { notification ->
notification.commentId == it.commentId && notification.createdAt == (it.time / 1000L).toInt()
}) {
val notification = Notification(
it.type.toString(),
System.currentTimeMillis().toInt(),
commentId = it.commentId,
notificationType = it.type.toString(),
mediaId = it.mediaId,
context = it.title + "\n" + it.content,
createdAt = (it.time / 1000L).toInt(),
)
newNotifications += notification
}
}
subscriptionStore.forEach {
if ((it.time > furthestTime * 1000L || !hasNextPage) && notificationList.none { notification ->
notification.mediaId == it.mediaId && notification.createdAt == (it.time / 1000L).toInt()
}) {
val notification = Notification(
it.type,
System.currentTimeMillis().toInt(),
commentId = it.mediaId,
mediaId = it.mediaId,
notificationType = it.type,
context = it.content,
createdAt = (it.time / 1000L).toInt(),
)
newNotifications += notification
}
}
newNotifications.sortByDescending { it.createdAt }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ class NotificationItem(
NotificationType.DANTOTSU_UPDATE -> {
image(user = true)
}

NotificationType.SUBSCRIPTION -> {
image(user = true, commentNotification = true)
binding.notificationCoverUser.setOnClickListener {
clickCallback(
notification.mediaId ?: 0, null, NotificationClickType.MEDIA
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.mediaId ?: 0, null, NotificationClickType.MEDIA
)
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/ani/dantotsu/settings/saving/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Color
import ani.dantotsu.connections.comments.AuthResponse
import ani.dantotsu.connections.mal.MAL
import ani.dantotsu.notifications.comment.CommentStore
import ani.dantotsu.notifications.subscription.SubscriptionStore
import ani.dantotsu.settings.saving.internal.Location
import ani.dantotsu.settings.saving.internal.Pref

Expand Down Expand Up @@ -185,6 +186,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
LogToFile(Pref(Location.Irrelevant, Boolean::class, false)),
RecentGlobalNotification(Pref(Location.Irrelevant, Int::class, 0)),
CommentNotificationStore(Pref(Location.Irrelevant, List::class, listOf<CommentStore>())),
SubscriptionNotificationStore(Pref(Location.Irrelevant, List::class, listOf<SubscriptionStore>())),
UnreadCommentNotifications(Pref(Location.Irrelevant, Int::class, 0)),
DownloadsDir(Pref(Location.Irrelevant, String::class, "")),
RefreshStatus(Pref(Location.Irrelevant, Boolean::class, false)),
Expand Down

0 comments on commit df2867c

Please sign in to comment.