fix: popup spam

This commit is contained in:
rebelonion 2024-03-10 03:59:24 -05:00
parent 913d74b285
commit 867a4f36b3

View file

@ -173,64 +173,64 @@ class CommentsFragment : Fragment() {
var isFetching = false var isFetching = false
binding.commentsList.setOnTouchListener( binding.commentsList.setOnTouchListener(
object : View.OnTouchListener { object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean { override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_UP) { if (event?.action == MotionEvent.ACTION_UP) {
if (pagesLoaded < totalPages) {
if (!binding.commentsList.canScrollVertically(1) && !isFetching && if (!binding.commentsList.canScrollVertically(1) && !isFetching &&
(binding.commentsList.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() == (binding.commentsList.adapter!!.itemCount - 1) (binding.commentsList.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() == (binding.commentsList.adapter!!.itemCount - 1)
) { ) {
binding.commentBottomRefresh.visibility = View.VISIBLE if (pagesLoaded < totalPages) {
loadMoreComments() binding.commentBottomRefresh.visibility = View.VISIBLE
lifecycleScope.launch { loadMoreComments()
kotlinx.coroutines.delay(1000) lifecycleScope.launch {
withContext(Dispatchers.Main) { kotlinx.coroutines.delay(1000)
binding.commentBottomRefresh.visibility = View.GONE withContext(Dispatchers.Main) {
binding.commentBottomRefresh.visibility = View.GONE
}
} }
} else {
snackString("No more comments")
} }
} }
} else { }
snackString("No more comments") return false
}
private fun loadMoreComments() {
isFetching = true
lifecycleScope.launch {
val comments = fetchComments()
comments?.comments?.forEach { comment ->
updateUIWithComment(comment)
}
totalPages = comments?.totalPages ?: 1
pagesLoaded++
isFetching = false
} }
} }
return false
}
private fun loadMoreComments() { private suspend fun fetchComments(): CommentResponse? {
isFetching = true return withContext(Dispatchers.IO) {
lifecycleScope.launch { CommentsAPI.getCommentsForId(mediaId, pagesLoaded + 1, filterTag)
val comments = fetchComments()
comments?.comments?.forEach { comment ->
updateUIWithComment(comment)
} }
totalPages = comments?.totalPages ?: 1
pagesLoaded++
isFetching = false
} }
}
private suspend fun fetchComments(): CommentResponse? { //adds additional comments to the section
return withContext(Dispatchers.IO) { private suspend fun updateUIWithComment(comment: Comment) {
CommentsAPI.getCommentsForId(mediaId, pagesLoaded + 1, filterTag) withContext(Dispatchers.Main) {
} section.add(
} CommentItem(
comment,
//adds additional comments to the section buildMarkwon(activity),
private suspend fun updateUIWithComment(comment: Comment) { section,
withContext(Dispatchers.Main) { this@CommentsFragment,
section.add( backgroundColor,
CommentItem( 0
comment, )
buildMarkwon(activity),
section,
this@CommentsFragment,
backgroundColor,
0
) )
) }
} }
} })
})
//if we have scrolled to the bottom of the list, load more comments //if we have scrolled to the bottom of the list, load more comments
/*binding.commentsList.addOnScrollListener(object : /*binding.commentsList.addOnScrollListener(object :