Commit 6df10aa4 by likorn

Refactoring in TimeFragment

parent 5456656a
......@@ -16,7 +16,7 @@ class TaskActivity : AppCompatActivity() {
private var millisToSolve: Long = 4000
private var numDigits: Int = 3
private var timerFragment: Fragment? = null
private lateinit var timerFragment: TimerFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -32,8 +32,11 @@ class TaskActivity : AppCompatActivity() {
}
if (savedInstanceState != null)
timerFragment = supportFragmentManager.getFragment(savedInstanceState, "timer_fragment")
timerFragment = supportFragmentManager.getFragment(savedInstanceState, "timer_fragment") as TimerFragment
else {
timerFragment = TimerFragment().apply {
arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) }
}
retrieveExtras()
startNewRound()
}
......@@ -54,12 +57,8 @@ class TaskActivity : AppCompatActivity() {
numDigits,
listOf(card_left_top, card_right_top, card_left_bottom, card_right_bottom)
)
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")
replace(R.id.fragment_timer, timerFragment, "timer_fragment")
}
setUpCards()
removeButtonNextFragment(supportFragmentManager)
......@@ -82,11 +81,11 @@ class TaskActivity : AppCompatActivity() {
private fun processAnswer(answer: Answer) {
if (answer.correct) {
answer.card.markCorrect(this@TaskActivity)
(supportFragmentManager.findFragmentByTag("timer_fragment") as TimerFragment).cancelCorrect()
timerFragment.cancel(State.CORRECT)
addButtonNextFragment(supportFragmentManager, true)
} else {
answer.card.markWrong(this@TaskActivity)
(supportFragmentManager.findFragmentByTag("timer_fragment") as TimerFragment).cancelWrong()
timerFragment.cancel(State.WRONG)
addButtonNextFragment(supportFragmentManager, false)
}
answerSet.forEach { answer -> answer.card.disable() }
......
......@@ -13,28 +13,37 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
private const val RESPONSE_CORRECT = "Correct!"
private const val RESPONSE_WRONG = "Wrong!"
private const val RESPONSE_TIME_OVER = "Time is over!"
private const val interval: Long = 1000
private const val KEY_MILLIS_TO_SOLVE = "millis_to_solve"
private const val KEY_COLOR_FROM = "color_from"
private const val KEY_STATE = "state"
enum class State(val response: String?) {
IN_PROGRESS(null),
CORRECT("Correct!"),
WRONG("Wrong!"),
TIME_IS_OVER("Time is over!")
}
class TimerFragment : Fragment() {
private lateinit var timer: CountDownTimer
private lateinit var tvResponse: TextView
private var colorAnimation: ValueAnimator? = null
private lateinit var state: State
private lateinit var mView: View
private var millisToSolve: Long = 0
private var colorFrom: Int = -1
private val interval: Long = 1000
private var finished: Boolean = false
private lateinit var mView: View
private var restoredColor: Int? = null
private var colorAnimation: ValueAnimator? = null
private var timer: CountDownTimer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
millisToSolve = arguments!!.getLong("millis_to_solve")
millisToSolve = arguments!!.getLong(KEY_MILLIS_TO_SOLVE)
colorFrom = Color.TRANSPARENT
state = State.IN_PROGRESS
}
override fun onCreateView(
......@@ -48,72 +57,58 @@ class TimerFragment : Fragment() {
if (savedInstanceState != null)
restoreState(savedInstanceState)
if (!finished) {
if (state == State.IN_PROGRESS) {
initTimer()
initColorAnimation()
}
return view
}
override fun onStart() {
super.onStart()
if (!finished) {
timer?.start()
colorAnimation?.start()
}
timer?.start()
colorAnimation?.start()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(KEY_STATE, state.name)
val text = tvResponse.text.toString()
if (text == RESPONSE_CORRECT || text == RESPONSE_WRONG || text == RESPONSE_TIME_OVER)
outState.putString("response_text", text)
else
outState.putLong("millis_to_solve", millisToSolve)
colorAnimation?.apply {
if (animatedValue != null)
restoredColor = animatedValue as Int
if (state == State.IN_PROGRESS)
outState.putLong(KEY_MILLIS_TO_SOLVE, millisToSolve)
colorAnimation?.let { animator ->
animator.animatedValue?.let {
value -> colorFrom = value as Int
}
}
restoredColor?.let { outState.putInt("color_from", it) }
outState.putInt(KEY_COLOR_FROM, colorFrom)
}
fun cancelCorrect() {
cancel()
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize(resources))
tvResponse.text = RESPONSE_CORRECT
}
fun cancelWrong() {
cancel()
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize(resources))
tvResponse.text = RESPONSE_WRONG
}
fun cancel(state: State) {
this.state = state
setResult()
timer?.cancel()
colorAnimation?.cancel() }
private fun restoreState(savedInstanceState: Bundle) {
val responseText = savedInstanceState.getString("response_text")
if (responseText != null) {
private fun setResult() {
if (isAdded) {
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize(resources))
tvResponse.text = responseText
restoredColor = savedInstanceState.getInt("color_from")
restoredColor?.let { setBackgroundFilter(it) }
finished = true
} else {
millisToSolve = savedInstanceState.getLong("millis_to_solve")
colorFrom = savedInstanceState.getInt("color_from")
tvResponse.text = state.response
}
}
private fun cancel() {
timer.cancel()
colorAnimation?.cancel()
}
private val setBackgroundFilter = {color: Int ->
if (isAdded) mView.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) }
private fun setBackgroundFilter(color: Int) {
if (isAdded)
mView.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
private fun restoreState(savedInstanceState: Bundle) {
state = State.valueOf(savedInstanceState.getString(KEY_STATE)!!)
colorFrom = savedInstanceState.getInt(KEY_COLOR_FROM)
if (state == State.IN_PROGRESS) {
millisToSolve = savedInstanceState.getLong(KEY_MILLIS_TO_SOLVE)
} else {
setResult()
setBackgroundFilter(colorFrom)
}
}
private fun initTimer() {
......@@ -124,12 +119,9 @@ class TimerFragment : Fragment() {
}
override fun onFinish() {
if (isAdded) {
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize(resources))
tvResponse.text = RESPONSE_TIME_OVER
(activity as TaskActivity).onTimeOver()
}
this@TimerFragment.state = State.TIME_IS_OVER
setResult()
if (isAdded) (activity as TaskActivity).onTimeOver()
}
}
}
......
......@@ -24,15 +24,6 @@ fun textSize(resources: Resources): Float {
return resources.getDimension(R.dimen.response_text_size) / screenDensity
}
fun addTimerFragment(supportFragmentManager: FragmentManager, millisToSolve: Long) {
val timerFragment = TimerFragment().apply {
arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) }
}
supportFragmentManager.commit(true) {
replace(R.id.fragment_timer, timerFragment, "timer_fragment")
}
}
fun addButtonNextFragment(supportFragmentManager: FragmentManager, correct: Boolean) {
val fragmentBtnNext = ButtonNextFragment().apply {
arguments = Bundle().apply { putBoolean("correct", correct) }
......
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