diff --git a/app/src/main/java/com/doubtless/doubtless/screens/answers/viewholder/AnswerViewHolder.kt b/app/src/main/java/com/doubtless/doubtless/screens/answers/viewholder/AnswerViewHolder.kt index d6c4ff5..3b1ce9d 100644 --- a/app/src/main/java/com/doubtless/doubtless/screens/answers/viewholder/AnswerViewHolder.kt +++ b/app/src/main/java/com/doubtless/doubtless/screens/answers/viewholder/AnswerViewHolder.kt @@ -1,6 +1,7 @@ package com.doubtless.doubtless.screens.answers.viewholder import android.view.View +import android.widget.CheckBox import android.widget.ImageView import android.widget.TextView import androidx.core.view.isVisible @@ -11,11 +12,11 @@ import com.doubtless.doubtless.R import com.doubtless.doubtless.screens.answers.AnswerData import com.doubtless.doubtless.screens.doubt.usecases.VotingUseCase import com.doubtless.doubtless.utils.Utils +import com.doubtless.doubtless.utils.addStateListAnimation import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import java.util.Date -import kotlin.math.ceil +import java.util.* import kotlin.math.floor @@ -32,8 +33,8 @@ class AnswerViewHolder(itemView: View, private val interactionListener: Interact private val ivDp: ImageView private val tvYear: TextView private val tvVotes: TextView - private val ivUpVote: ImageView - private val ivDownVote: ImageView + private val upVote: CheckBox + private val downVote: CheckBox init { authorName = itemView.findViewById(R.id.tv_author_name) @@ -42,8 +43,8 @@ class AnswerViewHolder(itemView: View, private val interactionListener: Interact ivDp = itemView.findViewById(R.id.iv_dp_author) tvYear = itemView.findViewById(R.id.user_year) tvVotes = itemView.findViewById(R.id.tv_votes) - ivUpVote = itemView.findViewById(R.id.iv_votes) - ivDownVote = itemView.findViewById(R.id.iv_downvote) + upVote = itemView.findViewById(R.id.cb_upvote) + downVote = itemView.findViewById(R.id.cb_downvote) } fun setData(answerData: AnswerData) { @@ -66,8 +67,10 @@ class AnswerViewHolder(itemView: View, private val interactionListener: Interact val votingUseCase = DoubtlessApp.getInstance().getAppCompRoot().getAnswerVotingDoubtCase(answerData.copy()) setVotesUi(answerData, votingUseCase) - ivUpVote.setOnClickListener { + upVote.setOnClickListener { CoroutineScope(Dispatchers.Main).launch { + if (it.stateListAnimator == null) + it.addStateListAnimation(R.animator.scale_votes_icon) val result = votingUseCase.upvoteDoubt() @@ -81,8 +84,10 @@ class AnswerViewHolder(itemView: View, private val interactionListener: Interact } } - ivDownVote.setOnClickListener { + downVote.setOnClickListener { CoroutineScope(Dispatchers.Main).launch { + if (it.stateListAnimator == null) + it.addStateListAnimation(R.animator.scale_votes_icon) val result = votingUseCase.downVoteDoubt() @@ -102,19 +107,22 @@ class AnswerViewHolder(itemView: View, private val interactionListener: Interact } private fun setVotesUi(answerData: AnswerData, votingUseCase: VotingUseCase) { - ivDownVote.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_off_alt_24)) - ivUpVote.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_off_alt_24)) tvVotes.text = floor(answerData.netVotes).toInt().toString() - CoroutineScope(Dispatchers.Main).launch { - val currentState = votingUseCase.getUserCurrentState() - - if (currentState == VotingUseCase.UPVOTED) - ivUpVote.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_filled)) - - if (currentState == VotingUseCase.DOWNVOTED) - ivDownVote.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_filled)) + when (votingUseCase.getUserCurrentState()) { + VotingUseCase.UPVOTED -> { + downVote.isClickable = false + upVote.isChecked = true + } + VotingUseCase.DOWNVOTED -> { + upVote.isClickable = false + downVote.isChecked = true + } + else -> { + downVote.isClickable = true + upVote.isClickable = true + } + } } } - } diff --git a/app/src/main/java/com/doubtless/doubtless/screens/doubt/view/viewholder/DoubtPreviewViewHolder.kt b/app/src/main/java/com/doubtless/doubtless/screens/doubt/view/viewholder/DoubtPreviewViewHolder.kt index 2d37b9b..b7279ca 100644 --- a/app/src/main/java/com/doubtless/doubtless/screens/doubt/view/viewholder/DoubtPreviewViewHolder.kt +++ b/app/src/main/java/com/doubtless/doubtless/screens/doubt/view/viewholder/DoubtPreviewViewHolder.kt @@ -1,13 +1,10 @@ package com.doubtless.doubtless.screens.doubt.view.viewholder -import android.content.res.ColorStateList import android.text.util.Linkify -import android.util.Log import android.view.View +import android.widget.CheckBox import android.widget.ImageView import android.widget.TextView -import android.widget.Toast -import androidx.core.content.ContextCompat import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide @@ -17,6 +14,7 @@ import com.doubtless.doubtless.screens.doubt.DoubtData import com.doubtless.doubtless.screens.doubt.usecases.VotingUseCase import com.doubtless.doubtless.utils.Utils import com.doubtless.doubtless.utils.Utils.flatten +import com.doubtless.doubtless.utils.addStateListAnimation import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -40,8 +38,8 @@ class DoubtPreviewViewHolder( private val description: TextView private val ivDp: ImageView private val tvNetVotes: TextView - private val ivUpvotes: ImageView - private val ivDownvotes: ImageView + private val upvotes: CheckBox + private val downvotes: CheckBox private val tvAnswers: TextView private val tvTags: TextView private val tvCollege: TextView @@ -60,11 +58,11 @@ class DoubtPreviewViewHolder( tvAnswers = view.findViewById(R.id.tv_answers) tvTags = view.findViewById(R.id.tv_tags) tvCollege = view.findViewById(R.id.user_college) - ivUpvotes = view.findViewById(R.id.iv_upvotes) - ivDownvotes = view.findViewById(R.id.iv_downvote) + upvotes = view.findViewById(R.id.cb_upvotes) + downvotes = view.findViewById(R.id.cb_downvote) - ivUpvotes.isVisible = showVotingLayout - ivDownvotes.isVisible = showVotingLayout + upvotes.isVisible = showVotingLayout + downvotes.isVisible = showVotingLayout } fun setData(doubtData: DoubtData) { @@ -114,8 +112,11 @@ class DoubtPreviewViewHolder( setVotesUi(doubtData, votingUseCase) - ivUpvotes.setOnClickListener { + + upvotes.setOnClickListener { CoroutineScope(Dispatchers.Main).launch { + if (it.stateListAnimator == null) + it.addStateListAnimation(R.animator.scale_votes_icon) val result = votingUseCase.upvoteDoubt() @@ -130,8 +131,10 @@ class DoubtPreviewViewHolder( } } - ivDownvotes.setOnClickListener { + downvotes.setOnClickListener { CoroutineScope(Dispatchers.Main).launch { + if (it.stateListAnimator == null) + it.addStateListAnimation(R.animator.scale_votes_icon) val result = votingUseCase.downVoteDoubt() @@ -148,23 +151,22 @@ class DoubtPreviewViewHolder( } private fun setVotesUi(doubtData: DoubtData, votingUseCase: VotingUseCase) { - ivDownvotes.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_off_alt_24)) - - ivUpvotes.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_off_alt_24)) - // ivUpvotes.setColorFilter(ContextCompat.getColor(itemView.context, R.color.grey), android.graphics.PorterDuff.Mode.SRC_IN); - tvNetVotes.text = floor(doubtData.netVotes).toInt().toString() - CoroutineScope(Dispatchers.Main).launch { - val currentState = votingUseCase.getUserCurrentState() - - if (currentState == VotingUseCase.UPVOTED) { - ivUpvotes.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_filled)) - // ivUpvotes.setColorFilter(ContextCompat.getColor(itemView.context, R.color.purple), android.graphics.PorterDuff.Mode.SRC_IN); + when (votingUseCase.getUserCurrentState()) { + VotingUseCase.UPVOTED -> { + downvotes.isClickable = false + upvotes.isChecked = true + } + VotingUseCase.DOWNVOTED -> { + upvotes.isClickable = false + downvotes.isChecked = true + } + else -> { + downvotes.isClickable = true + upvotes.isClickable = true + } } - - if (currentState == VotingUseCase.DOWNVOTED) - ivDownvotes.setImageDrawable(itemView.context.getDrawable(R.drawable.ic_baseline_thumb_up_filled)) } } } diff --git a/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt b/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt new file mode 100644 index 0000000..724d406 --- /dev/null +++ b/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt @@ -0,0 +1,12 @@ +package com.doubtless.doubtless.utils + +import android.animation.AnimatorInflater +import android.view.View +import androidx.annotation.AnimatorRes + +fun View.addStateListAnimation(@AnimatorRes animation: Int) { + this.stateListAnimator = AnimatorInflater.loadStateListAnimator( + this.context, + animation + ) +} \ No newline at end of file diff --git a/app/src/main/res/animator/scale_votes_icon.xml b/app/src/main/res/animator/scale_votes_icon.xml new file mode 100644 index 0000000..1e2e200 --- /dev/null +++ b/app/src/main/res/animator/scale_votes_icon.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/upvote_icon.xml b/app/src/main/res/drawable/upvote_icon.xml new file mode 100644 index 0000000..8f17949 --- /dev/null +++ b/app/src/main/res/drawable/upvote_icon.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/answer_layout.xml b/app/src/main/res/layout/answer_layout.xml index e7aaa65..fa0a675 100644 --- a/app/src/main/res/layout/answer_layout.xml +++ b/app/src/main/res/layout/answer_layout.xml @@ -100,15 +100,17 @@ app:layout_constraintStart_toStartOf="@+id/tv_author_name" app:layout_constraintTop_toBottomOf="@+id/iv_dp_author" /> - + app:layout_constraintTop_toBottomOf="@id/author_answer_description_2" /> + app:layout_constraintBottom_toBottomOf="@id/cb_upvote" + app:layout_constraintStart_toEndOf="@id/cb_upvote" + app:layout_constraintTop_toTopOf="@id/cb_upvote" /> - diff --git a/app/src/main/res/layout/doubt_layout.xml b/app/src/main/res/layout/doubt_layout.xml index 4a0324e..c44f2e2 100644 --- a/app/src/main/res/layout/doubt_layout.xml +++ b/app/src/main/res/layout/doubt_layout.xml @@ -136,7 +136,8 @@ android:textColor="@color/grey" android:textSize="12dp" app:layout_constraintStart_toStartOf="@id/iv_votes" - app:layout_constraintTop_toBottomOf="@id/iv_votes" /> + app:layout_constraintTop_toBottomOf="@id/iv_votes" + tools:text="Related to : attendance, marks, teachers" /> - + /> - + android:backgroundTint="@color/grey" + android:button="@null" + android:checked="false" + app:layout_constraintStart_toEndOf="@id/cb_upvotes" + app:layout_constraintTop_toTopOf="@id/cb_upvotes" />