fix: more strategic refresh in comments

This commit is contained in:
rebelonion 2024-03-16 22:44:45 -05:00
parent 5d1b220105
commit fda809bc8a
3 changed files with 52 additions and 39 deletions

View file

@ -43,6 +43,7 @@ import androidx.core.content.FileProvider
import androidx.core.math.MathUtils.clamp import androidx.core.math.MathUtils.clamp
import androidx.core.view.* import androidx.core.view.*
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -1145,7 +1146,8 @@ fun blurImage(imageView: ImageView, banner: String?){
* Builds the markwon instance with all the plugins * Builds the markwon instance with all the plugins
* @return the markwon instance * @return the markwon instance
*/ */
fun buildMarkwon(activity: Context, userInputContent: Boolean = true): Markwon { fun buildMarkwon(activity: Context, userInputContent: Boolean = true, fragment: Fragment? = null): Markwon {
val glideContext = fragment?.let { Glide.with(it) } ?: Glide.with(activity)
val markwon = Markwon.builder(activity) val markwon = Markwon.builder(activity)
.usePlugin(object : AbstractMarkwonPlugin() { .usePlugin(object : AbstractMarkwonPlugin() {
override fun configureConfiguration(builder: MarkwonConfiguration.Builder) { override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
@ -1168,38 +1170,41 @@ fun buildMarkwon(activity: Context, userInputContent: Boolean = true): Markwon {
}) })
.usePlugin(GlideImagesPlugin.create(object : GlideImagesPlugin.GlideStore { .usePlugin(GlideImagesPlugin.create(object : GlideImagesPlugin.GlideStore {
private val requestManager: RequestManager = private val requestManager: RequestManager = glideContext.apply {
Glide.with(activity).apply { addDefaultRequestListener(object : RequestListener<Any> {
addDefaultRequestListener(object : RequestListener<Any> { override fun onResourceReady(
override fun onResourceReady( resource: Any,
resource: Any, model: Any,
model: Any, target: Target<Any>,
target: Target<Any>, dataSource: DataSource,
dataSource: DataSource, isFirstResource: Boolean
isFirstResource: Boolean ): Boolean {
): Boolean { if (resource is GifDrawable) {
if (resource is GifDrawable) { resource.start()
resource.start()
}
return false
} }
Logger.log("Image loaded successfully: $model")
return false
}
override fun onLoadFailed( override fun onLoadFailed(
e: GlideException?, e: GlideException?,
model: Any?, model: Any?,
target: Target<Any>, target: Target<Any>,
isFirstResource: Boolean isFirstResource: Boolean
): Boolean { ): Boolean {
return false Logger.log("Image failed to load: $model")
} Logger.log(e as Exception)
}) return false
} }
})
}
override fun load(drawable: AsyncDrawable): RequestBuilder<Drawable> { override fun load(drawable: AsyncDrawable): RequestBuilder<Drawable> {
Logger.log("Loading image: ${drawable.destination}")
return requestManager.load(drawable.destination) return requestManager.load(drawable.destination)
} }
override fun cancel(target: Target<*>) { override fun cancel(target: Target<*>) {
Logger.log("Cancelling image load")
requestManager.clear(target) requestManager.clear(target)
} }
})) }))

View file

@ -30,7 +30,6 @@ import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.TimeZone import java.util.TimeZone
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.pow
import kotlin.math.sqrt import kotlin.math.sqrt
class CommentItem(val comment: Comment, class CommentItem(val comment: Comment,
@ -40,7 +39,7 @@ class CommentItem(val comment: Comment,
private val backgroundColor: Int, private val backgroundColor: Int,
val commentDepth: Int val commentDepth: Int
) : BindableItem<ItemCommentsBinding>() { ) : BindableItem<ItemCommentsBinding>() {
var binding: ItemCommentsBinding? = null lateinit var binding: ItemCommentsBinding
val adapter = GroupieAdapter() val adapter = GroupieAdapter()
private var subCommentIds: MutableList<Int> = mutableListOf() private var subCommentIds: MutableList<Int> = mutableListOf()
val repliesSection = Section() val repliesSection = Section()
@ -56,7 +55,7 @@ class CommentItem(val comment: Comment,
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
override fun bind(viewBinding: ItemCommentsBinding, position: Int) { override fun bind(viewBinding: ItemCommentsBinding, position: Int) {
binding = viewBinding binding = viewBinding
setAnimation(binding!!.root.context, binding!!.root) setAnimation(binding.root.context, binding.root)
viewBinding.commentRepliesList.layoutManager = LinearLayoutManager(commentsFragment.activity) viewBinding.commentRepliesList.layoutManager = LinearLayoutManager(commentsFragment.activity)
viewBinding.commentRepliesList.adapter = adapter viewBinding.commentRepliesList.adapter = adapter
val isUserComment = CommentsAPI.userId == comment.userId val isUserComment = CommentsAPI.userId == comment.userId
@ -222,17 +221,21 @@ class CommentItem(val comment: Comment,
return R.layout.item_comments return R.layout.item_comments
} }
fun containsGif(): Boolean {
return comment.content.contains(".gif")
}
override fun initializeViewBinding(view: View): ItemCommentsBinding { override fun initializeViewBinding(view: View): ItemCommentsBinding {
return ItemCommentsBinding.bind(view) return ItemCommentsBinding.bind(view)
} }
fun replying(isReplying: Boolean) { fun replying(isReplying: Boolean) {
binding?.commentReply?.text = if (isReplying) commentsFragment.activity.getString(R.string.cancel) else "Reply" binding.commentReply.text = if (isReplying) commentsFragment.activity.getString(R.string.cancel) else "Reply"
this.isReplying = isReplying this.isReplying = isReplying
} }
fun editing(isEditing: Boolean) { fun editing(isEditing: Boolean) {
binding?.commentEdit?.text = if (isEditing) commentsFragment.activity.getString(R.string.cancel) else commentsFragment.activity.getString(R.string.edit) binding.commentEdit.text = if (isEditing) commentsFragment.activity.getString(R.string.cancel) else commentsFragment.activity.getString(R.string.edit)
this.isEditing = isEditing this.isEditing = isEditing
} }

View file

@ -33,6 +33,7 @@ import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.toast import ani.dantotsu.toast
import com.bumptech.glide.Glide
import com.xwray.groupie.GroupieAdapter import com.xwray.groupie.GroupieAdapter
import com.xwray.groupie.Section import com.xwray.groupie.Section
import io.noties.markwon.editor.MarkwonEditor import io.noties.markwon.editor.MarkwonEditor
@ -82,7 +83,7 @@ class CommentsFragment : Fragment() {
this.mediaId = mediaId this.mediaId = mediaId
backgroundColor = (binding.root.background as? ColorDrawable)?.color ?: 0 backgroundColor = (binding.root.background as? ColorDrawable)?.color ?: 0
val markwon = buildMarkwon(activity) val markwon = buildMarkwon(activity, fragment = this@CommentsFragment)
activity.binding.commentUserAvatar.loadImage(Anilist.avatar) activity.binding.commentUserAvatar.loadImage(Anilist.avatar)
val markwonEditor = MarkwonEditor.create(markwon) val markwonEditor = MarkwonEditor.create(markwon)
@ -228,7 +229,7 @@ class CommentsFragment : Fragment() {
section.add( section.add(
CommentItem( CommentItem(
comment, comment,
buildMarkwon(activity), buildMarkwon(activity, fragment = this@CommentsFragment),
section, section,
this@CommentsFragment, this@CommentsFragment,
backgroundColor, backgroundColor,
@ -353,7 +354,11 @@ class CommentsFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
tag = null tag = null
adapter.notifyDataSetChanged() section.groups.forEach {
if (it is CommentItem && it.containsGif()) {
it.notifyChanged()
}
}
} }
enum class InteractionState { enum class InteractionState {
@ -381,7 +386,7 @@ class CommentsFragment : Fragment() {
section.add( section.add(
CommentItem( CommentItem(
it, it,
buildMarkwon(activity), buildMarkwon(activity, fragment = this@CommentsFragment),
section, section,
this@CommentsFragment, this@CommentsFragment,
backgroundColor, backgroundColor,
@ -411,7 +416,7 @@ class CommentsFragment : Fragment() {
section.add( section.add(
CommentItem( CommentItem(
comment, comment,
buildMarkwon(activity), buildMarkwon(activity, fragment = this@CommentsFragment),
section, section,
this@CommentsFragment, this@CommentsFragment,
backgroundColor, backgroundColor,
@ -534,7 +539,7 @@ class CommentsFragment : Fragment() {
if (depth >= comment.MAX_DEPTH) comment.registerSubComment(it.commentId) if (depth >= comment.MAX_DEPTH) comment.registerSubComment(it.commentId)
val newCommentItem = CommentItem( val newCommentItem = CommentItem(
it, it,
buildMarkwon(activity), buildMarkwon(activity, fragment = this@CommentsFragment),
section, section,
this@CommentsFragment, this@CommentsFragment,
backgroundColor, backgroundColor,
@ -659,7 +664,7 @@ class CommentsFragment : Fragment() {
if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) 0 else section.itemCount, if (commentWithInteraction!!.commentDepth + 1 > commentWithInteraction!!.MAX_DEPTH) 0 else section.itemCount,
CommentItem( CommentItem(
it, it,
buildMarkwon(activity), buildMarkwon(activity, fragment = this@CommentsFragment),
section, section,
this@CommentsFragment, this@CommentsFragment,
backgroundColor, backgroundColor,
@ -671,7 +676,7 @@ class CommentsFragment : Fragment() {
0, 0,
CommentItem( CommentItem(
it, it,
buildMarkwon(activity), buildMarkwon(activity, fragment = this@CommentsFragment),
section, section,
this@CommentsFragment, this@CommentsFragment,
backgroundColor, backgroundColor,