Skip to content

Commit

Permalink
fix: review rework
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush2622 committed May 19, 2024
1 parent f53d27b commit 114be6f
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 30 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/ani/dantotsu/home/status/CircleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import ani.dantotsu.getThemeColor

class CircleView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private var parts: Int = 3
private var gapAngle: Float = 9f
private var gapAngle: Float = 12f
private val path = Path()
private var isUser = false
private var booleanList = listOf<Boolean>()
private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeWidth = 6f
strokeCap = Paint.Cap.ROUND
}

@SuppressLint("DrawAllocation")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ani/dantotsu/home/status/Stories.kt
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class Stories @JvmOverloads constructor(
binding.activityLike.setColorFilter(if (story.isLiked == true) likeColor else notLikeColor)
binding.replyCount.text = story.replyCount.toString()
binding.activityLikeCount.text = story.likeCount.toString()
binding.activityLike.setColorFilter(ContextCompat.getColor(context, R.color.bg_opp))
binding.activityReplies.setColorFilter(ContextCompat.getColor(context, R.color.bg_opp))
binding.activityLikeContainer.setOnClickListener {
like()
}
Expand Down
33 changes: 7 additions & 26 deletions app/src/main/java/ani/dantotsu/media/ReviewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ReviewActivity : AppCompatActivity() {
binding.listBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }

lifecycleScope.launch(Dispatchers.IO) {
val response = Anilist.query.getReviews(mediaId)
val response = Anilist.query.getReviews(mediaId)?.data?.page
withContext(Dispatchers.Main) {
binding.listProgressBar.visibility = View.GONE
binding.listRecyclerView.setOnTouchListener { _, event ->
Expand All @@ -94,9 +94,9 @@ class ReviewActivity : AppCompatActivity() {
}
false
}
currentPage = response?.data?.page?.pageInfo?.currentPage ?: 1
hasNextPage = response?.data?.page?.pageInfo?.hasNextPage ?: false
response?.data?.page?.reviews?.let {
currentPage = response?.pageInfo?.currentPage ?: 1
hasNextPage = response?.pageInfo?.hasNextPage ?: false
response?.reviews?.let {
reviews.addAll(it)
fillList()
}
Expand All @@ -122,29 +122,10 @@ class ReviewActivity : AppCompatActivity() {
private fun fillList() {
adapter.clear()
reviews.forEach {
val username = it.user?.name ?: "Unknown"
val name = SpannableString(username + " - " + it.score)
//change the size of the score
name.setSpan(
android.text.style.RelativeSizeSpan(0.9f),
0,
name.length,
android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
//give the text an underline
name.setSpan(
android.text.style.UnderlineSpan(),
username.length + 3,
name.length,
android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
adapter.add(
FollowerItem(
it.id,
name,
it.user?.avatar?.medium,
it.user?.bannerImage,
it.summary,
ReviewAdapter(
it,
this,
this::onUserClick
)
)
Expand Down
129 changes: 129 additions & 0 deletions app/src/main/java/ani/dantotsu/media/ReviewAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package ani.dantotsu.media


import android.content.Context
import android.text.SpannableString
import android.view.View
import androidx.lifecycle.lifecycleScope
import ani.dantotsu.R
import ani.dantotsu.blurImage
import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.connections.anilist.api.Query
import ani.dantotsu.databinding.ItemFollowerBinding
import ani.dantotsu.databinding.ItemReviewsBinding
import ani.dantotsu.getThemeColor
import ani.dantotsu.loadImage
import ani.dantotsu.profile.activity.ActivityItemBuilder
import ani.dantotsu.toast
import com.xwray.groupie.viewbinding.BindableItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ReviewAdapter(
private var review: Query.Review,
val context: ReviewActivity,
val clickCallback: (Int) -> Unit

) : BindableItem<ItemReviewsBinding>() {
private lateinit var binding: ItemReviewsBinding

override fun bind(viewBinding: ItemReviewsBinding, position: Int) {
binding = viewBinding
binding.reviewUserName.text = review.user?.name
binding.reviewUserAvatar.loadImage(review.user?.avatar?.medium)
binding.reviewText.text = review.summary
binding.reviewPostTime.text = ActivityItemBuilder.getDateTime(review.createdAt)
binding.reviewTag.text = "[${review.score}]"
binding.root.setOnClickListener { clickCallback(review.id) }
userVote(review.userRating)
enableVote()
binding.reviewTotalVotes.text = review.rating.toString()
}

override fun getLayout(): Int {
return R.layout.item_reviews
}

override fun initializeViewBinding(view: View): ItemReviewsBinding {
return ItemReviewsBinding.bind(view)
}
private fun userVote(type: String) {
when (type) {
"NO_VOTE" -> {
binding.reviewUpVote.setImageResource(R.drawable.ic_round_upvote_inactive_24)
binding.reviewDownVote.setImageResource(R.drawable.ic_round_upvote_inactive_24)
binding.reviewUpVote.alpha = 0.6f
binding.reviewDownVote.alpha = 0.6f
}

"UP_VOTE" -> {
binding.reviewUpVote.setImageResource(R.drawable.ic_round_upvote_active_24)
binding.reviewDownVote.setImageResource(R.drawable.ic_round_upvote_inactive_24)
binding.reviewUpVote.alpha = 1f
binding.reviewDownVote.alpha = 0.6f
}

"DOWN_VOTE" -> {
binding.reviewUpVote.setImageResource(R.drawable.ic_round_upvote_inactive_24)
binding.reviewDownVote.setImageResource(R.drawable.ic_round_upvote_active_24)
binding.reviewDownVote.alpha = 1f
binding.reviewUpVote.alpha = 0.6f
}
}
}

private fun rateReview(rating: String) {
disableVote()
context.lifecycleScope.launch {
val result = Anilist.mutation.rateReview(review.id, rating)
if (result != null) {
withContext(Dispatchers.Main) {
val res = result.data.rateReview
review.rating = res.rating
review.ratingAmount = res.ratingAmount
review.userRating = res.userRating
userVote(review.userRating)
binding.reviewTotalVotes.text = review.rating.toString()
userVote(review.userRating)
enableVote()
}
} else {
withContext(Dispatchers.Main) {
toast(
context.getString(R.string.error_message, "response is null")
)
enableVote()
}
}
}
}

private fun disableVote() {
binding.reviewUpVote.setOnClickListener(null)
binding.reviewDownVote.setOnClickListener(null)
binding.reviewUpVote.isEnabled = false
binding.reviewDownVote.isEnabled = false
}

private fun enableVote() {
binding.reviewUpVote.setOnClickListener {
if (review.userRating == "UP_VOTE") {
rateReview("NO_VOTE")
} else {
rateReview("UP_VOTE")
}
disableVote()
}
binding.reviewDownVote.setOnClickListener {
if (review.userRating == "DOWN_VOTE") {
rateReview("NO_VOTE")
} else {
rateReview("DOWN_VOTE")
}
disableVote()
}
binding.reviewUpVote.isEnabled = true
binding.reviewDownVote.isEnabled = true
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_review_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/item_developer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="0dp"
android:gravity="center_vertical"
Expand All @@ -45,7 +45,9 @@
android:id="@+id/devRole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_bold" />
android:text="@string/lorem_ipsum"
android:maxLines="2"
android:fontFamily="@font/poppins_semi_bold" />
</LinearLayout>
</LinearLayout>

Expand Down
Loading

0 comments on commit 114be6f

Please sign in to comment.