Commit cc85b698 by likorn

WIP save timerFragment state

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