Commit 94774942 by likorn

Refactoring in AnswerSet and Fragment

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