Commit d2679ecf by likorn

Answers logic is moved to a fragment

parent 261ad107
......@@ -36,6 +36,8 @@ fun addButtonNextFragment(supportFragmentManager: FragmentManager, correct: Bool
fun removeButtonNextFragment(supportFragmentManager: FragmentManager) {
supportFragmentManager.findFragmentByTag("btn_next_fragment")?.let {fragment ->
supportFragmentManager.commit(true) { remove(fragment) }
supportFragmentManager.commit(true) {
remove(fragment)
}
}
}
\ No newline at end of file
package com.paktalin.quickmax.task.ui
import android.os.Bundle
import android.view.LayoutInflater
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
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
numDigits = arguments!!.getInt("num_digits")
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_answers, container, false)
mView = view
answerSet = AnswerSet(
numDigits,
listOf(view.card_left_top, view.card_right_top, view.card_left_bottom, view.card_right_bottom)
)
answerSet.forEach { answer ->
answer.card.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) processAnswer(answer)
}
answer.card.initial(context!!, answer.value)
}
return view
}
private fun processAnswer(answer: Answer) {
if (answer.correct) {
answer.card.markCorrect(context!!)
(activity as TaskActivity).onResponseCorrect()
} else {
answer.card.markWrong(context!!)
(activity as TaskActivity).onResponseWrong()
}
answerSet.forEach { answer -> answer.card.disable() }
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ class TaskActivity : AppCompatActivity() {
private var numDigits: Int = 3
private lateinit var timerFragment: TimerFragment
private lateinit var answersFragment: AnswersFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -37,9 +38,6 @@ class TaskActivity : AppCompatActivity() {
if (savedInstanceState != null)
timerFragment = supportFragmentManager.getFragment(savedInstanceState, "timer_fragment") as TimerFragment
else {
timerFragment = TimerFragment().apply {
arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) }
}
retrieveExtras()
startNewRound()
}
......@@ -49,21 +47,21 @@ class TaskActivity : AppCompatActivity() {
super.onSaveInstanceState(outState)
supportFragmentManager.putFragment(outState, "timer_fragment", timerFragment!!)
// TODO save selection
// TODO save animation state
// TODO save timer
// TODO save button next state
// TODO save
}
internal fun startNewRound() {
answerSet = AnswerSet(
numDigits,
listOf(card_left_top, card_right_top, card_left_bottom, card_right_bottom)
)
timerFragment = TimerFragment().apply {
arguments = Bundle().apply { putLong("millis_to_solve", millisToSolve) }
}
supportFragmentManager.commit(true) {
replace(R.id.fragment_timer, timerFragment, "timer_fragment")
replace(R.id.container_timer, timerFragment, "timer_fragment")
}
answersFragment = AnswersFragment().apply {
arguments = Bundle().apply { putInt("num_digits", numDigits) }
}
supportFragmentManager.commit(true) {
replace(R.id.container_answers, answersFragment, "answers_fragment")
}
setUpCards()
removeButtonNextFragment(supportFragmentManager)
}
......@@ -72,26 +70,14 @@ class TaskActivity : AppCompatActivity() {
millisToSolve = 1000 * intent.getIntExtra("sec_to_solve", 4).toLong()
}
private fun setUpCards() {
answerSet.forEach { answer ->
answer.card.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) processAnswer(answer)
}
answer.card.initial(this@TaskActivity, answer.value)
}
fun onResponseWrong() {
timerFragment.cancel(State.WRONG)
addButtonNextFragment(supportFragmentManager, false)
}
private fun processAnswer(answer: Answer) {
if (answer.correct) {
answer.card.markCorrect(this@TaskActivity)
fun onResponseCorrect() {
timerFragment.cancel(State.CORRECT)
addButtonNextFragment(supportFragmentManager, true)
} else {
answer.card.markWrong(this@TaskActivity)
timerFragment.cancel(State.WRONG)
addButtonNextFragment(supportFragmentManager, false)
}
answerSet.forEach { answer -> answer.card.disable() }
}
fun onTimeOver() {
......
......@@ -54,7 +54,7 @@ class TimerFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.gradient_timer, container, false)
val view = inflater.inflate(R.layout.fragment_timer, container, false)
mView = view
tvResponse = mView.findViewById(R.id.tv_response)
......
......@@ -9,7 +9,7 @@
tools:context="com.paktalin.quickmax.task.ui.TaskActivity">
<FrameLayout
android:id="@+id/fragment_timer"
android:id="@+id/container_timer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
......
......@@ -9,7 +9,7 @@
tools:context="com.paktalin.quickmax.task.ui.TaskActivity">
<FrameLayout
android:id="@+id/fragment_timer"
android:id="@+id/container_timer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/cv_task"
......@@ -42,7 +42,7 @@
app:contentPaddingLeft="16dp"
app:contentPaddingRight="16dp"
app:contentPaddingTop="40dp"
app:layout_constraintBottom_toTopOf="@+id/layout_answers"
app:layout_constraintBottom_toTopOf="@+id/container_answers"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
......@@ -67,7 +67,22 @@
</com.google.android.material.card.MaterialCardView>
<androidx.constraintlayout.widget.ConstraintLayout
<FrameLayout
android:id="@+id/container_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" />
<!--<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_answers"
android:layout_width="0dp"
android:layout_height="200dp"
......@@ -134,6 +149,6 @@
<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
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<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>
\ No newline at end of file
......@@ -12,7 +12,6 @@
android:layout_height="wrap_content"
android:elevation="2dp"
android:gravity="center"
android:text="@string/response_correct"
android:textColor="@android:color/white"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
......
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