Commit 94774942 by likorn

Refactoring in AnswerSet and Fragment

parent bc85a28a
package com.paktalin.quickmax.task.model
import android.view.View
import com.paktalin.quickmax.task.ui.AnswerCardView
import kotlinx.android.synthetic.main.fragment_answers.view.*
private const val NUM_ANSWERS = 4
class AnswerSet: Iterable<Answer> {
private lateinit var correctAnswer: Answer
val answers: MutableList<Answer> = mutableListOf()
constructor(numDigits: Int, cards: List<AnswerCardView>) {
constructor(numDigits: Int, view: View) {
val cards = cardsFromView(view)
val randomNumbers = generateNRandomNumbers(numDigits, NUM_ANSWERS)
for (i in 0 until NUM_ANSWERS)
answers.add(Answer(cards[i], randomNumbers.elementAt(i)))
setCorrectAnswer()
}
constructor(answerValues: IntArray, cards: List<AnswerCardView>) {
constructor(answerValues: IntArray, view: View) {
val cards = cardsFromView(view)
for (i in answerValues.indices)
answers.add(Answer(cards[i], answerValues[i]))
setCorrectAnswer()
......@@ -25,10 +28,7 @@ class AnswerSet: Iterable<Answer> {
private fun setCorrectAnswer() {
val correctAnswerNum = findSecondMax(answers.map { answer -> answer.value })
answers.forEach { answer ->
if (answer.value == correctAnswerNum) {
answer.correct
correctAnswer = answer
}
answer.correct = answer.value == correctAnswerNum
}
}
......@@ -36,4 +36,13 @@ class AnswerSet: Iterable<Answer> {
return answers.iterator()
}
private fun cardsFromView(view: View): List<AnswerCardView> {
return listOf(
view.card_left_top,
view.card_right_top,
view.card_left_bottom,
view.card_right_bottom
)
}
}
\ No newline at end of file
......@@ -17,8 +17,6 @@ class AnswersFragment : Fragment() {
private var numDigits: Int = 0
private var isReady: Boolean = false
private lateinit var cards: List<AnswerCardView>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
numDigits = arguments!!.getInt("num_digits")
......@@ -32,19 +30,10 @@ class AnswersFragment : Fragment() {
val view = inflater.inflate(R.layout.fragment_answers, container, false)
mView = view
cards = listOf(
mView.card_left_top,
mView.card_right_top,
mView.card_left_bottom,
mView.card_right_bottom
)
if (savedInstanceState != null)
restoreState(savedInstanceState)
else {
answerSet = AnswerSet(numDigits, cards)
startNewRound()
}
else
answerSet = AnswerSet(numDigits, mView)
isReady = true
startNewRound()
return view
......@@ -66,7 +55,8 @@ class AnswersFragment : Fragment() {
}
private fun restoreState(savedInstanceState: Bundle) {
answerSet = AnswerSet(savedInstanceState.getIntArray("answers")!!, cards)
val restoredAnswers = savedInstanceState.getIntArray("answers")!!
answerSet = AnswerSet(restoredAnswers, mView)
}
private fun processAnswer(answer: Answer) {
......
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