diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/data/model/question/Question.kt b/app/src/main/java/com/desiredsoftware/socialquiz/data/model/question/Question.kt index 9cd941f..866f4be 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/data/model/question/Question.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/data/model/question/Question.kt @@ -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 ) { + val answerVariants : @RawValue ArrayList ) : Parcelable { fun hasSeveralCorrectAnswers() : Boolean { diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeFragment.kt b/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeFragment.kt index 5cec3e8..22cee21 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeFragment.kt @@ -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() { @@ -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) { + if (questions!=null) + { + // TODO : Delete this hardcode + val currentQuestion = convertToQuestion(questions[0] as HashMap) + val action = HomeFragmentDirections.actionNavigationHomeToQuestionShowingFragment(currentQuestion) + navController.navigate(action) + } + } + }) } }) } diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeViewModel.kt b/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeViewModel.kt index 8d4f08b..7d6af6a 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/ui/home/HomeViewModel.kt @@ -3,6 +3,10 @@ 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() { @@ -10,4 +14,14 @@ class HomeViewModel : ViewModel() { value = "Select your category" } val text: LiveData = _text + + val apiClient: ApiClientFirebase = ApiClientFirebase() + + fun getNextQuestion(questionCategory : String, callback : GetQuestionCallback) : Question + { + apiClient.getQuestion(questionCategory, callback) + + return generateQuestion() + } + } \ No newline at end of file diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionResultFragment.kt b/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionResultFragment.kt index eb6331a..9642d75 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionResultFragment.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionResultFragment.kt @@ -4,11 +4,15 @@ 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() { @@ -16,10 +20,14 @@ class QuestionResultFragment : Fragment() { 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( @@ -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() + } } \ No newline at end of file diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingFragment.kt b/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingFragment.kt index 7e013d7..5a042d9 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingFragment.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingFragment.kt @@ -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 @@ -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?, @@ -51,33 +44,25 @@ class QuestionShowingFragment : Fragment() { framePlayerLayout.setAspectRatio(16f/9f) viewModel = QuestionShowingViewModel() - viewModel.getNextQuestion(args.categoryName, object : GetQuestionCallback{ - override fun onCallback(questions: ArrayList) { - if (questions!=null) - // TODO : Delete this hardcode - currentQuestion = convertToQuestion(questions[0] as HashMap) + 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() diff --git a/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingViewModel.kt b/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingViewModel.kt index e3a6d10..fa6e232 100644 --- a/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingViewModel.kt +++ b/app/src/main/java/com/desiredsoftware/socialquiz/ui/question/QuestionShowingViewModel.kt @@ -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 } diff --git a/app/src/main/res/layout/button_with_icon.xml b/app/src/main/res/layout/button_with_icon.xml index 0c93596..fa0099b 100644 --- a/app/src/main/res/layout/button_with_icon.xml +++ b/app/src/main/res/layout/button_with_icon.xml @@ -26,7 +26,7 @@