Commit 538cc042 by likorn

Put styling methods into extended MaterialCardView

parent 39bb884d
package com.example.quickmax
import android.content.Context
import android.graphics.Color
class CardStyle private constructor(var isCheckable: Boolean,
var isEnabled: Boolean,
var isChecked: Boolean,
var backgroundColor: Int,
var textColor: Int) {
companion object {
fun initial(context: Context): CardStyle {
return CardStyle(
isCheckable = true,
isEnabled = true,
isChecked = false,
backgroundColor = Color.WHITE,
textColor = color(context, R.color.transparent_black))
}
fun correct(context: Context): CardStyle {
return CardStyle(
isCheckable = true,
isEnabled = true,
isChecked = true,
backgroundColor = color(context, R.color.colorAccent),
textColor = color(context, R.color.transparent_black))
}
/* fun wrong(context: Context): CardStyle {
return CardStyle(
isCheckable = true,
isEnabled = true,
isChecked = true,
backgroundColor = color(context, R.color.colorAccent),
textColor = color(context, R.color.transparent_black))
}*/
}
}
\ No newline at end of file
package com.example.quickmax
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import com.google.android.material.card.MaterialCardView
class MyMaterialCard : MaterialCardView {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
fun clean(context: Context) {
isCheckable = true
isEnabled = true
isChecked = false
setCardBackgroundColor(Color.WHITE)
getTextView(this).setTextColor(color(context, R.color.transparent_black))
}
fun markCorrect() {
setCardBackgroundColor(color(context, R.color.colorAccent))
}
fun markWrong(context: Context) {
setCardBackgroundColor(color(context, R.color.colorPrimary))
getTextView(this).setTextColor(Color.WHITE)
}
fun disable() {
isCheckable = false
isEnabled = false
}
}
\ No newline at end of file
...@@ -13,7 +13,6 @@ import androidx.appcompat.app.AppCompatActivity ...@@ -13,7 +13,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.example.quickmax.answers.Answer import com.example.quickmax.answers.Answer
import com.example.quickmax.answers.AnswerSet import com.example.quickmax.answers.AnswerSet
import com.google.android.material.card.MaterialCardView
import kotlinx.android.synthetic.main.activity_task.* import kotlinx.android.synthetic.main.activity_task.*
class TaskActivity : AppCompatActivity() { class TaskActivity : AppCompatActivity() {
...@@ -50,7 +49,7 @@ class TaskActivity : AppCompatActivity() { ...@@ -50,7 +49,7 @@ class TaskActivity : AppCompatActivity() {
answer.card.setOnClickListener { answer.card.isChecked = true } answer.card.setOnClickListener { answer.card.isChecked = true }
answer.card.setOnCheckedChangeListener { _, isChecked -> if (isChecked) processAnswer(answer) } answer.card.setOnCheckedChangeListener { _, isChecked -> if (isChecked) processAnswer(answer) }
getTextView(answer.card).text = answer.value.toString() getTextView(answer.card).text = answer.value.toString()
styleCard(answer.card, CardStyle.initial(this)) answer.card.clean(this@TaskActivity)
} }
btn_back.setOnClickListener { startActivity(Intent(this@TaskActivity, MainActivity::class.java)) } btn_back.setOnClickListener { startActivity(Intent(this@TaskActivity, MainActivity::class.java)) }
btn_next.apply { btn_next.apply {
...@@ -59,14 +58,6 @@ class TaskActivity : AppCompatActivity() { ...@@ -59,14 +58,6 @@ class TaskActivity : AppCompatActivity() {
} }
} }
private fun styleCard(card: MaterialCardView, style: CardStyle) {
card.isCheckable = style.isCheckable
card.isEnabled = style.isEnabled
card.isChecked = style.isChecked
card.setCardBackgroundColor(style.backgroundColor)
getTextView(card).setTextColor(style.textColor)
}
private fun processAnswer(answer: Answer) { private fun processAnswer(answer: Answer) {
timer.cancel() timer.cancel()
colorAnimation.cancel() colorAnimation.cancel()
...@@ -79,25 +70,20 @@ class TaskActivity : AppCompatActivity() { ...@@ -79,25 +70,20 @@ class TaskActivity : AppCompatActivity() {
btn_next.visibility = View.VISIBLE btn_next.visibility = View.VISIBLE
if (answer.correct) { if (answer.correct) {
styleCard(answer.card, CardStyle.correct(this)) answer.card.markCorrect()
tv_response.text = resources.getString(R.string.response_correct) tv_response.text = resources.getString(R.string.response_correct)
answer.card.setCardBackgroundColor(color(this, R.color.colorAccent)) btn_next.backgroundTintList = ContextCompat.getColorStateList(this, R.color.colorAccent)
// btn_next.backgroundTintList = ContextCompat.getColorStateList(this, R.color.colorAccent)
btn_next.setTextColor(color(this, R.color.transparent_dark_black)) btn_next.setTextColor(color(this, R.color.transparent_dark_black))
} else { } else {
answer.card.markWrong(this@TaskActivity)
tv_response.text = resources.getString(R.string.response_wrong) tv_response.text = resources.getString(R.string.response_wrong)
answer.card.setCardBackgroundColor(color(this, R.color.colorPrimary))
btn_next.backgroundTintList = ContextCompat.getColorStateList(this, (R.color.colorPrimary)) btn_next.backgroundTintList = ContextCompat.getColorStateList(this, (R.color.colorPrimary))
btn_next.setTextColor(Color.WHITE) btn_next.setTextColor(Color.WHITE)
getTextView(answer.card).setTextColor(Color.WHITE)
} }
} }
private fun disableCards() { private fun disableCards() {
for (answer in answerSet) { answerSet.forEach { answer -> answer.card.disable()}
answer.card.isCheckable = false
answer.card.isEnabled = false
}
} }
private fun startProgressBarAnimation() { private fun startProgressBarAnimation() {
......
package com.example.quickmax.answers package com.example.quickmax.answers
import com.google.android.material.card.MaterialCardView import com.example.quickmax.MyMaterialCard
class Answer(val card: MaterialCardView, val value: Int) { class Answer(val card: MyMaterialCard, val value: Int) {
var correct: Boolean = false var correct: Boolean = false
} }
\ No newline at end of file
package com.example.quickmax.answers package com.example.quickmax.answers
import com.google.android.material.card.MaterialCardView import com.example.quickmax.MyMaterialCard
class AnswerSet(numDigits: Int, cards: List<MaterialCardView>): Iterable<Answer> { class AnswerSet(numDigits: Int, cards: List<MyMaterialCard>): Iterable<Answer> {
private val numAnswers = 4 private val numAnswers = 4
private lateinit var correctAnswer: Answer private lateinit var correctAnswer: Answer
......
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
app:layout_constraintVertical_bias="0.5" app:layout_constraintVertical_bias="0.5"
app:layout_constraintVertical_chainStyle="packed"> app:layout_constraintVertical_chainStyle="packed">
<com.google.android.material.card.MaterialCardView <com.example.quickmax.MyMaterialCard
android:id="@+id/card_left_top" android:id="@+id/card_left_top"
style="@style/MyCard" style="@style/MyCard"
app:cardCornerRadius="10dp" app:cardCornerRadius="10dp"
...@@ -102,9 +102,9 @@ ...@@ -102,9 +102,9 @@
<TextView style="@style/AnswerCardText" /> <TextView style="@style/AnswerCardText" />
</com.google.android.material.card.MaterialCardView> </com.example.quickmax.MyMaterialCard>
<com.google.android.material.card.MaterialCardView <com.example.quickmax.MyMaterialCard
android:id="@+id/card_right_top" android:id="@+id/card_right_top"
style="@style/MyCard" style="@style/MyCard"
app:layout_constraintBottom_toTopOf="@id/card_right_bottom" app:layout_constraintBottom_toTopOf="@id/card_right_bottom"
...@@ -115,9 +115,9 @@ ...@@ -115,9 +115,9 @@
<TextView style="@style/AnswerCardText" /> <TextView style="@style/AnswerCardText" />
</com.google.android.material.card.MaterialCardView> </com.example.quickmax.MyMaterialCard>
<com.google.android.material.card.MaterialCardView <com.example.quickmax.MyMaterialCard
android:id="@+id/card_left_bottom" android:id="@+id/card_left_bottom"
style="@style/MyCard" style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
...@@ -128,9 +128,9 @@ ...@@ -128,9 +128,9 @@
<TextView style="@style/AnswerCardText" /> <TextView style="@style/AnswerCardText" />
</com.google.android.material.card.MaterialCardView> </com.example.quickmax.MyMaterialCard>
<com.google.android.material.card.MaterialCardView <com.example.quickmax.MyMaterialCard
android:id="@+id/card_right_bottom" android:id="@+id/card_right_bottom"
style="@style/MyCard" style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
...@@ -141,7 +141,7 @@ ...@@ -141,7 +141,7 @@
<TextView style="@style/AnswerCardText" /> <TextView style="@style/AnswerCardText" />
</com.google.android.material.card.MaterialCardView> </com.example.quickmax.MyMaterialCard>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment