Skip to content

Commit

Permalink
Migrated quiz module from Java to Kotlin (#5952)
Browse files Browse the repository at this point in the history
* Rename .java to .kt

* Migrated quiz module to Kotlin

* unit test failing fixed

* unit test failing fixed
  • Loading branch information
Saifuddin53 authored Nov 24, 2024
1 parent bafae82 commit 00cfd83
Show file tree
Hide file tree
Showing 10 changed files with 658 additions and 628 deletions.
146 changes: 0 additions & 146 deletions app/src/main/java/fr/free/nrw/commons/quiz/QuizActivity.java

This file was deleted.

154 changes: 154 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/quiz/QuizActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package fr.free.nrw.commons.quiz

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle

import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat

import com.facebook.drawee.drawable.ProgressBarDrawable
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder

import fr.free.nrw.commons.databinding.ActivityQuizBinding
import java.util.ArrayList

import fr.free.nrw.commons.R


class QuizActivity : AppCompatActivity() {

private lateinit var binding: ActivityQuizBinding
private val quizController = QuizController()
private var quiz = ArrayList<QuizQuestion>()
private var questionIndex = 0
private var score = 0

/**
* isPositiveAnswerChecked : represents yes click event
*/
private var isPositiveAnswerChecked = false

/**
* isNegativeAnswerChecked : represents no click event
*/
private var isNegativeAnswerChecked = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityQuizBinding.inflate(layoutInflater)
setContentView(binding.root)

quizController.initialize(this)
setSupportActionBar(binding.toolbar.toolbar)
binding.nextButton.setOnClickListener { notKnowAnswer() }
displayQuestion()
}

/**
* To move to next question and check whether answer is selected or not
*/
fun setNextQuestion() {
if (questionIndex <= quiz.size && (isPositiveAnswerChecked || isNegativeAnswerChecked)) {
evaluateScore()
}
}

private fun notKnowAnswer() {
customAlert("Information", quiz[questionIndex].answerMessage)
}

/**
* To give warning before ending quiz
*/
override fun onBackPressed() {
AlertDialog.Builder(this)
.setTitle(getString(R.string.warning))
.setMessage(getString(R.string.quiz_back_button))
.setPositiveButton(R.string.continue_message) { dialog, _ ->
val intent = Intent(this, QuizResultActivity::class.java)
dialog.dismiss()
intent.putExtra("QuizResult", score)
startActivity(intent)
}
.setNegativeButton("Cancel") { dialogInterface, _ -> dialogInterface.dismiss() }
.create()
.show()
}

/**
* To display the question
*/
@SuppressLint("SetTextI18n")
private fun displayQuestion() {
quiz = quizController.getQuiz()
binding.question.questionText.text = quiz[questionIndex].question
binding.questionTitle.text = getString(R.string.question) + quiz[questionIndex].questionNumber

binding.question.questionImage.hierarchy = GenericDraweeHierarchyBuilder
.newInstance(resources)
.setFailureImage(VectorDrawableCompat.create(resources, R.drawable.ic_error_outline_black_24dp, theme))
.setProgressBarImage(ProgressBarDrawable())
.build()

binding.question.questionImage.setImageURI(quiz[questionIndex].getUrl())
isPositiveAnswerChecked = false
isNegativeAnswerChecked = false

binding.answer.quizPositiveAnswer.setOnClickListener {
isPositiveAnswerChecked = true
setNextQuestion()
}
binding.answer.quizNegativeAnswer.setOnClickListener {
isNegativeAnswerChecked = true
setNextQuestion()
}
}

/**
* To evaluate score and check whether answer is correct or wrong
*/
fun evaluateScore() {
if (
(quiz[questionIndex].isAnswer && isPositiveAnswerChecked)
||
(!quiz[questionIndex].isAnswer && isNegativeAnswerChecked)
) {
customAlert(
getString(R.string.correct),
quiz[questionIndex].answerMessage
)
score++
} else {
customAlert(
getString(R.string.wrong),
quiz[questionIndex].answerMessage
)
}
}

/**
* To display explanation after each answer, update questionIndex and move to next question
* @param title The alert title
* @param message The alert message
*/
fun customAlert(title: String, message: String) {
AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.continue_message) { dialog, _ ->
questionIndex++
if (questionIndex == quiz.size) {
val intent = Intent(this, QuizResultActivity::class.java)
dialog.dismiss()
intent.putExtra("QuizResult", score)
startActivity(intent)
} else {
displayQuestion()
}
}
.create()
.show()
}
}
Loading

0 comments on commit 00cfd83

Please sign in to comment.