Commit bc85a28a by likorn

WIP saving AnswerFragment state: values saved

parent bfcc0b31
...@@ -2,28 +2,33 @@ package com.paktalin.quickmax.task.model ...@@ -2,28 +2,33 @@ package com.paktalin.quickmax.task.model
import com.paktalin.quickmax.task.ui.AnswerCardView import com.paktalin.quickmax.task.ui.AnswerCardView
class AnswerSet(numDigits: Int, cards: List<AnswerCardView>): Iterable<Answer> { private const val NUM_ANSWERS = 4
private val numAnswers = 4
class AnswerSet: Iterable<Answer> {
private lateinit var correctAnswer: Answer private lateinit var correctAnswer: Answer
val answers: MutableList<Answer> = mutableListOf() val answers: MutableList<Answer> = mutableListOf()
init {
val randomNumbers = constructor(numDigits: Int, cards: List<AnswerCardView>) {
generateNRandomNumbers(numDigits, numAnswers) val randomNumbers = generateNRandomNumbers(numDigits, NUM_ANSWERS)
for (i in 0 until numAnswers) for (i in 0 until NUM_ANSWERS)
answers.add( answers.add(Answer(cards[i], randomNumbers.elementAt(i)))
Answer( setCorrectAnswer()
cards[i], }
randomNumbers.elementAt(i)
) constructor(answerValues: IntArray, cards: List<AnswerCardView>) {
) for (i in answerValues.indices)
answers.add(Answer(cards[i], answerValues[i]))
val correctAnswerNum = setCorrectAnswer()
findSecondMax(answers.map { answer -> answer.value }) }
answers.forEach { n ->
n.correct = n.value == correctAnswerNum private fun setCorrectAnswer() {
if (n.correct) val correctAnswerNum = findSecondMax(answers.map { answer -> answer.value })
correctAnswer = n answers.forEach { answer ->
if (answer.value == correctAnswerNum) {
answer.correct
correctAnswer = answer
}
} }
} }
......
...@@ -17,6 +17,8 @@ class AnswersFragment : Fragment() { ...@@ -17,6 +17,8 @@ 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")
...@@ -29,31 +31,44 @@ class AnswersFragment : Fragment() { ...@@ -29,31 +31,44 @@ class AnswersFragment : Fragment() {
): View? { ): View? {
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)
restoreState(savedInstanceState)
else {
answerSet = AnswerSet(numDigits, cards)
startNewRound()
}
isReady = true isReady = true
startNewRound() startNewRound()
return view return view
} }
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putIntArray("answers", answerSet.answers.map { answer -> answer.value }.toIntArray())
}
fun startNewRound() { fun startNewRound() {
if (isReady) { 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 -> answerSet.forEach { answer ->
answer.card.setOnCheckedChangeListener { _, isChecked -> answer.card
if (isChecked) processAnswer(answer) .apply { setOnCheckedChangeListener { _, isChecked -> if (isChecked) processAnswer(answer) } }
} .apply { initial(context!!, answer.value) }
answer.card.initial(context!!, answer.value)
} }
} }
} }
private fun restoreState(savedInstanceState: Bundle) {
answerSet = AnswerSet(savedInstanceState.getIntArray("answers")!!, cards)
}
private fun processAnswer(answer: Answer) { private fun processAnswer(answer: Answer) {
if (answer.correct) { if (answer.correct) {
answer.card.markCorrect(context!!) answer.card.markCorrect(context!!)
......
...@@ -35,11 +35,7 @@ class TaskActivity : AppCompatActivity() { ...@@ -35,11 +35,7 @@ class TaskActivity : AppCompatActivity() {
} }
if (savedInstanceState != null) { if (savedInstanceState != null) {
timerFragment = supportFragmentManager.getFragment( restoreState(savedInstanceState)
savedInstanceState,
"timer_fragment"
) as TimerFragment
answersFragment = supportFragmentManager.getFragment(savedInstanceState, "answers_fragment") as AnswersFragment
} else { } else {
retrieveExtras() retrieveExtras()
timerFragment = TimerFragment() timerFragment = TimerFragment()
...@@ -54,7 +50,6 @@ class TaskActivity : AppCompatActivity() { ...@@ -54,7 +50,6 @@ class TaskActivity : AppCompatActivity() {
} }
startNewRound() startNewRound()
} }
} }
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
...@@ -71,6 +66,14 @@ class TaskActivity : AppCompatActivity() { ...@@ -71,6 +66,14 @@ class TaskActivity : AppCompatActivity() {
removeButtonNextFragment(supportFragmentManager) removeButtonNextFragment(supportFragmentManager)
} }
private fun restoreState(savedInstanceState: Bundle) {
timerFragment = supportFragmentManager.getFragment(
savedInstanceState,
"timer_fragment"
) as TimerFragment
answersFragment = supportFragmentManager.getFragment(savedInstanceState, "answers_fragment") as AnswersFragment
}
private fun retrieveExtras() { private fun retrieveExtras() {
numDigits = intent.getIntExtra("num_digits", 3) numDigits = intent.getIntExtra("num_digits", 3)
millisToSolve = 1000 * intent.getIntExtra("sec_to_solve", 4).toLong() millisToSolve = 1000 * intent.getIntExtra("sec_to_solve", 4).toLong()
......
...@@ -98,20 +98,6 @@ class TimerFragment : Fragment() { ...@@ -98,20 +98,6 @@ class TimerFragment : Fragment() {
colorAnimation?.cancel() colorAnimation?.cancel()
} }
private fun setResult() {
if (isAdded) {
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 fun restoreState(savedInstanceState: Bundle) { private fun restoreState(savedInstanceState: Bundle) {
state = State.valueOf(savedInstanceState.getString(KEY_STATE)!!) state = State.valueOf(savedInstanceState.getString(KEY_STATE)!!)
millisToSolve = savedInstanceState.getLong(KEY_MILLIS_TO_SOLVE) millisToSolve = savedInstanceState.getLong(KEY_MILLIS_TO_SOLVE)
...@@ -125,6 +111,19 @@ class TimerFragment : Fragment() { ...@@ -125,6 +111,19 @@ class TimerFragment : Fragment() {
} }
} }
private fun setResult() {
if (isAdded) {
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 fun initTimer(toSolve: Long): CountDownTimer { private fun initTimer(toSolve: Long): CountDownTimer {
return object : CountDownTimer(toSolve, COUNTDOWN_INTERVAL) { return object : CountDownTimer(toSolve, COUNTDOWN_INTERVAL) {
override fun onTick(millisUntilFinished: Long) { override fun onTick(millisUntilFinished: Long) {
......
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
app:contentPaddingLeft="16dp" app:contentPaddingLeft="16dp"
app:contentPaddingRight="16dp" app:contentPaddingRight="16dp"
app:contentPaddingTop="40dp" app:contentPaddingTop="40dp"
app:layout_constraintBottom_toBottomOf="@+id/layout_answers" app:layout_constraintBottom_toBottomOf="@+id/container_answers"
app:layout_constraintEnd_toStartOf="@+id/guideline" app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_chainStyle="packed"> app:layout_constraintVertical_chainStyle="packed">
...@@ -66,8 +66,9 @@ ...@@ -66,8 +66,9 @@
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_answers" <FrameLayout
android:id="@+id/container_answers"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="200dp" android:layout_height="200dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
...@@ -75,56 +76,6 @@ ...@@ -75,56 +76,6 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline" app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent"/>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_left_top"
style="@style/MyCard"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toTopOf="@+id/card_left_bottom"
app:layout_constraintEnd_toStartOf="@+id/card_right_top"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_right_top"
style="@style/MyCard"
app:layout_constraintBottom_toTopOf="@id/card_right_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/card_left_top"
app:layout_constraintTop_toTopOf="parent">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_left_bottom"
style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/card_right_bottom"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_left_top">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_right_bottom"
style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/card_left_bottom"
app:layout_constraintTop_toBottomOf="@+id/card_right_top">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -82,73 +82,4 @@ ...@@ -82,73 +82,4 @@
app:layout_constraintVertical_bias="0.5" app:layout_constraintVertical_bias="0.5"
app:layout_constraintVertical_chainStyle="packed" /> app:layout_constraintVertical_chainStyle="packed" />
<!--<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_answers"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cv_task"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintVertical_chainStyle="packed">
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_left_top"
style="@style/MyCard"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toTopOf="@+id/card_left_bottom"
app:layout_constraintEnd_toStartOf="@+id/card_right_top"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_right_top"
style="@style/MyCard"
app:layout_constraintBottom_toTopOf="@id/card_right_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/card_left_top"
app:layout_constraintTop_toTopOf="parent">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_left_bottom"
style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/card_right_bottom"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_left_top">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
<com.paktalin.quickmax.task.ui.AnswerCardView
android:id="@+id/card_right_bottom"
style="@style/MyCard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/card_left_bottom"
app:layout_constraintTop_toBottomOf="@+id/card_right_top">
<TextView style="@style/AnswerCardText" />
</com.paktalin.quickmax.task.ui.AnswerCardView>
</androidx.constraintlayout.widget.ConstraintLayout>-->
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ 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