Commit cc85b698 by likorn

WIP save timerFragment state

parent 528f6dd0
......@@ -3,6 +3,8 @@ package com.paktalin.quickmax
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.paktalin.quickmax.answers.Answer
import com.paktalin.quickmax.answers.AnswerSet
import kotlinx.android.synthetic.main.activity_task.*
......@@ -14,6 +16,8 @@ class TaskActivity : AppCompatActivity() {
private var millisToSolve: Long = 4000
private var numDigits: Int = 3
private var timerFragment: Fragment? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
......@@ -26,12 +30,18 @@ class TaskActivity : AppCompatActivity() {
)
)
}
if (savedInstanceState != null)
timerFragment = supportFragmentManager.getFragment(savedInstanceState, "timer_fragment")
else {
retrieveExtras()
startNewRound()
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
supportFragmentManager.putFragment(outState, "timer_fragment", timerFragment!!)
// TODO save selection
// TODO save animation state
// TODO save timer
......@@ -44,7 +54,13 @@ class TaskActivity : AppCompatActivity() {
numDigits,
listOf(card_left_top, card_right_top, card_left_bottom, card_right_bottom)
)
addTimerFragment(supportFragmentManager, millisToSolve)
if (timerFragment == null)
timerFragment = TimerFragment().apply {
arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) }
}
supportFragmentManager.commit(true) {
replace(R.id.fragment_timer, timerFragment!!, "timer_fragment")
}
setUpCards()
removeButtonNextFragment(supportFragmentManager)
}
......
......@@ -16,11 +16,14 @@ import androidx.fragment.app.Fragment
class TimerFragment: Fragment() {
private lateinit var timer: CountDownTimer
private var millisToSolve: Long = 0
private val interval: Long = 1000
private lateinit var tvResponse: TextView
private lateinit var colorAnimation: ValueAnimator
private var millisToSolve: Long = 0
private val interval: Long = 1000
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
millisToSolve = arguments!!.getLong("millis_to_solve")
......@@ -29,15 +32,26 @@ class TimerFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.gradient_timer, container, false)
tvResponse = view!!.findViewById(R.id.tv_response)
if (savedInstanceState != null)
restoreState(savedInstanceState)
else {
initTimer()
initColorAnimation()
}
return view
}
override fun onStart() {
super.onStart()
timer.start()
colorAnimation.start()
// colorAnimation.start()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putLong("millis_to_solve", millisToSolve)
}
fun cancelCorrect() {
......@@ -50,6 +64,11 @@ class TimerFragment: Fragment() {
tvResponse.text = resources.getString(R.string.response_wrong)
}
private fun restoreState(savedInstanceState: Bundle) {
millisToSolve = savedInstanceState.getLong("millis_to_solve")
initTimer()
}
private fun cancel() {
timer.cancel()
colorAnimation.cancel()
......@@ -59,16 +78,19 @@ class TimerFragment: Fragment() {
private fun initTimer() {
timer = object : CountDownTimer(millisToSolve, interval) {
override fun onTick(millisUntilFinished: Long) {
millisToSolve = millisUntilFinished
tvResponse.text = (millisUntilFinished / interval).toString()
}
override fun onFinish() {
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize(resources) )
if (isAdded) {
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize(resources))
tvResponse.text = resources.getString(R.string.time_is_over)
(activity as TaskActivity).onTimeOver()
}
}
}
}
private fun initColorAnimation() {
val colorFrom = Color.TRANSPARENT
......
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