only fetch the question list ONCE
This commit is contained in:
parent
011ce40af1
commit
9156ad9776
2 changed files with 4 additions and 7 deletions
|
@ -31,8 +31,7 @@ fun PlayScreen(
|
||||||
idSet: Int,
|
idSet: Int,
|
||||||
model: PlayViewModel = viewModel()
|
model: PlayViewModel = viewModel()
|
||||||
) {
|
) {
|
||||||
// First update the list and set ID
|
// First update the list
|
||||||
model.setId.value = idSet
|
|
||||||
model.updateQuestionList(idSet)
|
model.updateQuestionList(idSet)
|
||||||
|
|
||||||
val question by model.currentQuestion
|
val question by model.currentQuestion
|
||||||
|
|
|
@ -7,14 +7,12 @@ import androidx.lifecycle.viewModelScope
|
||||||
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
|
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
|
||||||
import fr.uparis.diamantkennel.memorisationapplication.data.Question
|
import fr.uparis.diamantkennel.memorisationapplication.data.Question
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class PlayViewModel(application: Application) : AndroidViewModel(application) {
|
class PlayViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
private val dao = (application as MemoApplication).database.memoDao()
|
private val dao = (application as MemoApplication).database.memoDao()
|
||||||
private val initialId = -1 // invalid ID, mean that we don't have the ID yet
|
private lateinit var questions: Flow<List<Question>>
|
||||||
|
|
||||||
var setId = mutableStateOf(initialId)
|
|
||||||
private var questions = dao.loadQuestions(setId.value)
|
|
||||||
|
|
||||||
var currentQuestion = mutableStateOf<Question?>(null)
|
var currentQuestion = mutableStateOf<Question?>(null)
|
||||||
private var index = mutableStateOf(0)
|
private var index = mutableStateOf(0)
|
||||||
|
@ -26,7 +24,7 @@ class PlayViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
var showAnswer = mutableStateOf(false)
|
var showAnswer = mutableStateOf(false)
|
||||||
|
|
||||||
fun updateQuestionList(setId: Int) {
|
fun updateQuestionList(setId: Int) {
|
||||||
if (setId != initialId) {
|
if (currentQuestion.value == null) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
questions = dao.loadQuestions(setId)
|
questions = dao.loadQuestions(setId)
|
||||||
updateQuestion()
|
updateQuestion()
|
||||||
|
|
Reference in a new issue