feat: remember comment sort order

This commit is contained in:
rebelonion 2024-02-15 14:17:45 -06:00
parent 4c35f9a0cf
commit ee7cff0fea
3 changed files with 126 additions and 40 deletions

View file

@ -164,10 +164,12 @@ class CommentItem(val comment: Comment,
} }
} }
companion object {
fun timestampToMillis(timestamp: String): Long { fun timestampToMillis(timestamp: String): Long {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
dateFormat.timeZone = TimeZone.getTimeZone("UTC") dateFormat.timeZone = TimeZone.getTimeZone("UTC")
val parsedDate = dateFormat.parse(timestamp) val parsedDate = dateFormat.parse(timestamp)
return parsedDate?.time ?: 0 return parsedDate?.time ?: 0
} }
}
} }

View file

@ -15,6 +15,8 @@ import ani.dantotsu.connections.comments.CommentsAPI
import ani.dantotsu.databinding.ActivityCommentsBinding import ani.dantotsu.databinding.ActivityCommentsBinding
import ani.dantotsu.initActivity import ani.dantotsu.initActivity
import ani.dantotsu.loadImage import ani.dantotsu.loadImage
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.themes.ThemeManager import ani.dantotsu.themes.ThemeManager
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
@ -100,7 +102,26 @@ class CommentsActivity : AppCompatActivity() {
section.clear() section.clear()
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val comments = CommentsAPI.getCommentsForId(mediaId) val comments = CommentsAPI.getCommentsForId(mediaId)
comments?.comments?.forEach { val sorted = when (PrefManager.getVal(PrefName.CommentSortOrder, "newest")) {
"newest" -> comments?.comments?.sortedByDescending { comment ->
CommentItem.timestampToMillis(comment.timestamp)
}
"oldest" -> comments?.comments?.sortedBy { comment ->
CommentItem.timestampToMillis(comment.timestamp)
}
"highest_rated" -> comments?.comments?.sortedByDescending { comment ->
comment.upvotes - comment.downvotes
}
"lowest_rated" -> comments?.comments?.sortedBy { comment ->
comment.upvotes - comment.downvotes
}
else -> comments?.comments
}
sorted?.forEach {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
section.add(CommentItem(it, buildMarkwon(), section) { comment -> section.add(CommentItem(it, buildMarkwon(), section) { comment ->
editCallback(comment) editCallback(comment)
@ -123,7 +144,26 @@ class CommentsActivity : AppCompatActivity() {
binding.commentsList.visibility = View.GONE binding.commentsList.visibility = View.GONE
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val comments = CommentsAPI.getCommentsForId(mediaId) val comments = CommentsAPI.getCommentsForId(mediaId)
comments?.comments?.forEach { val sorted = when (PrefManager.getVal(PrefName.CommentSortOrder, "newest")) {
"newest" -> comments?.comments?.sortedByDescending { comment ->
CommentItem.timestampToMillis(comment.timestamp)
}
"oldest" -> comments?.comments?.sortedBy { comment ->
CommentItem.timestampToMillis(comment.timestamp)
}
"highest_rated" -> comments?.comments?.sortedByDescending { comment ->
comment.upvotes - comment.downvotes
}
"lowest_rated" -> comments?.comments?.sortedBy { comment ->
comment.upvotes - comment.downvotes
}
else -> comments?.comments
}
sorted?.forEach {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
section.add(CommentItem(it, buildMarkwon(), section) { comment -> section.add(CommentItem(it, buildMarkwon(), section) { comment ->
editCallback(comment) editCallback(comment)
@ -142,26 +182,29 @@ class CommentsActivity : AppCompatActivity() {
popup.setOnMenuItemClickListener { item -> popup.setOnMenuItemClickListener { item ->
when (item.itemId) { when (item.itemId) {
R.id.comment_sort_newest -> { R.id.comment_sort_newest -> {
PrefManager.setVal(PrefName.CommentSortOrder, "newest")
val groups = section.groups val groups = section.groups
groups.sortByDescending { comment -> groups.sortByDescending { comment ->
comment as CommentItem comment as CommentItem
comment.timestampToMillis(comment.comment.timestamp) CommentItem.timestampToMillis(comment.comment.timestamp)
} }
section.update(groups) section.update(groups)
binding.commentsList.scrollToPosition(0) binding.commentsList.scrollToPosition(0)
} }
R.id.comment_sort_oldest -> { R.id.comment_sort_oldest -> {
PrefManager.setVal(PrefName.CommentSortOrder, "oldest")
val groups = section.groups val groups = section.groups
groups.sortBy { comment -> groups.sortBy { comment ->
comment as CommentItem comment as CommentItem
comment.timestampToMillis(comment.comment.timestamp) CommentItem.timestampToMillis(comment.comment.timestamp)
} }
section.update(groups) section.update(groups)
binding.commentsList.scrollToPosition(0) binding.commentsList.scrollToPosition(0)
} }
R.id.comment_sort_highest_rated -> { R.id.comment_sort_highest_rated -> {
PrefManager.setVal(PrefName.CommentSortOrder, "highest_rated")
val groups = section.groups val groups = section.groups
groups.sortByDescending { comment -> groups.sortByDescending { comment ->
comment as CommentItem comment as CommentItem
@ -172,6 +215,7 @@ class CommentsActivity : AppCompatActivity() {
} }
R.id.comment_sort_lowest_rated -> { R.id.comment_sort_lowest_rated -> {
PrefManager.setVal(PrefName.CommentSortOrder, "lowest_rated")
val groups = section.groups val groups = section.groups
groups.sortBy { comment -> groups.sortBy { comment ->
comment as CommentItem comment as CommentItem
@ -246,6 +290,27 @@ class CommentsActivity : AppCompatActivity() {
snackString("You are banned from commenting :(") snackString("You are banned from commenting :(")
return@setOnClickListener return@setOnClickListener
} }
val firstComment = PrefManager.getVal(PrefName.FirstComment, false)
if (!firstComment) {
//show a dialog explaining the rules
val alertDialog = android.app.AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("Commenting Rules")
.setMessage(
"I WILL BAN YOU WITHOUT HESITATION\n" +
"1. No racism\n" +
"2. No hate speech\n" +
"3. No spam\n" +
"4. No NSFW content\n" +
"6. No advertising\n" +
"7. No self promotion\n" +
"8. No impersonation\n" +
"9. No harassment\n" +
"10. No illegal content\n" +
"11. Anything you know you shouldn't comment\n"
)
.setPositiveButton("I Understand") { dialog, _ ->
dialog.dismiss()
PrefManager.setVal(PrefName.FirstComment, false)
binding.commentInput.text.toString().let { binding.commentInput.text.toString().let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
binding.commentInput.text.clear() binding.commentInput.text.clear()
@ -260,9 +325,12 @@ class CommentsActivity : AppCompatActivity() {
if (item is CommentItem) { if (item is CommentItem) {
if (item.comment.commentId == editingCommentId) { if (item.comment.commentId == editingCommentId) {
item.comment.content = it item.comment.content = it
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") val dateFormat =
dateFormat.timeZone = TimeZone.getTimeZone("UTC") SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
item.comment.timestamp = dateFormat.format(System.currentTimeMillis()) dateFormat.timeZone =
TimeZone.getTimeZone("UTC")
item.comment.timestamp =
dateFormat.format(System.currentTimeMillis())
item.notifyChanged() item.notifyChanged()
snackString("Comment edited") snackString("Comment edited")
} }
@ -275,7 +343,13 @@ class CommentsActivity : AppCompatActivity() {
CommentsAPI.comment(mediaId, null, it) CommentsAPI.comment(mediaId, null, it)
} }
if (success != null) if (success != null)
section.add(0, CommentItem(success, buildMarkwon(), section) { comment -> section.add(
0,
CommentItem(
success,
buildMarkwon(),
section
) { comment ->
editCallback(comment) editCallback(comment)
}) })
} }
@ -285,6 +359,14 @@ class CommentsActivity : AppCompatActivity() {
} }
} }
} }
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
val dialog = alertDialog.show()
dialog?.window?.setDimAmount(0.8f)
}
}
} }
private fun buildMarkwon(): Markwon { private fun buildMarkwon(): Markwon {

View file

@ -63,6 +63,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
PopularAnimeList(Pref(Location.UI, Boolean::class, true)), PopularAnimeList(Pref(Location.UI, Boolean::class, true)),
AnimeListSortOrder(Pref(Location.UI, String::class, "score")), AnimeListSortOrder(Pref(Location.UI, String::class, "score")),
MangaListSortOrder(Pref(Location.UI, String::class, "score")), MangaListSortOrder(Pref(Location.UI, String::class, "score")),
CommentSortOrder(Pref(Location.UI, String::class, "newest")),
//Player //Player
DefaultSpeed(Pref(Location.Player, Int::class, 5)), DefaultSpeed(Pref(Location.Player, Int::class, 5)),
@ -152,6 +153,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
TagsListIsAdult(Pref(Location.Irrelevant, Set::class, setOf<String>())), TagsListIsAdult(Pref(Location.Irrelevant, Set::class, setOf<String>())),
TagsListNonAdult(Pref(Location.Irrelevant, Set::class, setOf<String>())), TagsListNonAdult(Pref(Location.Irrelevant, Set::class, setOf<String>())),
MakeDefault(Pref(Location.Irrelevant, Boolean::class, true)), MakeDefault(Pref(Location.Irrelevant, Boolean::class, true)),
FirstComment(Pref(Location.Irrelevant, Boolean::class, false)),
//Protected //Protected
DiscordToken(Pref(Location.Protected, String::class, "")), DiscordToken(Pref(Location.Protected, String::class, "")),