Commit dd87465c by likorn

WIP saving AnswerFragment state

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