feat: sort comments in api
This commit is contained in:
parent
a0b22e8d56
commit
af1bc944d8
2 changed files with 23 additions and 7 deletions
|
@ -31,12 +31,15 @@ object CommentsAPI {
|
||||||
var isMod: Boolean = false
|
var isMod: Boolean = false
|
||||||
var totalVotes: Int = 0
|
var totalVotes: Int = 0
|
||||||
|
|
||||||
suspend fun getCommentsForId(id: Int, page: Int = 1, tag: Int?): CommentResponse? {
|
suspend fun getCommentsForId(id: Int, page: Int = 1, tag: Int?, sort: String?): CommentResponse? {
|
||||||
var url = "$address/comments/$id/$page"
|
var url = "$address/comments/$id/$page"
|
||||||
val request = requestBuilder()
|
val request = requestBuilder()
|
||||||
tag?.let {
|
tag?.let {
|
||||||
url += "?tag=$it"
|
url += "?tag=$it"
|
||||||
}
|
}
|
||||||
|
sort?.let {
|
||||||
|
url += if (tag != null) "&sort=$it" else "?sort=$it"
|
||||||
|
}
|
||||||
val json = try {
|
val json = try {
|
||||||
request.get(url)
|
request.get(url)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
|
|
|
@ -13,7 +13,6 @@ import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
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.widget.PopupMenu
|
import androidx.appcompat.widget.PopupMenu
|
||||||
import androidx.core.animation.doOnEnd
|
import androidx.core.animation.doOnEnd
|
||||||
import androidx.core.content.res.ResourcesCompat
|
import androidx.core.content.res.ResourcesCompat
|
||||||
|
@ -34,7 +33,6 @@ import ani.dantotsu.settings.saving.PrefName
|
||||||
import ani.dantotsu.snackString
|
import ani.dantotsu.snackString
|
||||||
import ani.dantotsu.toast
|
import ani.dantotsu.toast
|
||||||
import ani.dantotsu.util.Logger
|
import ani.dantotsu.util.Logger
|
||||||
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
|
||||||
|
@ -140,9 +138,15 @@ class CommentsFragment : Fragment() {
|
||||||
R.id.comment_sort_lowest_rated -> "lowest_rated"
|
R.id.comment_sort_lowest_rated -> "lowest_rated"
|
||||||
else -> return@setOnMenuItemClickListener false
|
else -> return@setOnMenuItemClickListener false
|
||||||
}
|
}
|
||||||
|
|
||||||
PrefManager.setVal(PrefName.CommentSortOrder, sortOrder)
|
PrefManager.setVal(PrefName.CommentSortOrder, sortOrder)
|
||||||
sortComments(sortOrder)
|
if (totalPages > pagesLoaded) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
loadAndDisplayComments()
|
||||||
|
activity.binding.commentReplyToContainer.visibility = View.GONE
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sortComments(sortOrder)
|
||||||
|
}
|
||||||
binding.commentsList.scrollToPosition(0)
|
binding.commentsList.scrollToPosition(0)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -221,7 +225,12 @@ class CommentsFragment : Fragment() {
|
||||||
|
|
||||||
private suspend fun fetchComments(): CommentResponse? {
|
private suspend fun fetchComments(): CommentResponse? {
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
CommentsAPI.getCommentsForId(mediaId, pagesLoaded + 1, filterTag)
|
CommentsAPI.getCommentsForId(
|
||||||
|
mediaId,
|
||||||
|
pagesLoaded + 1,
|
||||||
|
filterTag,
|
||||||
|
PrefManager.getVal(PrefName.CommentSortOrder, "newest")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,7 +388,11 @@ class CommentsFragment : Fragment() {
|
||||||
section.clear()
|
section.clear()
|
||||||
|
|
||||||
val comments = withContext(Dispatchers.IO) {
|
val comments = withContext(Dispatchers.IO) {
|
||||||
CommentsAPI.getCommentsForId(mediaId, tag = filterTag)
|
CommentsAPI.getCommentsForId(
|
||||||
|
mediaId,
|
||||||
|
tag = filterTag,
|
||||||
|
sort = PrefManager.getVal(PrefName.CommentSortOrder, "newest")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
val sortedComments = sortComments(comments?.comments)
|
val sortedComments = sortComments(comments?.comments)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue