feat: notification to activity click

This commit is contained in:
rebelonion 2024-03-14 16:25:59 -05:00
parent 9e371778b7
commit b69e466853
6 changed files with 199 additions and 62 deletions

View file

@ -1351,8 +1351,9 @@ Page(page:$page,perPage:50) {
return res return res
} }
suspend fun getFeed(userId: Int?, global: Boolean = false, page: Int = 1): FeedResponse? { suspend fun getFeed(userId: Int?, global: Boolean = false, page: Int = 1, activityId: Int? = null): FeedResponse? {
val filter = if (userId != null) "userId:$userId," val filter = if (activityId != null) "id:$activityId,"
else if (userId != null) "userId:$userId,"
else if (global) "isFollowing:false,type:TEXT," else if (global) "isFollowing:false,type:TEXT,"
else "isFollowing:true,type_not:MESSAGE," else "isFollowing:true,type_not:MESSAGE,"
return executeQuery<FeedResponse>( return executeQuery<FeedResponse>(

View file

@ -236,7 +236,7 @@ class ProfileActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListene
override fun getItemCount(): Int = 3 override fun getItemCount(): Int = 3
override fun createFragment(position: Int): Fragment = when (position) { 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) 1 -> ProfileFragment.newInstance(user)
2 -> StatsFragment.newInstance(user) 2 -> StatsFragment.newInstance(user)
else -> ProfileFragment.newInstance(user) else -> ProfileFragment.newInstance(user)

View file

@ -39,7 +39,8 @@ class FeedActivity: AppCompatActivity() {
topMargin += statusBarHeight topMargin += statusBarHeight
} }
binding.listToolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> { topMargin += statusBarHeight } binding.listToolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> { 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.setCurrentItem(selected, false)
binding.feedViewPager.isUserInputEnabled = false binding.feedViewPager.isUserInputEnabled = false
navBar.selectTabAt(selected) navBar.selectTabAt(selected)
@ -67,14 +68,15 @@ class FeedActivity: AppCompatActivity() {
private class ViewPagerAdapter( private class ViewPagerAdapter(
fragmentManager: FragmentManager, fragmentManager: FragmentManager,
lifecycle: Lifecycle lifecycle: Lifecycle,
private val activityId: Int
) : FragmentStateAdapter(fragmentManager, lifecycle) { ) : FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int = 2 override fun getItemCount(): Int = 2
override fun createFragment(position: Int): Fragment { override fun createFragment(position: Int): Fragment {
return when (position) { return when (position) {
0 -> FeedFragment.newInstance(null, false) 0 -> FeedFragment.newInstance(null, false, activityId)
else -> FeedFragment.newInstance(null, true) else -> FeedFragment.newInstance(null, true, -1)
} }
} }
} }

View file

@ -31,6 +31,7 @@ class FeedFragment : Fragment() {
private var loadedFirstTime = false private var loadedFirstTime = false
private var userId: Int? = null private var userId: Int? = null
private var global: Boolean = false private var global: Boolean = false
private var activityId: Int = -1
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -49,6 +50,7 @@ class FeedFragment : Fragment() {
LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
binding.listProgressBar.visibility = ViewGroup.VISIBLE binding.listProgressBar.visibility = ViewGroup.VISIBLE
userId = arguments?.getInt("userId", -1) userId = arguments?.getInt("userId", -1)
activityId = arguments?.getInt("activityId", -1) ?: -1
if (userId == -1) userId = null if (userId == -1) userId = null
global = arguments?.getBoolean("global", false) ?: false global = arguments?.getBoolean("global", false) ?: false
} }
@ -60,11 +62,12 @@ class FeedFragment : Fragment() {
binding.root.requestLayout() binding.root.requestLayout()
if (!loadedFirstTime) { if (!loadedFirstTime) {
activity.lifecycleScope.launch(Dispatchers.IO) { 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) { withContext(Dispatchers.Main) {
res?.data?.page?.activities?.let { activities -> res?.data?.page?.activities?.let { activities ->
activityList = 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 it.recipient?.id != null && it.recipient.id != Anilist.userid
} }
adapter.update(filtered.map { ActivityItem(it) { _, _ -> } }) adapter.update(filtered.map { ActivityItem(it) { _, _ -> } })
@ -107,11 +110,12 @@ class FeedFragment : Fragment() {
} }
companion object { companion object {
fun newInstance(userId: Int?, global: Boolean): FeedFragment { fun newInstance(userId: Int?, global: Boolean, activityId: Int): FeedFragment {
val fragment = FeedFragment() val fragment = FeedFragment()
val args = Bundle() val args = Bundle()
args.putInt("userId", userId ?: -1) args.putInt("userId", userId ?: -1)
args.putBoolean("global", global) args.putBoolean("global", global)
args.putInt("activityId", activityId)
fragment.arguments = args fragment.arguments = args
return fragment return fragment
} }

View file

@ -107,6 +107,12 @@ class NotificationActivity : AppCompatActivity() {
.putExtra("mediaId", id), null .putExtra("mediaId", id), null
) )
} }
NotificationClickType.ACTIVITY -> {
ContextCompat.startActivity(
this, Intent(this, FeedActivity::class.java)
.putExtra("activityId", id), null
)
}
NotificationClickType.UNDEFINED -> { NotificationClickType.UNDEFINED -> {
// Do nothing // Do nothing
} }
@ -115,7 +121,7 @@ class NotificationActivity : AppCompatActivity() {
companion object { companion object {
enum class NotificationClickType { enum class NotificationClickType {
USER, MEDIA, UNDEFINED USER, MEDIA, ACTIVITY, UNDEFINED
} }
} }
} }

View file

@ -1,7 +1,5 @@
package ani.dantotsu.profile.activity package ani.dantotsu.profile.activity
import android.app.Activity
import android.content.Context
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
import ani.dantotsu.R 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.connections.anilist.api.NotificationType
import ani.dantotsu.databinding.ItemNotificationBinding import ani.dantotsu.databinding.ItemNotificationBinding
import ani.dantotsu.loadImage import ani.dantotsu.loadImage
import com.bumptech.glide.Glide import ani.dantotsu.profile.activity.NotificationActivity.Companion.NotificationClickType
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.request.RequestOptions
import com.xwray.groupie.viewbinding.BindableItem import com.xwray.groupie.viewbinding.BindableItem
import jp.wasabeef.glide.transformations.BlurTransformation
class NotificationItem( class NotificationItem(
private val notification: Notification, private val notification: Notification,
val clickCallback: (Int, NotificationActivity.Companion.NotificationClickType) -> Unit val clickCallback: (Int, NotificationClickType) -> Unit
): BindableItem<ItemNotificationBinding>() { ) : BindableItem<ItemNotificationBinding>() {
private lateinit var binding: ItemNotificationBinding private lateinit var binding: ItemNotificationBinding
private lateinit var clickType: NotificationActivity.Companion.NotificationClickType
private var id = 0
override fun bind(viewBinding: ItemNotificationBinding, position: Int) { override fun bind(viewBinding: ItemNotificationBinding, position: Int) {
binding = viewBinding binding = viewBinding
setBinding() setBinding()
@ -39,10 +31,20 @@ class NotificationItem(
private fun image(user: Boolean = false) { 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) blurImage(binding.notificationBannerImage, cover)
val defaultHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 170f, binding.root.context.resources.displayMetrics).toInt() val defaultHeight = TypedValue.applyDimension(
val userHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90f, binding.root.context.resources.displayMetrics).toInt() 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) { if (user) {
binding.notificationCover.visibility = View.GONE binding.notificationCover.visibility = View.GONE
@ -50,7 +52,7 @@ class NotificationItem(
binding.notificationCoverUserContainer.visibility = View.VISIBLE binding.notificationCoverUserContainer.visibility = View.VISIBLE
binding.notificationCoverUser.loadImage(notification.user?.avatar?.large) binding.notificationCoverUser.loadImage(notification.user?.avatar?.large)
binding.notificationBannerImage.layoutParams.height = userHeight binding.notificationBannerImage.layoutParams.height = userHeight
} else{ } else {
binding.notificationCover.visibility = View.VISIBLE binding.notificationCover.visibility = View.VISIBLE
binding.notificationCoverUser.visibility = View.VISIBLE binding.notificationCoverUser.visibility = View.VISIBLE
binding.notificationCoverUserContainer.visibility = View.GONE binding.notificationCoverUserContainer.visibility = View.GONE
@ -64,108 +66,230 @@ class NotificationItem(
NotificationType.valueOf(notification.notificationType) NotificationType.valueOf(notification.notificationType)
binding.notificationText.text = ActivityItemBuilder.getContent(notification) binding.notificationText.text = ActivityItemBuilder.getContent(notification)
binding.notificationDate.text = ActivityItemBuilder.getDateTime(notification.createdAt) binding.notificationDate.text = ActivityItemBuilder.getDateTime(notification.createdAt)
binding.root.setOnClickListener { clickCallback(id, clickType) }
when (notificationType) { when (notificationType) {
NotificationType.ACTIVITY_MESSAGE -> { NotificationType.ACTIVITY_MESSAGE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.activityId ?: 0, NotificationClickType.ACTIVITY
)
}
} }
NotificationType.ACTIVITY_REPLY -> { NotificationType.ACTIVITY_REPLY -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.activityId ?: 0, NotificationClickType.ACTIVITY
)
}
} }
NotificationType.FOLLOWING -> { NotificationType.FOLLOWING -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.userId ?: 0, NotificationClickType.USER
)
}
} }
NotificationType.ACTIVITY_MENTION -> { NotificationType.ACTIVITY_MENTION -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.activityId ?: 0, NotificationClickType.ACTIVITY
)
}
} }
NotificationType.THREAD_COMMENT_MENTION -> { NotificationType.THREAD_COMMENT_MENTION -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
} }
NotificationType.THREAD_SUBSCRIBED -> { NotificationType.THREAD_SUBSCRIBED -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
} }
NotificationType.THREAD_COMMENT_REPLY -> { NotificationType.THREAD_COMMENT_REPLY -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
} }
NotificationType.AIRING -> { NotificationType.AIRING -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large) binding.notificationCover.loadImage(notification.media?.coverImage?.large)
image() image()
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA binding.notificationBannerImage.setOnClickListener {
id = notification.media?.id ?: 0 clickCallback(
notification.media?.id ?: 0, NotificationClickType.MEDIA
)
}
} }
NotificationType.ACTIVITY_LIKE -> { NotificationType.ACTIVITY_LIKE -> {
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCover.loadImage(notification.user?.avatar?.large)
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_LIKE -> { NotificationType.ACTIVITY_REPLY_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.activityId ?: 0, NotificationClickType.ACTIVITY
)
}
} }
NotificationType.THREAD_LIKE -> { NotificationType.THREAD_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
} }
NotificationType.THREAD_COMMENT_LIKE -> { NotificationType.THREAD_COMMENT_LIKE -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
} }
NotificationType.ACTIVITY_REPLY_SUBSCRIBED -> { NotificationType.ACTIVITY_REPLY_SUBSCRIBED -> {
binding.notificationCover.loadImage(notification.user?.avatar?.large) binding.notificationCover.loadImage(notification.user?.avatar?.large)
image(true) image(true)
clickType = NotificationActivity.Companion.NotificationClickType.USER binding.notificationCoverUser.setOnClickListener {
id = notification.user?.id ?: 0 clickCallback(
notification.user?.id ?: 0, NotificationClickType.USER
)
}
binding.notificationBannerImage.setOnClickListener {
clickCallback(
notification.activityId ?: 0, NotificationClickType.ACTIVITY
)
}
} }
NotificationType.RELATED_MEDIA_ADDITION -> { NotificationType.RELATED_MEDIA_ADDITION -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large) binding.notificationCover.loadImage(notification.media?.coverImage?.large)
image() image()
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA binding.notificationBannerImage.setOnClickListener {
id = notification.media?.id ?: 0 clickCallback(
notification.media?.id ?: 0, NotificationClickType.MEDIA
)
}
} }
NotificationType.MEDIA_DATA_CHANGE -> { NotificationType.MEDIA_DATA_CHANGE -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large) binding.notificationCover.loadImage(notification.media?.coverImage?.large)
image() image()
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA binding.notificationBannerImage.setOnClickListener {
id = notification.media?.id ?: 0 clickCallback(
notification.media?.id ?: 0, NotificationClickType.MEDIA
)
}
} }
NotificationType.MEDIA_MERGE -> { NotificationType.MEDIA_MERGE -> {
binding.notificationCover.loadImage(notification.media?.coverImage?.large) binding.notificationCover.loadImage(notification.media?.coverImage?.large)
image() image()
clickType = NotificationActivity.Companion.NotificationClickType.MEDIA binding.notificationBannerImage.setOnClickListener {
id = notification.media?.id ?: 0 clickCallback(
notification.media?.id ?: 0, NotificationClickType.MEDIA
)
}
} }
NotificationType.MEDIA_DELETION -> { NotificationType.MEDIA_DELETION -> {
binding.notificationCover.visibility = View.GONE binding.notificationCover.visibility = View.GONE
clickType = NotificationActivity.Companion.NotificationClickType.UNDEFINED
id = 0
} }
} }
} }