Commit 6df10aa4 by likorn

Refactoring in TimeFragment

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