Commit c183bc06 by likorn

Polished the TimerFragment logic

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