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 5ea4bcb..f812f18 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt @@ -31,7 +31,7 @@ fun PlayScreen( idSet: Int, model: PlayViewModel = viewModel() ) { - // First update the list + // First update the list of questions model.updateQuestionList(idSet) val question by model.currentQuestion 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 09cb4c0..aec6276 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 @@ -7,12 +7,11 @@ import androidx.lifecycle.viewModelScope import fr.uparis.diamantkennel.memorisationapplication.MemoApplication import fr.uparis.diamantkennel.memorisationapplication.data.Question import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch class PlayViewModel(application: Application) : AndroidViewModel(application) { private val dao = (application as MemoApplication).database.memoDao() - private lateinit var questions: Flow> + private lateinit var questions: List var currentQuestion = mutableStateOf(null) private var index = mutableStateOf(0) @@ -26,22 +25,20 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { fun updateQuestionList(setId: Int) { if (currentQuestion.value == null) { viewModelScope.launch(Dispatchers.IO) { - questions = dao.loadQuestions(setId) - updateQuestion() + dao.loadQuestions(setId).collect { questionList -> + questions = questionList.shuffled() + updateQuestion() + } } } } private fun updateQuestion() { - viewModelScope.launch { - questions.collect { questionList -> - if (index.value >= questionList.size) { - /* Fin des questions */ - index.value = 0 - } - currentQuestion.value = questionList[index.value] - } + if (index.value >= questions.size) { + /* Fin des questions */ + index.value = 0 } + currentQuestion.value = questions[index.value] } private fun reset() {