Commit 24244d9e by likorn

NumDigits and SecondsToSolve are passed dynamically from MainActivity

parent 49eba251
......@@ -2,7 +2,10 @@ package com.example.quickmax
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity: AppCompatActivity() {
......@@ -16,8 +19,19 @@ class MainActivity: AppCompatActivity() {
}
seek_bar.setMinStartValue(4f).apply()
card_3_digits.setOnClickListener {
startActivity(Intent(this, TaskActivity::class.java))
card_2_digits.setOnClickListener(cardOnClickListener)
card_3_digits.setOnClickListener(cardOnClickListener)
card_4_digits.setOnClickListener(cardOnClickListener)
}
private val cardOnClickListener = View.OnClickListener { card ->
val numDigits = ((card as CardView).getChildAt(0) as TextView).text.toString().toInt()
val secToSolve = seek_bar.selectedMinValue.toInt()
val intent = Intent(this, TaskActivity::class.java).also { i ->
i.putExtra("num_digits", numDigits)
i.putExtra("sec_to_solve", secToSolve)
}
startActivity(intent)
}
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ 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
......@@ -16,17 +15,28 @@ import kotlinx.android.synthetic.main.activity_task.*
class TaskActivity : AppCompatActivity() {
private lateinit var answerSet: AnswerSet
private val timeToSolve:Long = 4000
private var millisToSolve: Long = 4000
private lateinit var timer: CountDownTimer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
answerSet = AnswerSet(3)
val numDigits = intent.getIntExtra("num_digits", 3)
millisToSolve = 1000 * intent.getIntExtra("sec_to_solve", 4).toLong()
initTimer()
answerSet = AnswerSet(numDigits)
setUpAnswerButtons()
timer.start()
startProgressBarAnimation()
}
fun reload() {
val intent = intent
finish()
startActivity(intent)
}
private fun setUpAnswerButtons() {
for (answer in answerSet) {
(findViewById<CardView>(answer.buttonId).getChildAt(0) as TextView).text = answer.value.toString()
......@@ -58,7 +68,7 @@ class TaskActivity : AppCompatActivity() {
val colorFrom = Color.TRANSPARENT
val colorTo = ContextCompat.getColor(this, R.color.transparent_red)
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
colorAnimation.duration = timeToSolve
colorAnimation.duration = millisToSolve
colorAnimation.addUpdateListener { animator ->
layout_gradient.background.setColorFilter(animator.animatedValue as Int,
android.graphics.PorterDuff.Mode.SRC_ATOP)
......@@ -66,24 +76,20 @@ class TaskActivity : AppCompatActivity() {
colorAnimation.start()
}
fun reload() {
val intent = intent
finish()
startActivity(intent)
}
private val timer = object : CountDownTimer(timeToSolve, 1000) {
override fun onTick(millisUntilFinished: Long) {
tv_timer.text = (millisUntilFinished/1000).toString()
}
private fun initTimer() {
timer = object : CountDownTimer(millisToSolve, 1000) {
override fun onTick(millisUntilFinished: Long) {
tv_timer.text = (millisUntilFinished/1000).toString()
}
override fun onFinish() {
cv_task.elevation = 0f
makeButtonsUncheckable()
supportFragmentManager
.beginTransaction()
.replace(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
.commitAllowingStateLoss()
override fun onFinish() {
cv_task.elevation = 0f
makeButtonsUncheckable()
supportFragmentManager
.beginTransaction()
.replace(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
.commitAllowingStateLoss()
}
}
}
}
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