From 408737d510749c0f628c11b12c5b52d60c3191e9 Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Mon, 11 Mar 2024 04:28:13 -0500 Subject: [PATCH] feat: activity replies --- app/src/main/java/ani/dantotsu/Functions.kt | 2 +- .../dantotsu/connections/anilist/api/Feed.kt | 4 +- .../dantotsu/profile/activity/ActivityItem.kt | 30 ++++- .../profile/activity/ActivityReplyItem.kt | 40 +++++++ app/src/main/res/layout/item_activity.xml | 7 ++ .../main/res/layout/item_activity_reply.xml | 104 ++++++++++++++++++ 6 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/ani/dantotsu/profile/activity/ActivityReplyItem.kt create mode 100644 app/src/main/res/layout/item_activity_reply.xml diff --git a/app/src/main/java/ani/dantotsu/Functions.kt b/app/src/main/java/ani/dantotsu/Functions.kt index aafad39f2c..bee6fe854e 100644 --- a/app/src/main/java/ani/dantotsu/Functions.kt +++ b/app/src/main/java/ani/dantotsu/Functions.kt @@ -1110,7 +1110,7 @@ fun logToFile(context: Context, message: String) { * Builds the markwon instance with all the plugins * @return the markwon instance */ -fun buildMarkwon(activity: Activity, userInputContent: Boolean = true): Markwon { +fun buildMarkwon(activity: Context, userInputContent: Boolean = true): Markwon { val markwon = Markwon.builder(activity) .usePlugin(object : AbstractMarkwonPlugin() { override fun configureConfiguration(builder: MarkwonConfiguration.Builder) { diff --git a/app/src/main/java/ani/dantotsu/connections/anilist/api/Feed.kt b/app/src/main/java/ani/dantotsu/connections/anilist/api/Feed.kt index 8b6e28142a..5f0af95680 100644 --- a/app/src/main/java/ani/dantotsu/connections/anilist/api/Feed.kt +++ b/app/src/main/java/ani/dantotsu/connections/anilist/api/Feed.kt @@ -70,13 +70,13 @@ data class Activity( @SerialName("media") val media: Media?, @SerialName("replies") - val replies: List?, + val replies: List?, @SerialName("likes") val likes: List?, ) : java.io.Serializable @Serializable -data class Reply( +data class ActivityReply( @SerialName("id") val id: Int, @SerialName("userId") diff --git a/app/src/main/java/ani/dantotsu/profile/activity/ActivityItem.kt b/app/src/main/java/ani/dantotsu/profile/activity/ActivityItem.kt index db702030c5..af09efaf9b 100644 --- a/app/src/main/java/ani/dantotsu/profile/activity/ActivityItem.kt +++ b/app/src/main/java/ani/dantotsu/profile/activity/ActivityItem.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.content.Context import android.view.View import androidx.core.content.ContextCompat +import androidx.recyclerview.widget.LinearLayoutManager import ani.dantotsu.R import ani.dantotsu.buildMarkwon import ani.dantotsu.connections.anilist.Anilist @@ -16,6 +17,7 @@ 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 com.xwray.groupie.GroupieAdapter import com.xwray.groupie.viewbinding.BindableItem import jp.wasabeef.glide.transformations.BlurTransformation import kotlinx.coroutines.CoroutineScope @@ -29,11 +31,19 @@ class ActivityItem( val clickCallback: (Int, type: String) -> Unit ) : BindableItem() { private lateinit var binding: ItemActivityBinding + private lateinit var repliesAdapter: GroupieAdapter @SuppressLint("SetTextI18n") override fun bind(viewBinding: ItemActivityBinding, position: Int) { binding = viewBinding + repliesAdapter = GroupieAdapter() + binding.activityReplies.adapter = repliesAdapter + binding.activityReplies.layoutManager = LinearLayoutManager( + binding.root.context, + LinearLayoutManager.VERTICAL, + false + ) binding.activityUserName.text = activity.user?.name ?: activity.messenger?.name binding.activityUserAvatar.loadImage(activity.user?.avatar?.medium ?: activity.messenger?.avatar?.medium) binding.activityTime.text = ActivityItemBuilder.getDateTime(activity.createdAt) @@ -42,8 +52,24 @@ class ActivityItem( binding.activityLike.setColorFilter(if (activity.isLiked == true) likeColor else notLikeColor) binding.commentRepliesContainer.visibility = if (activity.replyCount > 0) View.VISIBLE else View.GONE - binding.activityLikeCount.text = (activity.likeCount?:0).toString() + binding.commentRepliesContainer.setOnClickListener { + when (binding.activityReplies.visibility) { + View.GONE -> { + repliesAdapter.addAll( + activity.replies?.map { ActivityReplyItem(it) } ?: emptyList() + ) + binding.activityReplies.visibility = View.VISIBLE + binding.commentTotalReplies.text = "Hide replies" + } + else -> { + repliesAdapter.clear() + binding.activityReplies.visibility = View.GONE + binding.commentTotalReplies.text = "View replies" + } + } + } + binding.activityLikeCount.text = (activity.likeCount?:0).toString() binding.activityLike.setOnClickListener { val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) scope.launch { @@ -106,7 +132,7 @@ class ActivityItem( binding.activityContent.visibility = View.VISIBLE if (!(context as android.app.Activity).isDestroyed) { val markwon = buildMarkwon(context, false) - markwon.setMarkdown(binding.activityContent, activity.message ?: "") + markwon.setMarkdown(binding.activityContent, getBasicAniHTML(activity.message ?: "")) } } } diff --git a/app/src/main/java/ani/dantotsu/profile/activity/ActivityReplyItem.kt b/app/src/main/java/ani/dantotsu/profile/activity/ActivityReplyItem.kt new file mode 100644 index 0000000000..40d3192a29 --- /dev/null +++ b/app/src/main/java/ani/dantotsu/profile/activity/ActivityReplyItem.kt @@ -0,0 +1,40 @@ +package ani.dantotsu.profile.activity + +import android.view.View +import androidx.core.content.ContextCompat +import ani.dantotsu.R +import ani.dantotsu.buildMarkwon +import ani.dantotsu.connections.anilist.api.ActivityReply +import ani.dantotsu.databinding.ItemActivityReplyBinding +import ani.dantotsu.loadImage +import ani.dantotsu.util.AniMarkdown.Companion.getBasicAniHTML +import com.xwray.groupie.viewbinding.BindableItem + +class ActivityReplyItem( + private val reply: ActivityReply +) : BindableItem() { + private lateinit var binding: ItemActivityReplyBinding + + override fun bind(viewBinding: ItemActivityReplyBinding, position: Int) { + binding = viewBinding + + binding.activityUserAvatar.loadImage(reply.user.avatar?.medium) + binding.activityUserName.text = reply.user.name + binding.activityTime.text = ActivityItemBuilder.getDateTime(reply.createdAt) + binding.activityLikeCount.text = reply.likeCount.toString() + val likeColor = ContextCompat.getColor(binding.root.context, R.color.yt_red) + val notLikeColor = ContextCompat.getColor(binding.root.context, R.color.bg_opp) + binding.activityLike.setColorFilter(if (reply.isLiked) likeColor else notLikeColor) + val markwon = buildMarkwon(binding.root.context) + markwon.setMarkdown(binding.activityContent, getBasicAniHTML(reply.text)) + + } + + override fun getLayout(): Int { + return R.layout.item_activity_reply + } + + override fun initializeViewBinding(view: View): ItemActivityReplyBinding { + return ItemActivityReplyBinding.bind(view) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/item_activity.xml b/app/src/main/res/layout/item_activity.xml index dbf70a60b2..ee5a88480e 100644 --- a/app/src/main/res/layout/item_activity.xml +++ b/app/src/main/res/layout/item_activity.xml @@ -214,4 +214,11 @@ tools:ignore="HardcodedText" /> + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_activity_reply.xml b/app/src/main/res/layout/item_activity_reply.xml new file mode 100644 index 0000000000..238d5316ca --- /dev/null +++ b/app/src/main/res/layout/item_activity_reply.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file