|
| 1 | +package fr.free.nrw.commons.quiz |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Intent |
| 5 | +import android.os.Bundle |
| 6 | + |
| 7 | +import androidx.appcompat.app.AlertDialog |
| 8 | +import androidx.appcompat.app.AppCompatActivity |
| 9 | +import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat |
| 10 | + |
| 11 | +import com.facebook.drawee.drawable.ProgressBarDrawable |
| 12 | +import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder |
| 13 | + |
| 14 | +import fr.free.nrw.commons.databinding.ActivityQuizBinding |
| 15 | +import java.util.ArrayList |
| 16 | + |
| 17 | +import fr.free.nrw.commons.R |
| 18 | + |
| 19 | + |
| 20 | +class QuizActivity : AppCompatActivity() { |
| 21 | + |
| 22 | + private lateinit var binding: ActivityQuizBinding |
| 23 | + private val quizController = QuizController() |
| 24 | + private var quiz = ArrayList<QuizQuestion>() |
| 25 | + private var questionIndex = 0 |
| 26 | + private var score = 0 |
| 27 | + |
| 28 | + /** |
| 29 | + * isPositiveAnswerChecked : represents yes click event |
| 30 | + */ |
| 31 | + private var isPositiveAnswerChecked = false |
| 32 | + |
| 33 | + /** |
| 34 | + * isNegativeAnswerChecked : represents no click event |
| 35 | + */ |
| 36 | + private var isNegativeAnswerChecked = false |
| 37 | + |
| 38 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 39 | + super.onCreate(savedInstanceState) |
| 40 | + binding = ActivityQuizBinding.inflate(layoutInflater) |
| 41 | + setContentView(binding.root) |
| 42 | + |
| 43 | + quizController.initialize(this) |
| 44 | + setSupportActionBar(binding.toolbar.toolbar) |
| 45 | + binding.nextButton.setOnClickListener { notKnowAnswer() } |
| 46 | + displayQuestion() |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * To move to next question and check whether answer is selected or not |
| 51 | + */ |
| 52 | + fun setNextQuestion() { |
| 53 | + if (questionIndex <= quiz.size && (isPositiveAnswerChecked || isNegativeAnswerChecked)) { |
| 54 | + evaluateScore() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private fun notKnowAnswer() { |
| 59 | + customAlert("Information", quiz[questionIndex].answerMessage) |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * To give warning before ending quiz |
| 64 | + */ |
| 65 | + override fun onBackPressed() { |
| 66 | + AlertDialog.Builder(this) |
| 67 | + .setTitle(getString(R.string.warning)) |
| 68 | + .setMessage(getString(R.string.quiz_back_button)) |
| 69 | + .setPositiveButton(R.string.continue_message) { dialog, _ -> |
| 70 | + val intent = Intent(this, QuizResultActivity::class.java) |
| 71 | + dialog.dismiss() |
| 72 | + intent.putExtra("QuizResult", score) |
| 73 | + startActivity(intent) |
| 74 | + } |
| 75 | + .setNegativeButton("Cancel") { dialogInterface, _ -> dialogInterface.dismiss() } |
| 76 | + .create() |
| 77 | + .show() |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * To display the question |
| 82 | + */ |
| 83 | + @SuppressLint("SetTextI18n") |
| 84 | + private fun displayQuestion() { |
| 85 | + quiz = quizController.getQuiz() |
| 86 | + binding.question.questionText.text = quiz[questionIndex].question |
| 87 | + binding.questionTitle.text = getString(R.string.question) + quiz[questionIndex].questionNumber |
| 88 | + |
| 89 | + binding.question.questionImage.hierarchy = GenericDraweeHierarchyBuilder |
| 90 | + .newInstance(resources) |
| 91 | + .setFailureImage(VectorDrawableCompat.create(resources, R.drawable.ic_error_outline_black_24dp, theme)) |
| 92 | + .setProgressBarImage(ProgressBarDrawable()) |
| 93 | + .build() |
| 94 | + |
| 95 | + binding.question.questionImage.setImageURI(quiz[questionIndex].getUrl()) |
| 96 | + isPositiveAnswerChecked = false |
| 97 | + isNegativeAnswerChecked = false |
| 98 | + |
| 99 | + binding.answer.quizPositiveAnswer.setOnClickListener { |
| 100 | + isPositiveAnswerChecked = true |
| 101 | + setNextQuestion() |
| 102 | + } |
| 103 | + binding.answer.quizNegativeAnswer.setOnClickListener { |
| 104 | + isNegativeAnswerChecked = true |
| 105 | + setNextQuestion() |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * To evaluate score and check whether answer is correct or wrong |
| 111 | + */ |
| 112 | + fun evaluateScore() { |
| 113 | + if ( |
| 114 | + (quiz[questionIndex].isAnswer && isPositiveAnswerChecked) |
| 115 | + || |
| 116 | + (!quiz[questionIndex].isAnswer && isNegativeAnswerChecked) |
| 117 | + ) { |
| 118 | + customAlert( |
| 119 | + getString(R.string.correct), |
| 120 | + quiz[questionIndex].answerMessage |
| 121 | + ) |
| 122 | + score++ |
| 123 | + } else { |
| 124 | + customAlert( |
| 125 | + getString(R.string.wrong), |
| 126 | + quiz[questionIndex].answerMessage |
| 127 | + ) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * To display explanation after each answer, update questionIndex and move to next question |
| 133 | + * @param title The alert title |
| 134 | + * @param message The alert message |
| 135 | + */ |
| 136 | + fun customAlert(title: String, message: String) { |
| 137 | + AlertDialog.Builder(this) |
| 138 | + .setTitle(title) |
| 139 | + .setMessage(message) |
| 140 | + .setPositiveButton(R.string.continue_message) { dialog, _ -> |
| 141 | + questionIndex++ |
| 142 | + if (questionIndex == quiz.size) { |
| 143 | + val intent = Intent(this, QuizResultActivity::class.java) |
| 144 | + dialog.dismiss() |
| 145 | + intent.putExtra("QuizResult", score) |
| 146 | + startActivity(intent) |
| 147 | + } else { |
| 148 | + displayQuestion() |
| 149 | + } |
| 150 | + } |
| 151 | + .create() |
| 152 | + .show() |
| 153 | + } |
| 154 | +} |
0 commit comments