forked from tom.biard/ScienceQuest
Merge branch 'Android' of https://codefirst.iut.uca.fr/git/tom.biard/ScienceQuest into Android
# Conflicts: # android/app/src/main/java/fr/iut/sciencequest/ViewModels/PenduViewModel.kt # android/app/src/main/java/fr/iut/sciencequest/view/games/Pendu.ktRepositoryAndroid^2
commit
3288aa1ee1
@ -0,0 +1,52 @@
|
||||
package fr.iut.sciencequest.ViewModels
|
||||
|
||||
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())
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
fun lancerPartie() {
|
||||
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
|
||||
fun ajouterPoints(tpsReponse: Long) {
|
||||
Log.d("KahootViewModel","Je reçois une réponse")
|
||||
if (uiState.value.reponseChoisie) {
|
||||
Log.d("KahootViewModel","Le joueur a déjà répondu")
|
||||
return
|
||||
}
|
||||
val nbPoints: Int = (10_000 - tpsReponse).toInt()
|
||||
uiState.value = KahootUIState(uiState.value.question,
|
||||
duréePartie = uiState.value.duréePartie,
|
||||
nbPoints = uiState.value.nbPoints + nbPoints,
|
||||
reponseChoisie = true)
|
||||
Log.d("KahootViewModel","Le joueur à ${uiState.value.nbPoints}")
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package fr.iut.sciencequest.ViewModels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.iut.sciencequest.ViewModels.UiStates.LoginUIState
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
||||
class LoginViewModel: ViewModel() {
|
||||
var uiState = MutableStateFlow(LoginUIState())
|
||||
|
||||
fun setPseudo(pseudo: String) {
|
||||
uiState.value = LoginUIState(pseudo, uiState.value.mdp)
|
||||
}
|
||||
|
||||
fun setMdp(mdp: String) {
|
||||
uiState.value = LoginUIState(uiState.value.pseudo, mdp)
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package fr.iut.sciencequest.ViewModels.UiStates
|
||||
|
||||
import fr.iut.sciencequest.model.dto.question.QuestionWithSimpleResponseDTO
|
||||
import fr.iut.sciencequest.model.dto.reponse.ReponseSimpleDTO
|
||||
import fr.iut.sciencequest.stub.StubQuestionWithReponses
|
||||
|
||||
data class KahootUIState (
|
||||
val question: QuestionWithSimpleResponseDTO = StubQuestionWithReponses,
|
||||
val reponseChoisie: Boolean = false,
|
||||
// NOTE : Supposé en millisecondes
|
||||
val duréePartie: Long = 10_000,
|
||||
val nbPoints: Int = 0
|
||||
)
|
@ -0,0 +1,6 @@
|
||||
package fr.iut.sciencequest.ViewModels.UiStates
|
||||
|
||||
data class LoginUIState (
|
||||
val pseudo: String = "Pseudo",
|
||||
val mdp: String = "Mot de passe"
|
||||
)
|
@ -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())
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package fr.iut.sciencequest.model.dto
|
||||
|
||||
class JeuDTO (
|
||||
val id: Int,
|
||||
val nom: String,
|
||||
val nbrParties: UInt
|
||||
)
|
@ -0,0 +1,11 @@
|
||||
package fr.iut.sciencequest.model.dto
|
||||
|
||||
import fr.iut.sciencequest.model.dto.joueur.JoueurSimpleDTO
|
||||
|
||||
class PartieDTO (
|
||||
val id: Int,
|
||||
val codeInvitation: String,
|
||||
val joueurs: List<JoueurSimpleDTO>,
|
||||
val jeu: JeuDTO,
|
||||
val thematiques: List<ThematiqueDTO>
|
||||
)
|
@ -1,4 +1,4 @@
|
||||
package fr.iut.sciencequest.model.dto
|
||||
package fr.iut.sciencequest.model.dto.difficulte
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
@ -0,0 +1,6 @@
|
||||
package fr.iut.sciencequest.model.dto.difficulte
|
||||
|
||||
class DifficulteSimpleDTO (
|
||||
val id: Int,
|
||||
val libelle: String
|
||||
)
|
@ -0,0 +1,9 @@
|
||||
package fr.iut.sciencequest.model.dto.joueur
|
||||
|
||||
import fr.iut.sciencequest.model.dto.PartieDTO
|
||||
|
||||
class JoueurDTO (
|
||||
val id: Int,
|
||||
val pseudo: String,
|
||||
val partie: PartieDTO
|
||||
)
|
@ -0,0 +1,6 @@
|
||||
package fr.iut.sciencequest.model.dto.joueur
|
||||
|
||||
class JoueurSimpleDTO (
|
||||
val id: Int,
|
||||
val pseudo: String,
|
||||
)
|
Loading…
Reference in new issue