TaskActivity.kt
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.example.quickmax
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import com.example.quickmax.answers.AnswerSet
import kotlinx.android.synthetic.main.activity_task.*
class TaskActivity : AppCompatActivity() {
private lateinit var answerSet: AnswerSet
private val timeToSolve:Long = 4000
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
answerSet = AnswerSet(3)
setUpAnswerButtons()
timer.start()
startProgressBarAnimation()
}
private fun setUpAnswerButtons() {
for (answer in answerSet) {
(findViewById<CardView>(answer.buttonId).getChildAt(0) as TextView).text = answer.value.toString()
findViewById<CardView>(answer.buttonId).setOnClickListener { processAnswer(answer.correct) }
}
}
private fun processAnswer(correct: Boolean) {
timer.cancel()
makeButtonsUncheckable()
val responseFragment = ResponseFragment.newInstance().also {
f -> f.arguments = Bundle().also {
b -> b.putBoolean("correct", correct) } }
supportFragmentManager
.beginTransaction()
.add(R.id.main_layout, responseFragment, "response")
.commitAllowingStateLoss()
}
private fun makeButtonsUncheckable() {
for (answer in answerSet) {
findViewById<CardView>(answer.buttonId).isClickable = false
}
}
private fun startProgressBarAnimation() {
val colorFrom = ContextCompat.getColor(this, R.color.colorPrimary)
val colorTo = Color.RED
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
colorAnimation.duration = timeToSolve
colorAnimation.addUpdateListener { animator ->
progress_bar.progressDrawable.setColorFilter(animator.animatedValue as Int,
android.graphics.PorterDuff.Mode.SRC_ATOP)
}
colorAnimation.start()
}
fun reload() {
val intent = intent
finish()
startActivity(intent)
}
private val timer = object : CountDownTimer(timeToSolve, 100) {
override fun onTick(millisUntilFinished: Long) {
val progress = ((timeToSolve - millisUntilFinished).toFloat() / timeToSolve) * 100
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
progress_bar.setProgress(progress.toInt(), true)
else
progress_bar.progress = progress.toInt()
}
override fun onFinish() {
cv_task.elevation = 0f
makeButtonsUncheckable()
supportFragmentManager
.beginTransaction()
.replace(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
.commitAllowingStateLoss()
}
}
}