feat: anilist notifications

This commit is contained in:
rebelonion 2024-03-07 02:51:04 -06:00
parent e2eae6250b
commit 7ac679f927
15 changed files with 572 additions and 28 deletions

View file

@ -70,13 +70,14 @@ class ProfileFragment() : Fragment() {
binding.profileUserBio.settings.loadWithOverviewMode = true
binding.profileUserBio.settings.useWideViewPort = true
binding.profileUserBio.setInitialScale(1)
val styledHtml = styled(
convertMarkdownToHtml(user.about ?: ""),
backGroundColorTypedValue.data,
textColorTypedValue.data
)
binding.profileUserBio.loadDataWithBaseURL(
null,
styled(
convertMarkdownToHtml(user.about ?: ""),
backGroundColorTypedValue.data,
textColorTypedValue.data
),
styledHtml,
"text/html; charset=utf-8",
"UTF-8",
null
@ -215,7 +216,22 @@ class ProfileFragment() : Fragment() {
}
}
private fun styled(html: String, backGroundColor: Int, textColor: Int): String {
private fun styled(html: String, backGroundColor: Int, textColor: Int): String { //istg anilist has the worst api
//remove some of the html entities
val step1 = html.replace(" ", " ")
.replace("&", "&")
.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&quot;", "\"")
.replace("&apos;", "'")
.replace("<pre>", "")
.replace("`", "")
.replace("~", "")
val step2 = step1.replace("(?s)___(.*?)___".toRegex(), "<br><em><strong>$1</strong></em><br>")
val step3 = step2.replace("(?s)__(.*?)__".toRegex(), "<br><strong>$1</strong><br>")
return """
<html>
<head>
@ -233,6 +249,10 @@ class ProfileFragment() : Fragment() {
max-width: 100%;
height: auto; /* Maintain aspect ratio */
}
video {
max-width: 100%;
height: auto; /* Maintain aspect ratio */
}
a {
color: ${textColor.toCssColor()};
}
@ -240,7 +260,7 @@ class ProfileFragment() : Fragment() {
</style>
</head>
<body>
$html
$step3
</body>
""".trimIndent()

View file

@ -0,0 +1,22 @@
package ani.dantotsu.profile.activity
import android.view.View
import ani.dantotsu.R
import ani.dantotsu.databinding.ItemNotificationBinding
import com.xwray.groupie.viewbinding.BindableItem
class ActivityItem(
): BindableItem<ItemNotificationBinding>() {
private lateinit var binding: ItemNotificationBinding
override fun bind(viewBinding: ItemNotificationBinding, position: Int) {
binding = viewBinding
}
override fun getLayout(): Int {
return R.layout.item_notification
}
override fun initializeViewBinding(view: View): ItemNotificationBinding {
return ItemNotificationBinding.bind(view)
}
}

View file

@ -0,0 +1,144 @@
package ani.dantotsu.profile.activity
import android.view.View
import ani.dantotsu.R
import ani.dantotsu.connections.anilist.api.Notification
import ani.dantotsu.connections.anilist.api.NotificationType
import ani.dantotsu.databinding.ItemNotificationBinding
import ani.dantotsu.loadImage
import ani.dantotsu.notifications.NotificationActivity
import com.xwray.groupie.viewbinding.BindableItem
class NotificationItem(
private val notification: Notification,
val clickCallback: (Int, NotificationActivity.Companion.NotificationClickType) -> Unit
): BindableItem<ItemNotificationBinding>() {
private lateinit var binding: ItemNotificationBinding
private lateinit var clickType: NotificationActivity.Companion.NotificationClickType
private var id = 0
override fun bind(viewBinding: ItemNotificationBinding, position: Int) {
binding = viewBinding
setBinding()
}
override fun getLayout(): Int {
return R.layout.item_notification
}
override fun initializeViewBinding(view: View): ItemNotificationBinding {
return ItemNotificationBinding.bind(view)
}
private fun setBinding() {
val notificationType: NotificationType =
NotificationType.valueOf(notification.notificationType)
binding.notificationText.text = NotificationItemBuilder.getContent(notification)
binding.notificationDate.text = NotificationItemBuilder.getDateTime(notification.createdAt)
binding.root.setOnClickListener { clickCallback(id, clickType) }
when (notificationType) {
NotificationType.ACTIVITY_MESSAGE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.ACTIVITY_REPLY -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.FOLLOWING -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.ACTIVITY_MENTION -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.THREAD_COMMENT_MENTION -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.THREAD_SUBSCRIBED -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.THREAD_COMMENT_REPLY -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.AIRING -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large)
binding.notificationBannerImage.loadImage(notification.media?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA
id = notification.media?.id ?: 0
}
NotificationType.ACTIVITY_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.ACTIVITY_REPLY_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.THREAD_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.THREAD_COMMENT_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.ACTIVITY_REPLY_SUBSCRIBED -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.loadImage(notification.user?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.USER
id = notification.user?.id ?: 0
}
NotificationType.RELATED_MEDIA_ADDITION -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large)
binding.notificationBannerImage.loadImage(notification.media?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA
id = notification.media?.id ?: 0
}
NotificationType.MEDIA_DATA_CHANGE -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large)
binding.notificationBannerImage.loadImage(notification.media?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA
id = notification.media?.id ?: 0
}
NotificationType.MEDIA_MERGE -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large)
binding.notificationBannerImage.loadImage(notification.media?.bannerImage)
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA
id = notification.media?.id ?: 0
}
NotificationType.MEDIA_DELETION -> {
binding.notificationCover.visibility = View.GONE
clickType = NotificationActivity.Companion.NotificationClickType.UNDEFINED
id = 0
}
}
}
}

View file

@ -0,0 +1,148 @@
package ani.dantotsu.profile.activity
import ani.dantotsu.connections.anilist.api.Notification
import ani.dantotsu.connections.anilist.api.NotificationType
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
/*
* ACTIVITY_MESSAGE
A user has sent you message
ACTIVITY_REPLY
A user has replied to your activity
FOLLOWING
A user has followed you
ACTIVITY_MENTION
A user has mentioned you in their activity
THREAD_COMMENT_MENTION
A user has mentioned you in a forum comment
THREAD_SUBSCRIBED
A user has commented in one of your subscribed forum threads
THREAD_COMMENT_REPLY
A user has replied to your forum comment
AIRING
An anime you are currently watching has aired
ACTIVITY_LIKE
A user has liked your activity
ACTIVITY_REPLY_LIKE
A user has liked your activity reply
THREAD_LIKE
A user has liked your forum thread
THREAD_COMMENT_LIKE
A user has liked your forum comment
ACTIVITY_REPLY_SUBSCRIBED
A user has replied to activity you have also replied to
RELATED_MEDIA_ADDITION
A new anime or manga has been added to the site where its related media is on the user's list
MEDIA_DATA_CHANGE
An anime or manga has had a data change that affects how a user may track it in their lists
MEDIA_MERGE
Anime or manga entries on the user's list have been merged into a single entry
MEDIA_DELETION
An anime or manga on the user's list has been deleted from the site
* */
interface NotificationItemBuilder {
companion object {
fun getContent(notification: Notification): String {
val notificationType: NotificationType =
NotificationType.valueOf(notification.notificationType)
return when (notificationType) {
NotificationType.ACTIVITY_MESSAGE -> {
"${notification.user?.name} sent you a message"
}
NotificationType.ACTIVITY_REPLY -> {
"${notification.user?.name} replied to your activity"
}
NotificationType.FOLLOWING -> {
"${notification.user?.name} followed you"
}
NotificationType.ACTIVITY_MENTION -> {
"${notification.user?.name} mentioned you in their activity"
}
NotificationType.THREAD_COMMENT_MENTION -> {
"${notification.user?.name} mentioned you in a forum comment"
}
NotificationType.THREAD_SUBSCRIBED -> {
"${notification.user?.name} commented in one of your subscribed forum threads"
}
NotificationType.THREAD_COMMENT_REPLY -> {
"${notification.user?.name} replied to your forum comment"
}
NotificationType.AIRING -> {
"Episode ${notification.episode} of ${notification.media?.title?.english ?: notification.media?.title?.romaji} has aired"
}
NotificationType.ACTIVITY_LIKE -> {
"${notification.user?.name} liked your activity"
}
NotificationType.ACTIVITY_REPLY_LIKE -> {
"${notification.user?.name} liked your reply"
}
NotificationType.THREAD_LIKE -> {
"${notification.user?.name} liked your forum thread"
}
NotificationType.THREAD_COMMENT_LIKE -> {
"${notification.user?.name} liked your forum comment"
}
NotificationType.ACTIVITY_REPLY_SUBSCRIBED -> {
"${notification.user?.name} replied to activity you have also replied to"
}
NotificationType.RELATED_MEDIA_ADDITION -> {
"${notification.media?.title?.english ?: notification.media?.title?.romaji} has been added to the site"
}
NotificationType.MEDIA_DATA_CHANGE -> {
"${notification.media?.title?.english ?: notification.media?.title?.romaji} has had a data change: ${notification.reason}"
}
NotificationType.MEDIA_MERGE -> {
"${notification.deletedMediaTitles?.joinToString(", ")} have been merged into ${notification.media?.title?.english ?: notification.media?.title?.romaji}"
}
NotificationType.MEDIA_DELETION -> {
"${notification.deletedMediaTitle} has been deleted from the site"
}
}
}
fun getDateTime(time: Int): String {
val date = Date(time * 1000L)
val sdf = SimpleDateFormat("dd/MM/yyyy hh:mm a", Locale.getDefault())
return sdf.format(date)
}
}
}