[ADD] Va chercher une liste de question sur l'API (index en dur)

RepositoryAndroid
Renaud BEURET 1 year ago
parent a8a5925009
commit 15a6829ee8

@ -4,9 +4,12 @@ import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import fr.iut.sciencequest.ViewModels.UiStates.KahootUIState
import fr.iut.sciencequest.model.buisness.Question.fetchQuestions
import fr.iut.sciencequest.stub.StubQuestionWithReponses2
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
class KahootViewModel: ViewModel() {
var uiState = MutableStateFlow(KahootUIState())
@ -14,16 +17,22 @@ class KahootViewModel: ViewModel() {
private val handler = Handler(Looper.getMainLooper())
fun lancerPartie() {
handler.postDelayed(
{
Log.d("KahootViewModel","J'actualise les questions")
uiState.value = KahootUIState(StubQuestionWithReponses2,
duréePartie = uiState.value.duréePartie,
nbPoints = uiState.value.nbPoints,
reponseChoisie = false)
},
uiState.value.duréePartie
)
viewModelScope.launch {
fetchQuestions(2).collect() {
for (question in it.questions) {
handler.postDelayed(
{
Log.d("KahootViewModel","J'actualise les questions")
uiState.value = KahootUIState(question,
duréePartie = uiState.value.duréePartie,
nbPoints = uiState.value.nbPoints,
reponseChoisie = false)
},
uiState.value.duréePartie
)
}
}
}
}
// NOTE : tpsReponse en ms

@ -7,5 +7,5 @@ import retrofit2.http.Query
interface QuestionRequestService {
@GET("questions?page")
fun getQuestions(@Query("page") index: Int): Call<QuestionListDTO>
suspend fun getQuestions(@Query("page") index: Int): QuestionListDTO
}

@ -1,45 +1,22 @@
package fr.iut.sciencequest.model.buisness.Question
import android.util.Log
import fr.iut.sciencequest.model.buisness.Scientifique.ScientifiqueRequestService
import fr.iut.sciencequest.model.buisness.createRequestService
import fr.iut.sciencequest.model.dto.question.QuestionListDTO
import kotlinx.coroutines.flow.flow
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.create
import java.lang.IllegalArgumentException
fun fetchQuestions(index: Int) {
//val serviceClient = createRequestService().create<QuestionRequestService>()
//Log.d("Requete API","Fetch un scientifique")
//serviceClient.getQuestion(index).enqueue(
// object: Callback<QuestionListDTO> {
// override fun onResponse(
// call: Call<QuestionListDTO>,
// response: Response<QuestionListDTO>
// ) {
// // NOTE : il faudrait probablement utiliser une autre exception
// // exception personnalisée ?
// val data = response.body() ?:
// throw IllegalArgumentException("ERREUR : l'api a donné une réponse vide")
// // Devrait appeler le ModelView, la méthode onResponse ne renvoit rien
// // Pour le moment des print pour vérifier que la requêtre fonctionne
// // sans avoir besoin des vues.
// for (question in data.questions) {
// Log.d("Requete Question", "id question: " + question.id.toString())
// Log.d("Requete Question","Libelle: " + question.question)
// Log.d("Requete Question","Reponses: ")
// for (reponse in question.reponses) {
// Log.d("Requete Question","id: " + reponse.id)
// Log.d("Requete Question","Libelle: " + reponse.reponse)
// }
// }
// }
//
// override fun onFailure(call: Call<QuestionListDTO>, t: Throwable) {
// Log.e("Requete API","Erreur lors d'une requete api")
// throw t
// }
// }
//)
}
suspend fun fetchQuestions(index: Int) = flow {
val serviceClient = createRequestService().create<QuestionRequestService>()
try {
val response = serviceClient.getQuestions(index)
emit(response)
} catch (e: Exception) {
Log.e("Requete API Question",e.message.toString())
}
}

Loading…
Cancel
Save