-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated quiz module from Java to Kotlin (#5952)
* Rename .java to .kt * Migrated quiz module to Kotlin * unit test failing fixed * unit test failing fixed
- Loading branch information
1 parent
bafae82
commit 00cfd83
Showing
10 changed files
with
658 additions
and
628 deletions.
There are no files selected for viewing
146 changes: 0 additions & 146 deletions
146
app/src/main/java/fr/free/nrw/commons/quiz/QuizActivity.java
This file was deleted.
Oops, something went wrong.
154 changes: 154 additions & 0 deletions
154
app/src/main/java/fr/free/nrw/commons/quiz/QuizActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.