Commit c183bc06 by likorn

Polished the TimerFragment logic

parent d2679ecf
...@@ -64,7 +64,7 @@ class MainActivity : AppCompatActivity() { ...@@ -64,7 +64,7 @@ class MainActivity : AppCompatActivity() {
} }
private fun startTaskActivity() { private fun startTaskActivity() {
val intent = Intent(this, TaskActivity::class.java) val intent = Intent(this@MainActivity, TaskActivity::class.java)
.apply { putExtra("num_digits", numDigits) } .apply { putExtra("num_digits", numDigits) }
.apply { putExtra("sec_to_solve", secToSolve) } .apply { putExtra("sec_to_solve", secToSolve) }
startActivity(intent) startActivity(intent)
......
...@@ -20,9 +20,14 @@ fun color(context: Context, id: Int): Int { ...@@ -20,9 +20,14 @@ fun color(context: Context, id: Int): Int {
return ContextCompat.getColor(context, id) return ContextCompat.getColor(context, id)
} }
fun textSize(resources: Resources): Float { fun textSizeSmall(resources: Resources): Float {
val screenDensity = resources.displayMetrics.density val screenDensity = resources.displayMetrics.density
return resources.getDimension(R.dimen.response_text_size) / screenDensity return resources.getDimension(R.dimen.response_small) / screenDensity
}
fun textSizeLarge(resources: Resources): Float {
val screenDensity = resources.displayMetrics.density
return resources.getDimension(R.dimen.response_large) / screenDensity
} }
fun addButtonNextFragment(supportFragmentManager: FragmentManager, correct: Boolean) { fun addButtonNextFragment(supportFragmentManager: FragmentManager, correct: Boolean) {
......
...@@ -15,6 +15,7 @@ class AnswersFragment : Fragment() { ...@@ -15,6 +15,7 @@ class AnswersFragment : Fragment() {
private lateinit var answerSet: AnswerSet private lateinit var answerSet: AnswerSet
private lateinit var mView: View private lateinit var mView: View
private var numDigits: Int = 0 private var numDigits: Int = 0
private var isReady: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -28,9 +29,21 @@ class AnswersFragment : Fragment() { ...@@ -28,9 +29,21 @@ class AnswersFragment : Fragment() {
): View? { ): View? {
val view = inflater.inflate(R.layout.fragment_answers, container, false) val view = inflater.inflate(R.layout.fragment_answers, container, false)
mView = view mView = view
isReady = true
startNewRound()
return view
}
fun startNewRound() {
if (isReady) {
answerSet = AnswerSet( answerSet = AnswerSet(
numDigits, numDigits,
listOf(view.card_left_top, view.card_right_top, view.card_left_bottom, view.card_right_bottom) listOf(
mView.card_left_top,
mView.card_right_top,
mView.card_left_bottom,
mView.card_right_bottom
)
) )
answerSet.forEach { answer -> answerSet.forEach { answer ->
answer.card.setOnCheckedChangeListener { _, isChecked -> answer.card.setOnCheckedChangeListener { _, isChecked ->
...@@ -38,8 +51,7 @@ class AnswersFragment : Fragment() { ...@@ -38,8 +51,7 @@ class AnswersFragment : Fragment() {
} }
answer.card.initial(context!!, answer.value) answer.card.initial(context!!, answer.value)
} }
}
return view
} }
private fun processAnswer(answer: Answer) { private fun processAnswer(answer: Answer) {
......
...@@ -8,7 +8,6 @@ import com.paktalin.quickmax.MainActivity ...@@ -8,7 +8,6 @@ import com.paktalin.quickmax.MainActivity
import com.paktalin.quickmax.R import com.paktalin.quickmax.R
import com.paktalin.quickmax.addButtonNextFragment import com.paktalin.quickmax.addButtonNextFragment
import com.paktalin.quickmax.removeButtonNextFragment import com.paktalin.quickmax.removeButtonNextFragment
import com.paktalin.quickmax.task.model.Answer
import com.paktalin.quickmax.task.model.AnswerSet import com.paktalin.quickmax.task.model.AnswerSet
import kotlinx.android.synthetic.main.activity_task.* import kotlinx.android.synthetic.main.activity_task.*
...@@ -35,33 +34,40 @@ class TaskActivity : AppCompatActivity() { ...@@ -35,33 +34,40 @@ class TaskActivity : AppCompatActivity() {
) )
} }
if (savedInstanceState != null) if (savedInstanceState != null) {
timerFragment = supportFragmentManager.getFragment(savedInstanceState, "timer_fragment") as TimerFragment timerFragment = supportFragmentManager.getFragment(
else { savedInstanceState,
"timer_fragment"
) as TimerFragment
answersFragment = supportFragmentManager.getFragment(savedInstanceState, "answers_fragment") as AnswersFragment
} else {
retrieveExtras() retrieveExtras()
timerFragment = TimerFragment()
.apply { arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) } }
.apply { supportFragmentManager.commit(true) { add(R.id.container_timer, this@apply, "timer_fragment")} }
answersFragment = AnswersFragment().apply {
arguments = Bundle().apply { putInt("num_digits", numDigits) }
}
supportFragmentManager.commit(true) {
replace(R.id.container_answers, answersFragment, "answers_fragment")
}
startNewRound() startNewRound()
} }
} }
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
supportFragmentManager.putFragment(outState, "timer_fragment", timerFragment!!) supportFragmentManager.putFragment(outState, "timer_fragment", timerFragment)
supportFragmentManager.putFragment(outState, "answers_fragment", answersFragment)
// TODO save selection // TODO save selection
} }
internal fun startNewRound() { internal fun startNewRound() {
timerFragment = TimerFragment().apply { timerFragment.startNewRound()
arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) } answersFragment.startNewRound()
}
supportFragmentManager.commit(true) {
replace(R.id.container_timer, timerFragment, "timer_fragment")
}
answersFragment = AnswersFragment().apply {
arguments = Bundle().apply { putInt("num_digits", numDigits) }
}
supportFragmentManager.commit(true) {
replace(R.id.container_answers, answersFragment, "answers_fragment")
}
removeButtonNextFragment(supportFragmentManager) removeButtonNextFragment(supportFragmentManager)
} }
......
...@@ -10,11 +10,12 @@ import android.util.TypedValue ...@@ -10,11 +10,12 @@ import android.util.TypedValue
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.paktalin.quickmax.R import com.paktalin.quickmax.R
import com.paktalin.quickmax.color import com.paktalin.quickmax.color
import com.paktalin.quickmax.textSize import com.paktalin.quickmax.textSizeLarge
import com.paktalin.quickmax.textSizeSmall
import kotlinx.android.synthetic.main.fragment_timer.view.*
private const val interval: Long = 1000 private const val interval: Long = 1000
...@@ -23,6 +24,7 @@ private const val KEY_COLOR_FROM = "color_from" ...@@ -23,6 +24,7 @@ private const val KEY_COLOR_FROM = "color_from"
private const val KEY_STATE = "state" private const val KEY_STATE = "state"
enum class State(val response: String?) { enum class State(val response: String?) {
NONE(null),
IN_PROGRESS(null), IN_PROGRESS(null),
CORRECT("Correct!"), CORRECT("Correct!"),
WRONG("Wrong!"), WRONG("Wrong!"),
...@@ -31,7 +33,6 @@ enum class State(val response: String?) { ...@@ -31,7 +33,6 @@ enum class State(val response: String?) {
class TimerFragment : Fragment() { class TimerFragment : Fragment() {
private lateinit var tvResponse: TextView
private lateinit var state: State private lateinit var state: State
private lateinit var mView: View private lateinit var mView: View
...@@ -41,12 +42,13 @@ class TimerFragment : Fragment() { ...@@ -41,12 +42,13 @@ class TimerFragment : Fragment() {
private var colorAnimation: ValueAnimator? = null private var colorAnimation: ValueAnimator? = null
private var timer: CountDownTimer? = null private var timer: CountDownTimer? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
millisToSolve = arguments!!.getLong(KEY_MILLIS_TO_SOLVE) millisToSolve = arguments!!.getLong(KEY_MILLIS_TO_SOLVE)
colorFrom = Color.TRANSPARENT colorFrom = Color.TRANSPARENT
state = State.IN_PROGRESS state = State.NONE
} }
override fun onCreateView( override fun onCreateView(
...@@ -56,26 +58,14 @@ class TimerFragment : Fragment() { ...@@ -56,26 +58,14 @@ class TimerFragment : Fragment() {
): View? { ): View? {
val view = inflater.inflate(R.layout.fragment_timer, container, false) val view = inflater.inflate(R.layout.fragment_timer, container, false)
mView = view mView = view
tvResponse = mView.findViewById(R.id.tv_response)
if (savedInstanceState != null) if (savedInstanceState != null)
restoreState(savedInstanceState) restoreState(savedInstanceState)
if (state == State.IN_PROGRESS) { else
initTimer() startNewRound()
initColorAnimation()
} else {
setResult()
setBackgroundFilter(colorFrom)
}
return view return view
} }
override fun onStart() {
super.onStart()
timer?.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) outState.putString(KEY_STATE, state.name)
...@@ -89,6 +79,24 @@ class TimerFragment : Fragment() { ...@@ -89,6 +79,24 @@ class TimerFragment : Fragment() {
outState.putInt(KEY_COLOR_FROM, colorFrom) outState.putInt(KEY_COLOR_FROM, colorFrom)
} }
fun startNewRound() {
if (isAdded) {
state = State.IN_PROGRESS
mView.tv_response
.apply { text = "" }
.apply { setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeLarge(resources)) }
if (timer == null)
initTimer()
timer?.start()
if (colorAnimation == null)
initColorAnimation()
colorAnimation?.start()
}
}
fun cancel(state: State) { fun cancel(state: State) {
this.state = state this.state = state
setResult() setResult()
...@@ -97,27 +105,36 @@ class TimerFragment : Fragment() { ...@@ -97,27 +105,36 @@ class TimerFragment : Fragment() {
private fun setResult() { private fun setResult() {
if (isAdded) { if (isAdded) {
tvResponse.setTextSize(TypedValue.COMPLEX_UNIT_SP, mView.tv_response
textSize(resources) .apply { text = "" }
) .apply { setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall(resources)) }
tvResponse.text = state.response .apply { text = state.response }
} }
} }
private val setBackgroundFilter = {color: Int -> private val setBackgroundFilter = {color: Int ->
if (isAdded) mView.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) } if (isAdded) mView.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) }
private val startTimer = {}
private fun restoreState(savedInstanceState: Bundle) { private fun restoreState(savedInstanceState: Bundle) {
state = State.valueOf(savedInstanceState.getString(KEY_STATE)!!) state = State.valueOf(savedInstanceState.getString(KEY_STATE)!!)
colorFrom = savedInstanceState.getInt(KEY_COLOR_FROM) colorFrom = savedInstanceState.getInt(KEY_COLOR_FROM)
millisToSolve = savedInstanceState.getLong(KEY_MILLIS_TO_SOLVE, 0) millisToSolve = savedInstanceState.getLong(KEY_MILLIS_TO_SOLVE)
if (state == State.IN_PROGRESS)
startNewRound()
else {
setResult()
setBackgroundFilter(colorFrom)
}
} }
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 millisToSolve = millisUntilFinished
tvResponse.text = (millisUntilFinished / interval).toString() mView.tv_response.text = (millisUntilFinished / interval).toString()
} }
override fun onFinish() { override fun onFinish() {
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textSize="60sp" android:textSize="@dimen/response_large"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline2" app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
android:elevation="2dp" android:elevation="2dp"
android:gravity="center" android:gravity="center"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textSize="60sp" android:textSize="@dimen/response_large"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<dimen name="response_text_size">40sp</dimen> <dimen name="response_small">40sp</dimen>
<dimen name="response_large">60sp</dimen>
</resources> </resources>
\ No newline at end of file
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