From f75d844633204421fd948790bb852a0b72efab46 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 4 Jan 2024 14:56:29 +0100 Subject: [PATCH] wip: play --- .../memorisationapplication/HomeScreen.kt | 2 +- .../memorisationapplication/MainActivity.kt | 10 ++- .../memorisationapplication/PlayScreen.kt | 62 ++++++++++++++++- .../memorisationapplication/Routes.kt | 1 + .../memorisationapplication/ui/AnswerType.kt | 5 ++ .../ui/PlayViewModel.kt | 67 +++++++++++++++++++ 6 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt index 7891aed..d7b5c4d 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt @@ -114,7 +114,7 @@ fun HomeScreen( Button( enabled = currentSelection != null, - onClick = { navController.navigate(PLAY) }) { + onClick = { navController.navigate("$PLAY/${currentSelection?.idSet}") }) { Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp) } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt index 5c00dfd..e23b3f7 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt @@ -75,7 +75,15 @@ fun MainScreen() { ) } } - composable(PLAY) { PlayScreen(padding, navController) } + composable("$PLAY/{$PLAY_SET_ARG}") { + it.arguments?.getString(PLAY_SET_ARG)?.let { idSet -> + PlayScreen( + padding, + navController, + idSet.toInt() + ) + } + } } } } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt index 736aee3..d4f8192 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt @@ -1,20 +1,78 @@ package fr.uparis.diamantkennel.memorisationapplication -import android.widget.Toast +import android.util.Log +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Button +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController +import fr.uparis.diamantkennel.memorisationapplication.ui.AnswerType import fr.uparis.diamantkennel.memorisationapplication.ui.PlayViewModel +@OptIn(ExperimentalMaterial3Api::class) @Composable fun PlayScreen( padding: PaddingValues, navController: NavController, + idSet: Int, model: PlayViewModel = viewModel() ) { + // First update the list and set ID + model.setId.value = idSet + model.updateQuestionList(idSet) + val context = LocalContext.current - Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() + val question by model.currentQuestion + if (question == null) { + return + } + val reponse by model.proposedAnswer + val correction by model.evaluatedAnswer + + if (correction == AnswerType.GOOD) { + Log.d("1312", "Bonne réponse !") + model.newQuestion() + } + + if (correction == AnswerType.BAD) { + Log.d("1312", "Mauvaise réponse !") + model.reset() + } + + Column( + modifier = Modifier.padding(padding), + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text(text = question!!.enonce) + OutlinedTextField( + value = reponse, + label = { Text(text = "Réponse") }, + onValueChange = model::updateAnswer + ) + + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { + Button(onClick = model::checkAnswer) { + Text(text = "Répondre") + } + + Button( + enabled = false /* TODO: s'activer au bout de 3 secondes */, + onClick = { /*TODO*/ }) { + Text(text = "Voir réponse") + } + } + } } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/Routes.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/Routes.kt index 23513f6..5483e01 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/Routes.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/Routes.kt @@ -4,3 +4,4 @@ const val HOME = "home" const val MODIFY_SET = "modify_set" const val MODIFY_SET_ARGS = "id_set" const val PLAY = "play" +const val PLAY_SET_ARG = "id_set" diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt new file mode 100644 index 0000000..d09b197 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/AnswerType.kt @@ -0,0 +1,5 @@ +package fr.uparis.diamantkennel.memorisationapplication.ui + +enum class AnswerType { + NONE, GOOD, BAD +} diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt index 7abc73c..86a15d5 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/PlayViewModel.kt @@ -1,10 +1,77 @@ package fr.uparis.diamantkennel.memorisationapplication.ui import android.app.Application +import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope import fr.uparis.diamantkennel.memorisationapplication.MemoApplication +import fr.uparis.diamantkennel.memorisationapplication.data.Question +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class PlayViewModel(application: Application) : AndroidViewModel(application) { private val dao = (application as MemoApplication).database.memoDao() + private val initialId = -1 // invalid ID, mean that we don't have the ID yet + + var setId = mutableStateOf(initialId) + private var questions = dao.loadQuestions(setId.value) + + var currentQuestion = mutableStateOf(null) + private var index = mutableStateOf(0) + + var proposedAnswer = mutableStateOf("") + var evaluatedAnswer = mutableStateOf(AnswerType.NONE) + + fun updateQuestionList(setId: Int) { + if (setId != initialId) { + viewModelScope.launch(Dispatchers.IO) { + questions = dao.loadQuestions(setId) + updateQuestion() + } + } + } + + private fun updateQuestion() { + viewModelScope.launch(Dispatchers.IO) { + questions.collect { questionList -> + if (index.value >= questionList.size) { + index.value = 0 + } + currentQuestion.value = questionList[index.value] + } + } + } + + fun reset() { + proposedAnswer.value = "" + evaluatedAnswer.value = AnswerType.NONE + } + + fun newQuestion() { + reset() + index.value += 1 + updateQuestion() + } + + fun updateAnswer(text: String) { + proposedAnswer.value = text + } + + private fun calcSimilarite(str1: String, str2: String): Float { + val set1 = str1.lowercase().toSet() + val set2 = str2.lowercase().toSet() + + return set1.intersect(set2).size.toFloat() / set1.union(set2).size.toFloat() + } + + fun checkAnswer() { + val probaReponse = calcSimilarite(currentQuestion.value!!.reponse, proposedAnswer.value) + if (probaReponse >= .60f) { + evaluatedAnswer.value = AnswerType.GOOD + } else { + evaluatedAnswer.value = AnswerType.BAD + } + } + }