From b69e46685324f06b5a5e561670d06319efe8becc Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Thu, 14 Mar 2024 16:25:59 -0500 Subject: [PATCH] feat: notification to activity click --- .../connections/anilist/AnilistQueries.kt | 5 +- .../ani/dantotsu/profile/ProfileActivity.kt | 2 +- .../dantotsu/profile/activity/FeedActivity.kt | 10 +- .../dantotsu/profile/activity/FeedFragment.kt | 10 +- .../profile/activity/NotificationActivity.kt | 8 +- .../profile/activity/NotificationItem.kt | 226 ++++++++++++++---- 6 files changed, 199 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/connections/anilist/AnilistQueries.kt b/app/src/main/java/ani/dantotsu/connections/anilist/AnilistQueries.kt index f92a7f1b97b..e20977a45b1 100644 --- a/app/src/main/java/ani/dantotsu/connections/anilist/AnilistQueries.kt +++ b/app/src/main/java/ani/dantotsu/connections/anilist/AnilistQueries.kt @@ -1351,8 +1351,9 @@ Page(page:$page,perPage:50) { return res } - suspend fun getFeed(userId: Int?, global: Boolean = false, page: Int = 1): FeedResponse? { - val filter = if (userId != null) "userId:$userId," + suspend fun getFeed(userId: Int?, global: Boolean = false, page: Int = 1, activityId: Int? = null): FeedResponse? { + val filter = if (activityId != null) "id:$activityId," + else if (userId != null) "userId:$userId," else if (global) "isFollowing:false,type:TEXT," else "isFollowing:true,type_not:MESSAGE," return executeQuery( diff --git a/app/src/main/java/ani/dantotsu/profile/ProfileActivity.kt b/app/src/main/java/ani/dantotsu/profile/ProfileActivity.kt index c493f6e4dc2..e3142cd1964 100644 --- a/app/src/main/java/ani/dantotsu/profile/ProfileActivity.kt +++ b/app/src/main/java/ani/dantotsu/profile/ProfileActivity.kt @@ -236,7 +236,7 @@ class ProfileActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListene override fun getItemCount(): Int = 3 override fun createFragment(position: Int): Fragment = when (position) { - 0 -> FeedFragment.newInstance(user.id, false) + 0 -> FeedFragment.newInstance(user.id, false, -1) 1 -> ProfileFragment.newInstance(user) 2 -> StatsFragment.newInstance(user) else -> ProfileFragment.newInstance(user) diff --git a/app/src/main/java/ani/dantotsu/profile/activity/FeedActivity.kt b/app/src/main/java/ani/dantotsu/profile/activity/FeedActivity.kt index 644e34dbfa0..3cea775ca41 100644 --- a/app/src/main/java/ani/dantotsu/profile/activity/FeedActivity.kt +++ b/app/src/main/java/ani/dantotsu/profile/activity/FeedActivity.kt @@ -39,7 +39,8 @@ class FeedActivity: AppCompatActivity() { topMargin += statusBarHeight } binding.listToolbar.updateLayoutParams { topMargin += statusBarHeight } - binding.feedViewPager.adapter = ViewPagerAdapter(supportFragmentManager, lifecycle) + val activityId = intent.getIntExtra("activityId", -1) + binding.feedViewPager.adapter = ViewPagerAdapter(supportFragmentManager, lifecycle, activityId) binding.feedViewPager.setCurrentItem(selected, false) binding.feedViewPager.isUserInputEnabled = false navBar.selectTabAt(selected) @@ -67,14 +68,15 @@ class FeedActivity: AppCompatActivity() { private class ViewPagerAdapter( fragmentManager: FragmentManager, - lifecycle: Lifecycle + lifecycle: Lifecycle, + private val activityId: Int ) : FragmentStateAdapter(fragmentManager, lifecycle) { override fun getItemCount(): Int = 2 override fun createFragment(position: Int): Fragment { return when (position) { - 0 -> FeedFragment.newInstance(null, false) - else -> FeedFragment.newInstance(null, true) + 0 -> FeedFragment.newInstance(null, false, activityId) + else -> FeedFragment.newInstance(null, true, -1) } } } diff --git a/app/src/main/java/ani/dantotsu/profile/activity/FeedFragment.kt b/app/src/main/java/ani/dantotsu/profile/activity/FeedFragment.kt index f7fa8eecc28..f6ac6a8ada1 100644 --- a/app/src/main/java/ani/dantotsu/profile/activity/FeedFragment.kt +++ b/app/src/main/java/ani/dantotsu/profile/activity/FeedFragment.kt @@ -31,6 +31,7 @@ class FeedFragment : Fragment() { private var loadedFirstTime = false private var userId: Int? = null private var global: Boolean = false + private var activityId: Int = -1 override fun onCreateView( inflater: LayoutInflater, @@ -49,6 +50,7 @@ class FeedFragment : Fragment() { LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) binding.listProgressBar.visibility = ViewGroup.VISIBLE userId = arguments?.getInt("userId", -1) + activityId = arguments?.getInt("activityId", -1) ?: -1 if (userId == -1) userId = null global = arguments?.getBoolean("global", false) ?: false } @@ -60,11 +62,12 @@ class FeedFragment : Fragment() { binding.root.requestLayout() if (!loadedFirstTime) { activity.lifecycleScope.launch(Dispatchers.IO) { - val res = Anilist.query.getFeed(userId, global) + val nulledId = if (activityId == -1) null else activityId + val res = Anilist.query.getFeed(userId, global, activityId = nulledId) withContext(Dispatchers.Main) { res?.data?.page?.activities?.let { activities -> activityList = activities - val filtered = activities.filterNot { //filter out messages that are not directed to the user + val filtered = activityList.filterNot { //filter out messages that are not directed to the user it.recipient?.id != null && it.recipient.id != Anilist.userid } adapter.update(filtered.map { ActivityItem(it) { _, _ -> } }) @@ -107,11 +110,12 @@ class FeedFragment : Fragment() { } companion object { - fun newInstance(userId: Int?, global: Boolean): FeedFragment { + fun newInstance(userId: Int?, global: Boolean, activityId: Int): FeedFragment { val fragment = FeedFragment() val args = Bundle() args.putInt("userId", userId ?: -1) args.putBoolean("global", global) + args.putInt("activityId", activityId) fragment.arguments = args return fragment } diff --git a/app/src/main/java/ani/dantotsu/profile/activity/NotificationActivity.kt b/app/src/main/java/ani/dantotsu/profile/activity/NotificationActivity.kt index ff228018658..6ccd9a770a2 100644 --- a/app/src/main/java/ani/dantotsu/profile/activity/NotificationActivity.kt +++ b/app/src/main/java/ani/dantotsu/profile/activity/NotificationActivity.kt @@ -107,6 +107,12 @@ class NotificationActivity : AppCompatActivity() { .putExtra("mediaId", id), null ) } + NotificationClickType.ACTIVITY -> { + ContextCompat.startActivity( + this, Intent(this, FeedActivity::class.java) + .putExtra("activityId", id), null + ) + } NotificationClickType.UNDEFINED -> { // Do nothing } @@ -115,7 +121,7 @@ class NotificationActivity : AppCompatActivity() { companion object { enum class NotificationClickType { - USER, MEDIA, UNDEFINED + USER, MEDIA, ACTIVITY, UNDEFINED } } } \ No newline at end of file diff --git a/app/src/main/java/ani/dantotsu/profile/activity/NotificationItem.kt b/app/src/main/java/ani/dantotsu/profile/activity/NotificationItem.kt index 23fa8676e90..6c5fa9e56a1 100644 --- a/app/src/main/java/ani/dantotsu/profile/activity/NotificationItem.kt +++ b/app/src/main/java/ani/dantotsu/profile/activity/NotificationItem.kt @@ -1,7 +1,5 @@ package ani.dantotsu.profile.activity -import android.app.Activity -import android.content.Context import android.util.TypedValue import android.view.View import ani.dantotsu.R @@ -10,20 +8,14 @@ import ani.dantotsu.connections.anilist.api.Notification import ani.dantotsu.connections.anilist.api.NotificationType import ani.dantotsu.databinding.ItemNotificationBinding import ani.dantotsu.loadImage -import com.bumptech.glide.Glide -import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.load.model.GlideUrl -import com.bumptech.glide.request.RequestOptions +import ani.dantotsu.profile.activity.NotificationActivity.Companion.NotificationClickType import com.xwray.groupie.viewbinding.BindableItem -import jp.wasabeef.glide.transformations.BlurTransformation class NotificationItem( private val notification: Notification, - val clickCallback: (Int, NotificationActivity.Companion.NotificationClickType) -> Unit -): BindableItem() { + val clickCallback: (Int, NotificationClickType) -> Unit +) : BindableItem() { 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() @@ -39,10 +31,20 @@ class NotificationItem( private fun image(user: Boolean = false) { - val cover = if (user) notification.user?.bannerImage ?: notification.user?.avatar?.medium else notification.media?.bannerImage ?: notification.media?.coverImage?.large + val cover = if (user) notification.user?.bannerImage + ?: notification.user?.avatar?.medium else notification.media?.bannerImage + ?: notification.media?.coverImage?.large blurImage(binding.notificationBannerImage, cover) - val defaultHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 170f, binding.root.context.resources.displayMetrics).toInt() - val userHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90f, binding.root.context.resources.displayMetrics).toInt() + val defaultHeight = TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + 170f, + binding.root.context.resources.displayMetrics + ).toInt() + val userHeight = TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + 90f, + binding.root.context.resources.displayMetrics + ).toInt() if (user) { binding.notificationCover.visibility = View.GONE @@ -50,7 +52,7 @@ class NotificationItem( binding.notificationCoverUserContainer.visibility = View.VISIBLE binding.notificationCoverUser.loadImage(notification.user?.avatar?.large) binding.notificationBannerImage.layoutParams.height = userHeight - } else{ + } else { binding.notificationCover.visibility = View.VISIBLE binding.notificationCoverUser.visibility = View.VISIBLE binding.notificationCoverUserContainer.visibility = View.GONE @@ -64,108 +66,230 @@ class NotificationItem( NotificationType.valueOf(notification.notificationType) binding.notificationText.text = ActivityItemBuilder.getContent(notification) binding.notificationDate.text = ActivityItemBuilder.getDateTime(notification.createdAt) - binding.root.setOnClickListener { clickCallback(id, clickType) } - + when (notificationType) { NotificationType.ACTIVITY_MESSAGE -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.activityId ?: 0, NotificationClickType.ACTIVITY + ) + } } + NotificationType.ACTIVITY_REPLY -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.activityId ?: 0, NotificationClickType.ACTIVITY + ) + } } + NotificationType.FOLLOWING -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.userId ?: 0, NotificationClickType.USER + ) + } } + NotificationType.ACTIVITY_MENTION -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.activityId ?: 0, NotificationClickType.ACTIVITY + ) + } } + NotificationType.THREAD_COMMENT_MENTION -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } } + NotificationType.THREAD_SUBSCRIBED -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } } + NotificationType.THREAD_COMMENT_REPLY -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } } + NotificationType.AIRING -> { binding.notificationCover.loadImage(notification.media?.coverImage?.large) image() - clickType = NotificationActivity.Companion.NotificationClickType.MEDIA - id = notification.media?.id ?: 0 + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.media?.id ?: 0, NotificationClickType.MEDIA + ) + } } + NotificationType.ACTIVITY_LIKE -> { image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCover.loadImage(notification.user?.avatar?.large) + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.activityId ?: 0, NotificationClickType.ACTIVITY + ) + } } + NotificationType.ACTIVITY_REPLY_LIKE -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.activityId ?: 0, NotificationClickType.ACTIVITY + ) + } } + NotificationType.THREAD_LIKE -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } } + NotificationType.THREAD_COMMENT_LIKE -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } } + NotificationType.ACTIVITY_REPLY_SUBSCRIBED -> { binding.notificationCover.loadImage(notification.user?.avatar?.large) image(true) - clickType = NotificationActivity.Companion.NotificationClickType.USER - id = notification.user?.id ?: 0 + binding.notificationCoverUser.setOnClickListener { + clickCallback( + notification.user?.id ?: 0, NotificationClickType.USER + ) + } + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.activityId ?: 0, NotificationClickType.ACTIVITY + ) + } } + NotificationType.RELATED_MEDIA_ADDITION -> { binding.notificationCover.loadImage(notification.media?.coverImage?.large) image() - clickType = NotificationActivity.Companion.NotificationClickType.MEDIA - id = notification.media?.id ?: 0 + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.media?.id ?: 0, NotificationClickType.MEDIA + ) + } } + NotificationType.MEDIA_DATA_CHANGE -> { binding.notificationCover.loadImage(notification.media?.coverImage?.large) image() - clickType = NotificationActivity.Companion.NotificationClickType.MEDIA - id = notification.media?.id ?: 0 + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.media?.id ?: 0, NotificationClickType.MEDIA + ) + } } + NotificationType.MEDIA_MERGE -> { binding.notificationCover.loadImage(notification.media?.coverImage?.large) image() - clickType = NotificationActivity.Companion.NotificationClickType.MEDIA - id = notification.media?.id ?: 0 + binding.notificationBannerImage.setOnClickListener { + clickCallback( + notification.media?.id ?: 0, NotificationClickType.MEDIA + ) + } } + NotificationType.MEDIA_DELETION -> { binding.notificationCover.visibility = View.GONE - clickType = NotificationActivity.Companion.NotificationClickType.UNDEFINED - id = 0 } } }