Second Challenge: Tracking cheat status by question

In this one I created a list in MainActivity

//questions they cheated on
var cheatedList = mutableListOf<Int>()

and added the index of the cheated questions to it from the clicklistener of the CheatActivity

 showAnswerButton.setOnClickListener {
        val answerText = when{
            answerIsTrue -> R.string.true_button
            else -> R.string.false_button
        }
        answerTextView.setText(answerText)
        //create a function to return the result to MainActivity
        setAnswerShownResult(true)
        cheaterStatus = true

        cheatedList.add(currentIndex)

    }

then in the checkAnswer function, i checked if the list contains the current index, if so display the judgment message

private fun checkAnswer(userAnswer : Boolean) {
        val correctAnswer = quizViewModel.questionBank[quizViewModel.currentIndex].answer
        val messageResId = if (cheatedList.contains(quizViewModel.currentIndex)) {
            R.string.judgment_toast
        } else if (userAnswer == correctAnswer) {

                if (::answersMap.isInitialized) {
                    answersMap.put(quizViewModel.currentIndex, true)
                } else {
                    answersMap = mutableMapOf(quizViewModel.currentIndex to true)
                }
                R.string.correct_toast

            } else {
                if (::answersMap.isInitialized) {
                    answersMap.put(quizViewModel.currentIndex, false)
                } else {
                    answersMap = mutableMapOf(quizViewModel.currentIndex to false)
                }
                R.string.false_toast
            }

            Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show()

        }

there you go…

I stored the cheat status of each question in a list of Booleans.

I just competed the Big Nerd Ranch Kotlin book and this is one of my first tries at applying it in Android, so any tips or advice on how I could improve this would be welcome.

QuizViewModel

var cheated = false      // renamed isCheater to cheated
var cheatedQuestions = mutableListOf<Boolean>()

fun initCheatProtection() {
    for (i in 0 until questionBank.size) {
        cheatedQuestions.add(false)
    }
}

fun setCheatedQuestion(questionIndex: Int) {
    cheatedQuestions[questionIndex] = true
}

MainActivity

quizViewModel.initCheatProtection()      // at the end of onCreate()

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
...
        if (requestCode == REQUEST_CODE_CHEAT) {
            quizViewModel.cheated = data?.getBooleanExtra(EXTRA_ANSWER_SHOWN, false) ?: false
            if (quizViewModel.cheated) {
                quizViewModel.setCheatedQuestion(quizViewModel.currentIndex)
            }
        }
    }

private fun checkAnswer(userAnswer: Boolean) {
   ...
        val messageResId = when {
            quizViewModel.cheatedQuestions[quizViewModel.currentIndex] -> R.string.judgement_toast
            userAnswer == correctAnswer -> R.string.correct_toast
            else -> R.string.incorrect_toast
        }
...
    }
1 Like