Skip to content

Commit

Permalink
Navigation for TryAgain, NextQuestionShowing, ChangeCategory events i…
Browse files Browse the repository at this point in the history
…mplemented.
  • Loading branch information
kirkaDev committed Dec 16, 2020
1 parent b9ea9a7 commit 3294018
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.desiredsoftware.socialquiz.data.model.question

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
import kotlinx.android.parcel.RawValue

@Parcelize
class Question (val categoryName: String,
val questionType: String,
val questionBody: String,
val questionOwner: String,
val answerVariants : ArrayList<Answer> ) {
val answerVariants : @RawValue ArrayList<Answer> ) : Parcelable {

fun hasSeveralCorrectAnswers() : Boolean
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import androidx.recyclerview.widget.RecyclerView
import com.desiredsoftware.socialquiz.R
import com.desiredsoftware.socialquiz.api.`in`.ApiClientFirebase
import com.desiredsoftware.socialquiz.api.`in`.category.GetCategoriesCallback
import com.desiredsoftware.socialquiz.data.model.question.GetQuestionCallback
import com.desiredsoftware.socialquiz.data.model.question.QuestionCategory
import com.desiredsoftware.socialquiz.ui.components.CategoriesAdapter
import com.desiredsoftware.socialquiz.ui.components.OnClickCategoryListener
import com.desiredsoftware.socialquiz.utils.convertToQuestion

class HomeFragment : Fragment() {

Expand Down Expand Up @@ -60,8 +62,17 @@ class HomeFragment : Fragment() {
object : OnClickCategoryListener {
override fun onClicked(categoryName: String) {
Log.d("RecyclerView clicked", "Category name = $categoryName was selected")
val action = HomeFragmentDirections.actionNavigationHomeToQuestionShowingFragment(categoryName)
navController.navigate(action)
homeViewModel.getNextQuestion(categoryName, object : GetQuestionCallback {
override fun onCallback(questions: ArrayList<Any>) {
if (questions!=null)
{
// TODO : Delete this hardcode
val currentQuestion = convertToQuestion(questions[0] as HashMap<String, Any>)
val action = HomeFragmentDirections.actionNavigationHomeToQuestionShowingFragment(currentQuestion)
navController.navigate(action)
}
}
})
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@ package com.desiredsoftware.socialquiz.ui.home
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.desiredsoftware.socialquiz.api.`in`.ApiClientFirebase
import com.desiredsoftware.socialquiz.data.model.question.GetQuestionCallback
import com.desiredsoftware.socialquiz.data.model.question.Question
import com.desiredsoftware.socialquiz.utils.generateQuestion

class HomeViewModel : ViewModel() {

private val _text = MutableLiveData<String>().apply {
value = "Select your category"
}
val text: LiveData<String> = _text

val apiClient: ApiClientFirebase = ApiClientFirebase()

fun getNextQuestion(questionCategory : String, callback : GetQuestionCallback) : Question
{
apiClient.getQuestion(questionCategory, callback)

return generateQuestion()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.desiredsoftware.socialquiz.R
import com.desiredsoftware.socialquiz.data.model.question.Question
import com.desiredsoftware.socialquiz.utils.generateQuestion

class QuestionResultFragment : Fragment() {

companion object {
fun newInstance() = QuestionResultFragment()
}

private lateinit var viewModel: QuestionResultViewModel
//private lateinit var viewModel: QuestionResultViewModel


val args: QuestionResultFragmentArgs by navArgs()

lateinit var viewTryAgain: View
lateinit var viewNextQuestion : View
lateinit var viewChangeCategory : View
lateinit var root : View

override fun onCreateView(
Expand All @@ -28,47 +36,65 @@ class QuestionResultFragment : Fragment() {
): View? {

root = inflater.inflate(R.layout.fragment_question_result, container, false)
viewTryAgain = root.findViewById(R.id.buttonTryAgain)
viewNextQuestion = root.findViewById(R.id.buttonNextQuestion)
viewChangeCategory = root.findViewById(R.id.buttonChangeCategory)

val buttonTryAgain : Button = viewTryAgain.findViewById(R.id.buttonWithIcon)
val buttonNextQuestion : Button = viewNextQuestion.findViewById(R.id.buttonWithIcon)
val buttonChangeCategory : Button = viewChangeCategory.findViewById(R.id.buttonWithIcon)

val buttonNextQuestion: View = root.findViewById(R.id.buttonNextQuestion)
var textviewNextQuestion: TextView = buttonNextQuestion.findViewById(R.id.textView)

var textviewNextQuestion: TextView = viewNextQuestion.findViewById(R.id.buttonWithIcon)
textviewNextQuestion.setText(R.string.nextQuestion)

val buttonChangeCategory: View = root.findViewById(R.id.buttonChangeCategory)
var textviewChangeCategory: TextView = buttonChangeCategory.findViewById(R.id.textView)
var textviewChangeCategory: Button = viewChangeCategory.findViewById(R.id.buttonWithIcon)
textviewChangeCategory.setText(R.string.changeCategory)



if (args.answerIsCorrect)
{
configureCorrectAnswer()
}
else {
configureWrongAnswer()
}
buttonTryAgain.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
val action = QuestionResultFragmentDirections.actionQuestionResultFragmentToQuestionShowingFragment(args.currentQuestion)
val navController = requireParentFragment().findNavController()
navController.navigate(action)
}
})

buttonNextQuestion.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
// TODO: Replace for real method, at now imitation working
val nextQuestion : Question = getNextQuestion()
val action = QuestionResultFragmentDirections.actionQuestionResultFragmentToQuestionShowingFragment(nextQuestion)
requireParentFragment().findNavController().navigate(action)
}
})

buttonChangeCategory.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
val action = QuestionResultFragmentDirections.actionQuestionResultFragmentToNavigationHome()
findNavController().navigate(action)
}
})
if (args.answerIsCorrect) configureCorrectAnswer()
else configureWrongAnswer()

return root

}

/* override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(QuestionResultViewModel::class.java)
// TODO: Use the ViewModel
}*/

fun configureWrongAnswer() {
val viewTryAgain: View = root.findViewById(R.id.buttonTryAgain)
var textviewTryAgain: TextView = viewTryAgain.findViewById(R.id.textView)
var textviewTryAgain: TextView = viewTryAgain.findViewById(R.id.buttonWithIcon)
textviewTryAgain.setText(R.string.tryAgain)
}

fun configureCorrectAnswer() {
val textViewResult : TextView = root.findViewById(R.id.textViewResult)
textViewResult.setText(R.string.congrat)
val viewTryAgain: View = root.findViewById(R.id.buttonTryAgain)
viewTryAgain.isVisible = false
}

fun getNextQuestion() : Question
{
return generateQuestion()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.desiredsoftware.socialquiz.R
import com.desiredsoftware.socialquiz.data.model.question.GetQuestionCallback
import com.desiredsoftware.socialquiz.data.model.question.Question
import com.desiredsoftware.socialquiz.ui.components.AnswerVariantsAdapter
import com.desiredsoftware.socialquiz.ui.components.OnClickAnswerListener
import com.desiredsoftware.socialquiz.utils.convertToQuestion
import com.desiredsoftware.socialquiz.utils.generateMediaURI
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer
Expand All @@ -35,11 +33,6 @@ class QuestionShowingFragment : Fragment() {
lateinit var player : SimpleExoPlayer
lateinit var playerControlView: StyledPlayerView
lateinit var root : View
lateinit var categoryName : String

lateinit var questionType : String
lateinit var currentQuestion : Question


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -51,33 +44,25 @@ class QuestionShowingFragment : Fragment() {
framePlayerLayout.setAspectRatio(16f/9f)

viewModel = QuestionShowingViewModel()
viewModel.getNextQuestion(args.categoryName, object : GetQuestionCallback{
override fun onCallback(questions: ArrayList<Any>) {
if (questions!=null)
// TODO : Delete this hardcode
currentQuestion = convertToQuestion(questions[0] as HashMap<String, Any>)
viewModel.currentQuestion = args.question

val recyclerView : RecyclerView = root.findViewById(R.id.recyclerViewAnswerVariants)
recyclerView.layoutManager = GridLayoutManager(requireContext(),1)
recyclerView.adapter = AnswerVariantsAdapter(currentQuestion.answerVariants,
recyclerView.adapter = AnswerVariantsAdapter(viewModel.currentQuestion.answerVariants,
object : OnClickAnswerListener{
override fun onClicked(answerVariant: Question.Answer) {
// TODO: Show QuestionResultsFragment
val answerIsCorrect : Boolean = answerVariant.isCorrect
val action = QuestionShowingFragmentDirections.actionQuestionShowingFragmentToQuestionResultFragment(answerIsCorrect)
val action = QuestionShowingFragmentDirections.actionQuestionShowingFragmentToQuestionResultFragment(answerIsCorrect, viewModel.currentQuestion)
val navController = requireParentFragment().findNavController()
navController.navigate(action)
}

})

if (currentQuestion.questionType.equals("video"))
configurePlayer(currentQuestion.questionBody)
}
})
if (viewModel.currentQuestion.questionType.equals("video"))
configurePlayer(viewModel.currentQuestion.questionBody)

return root
}
return root
}

override fun onResume() {
super.onResume()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ package com.desiredsoftware.socialquiz.ui.question

import androidx.lifecycle.ViewModel
import com.desiredsoftware.socialquiz.api.`in`.ApiClientFirebase
import com.desiredsoftware.socialquiz.data.model.question.GetQuestionCallback
import com.desiredsoftware.socialquiz.data.model.question.Question
import com.desiredsoftware.socialquiz.utils.generateQuestion

class QuestionShowingViewModel () : ViewModel() {

val apiClient: ApiClientFirebase = ApiClientFirebase()

fun getNextQuestion(questionCategory : String, callback : GetQuestionCallback) : Question
{
apiClient.getQuestion(questionCategory, callback)

return generateQuestion()
}
lateinit var currentQuestion : Question

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/button_with_icon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ImageView>

<Button
android:id="@+id/textView"
android:id="@+id/buttonWithIcon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/navigation/mobile_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
android:id="@+id/action_questionShowingFragment_to_navigation_home"
app:destination="@id/navigation_home" />
<argument
android:name="categoryName"
app:argType="string" />
android:name="question"
app:argType="com.desiredsoftware.socialquiz.data.model.question.Question" />
</fragment>
<fragment
android:id="@+id/questionResultFragment"
Expand All @@ -59,6 +59,12 @@
<argument
android:name="answerIsCorrect"
app:argType="boolean" />
<argument
android:name="currentQuestion"
app:argType="com.desiredsoftware.socialquiz.data.model.question.Question" />
<action
android:id="@+id/action_questionResultFragment_to_navigation_home"
app:destination="@id/navigation_home" />
</fragment>
<fragment
android:id="@+id/addOwnQuestionFragment"
Expand Down

0 comments on commit 3294018

Please sign in to comment.