Commit 8d4a1791 by likorn

United ResponseCorrect and ResponseWrong fragments

parent 4a6665cc
......@@ -12,6 +12,7 @@ import androidx.core.view.get
import androidx.test.espresso.ViewAction
import androidx.test.espresso.UiController
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import junit.framework.Assert.assertNotNull
import junit.framework.Assert.assertTrue
......@@ -33,9 +34,8 @@ class MainActivityTest {
val correctAnswerIndex = getCorrectAnswerIndex()
val correctAnswerId = radioGroup[correctAnswerIndex].id
onView(withId(correctAnswerId)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
assertTrue(testRule.activity.supportFragmentManager.fragments[0] is ResponseCorrectFragment)
onView(withId(R.id.tv_response))
.check(matches(withText(testRule.activity.resources.getString(R.string.response_correct))))
}
@Test
......@@ -44,9 +44,8 @@ class MainActivityTest {
val wrongAnswerIndex = if (correctAnswerIndex == 3) 2 else 3
val wrongAnswerId = radioGroup[wrongAnswerIndex].id
onView(withId(wrongAnswerId)).perform(click())
assertTrue(testRule.activity.supportFragmentManager.fragments.size == 1)
assertTrue(testRule.activity.supportFragmentManager.fragments[0] is ResponseWrongFragment)
onView(withId(R.id.tv_response))
.check(matches(withText(testRule.activity.resources.getString(R.string.response_wrong))))
}
@Test
......
......@@ -34,10 +34,9 @@ class MainActivity : AppCompatActivity() {
timer.cancel()
makeRadioButtonsUncheckable()
val responseFragment: Fragment = if (numberSet.isCorrect(answer)) {
ResponseCorrectFragment.newInstance()
} else
ResponseWrongFragment.newInstance()
val responseFragment = ResponseFragment.newInstance().also {
f -> f.arguments = Bundle().also {
b -> b.putBoolean("correct", numberSet.isCorrect(answer)) } }
supportFragmentManager
.beginTransaction()
......
package com.example.quickmax
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
class ResponseCorrectFragment: Fragment() {
class ResponseFragment: Fragment() {
private val greenColor = Color.parseColor("#4CAF50")
private val redColor = Color.parseColor("#F44336")
companion object {
fun newInstance(): ResponseCorrectFragment {
return ResponseCorrectFragment()
fun newInstance(): ResponseFragment {
return ResponseFragment()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_response_correct, container, false)
val view = inflater.inflate(R.layout.fragment_response, container, false)
val correct = arguments!!.getBoolean("correct")
return if (correct)
view(view, greenColor, R.string.response_correct)
else
view(view, redColor, R.string.response_wrong)
}
private fun view(view: View, color: Int, responseId: Int): View {
view.findViewById<TextView>(R.id.tv_response).text = resources.getString(responseId)
view.findViewById<ConstraintLayout>(R.id.response_layout).setBackgroundColor(color)
view.findViewById<ImageButton>(R.id.btn_next).setOnClickListener { reloadActivity() }
return view
}
......
package com.example.quickmax
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class ResponseWrongFragment: Fragment() {
companion object {
fun newInstance(): ResponseWrongFragment {
return ResponseWrongFragment()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_response_wrong, container, false)
}
}
\ No newline at end of file
......@@ -4,7 +4,6 @@
android:id="@+id/response_layout"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#F44336"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
......@@ -24,10 +23,10 @@
app:srcCompat="@drawable/ic_double_arrow" />
<TextView
android:id="@+id/tv_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="@string/response_wrong"
android:textColor="@android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/response_layout"
android:layout_width="match_parent"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="#4CAF50"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageButton
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:background="#009C27B0"
android:tint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_double_arrow" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="@string/response_correct"
android:textColor="@android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</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