Finish cheat plugin integration

main
Alexis Drai 3 years ago
parent af21a58dee
commit b3cc1687c6

@ -1,12 +1,17 @@
package fr.iut.pm
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
const val HAS_CHEATED: String = "hasCheated"
class CheatActivity : AppCompatActivity() {
private var cheatAnswer: Boolean = false
private var hasCheated: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -19,6 +24,13 @@ class CheatActivity : AppCompatActivity() {
findViewById<Button>(R.id.btnCheat).setOnClickListener {
findViewById<TextView>(R.id.textViewAnswerCheat).text = cheatAnswer.toString()
hasCheated = true
prepareOutIntent()
}
}
private fun prepareOutIntent() {
val outIntent: Intent = Intent().apply { putExtra(HAS_CHEATED, hasCheated) }
setResult(Activity.RESULT_OK, outIntent)
}
}

@ -1,5 +1,6 @@
package fr.iut.pm
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
@ -7,6 +8,7 @@ import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import fr.iut.pm.data.Stub
import fr.iut.pm.model.TrueFalseQuestion
@ -20,12 +22,19 @@ class QuizActivity : AppCompatActivity() {
private var questionIndex: Int = 0
private var cheatAnswer: Boolean = false
private var isCheater: Boolean = false
private val cheatLauncher = registerForActivityResult(StartActivityForResult())
{ result ->
if (result.resultCode == Activity.RESULT_OK)
isCheater = result.data?.getBooleanExtra(HAS_CHEATED, false) == true
}
override fun onCreate(savedInstanceState: Bundle?) {
Log.println(Log.INFO, TAG, "Creating...")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)
val questions = Stub().loadQuestions(resources)
val textViewQuestion = findViewById<TextView>(R.id.textViewQuestion)
@ -44,9 +53,14 @@ class QuizActivity : AppCompatActivity() {
}
findViewById<Button>(R.id.btnCheat).setOnClickListener {
val cheatIntent = Intent(this, CheatActivity::class.java)
cheatIntent.putExtra(CHEAT_ANSWER, cheatAnswer)
startActivity(cheatIntent)
val cheatIntent = Intent(this, CheatActivity::class.java).apply {
putExtra(CHEAT_ANSWER, cheatAnswer)
}
cheatLauncher.launch(cheatIntent)
}
if (isCheater) {
Toast.makeText(this, "cheater", Toast.LENGTH_LONG).show()
}
}
@ -88,9 +102,14 @@ class QuizActivity : AppCompatActivity() {
private fun assignAnswerToButton(btn: Button, toast: String) {
btn.setOnClickListener {
if (isCheater) {
Toast.makeText(this, "cheater", Toast.LENGTH_LONG).show()
isCheater = false
} else {
Toast.makeText(this, toast, Toast.LENGTH_SHORT).show()
}
}
}
private fun nextIndex(size: Int) {
if (questionIndex >= size - 1) {

Loading…
Cancel
Save