Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added comments counter #430

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions app/src/main/java/ani/dantotsu/media/comments/CommentsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.widget.PopupMenu
import androidx.core.animation.doOnEnd
import androidx.core.content.res.ResourcesCompat
Expand Down Expand Up @@ -82,7 +83,7 @@ class CommentsFragment : Fragment() {
activity.binding.commentInputLayout
)

//get the media id from the intent
// Get the media id from the intent
val mediaId = arguments?.getInt("mediaId") ?: -1
mediaName = arguments?.getString("mediaName") ?: "unknown"
if (mediaId == -1) {
Expand All @@ -105,11 +106,16 @@ class CommentsFragment : Fragment() {
binding.commentsRefresh.setOnRefreshListener {
lifecycleScope.launch {
loadAndDisplayComments()
binding.commentCounter.text = commentCounter(mediaId)
binding.commentsRefresh.isRefreshing = false
}
activity.binding.commentReplyToContainer.visibility = View.GONE
}

lifecycleScope.launch {
binding.commentCounter.text = commentCounter(mediaId)
}

binding.commentsList.adapter = adapter
binding.commentsList.layoutManager = LinearLayoutManager(activity)

Expand Down Expand Up @@ -236,7 +242,7 @@ class CommentsFragment : Fragment() {
}
}

//adds additional comments to the section
// Adds additional comments to the section
private suspend fun updateUIWithComment(comment: Comment) {
withContext(Dispatchers.Main) {
section.add(
Expand Down Expand Up @@ -297,7 +303,7 @@ class CommentsFragment : Fragment() {
}

activity.binding.commentLabel.setOnClickListener {
//alert dialog to enter a number, with a cancel and ok button
// Alert dialog to enter a number, with a cancel and ok button
activity.customAlertDialog().apply {
val customView = DialogEdittextBinding.inflate(layoutInflater)
setTitle("Enter a chapter/episode number tag")
Expand Down Expand Up @@ -588,6 +594,30 @@ class CommentsFragment : Fragment() {
}
}


private suspend fun commentCounter(mediaId: Int): String {
var totalComments = 0
var currentPage = 1

while (true) {
val response = CommentsAPI.getCommentsForId(mediaId, page = currentPage, tag = null, sort = null)
totalPages = response?.totalPages ?: 1
totalComments += response?.comments?.size ?: 0

if (currentPage >= totalPages) {
break
}
currentPage++
}

return if (totalComments > 0) {
resources.getString(R.string.comments_counter, totalComments.toString())
} else {
resources.getString(R.string.no_comments_found)
}
}


private fun processComment() {
val commentText = activity.binding.commentInput.text.toString()
if (commentText.isEmpty()) {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/fragment_comments.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">

<TextView
android:id="@+id/commentCounter"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="18dp"
android:fontFamily="@font/poppins_bold"
android:gravity="center_vertical"
android:text="@string/loading" />

<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />

<ImageView
android:id="@+id/commentFilter"
android:layout_width="28dp"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,8 @@
<string name="try_internal_cast_experimental">Try Internal Cast (Experimental)</string>

<string name="comments">Comments</string>
<string name="comments_counter">%1$s Comments</string>
<string name="no_comments_found">No Comments found</string>
<string name="newest">Newest</string>
<string name="oldest">Oldest</string>
<string name="highest_rated">Highest rated</string>
Expand Down