Currently, when the user cheats once it is a cheater for the entire app. Instead, we should show him as cheater only for particular questions.
Here is my code:
MainActivity.kt
private const val TAG = "MainActivity"
private const val KEY_INDEX = "index"
private const val REQUEST_CODE_CHEAT = 0
private const val EXTRA_ANSWER_SHOWN = "answer_is_shown"
private const val IS_CHEATER = "isCheater"
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var nextButton: Button
private lateinit var cheatButton: Button
private lateinit var questionTextView: TextView
private val quizModel: QuizViewModel by lazy {
ViewModelProviders.of(this).get(QuizViewModel::class.java)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Log.i(TAG,"onSaveInstanceState")
outState.putInt(KEY_INDEX,quizModel.currentIndex)
outState.putBoolean(IS_CHEATER,quizModel.isCheater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG,"onCreate called")
setContentView(R.layout.activity_main)
val currentIndex = savedInstanceState?.getInt(KEY_INDEX,0)?:0
quizModel.currentIndex=currentIndex
quizModel.isCheater = savedInstanceState?.getBoolean(IS_CHEATER,false)?:false
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
nextButton = findViewById(R.id.next_button)
questionTextView = findViewById(R.id.question_text_view)
cheatButton = findViewById(R.id.cheat_button)
cheatButton.setOnClickListener {
val answerIsTrue = quizModel.currentQuestionAnswer
val intent = CheatActivity.newIntent(this@MainActivity,answerIsTrue)
startActivityForResult(intent, REQUEST_CODE_CHEAT)
}
trueButton.setOnClickListener { view ->
checkAnswer(true)
}
falseButton.setOnClickListener { view ->
checkAnswer(false)
}
nextButton.setOnClickListener {
quizModel.isCheater=false
quizModel.moveToNextQuestion()
updateQuestion()
}
updateQuestion()
}
override fun onStart() {
super.onStart()
Log.d(TAG,"onStart called")
}
override fun onResume() {
super.onResume()
Log.d(TAG,"onResume called")
}
override fun onPause() {
super.onPause()
Log.d(TAG,"onPause called")
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG,"onDestroy called")
}
override fun onStop() {
super.onStop()
Log.d(TAG,"onStop called")
}
private fun updateQuestion(){
val questionTextResId = quizModel.currentQuestionText
questionTextView.setText(questionTextResId)
}
private fun checkAnswer(userAnswer:Boolean){
val actualAnswer = quizModel.currentQuestionAnswer
val message = when{
quizModel.isCheater->getString(R.string.judgement_toast)
(actualAnswer == userAnswer) -> getString(R.string.correct_toast)
else -> getString(R.string.incorrect_toast)
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode != Activity.RESULT_OK) return
if(requestCode== REQUEST_CODE_CHEAT){
quizModel.isCheater = data?.getBooleanExtra(EXTRA_ANSWER_SHOWN,false)?:false
}
}
}
CheatActivity.kt
private const val EXTRA_ANSWER_IS_TRUE = "answer_is_true"
private const val EXTRA_ANSWER_SHOWN = "answer_is_shown"
private const val TAG = "CheatActivity"
private const val IS_CHEATER = "isCheater"
class CheatActivity : AppCompatActivity() {
private var answerIsTrue = false
private lateinit var answerText: TextView
private lateinit var showAnswer: Button
private val quizModel: QuizViewModel by lazy {
ViewModelProviders.of(this).get(QuizViewModel::class.java)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Log.i(TAG,"onSaveInstanceState")
outState.putBoolean(IS_CHEATER,quizModel.isCheater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cheat)
answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false)
answerText = findViewById(R.id.answer_text_view)
showAnswer = findViewById(R.id.show_answer_button)
showAnswer.setOnClickListener {
answerText.text = displayAnswer
setAnswerShownResult(true)
}
if(quizModel.isCheater){
answerText.text = displayAnswer
setAnswerShownResult(true)
}
}
private val displayAnswer:String
get() =when{
answerIsTrue -> getString(R.string.true_button)
else -> getString(R.string.false_button)
}
companion object{
fun newIntent(packageContext: Context, answerIsTrue:Boolean): Intent {
return Intent(packageContext, CheatActivity::class.java).apply { putExtra(
EXTRA_ANSWER_IS_TRUE,answerIsTrue) }
}
}
private fun setAnswerShownResult(isAnswerShown:Boolean){
quizModel.isCheater=true
val data = Intent().apply { putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown) }
setResult(Activity.RESULT_OK,data)
}
}
Please let me know for any errors.