feat: delete comment and subscription notification

This commit is contained in:
aayush262
2024-06-26 19:27:26 +05:30
parent c22fd6b66d
commit 46d16be835
9 changed files with 136 additions and 58 deletions

View File

@@ -16,7 +16,8 @@ import ani.dantotsu.initActivity
import ani.dantotsu.navBarHeight
import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.*
import ani.dantotsu.profile.notification.NotificationFragment.Companion.newInstance
import nl.joery.animatedbottombar.AnimatedBottomBar
class NotificationActivity : AppCompatActivity() {
@@ -48,6 +49,7 @@ class NotificationActivity : AppCompatActivity() {
binding.notificationBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
val getOne = intent.getIntExtra("activityId", -1)
if (getOne != -1) navBar.isVisible = false
binding.notificationViewPager.isUserInputEnabled = false
binding.notificationViewPager.adapter = ViewPagerAdapter(supportFragmentManager, lifecycle, getOne)
binding.notificationViewPager.setCurrentItem(selected, false)
navBar.selectTabAt(selected)
@@ -59,13 +61,7 @@ class NotificationActivity : AppCompatActivity() {
newTab: AnimatedBottomBar.Tab
) {
selected = newIndex
binding.notificationViewPager.setCurrentItem(selected, true)
}
})
binding.notificationViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
navBar.selectTabAt(position)
binding.notificationViewPager.setCurrentItem(selected, false)
}
})
}
@@ -83,11 +79,11 @@ class NotificationActivity : AppCompatActivity() {
override fun getItemCount(): Int = if (id != -1) 1 else 4
override fun createFragment(position: Int): Fragment = when (position) {
0 -> NotificationFragment.newInstance(NotificationType.USER)
1 -> NotificationFragment.newInstance(if (id != -1) NotificationType.ONE else NotificationType.MEDIA, id)
2 -> NotificationFragment.newInstance(NotificationType.SUBSCRIPTION)
3 -> NotificationFragment.newInstance(NotificationType.COMMENT)
else -> NotificationFragment.newInstance(NotificationType.MEDIA)
0 -> newInstance(USER)
1 -> newInstance(if (id != -1) ONE else MEDIA, id)
2 -> newInstance(SUBSCRIPTION)
3 -> newInstance(COMMENT)
else -> newInstance(MEDIA)
}
}
}

View File

@@ -20,7 +20,11 @@ import ani.dantotsu.notifications.comment.CommentStore
import ani.dantotsu.notifications.subscription.SubscriptionStore
import ani.dantotsu.profile.ProfileActivity
import ani.dantotsu.profile.activity.FeedActivity
import ani.dantotsu.setBaseline
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.COMMENT
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.MEDIA
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.ONE
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.SUBSCRIPTION
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.USER
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import com.xwray.groupie.GroupieAdapter
@@ -29,8 +33,8 @@ import kotlinx.coroutines.launch
class NotificationFragment : Fragment() {
private lateinit var type : NotificationType
private var getID : Int = -1
private lateinit var type: NotificationType
private var getID: Int = -1
private lateinit var binding: FragmentNotificationsBinding
private var adapter: GroupieAdapter = GroupieAdapter()
private var currentPage = 1
@@ -53,12 +57,10 @@ class NotificationFragment : Fragment() {
binding.notificationRecyclerView.adapter = adapter
binding.notificationRecyclerView.layoutManager = LinearLayoutManager(context)
binding.notificationProgressBar.isVisible = true
binding.emptyTextView.text = getString(R.string.no_notifications)
binding.emptyTextView.text = getString(R.string.nothing_here)
lifecycleScope.launch {
getList()
if (adapter.itemCount == 0) {
binding.emptyTextView.isVisible = true
}
binding.notificationProgressBar.isVisible = false
}
binding.notificationSwipeRefresh.setOnRefreshListener {
@@ -87,13 +89,16 @@ class NotificationFragment : Fragment() {
private suspend fun getList() {
val list = when (type) {
NotificationType.ONE -> getNotificationsFiltered(false) { it.id == getID }
NotificationType.MEDIA -> getNotificationsFiltered(type = true) { it.media != null }
NotificationType.USER -> getNotificationsFiltered { it.media == null }
NotificationType.SUBSCRIPTION -> getSubscriptions()
NotificationType.COMMENT -> getComments()
ONE -> getNotificationsFiltered(false) { it.id == getID }
MEDIA -> getNotificationsFiltered(type = true) { it.media != null }
USER -> getNotificationsFiltered { it.media == null }
SUBSCRIPTION -> getSubscriptions()
COMMENT -> getComments()
}
adapter.addAll(list.map { NotificationItem(it, type, adapter, ::onClick) })
if (adapter.itemCount == 0) {
binding.emptyTextView.isVisible = true
}
adapter.addAll(list.map { NotificationItem(it, ::onClick) })
}
private suspend fun getNotificationsFiltered(
@@ -114,8 +119,11 @@ class NotificationFragment : Fragment() {
PrefName.SubscriptionNotificationStore,
null
) ?: listOf()
return list.sortedByDescending { (it.time / 1000L).toInt() }
.filter { it.image != null }.map {
return list
.sortedByDescending { (it.time / 1000L).toInt() }
.filter { it.image != null } // to remove old data
.map {
Notification(
it.type,
System.currentTimeMillis().toInt(),
@@ -162,19 +170,31 @@ class NotificationFragment : Fragment() {
fun onClick(id: Int, optional: Int?, type: NotificationClickType) {
val intent = when (type) {
NotificationClickType.USER -> Intent(requireContext(), ProfileActivity::class.java).apply {
NotificationClickType.USER -> Intent(
requireContext(),
ProfileActivity::class.java
).apply {
putExtra("userId", id)
}
NotificationClickType.MEDIA -> Intent(requireContext(), MediaDetailsActivity::class.java).apply {
NotificationClickType.MEDIA -> Intent(
requireContext(),
MediaDetailsActivity::class.java
).apply {
putExtra("mediaId", id)
}
NotificationClickType.ACTIVITY -> Intent(requireContext(), FeedActivity::class.java).apply {
NotificationClickType.ACTIVITY -> Intent(
requireContext(),
FeedActivity::class.java
).apply {
putExtra("activityId", id)
}
NotificationClickType.COMMENT -> Intent(requireContext(), MediaDetailsActivity::class.java).apply {
NotificationClickType.COMMENT -> Intent(
requireContext(),
MediaDetailsActivity::class.java
).apply {
putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
putExtra("mediaId", id)
putExtra("commentId", optional ?: -1)
@@ -188,6 +208,7 @@ class NotificationFragment : Fragment() {
}
}
override fun onResume() {
super.onResume()
if (this::binding.isInitialized) {

View File

@@ -8,16 +8,27 @@ 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.profile.notification.NotificationFragment.Companion.NotificationClickType
import ani.dantotsu.notifications.comment.CommentStore
import ani.dantotsu.notifications.subscription.SubscriptionStore
import ani.dantotsu.profile.activity.ActivityItemBuilder
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationClickType
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.COMMENT
import ani.dantotsu.profile.notification.NotificationFragment.Companion.NotificationType.SUBSCRIPTION
import ani.dantotsu.setAnimation
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.toPx
import ani.dantotsu.util.customAlertDialog
import com.xwray.groupie.GroupieAdapter
import com.xwray.groupie.viewbinding.BindableItem
class NotificationItem(
private val notification: Notification,
val clickCallback: (Int, Int?, NotificationClickType) -> Unit
) : BindableItem<ItemNotificationBinding>() {
val type: NotificationFragment.Companion.NotificationType,
val parentAdapter: GroupieAdapter,
val clickCallback: (Int, Int?, NotificationClickType) -> Unit,
) : BindableItem<ItemNotificationBinding>() {
private lateinit var binding: ItemNotificationBinding
override fun bind(viewBinding: ItemNotificationBinding, position: Int) {
binding = viewBinding
@@ -25,6 +36,48 @@ class NotificationItem(
setBinding()
}
fun dialog() {
when (type) {
COMMENT, SUBSCRIPTION -> {
binding.root.context.customAlertDialog().apply {
setTitle(R.string.delete)
setMessage(ActivityItemBuilder.getContent(notification))
setPosButton(R.string.yes) {
when (type) {
COMMENT -> {
val list = PrefManager.getNullableVal<List<CommentStore>>(
PrefName.CommentNotificationStore,
null
) ?: listOf()
val newList = list.filter { it.commentId != notification.commentId }
PrefManager.setVal(PrefName.CommentNotificationStore, newList)
parentAdapter.remove(this@NotificationItem)
}
SUBSCRIPTION -> {
val list = PrefManager.getNullableVal<List<SubscriptionStore>>(
PrefName.SubscriptionNotificationStore,
null
) ?: listOf()
val newList = list.filter { (it.time / 1000L).toInt() != notification.createdAt}
PrefManager.setVal(PrefName.SubscriptionNotificationStore, newList)
parentAdapter.remove(this@NotificationItem)
}
else -> {}
}
}
setNegButton(R.string.no)
show()
}
}
else -> {}
}
}
override fun getLayout(): Int {
return R.layout.item_notification
}
@@ -33,7 +86,11 @@ class NotificationItem(
return ItemNotificationBinding.bind(view)
}
private fun image(user: Boolean = false, commentNotification: Boolean = false, newRelease: Boolean = false) {
private fun image(
user: Boolean = false,
commentNotification: Boolean = false,
newRelease: Boolean = false
) {
val cover = if (user) notification.user?.bannerImage
?: notification.user?.avatar?.medium else notification.media?.bannerImage
@@ -348,6 +405,14 @@ class NotificationItem(
}
}
}
binding.notificationCoverUser.setOnLongClickListener {
dialog()
true
}
binding.notificationBannerImage.setOnLongClickListener {
dialog()
true
}
}
}