fix: comment pagination
This commit is contained in:
parent
14115ada4c
commit
d04ced94ea
2 changed files with 122 additions and 84 deletions
|
@ -5,13 +5,12 @@ import android.annotation.SuppressLint
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
import android.content.Context.INPUT_METHOD_SERVICE
|
import android.content.Context.INPUT_METHOD_SERVICE
|
||||||
import android.graphics.drawable.ColorDrawable
|
import android.graphics.drawable.ColorDrawable
|
||||||
import android.graphics.drawable.Drawable
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.text.TextWatcher
|
import android.text.TextWatcher
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
import android.view.MotionEvent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.WindowManager
|
|
||||||
import android.view.inputmethod.InputMethodManager
|
import android.view.inputmethod.InputMethodManager
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
@ -27,36 +26,15 @@ import ani.dantotsu.connections.anilist.Anilist
|
||||||
import ani.dantotsu.connections.comments.Comment
|
import ani.dantotsu.connections.comments.Comment
|
||||||
import ani.dantotsu.connections.comments.CommentResponse
|
import ani.dantotsu.connections.comments.CommentResponse
|
||||||
import ani.dantotsu.connections.comments.CommentsAPI
|
import ani.dantotsu.connections.comments.CommentsAPI
|
||||||
import ani.dantotsu.copyToClipboard
|
|
||||||
import ani.dantotsu.databinding.FragmentCommentsBinding
|
import ani.dantotsu.databinding.FragmentCommentsBinding
|
||||||
import ani.dantotsu.loadImage
|
import ani.dantotsu.loadImage
|
||||||
import ani.dantotsu.media.MediaDetailsActivity
|
|
||||||
import ani.dantotsu.settings.saving.PrefManager
|
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 com.bumptech.glide.Glide
|
|
||||||
import com.bumptech.glide.RequestBuilder
|
|
||||||
import com.bumptech.glide.RequestManager
|
|
||||||
import com.bumptech.glide.load.DataSource
|
|
||||||
import com.bumptech.glide.load.engine.GlideException
|
|
||||||
import com.bumptech.glide.load.resource.gif.GifDrawable
|
|
||||||
import com.bumptech.glide.request.RequestListener
|
|
||||||
import com.bumptech.glide.request.target.Target
|
|
||||||
import com.xwray.groupie.GroupieAdapter
|
import com.xwray.groupie.GroupieAdapter
|
||||||
import com.xwray.groupie.Section
|
import com.xwray.groupie.Section
|
||||||
import io.noties.markwon.AbstractMarkwonPlugin
|
|
||||||
import io.noties.markwon.Markwon
|
|
||||||
import io.noties.markwon.MarkwonConfiguration
|
|
||||||
import io.noties.markwon.SoftBreakAddsNewLinePlugin
|
|
||||||
import io.noties.markwon.editor.MarkwonEditor
|
import io.noties.markwon.editor.MarkwonEditor
|
||||||
import io.noties.markwon.editor.MarkwonEditorTextWatcher
|
import io.noties.markwon.editor.MarkwonEditorTextWatcher
|
||||||
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
|
|
||||||
import io.noties.markwon.ext.tables.TablePlugin
|
|
||||||
import io.noties.markwon.ext.tasklist.TaskListPlugin
|
|
||||||
import io.noties.markwon.html.HtmlPlugin
|
|
||||||
import io.noties.markwon.html.TagHandlerNoOp
|
|
||||||
import io.noties.markwon.image.AsyncDrawable
|
|
||||||
import io.noties.markwon.image.glide.GlideImagesPlugin
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
@ -64,6 +42,7 @@ import java.text.SimpleDateFormat
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.TimeZone
|
import java.util.TimeZone
|
||||||
|
|
||||||
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
class CommentsFragment : Fragment() {
|
class CommentsFragment : Fragment() {
|
||||||
lateinit var binding: FragmentCommentsBinding
|
lateinit var binding: FragmentCommentsBinding
|
||||||
lateinit var activity: AppCompatActivity
|
lateinit var activity: AppCompatActivity
|
||||||
|
@ -193,8 +172,68 @@ class CommentsFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var isFetching = false
|
var isFetching = false
|
||||||
|
binding.commentsList.setOnTouchListener(
|
||||||
|
object : View.OnTouchListener {
|
||||||
|
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
|
||||||
|
if (event?.action == MotionEvent.ACTION_UP) {
|
||||||
|
if (pagesLoaded < totalPages) {
|
||||||
|
if (!binding.commentsList.canScrollVertically(1) && !isFetching &&
|
||||||
|
(binding.commentsList.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() == (binding.commentsList.adapter!!.itemCount - 1)
|
||||||
|
) {
|
||||||
|
binding.commentBottomRefresh.visibility = View.VISIBLE
|
||||||
|
loadMoreComments()
|
||||||
|
lifecycleScope.launch {
|
||||||
|
kotlinx.coroutines.delay(1000)
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
binding.commentBottomRefresh.visibility = View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun fetchComments(): CommentResponse? {
|
||||||
|
return withContext(Dispatchers.IO) {
|
||||||
|
CommentsAPI.getCommentsForId(mediaId, pagesLoaded + 1, filterTag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//adds additional comments to the section
|
||||||
|
private suspend fun updateUIWithComment(comment: Comment) {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
section.add(
|
||||||
|
CommentItem(
|
||||||
|
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 :
|
||||||
androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
|
androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
|
||||||
override fun onScrolled(
|
override fun onScrolled(
|
||||||
recyclerView: androidx.recyclerview.widget.RecyclerView,
|
recyclerView: androidx.recyclerview.widget.RecyclerView,
|
||||||
|
@ -245,7 +284,7 @@ class CommentsFragment : Fragment() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})*/
|
||||||
|
|
||||||
|
|
||||||
binding.commentInput.addTextChangedListener(object : TextWatcher {
|
binding.commentInput.addTextChangedListener(object : TextWatcher {
|
||||||
|
|
|
@ -21,65 +21,64 @@
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<androidx.core.widget.NestedScrollView
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="wrap_content"
|
||||||
android:fillViewport="true">
|
android:gravity="end"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
tools:ignore="UseCompoundDrawables">
|
||||||
|
|
||||||
<LinearLayout
|
<ImageView
|
||||||
|
android:id="@+id/commentFilter"
|
||||||
|
android:layout_width="28dp"
|
||||||
|
android:layout_height="28dp"
|
||||||
|
android:layout_marginEnd="12dp"
|
||||||
|
android:background="?android:attr/selectableItemBackground"
|
||||||
|
android:contentDescription="@string/sort_by"
|
||||||
|
app:srcCompat="@drawable/ic_round_filter_alt_24"
|
||||||
|
app:tint="?attr/colorOnBackground" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/commentSort"
|
||||||
|
android:layout_width="28dp"
|
||||||
|
android:layout_height="28dp"
|
||||||
|
android:layout_marginEnd="12dp"
|
||||||
|
android:background="?android:attr/selectableItemBackground"
|
||||||
|
android:contentDescription="@string/sort_by"
|
||||||
|
app:srcCompat="@drawable/ic_round_sort_24"
|
||||||
|
app:tint="?attr/colorOnBackground" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/commentsProgressBar"
|
||||||
|
style="?android:attr/progressBarStyle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="bottom|center_horizontal"
|
||||||
|
android:layout_marginTop="64dp" />
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/commentsList"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:nestedScrollingEnabled="true"
|
||||||
|
android:visibility="gone"
|
||||||
|
tools:listitem="@layout/item_comments"
|
||||||
|
tools:visibility="visible" />
|
||||||
|
|
||||||
<LinearLayout
|
<ProgressBar
|
||||||
android:layout_width="match_parent"
|
android:id="@+id/commentBottomRefresh"
|
||||||
android:layout_height="wrap_content"
|
style="?android:attr/progressBarStyle"
|
||||||
android:gravity="end"
|
android:layout_width="wrap_content"
|
||||||
android:orientation="horizontal"
|
android:layout_height="wrap_content"
|
||||||
tools:ignore="UseCompoundDrawables">
|
android:layout_gravity="bottom|center_horizontal"
|
||||||
|
android:layout_marginBottom="32dp"
|
||||||
<ImageView
|
android:visibility="gone" />
|
||||||
android:id="@+id/commentFilter"
|
</FrameLayout>
|
||||||
android:layout_width="28dp"
|
|
||||||
android:layout_height="28dp"
|
|
||||||
android:layout_marginEnd="12dp"
|
|
||||||
android:background="?android:attr/selectableItemBackground"
|
|
||||||
android:contentDescription="@string/sort_by"
|
|
||||||
app:srcCompat="@drawable/ic_round_filter_alt_24"
|
|
||||||
app:tint="?attr/colorOnBackground" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/commentSort"
|
|
||||||
android:layout_width="28dp"
|
|
||||||
android:layout_height="28dp"
|
|
||||||
android:layout_marginEnd="12dp"
|
|
||||||
android:background="?android:attr/selectableItemBackground"
|
|
||||||
android:contentDescription="@string/sort_by"
|
|
||||||
app:srcCompat="@drawable/ic_round_sort_24"
|
|
||||||
app:tint="?attr/colorOnBackground" />
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
|
|
||||||
<ProgressBar
|
|
||||||
android:id="@+id/commentsProgressBar"
|
|
||||||
style="?android:attr/progressBarStyle"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="bottom|center_horizontal"
|
|
||||||
android:layout_marginTop="64dp" />
|
|
||||||
|
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
|
||||||
android:id="@+id/commentsList"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:nestedScrollingEnabled="false"
|
|
||||||
android:visibility="gone"
|
|
||||||
tools:listitem="@layout/item_comments"
|
|
||||||
tools:visibility="visible" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
</androidx.core.widget.NestedScrollView>
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
|
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
|
||||||
|
|
||||||
|
@ -133,8 +132,8 @@
|
||||||
android:id="@+id/commentInputLayout"
|
android:id="@+id/commentInputLayout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
|
||||||
android:background="?android:colorBackground"
|
android:background="?android:colorBackground"
|
||||||
|
android:orientation="horizontal"
|
||||||
android:paddingTop="8dp"
|
android:paddingTop="8dp"
|
||||||
android:paddingBottom="8dp">
|
android:paddingBottom="8dp">
|
||||||
|
|
||||||
|
@ -172,18 +171,18 @@
|
||||||
android:layout_width="24dp"
|
android:layout_width="24dp"
|
||||||
android:layout_height="24dp"
|
android:layout_height="24dp"
|
||||||
android:layout_gravity="center"
|
android:layout_gravity="center"
|
||||||
android:background="@drawable/ic_label_off_24"
|
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
|
android:background="@drawable/ic_label_off_24"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
tools:visibility="visible"
|
tools:ignore="ContentDescription"
|
||||||
tools:ignore="ContentDescription" />
|
tools:visibility="visible" />
|
||||||
|
|
||||||
<ImageButton
|
<ImageButton
|
||||||
android:id="@+id/commentSend"
|
android:id="@+id/commentSend"
|
||||||
android:layout_width="32dp"
|
android:layout_width="32dp"
|
||||||
android:layout_height="32dp"
|
android:layout_height="32dp"
|
||||||
android:layout_marginEnd="12dp"
|
|
||||||
android:layout_gravity="center"
|
android:layout_gravity="center"
|
||||||
|
android:layout_marginEnd="12dp"
|
||||||
android:background="@drawable/ic_round_send_24"
|
android:background="@drawable/ic_round_send_24"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
tools:ignore="ContentDescription"
|
tools:ignore="ContentDescription"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue