Commit dd87465c by likorn

WIP saving AnswerFragment state

parent 94774942
package com.paktalin.quickmax.task.model package com.paktalin.quickmax.task.model
import android.content.Context
import android.view.View import android.view.View
import com.paktalin.quickmax.task.ui.AnswerCardView import com.paktalin.quickmax.task.ui.AnswerCardView
import kotlinx.android.synthetic.main.fragment_answers.view.* import kotlinx.android.synthetic.main.fragment_answers.view.*
...@@ -25,11 +26,19 @@ class AnswerSet: Iterable<Answer> { ...@@ -25,11 +26,19 @@ class AnswerSet: Iterable<Answer> {
setCorrectAnswer() setCorrectAnswer()
} }
private fun setCorrectAnswer() { fun setOnCheckedChangeListener(onChecked: (answer: Answer) -> Unit) {
val correctAnswerNum = findSecondMax(answers.map { answer -> answer.value })
answers.forEach { answer -> answers.forEach { answer ->
answer.correct = answer.value == correctAnswerNum answer.card.setOnCheckedChangeListener { _, isChecked -> if (isChecked) onChecked(answer) }
}
}
fun setInitialStyle(context: Context) {
answers.forEach { answer -> answer.card.initial(context, answer.value) }
} }
private fun setCorrectAnswer() {
val correctAnswerNum = findSecondMax(answers.map { answer -> answer.value })
answers.find { answer -> answer.value == correctAnswerNum }!!.correct = true
} }
override fun iterator(): Iterator<Answer> { override fun iterator(): Iterator<Answer> {
......
...@@ -6,16 +6,13 @@ import android.view.View ...@@ -6,16 +6,13 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.paktalin.quickmax.R import com.paktalin.quickmax.R
import com.paktalin.quickmax.addButtonNextFragment
import com.paktalin.quickmax.task.model.Answer 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.fragment_answers.view.*
class AnswersFragment : Fragment() { 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)
...@@ -33,8 +30,6 @@ class AnswersFragment : Fragment() { ...@@ -33,8 +30,6 @@ class AnswersFragment : Fragment() {
if (savedInstanceState != null) if (savedInstanceState != null)
restoreState(savedInstanceState) restoreState(savedInstanceState)
else else
answerSet = AnswerSet(numDigits, mView)
isReady = true
startNewRound() startNewRound()
return view return view
} }
...@@ -44,19 +39,21 @@ class AnswersFragment : Fragment() { ...@@ -44,19 +39,21 @@ class AnswersFragment : Fragment() {
outState.putIntArray("answers", answerSet.answers.map { answer -> answer.value }.toIntArray()) outState.putIntArray("answers", answerSet.answers.map { answer -> answer.value }.toIntArray())
} }
fun startNewRound() { fun startNewRound(answerSet: AnswerSet? = null) {
if (isReady) { if (isAdded) {
answerSet.forEach { answer -> this.answerSet = answerSet ?: AnswerSet(numDigits, mView)
answer.card .apply { setOnCheckedChangeListener { answer -> processAnswer(answer) } }
.apply { setOnCheckedChangeListener { _, isChecked -> if (isChecked) processAnswer(answer) } } .apply { setInitialStyle(mView.context) }
.apply { initial(context!!, answer.value) }
} }
} }
fun disableCards() {
answerSet.forEach { answer -> answer.card.disable() }
} }
private fun restoreState(savedInstanceState: Bundle) { private fun restoreState(savedInstanceState: Bundle) {
val restoredAnswers = savedInstanceState.getIntArray("answers")!! val restoredAnswers = savedInstanceState.getIntArray("answers")!!
answerSet = AnswerSet(restoredAnswers, mView) startNewRound(AnswerSet(restoredAnswers, mView))
} }
private fun processAnswer(answer: Answer) { private fun processAnswer(answer: Answer) {
...@@ -67,6 +64,6 @@ class AnswersFragment : Fragment() { ...@@ -67,6 +64,6 @@ class AnswersFragment : Fragment() {
answer.card.markWrong(context!!) answer.card.markWrong(context!!)
(activity as TaskActivity).onResponseWrong() (activity as TaskActivity).onResponseWrong()
} }
answerSet.forEach { answer -> answer.card.disable() } disableCards()
} }
} }
\ No newline at end of file
...@@ -14,7 +14,6 @@ import kotlinx.android.synthetic.main.activity_task.* ...@@ -14,7 +14,6 @@ import kotlinx.android.synthetic.main.activity_task.*
// TODO save state // TODO save state
class TaskActivity : AppCompatActivity() { class TaskActivity : AppCompatActivity() {
private lateinit var answerSet: AnswerSet
private var millisToSolve: Long = 4000 private var millisToSolve: Long = 4000
private var numDigits: Int = 3 private var numDigits: Int = 3
...@@ -56,8 +55,6 @@ class TaskActivity : AppCompatActivity() { ...@@ -56,8 +55,6 @@ class TaskActivity : AppCompatActivity() {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
supportFragmentManager.putFragment(outState, "timer_fragment", timerFragment) supportFragmentManager.putFragment(outState, "timer_fragment", timerFragment)
supportFragmentManager.putFragment(outState, "answers_fragment", answersFragment) supportFragmentManager.putFragment(outState, "answers_fragment", answersFragment)
// TODO save selection
} }
internal fun startNewRound() { internal fun startNewRound() {
...@@ -91,6 +88,6 @@ class TaskActivity : AppCompatActivity() { ...@@ -91,6 +88,6 @@ class TaskActivity : AppCompatActivity() {
fun onTimeOver() { fun onTimeOver() {
addButtonNextFragment(supportFragmentManager, false) addButtonNextFragment(supportFragmentManager, false)
// answerSet.forEach { answer -> answer.card.disable() } answersFragment.disableCards()
} }
} }
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