From 9156ad9776401dee96e8bbd56ab72419125d9686 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 4 Jan 2024 16:37:42 +0100 Subject: [PATCH] only fetch the question list ONCE --- .../diamantkennel/memorisationapplication/PlayScreen.kt | 3 +-- .../memorisationapplication/ui/PlayViewModel.kt | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) 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 fd667ff..5ea4bcb 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/PlayScreen.kt @@ -31,8 +31,7 @@ fun PlayScreen( idSet: Int, model: PlayViewModel = viewModel() ) { - // First update the list and set ID - model.setId.value = idSet + // First update the list 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 c42c8d2..09cb4c0 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,14 +7,12 @@ 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 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) + private lateinit var questions: Flow> var currentQuestion = mutableStateOf(null) private var index = mutableStateOf(0) @@ -26,7 +24,7 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) { var showAnswer = mutableStateOf(false) fun updateQuestionList(setId: Int) { - if (setId != initialId) { + if (currentQuestion.value == null) { viewModelScope.launch(Dispatchers.IO) { questions = dao.loadQuestions(setId) updateQuestion()