Commit bc85a28a by likorn

WIP saving AnswerFragment state: values saved

parent bfcc0b31
......@@ -2,28 +2,33 @@ package com.paktalin.quickmax.task.model
import com.paktalin.quickmax.task.ui.AnswerCardView
class AnswerSet(numDigits: Int, cards: List<AnswerCardView>): Iterable<Answer> {
private val numAnswers = 4
private const val NUM_ANSWERS = 4
class AnswerSet: Iterable<Answer> {
private lateinit var correctAnswer: Answer
val answers: MutableList<Answer> = mutableListOf()
init {
val randomNumbers =
generateNRandomNumbers(numDigits, numAnswers)
for (i in 0 until numAnswers)
answers.add(
Answer(
cards[i],
randomNumbers.elementAt(i)
)
)
val correctAnswerNum =
findSecondMax(answers.map { answer -> answer.value })
answers.forEach { n ->
n.correct = n.value == correctAnswerNum
if (n.correct)
correctAnswer = n
constructor(numDigits: Int, cards: List<AnswerCardView>) {
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>) {
for (i in answerValues.indices)
answers.add(Answer(cards[i], answerValues[i]))
setCorrectAnswer()
}
private fun setCorrectAnswer() {
val correctAnswerNum = findSecondMax(answers.map { answer -> answer.value })
answers.forEach { answer ->
if (answer.value == correctAnswerNum) {
answer.correct
correctAnswer = answer
}
}
}
......
......@@ -17,6 +17,8 @@ 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")
......@@ -29,29 +31,42 @@ class AnswersFragment : Fragment() {
): View? {
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()
}
isReady = true
startNewRound()
return view
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putIntArray("answers", answerSet.answers.map { answer -> answer.value }.toIntArray())
}
fun startNewRound() {
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 ->
answer.card.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) processAnswer(answer)
answer.card
.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) {
......
......@@ -35,11 +35,7 @@ class TaskActivity : AppCompatActivity() {
}
if (savedInstanceState != null) {
timerFragment = supportFragmentManager.getFragment(
savedInstanceState,
"timer_fragment"
) as TimerFragment
answersFragment = supportFragmentManager.getFragment(savedInstanceState, "answers_fragment") as AnswersFragment
restoreState(savedInstanceState)
} else {
retrieveExtras()
timerFragment = TimerFragment()
......@@ -54,7 +50,6 @@ class TaskActivity : AppCompatActivity() {
}
startNewRound()
}
}
override fun onSaveInstanceState(outState: Bundle) {
......@@ -71,6 +66,14 @@ class TaskActivity : AppCompatActivity() {
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() {
numDigits = intent.getIntExtra("num_digits", 3)
millisToSolve = 1000 * intent.getIntExtra("sec_to_solve", 4).toLong()
......
......@@ -98,20 +98,6 @@ class TimerFragment : Fragment() {
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) {
state = State.valueOf(savedInstanceState.getString(KEY_STATE)!!)
millisToSolve = savedInstanceState.getLong(KEY_MILLIS_TO_SOLVE)
......@@ -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 {
return object : CountDownTimer(toSolve, COUNTDOWN_INTERVAL) {
override fun onTick(millisUntilFinished: Long) {
......
......@@ -48,7 +48,7 @@
app:contentPaddingLeft="16dp"
app:contentPaddingRight="16dp"
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_constraintStart_toStartOf="parent"
app:layout_constraintVertical_chainStyle="packed">
......@@ -66,8 +66,9 @@
</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_height="200dp"
android:layout_marginStart="8dp"
......@@ -75,56 +76,6 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
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>
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -82,73 +82,4 @@
app:layout_constraintVertical_bias="0.5"
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>
\ 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