forked from tom.biard/ScienceQuest
Merge pull request 'RepositoryAndroid' (#25) from RepositoryAndroid into Android
Reviewed-on: tom.biard/ScienceQuest#25 je me fais confianceAndroid
commit
08cd13867d
@ -1,52 +0,0 @@
|
|||||||
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}")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package fr.iut.sciencequest.ViewModels
|
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import androidx.compose.runtime.MutableState
|
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
|
||||||
import androidx.compose.runtime.toMutableStateList
|
|
||||||
import androidx.lifecycle.LiveData
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import fr.iut.sciencequest.ViewModels.UiStates.ScientifiqueDecouvertsUIState
|
|
||||||
import fr.iut.sciencequest.model.buisness.Scientifique.fetchScientifiqueById
|
|
||||||
import fr.iut.sciencequest.model.buisness.Scientifique.fetchScientifiques
|
|
||||||
import fr.iut.sciencequest.model.dto.extensions.ToModel
|
|
||||||
import fr.iut.sciencequest.model.metier.Scientifique
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.collect
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
|
|
||||||
class ScientifiquesDecouvertsVM : ViewModel() {
|
|
||||||
var listeScientifique: MutableStateFlow<ScientifiqueDecouvertsUIState> = MutableStateFlow(ScientifiqueDecouvertsUIState())
|
|
||||||
|
|
||||||
// fun getScientifiqueById(id: Int) {
|
|
||||||
// Log.d("ViewModelScientifique", "Recup un scientifique d'id: $id")
|
|
||||||
// var scientifique: Scientifique
|
|
||||||
// viewModelScope.launch {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
fun getScientifiques(page: Int) {
|
|
||||||
Log.d("ViewModelScientifique","Recup la liste de scientifiques")
|
|
||||||
viewModelScope.launch {
|
|
||||||
fetchScientifiques(page).collect {
|
|
||||||
listeScientifique.value = ScientifiqueDecouvertsUIState(it.scientifiques.ToModel().toMutableList())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
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
|
|
||||||
)
|
|
@ -1,22 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
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,30 @@
|
|||||||
|
package fr.iut.sciencequest.model.dto.extensions
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.dto.question.QuestionDTO
|
||||||
|
import fr.iut.sciencequest.model.dto.question.QuestionWithSimpleResponseDTO
|
||||||
|
import fr.iut.sciencequest.model.metier.question.Question
|
||||||
|
import fr.iut.sciencequest.model.metier.question.QuestionWithSimpleReponse
|
||||||
|
|
||||||
|
fun QuestionDTO.ToModel(): Question {
|
||||||
|
return Question(
|
||||||
|
this.id,
|
||||||
|
this.question,
|
||||||
|
this.reponses.ToModel()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<QuestionDTO>.ToModel(): List<Question> {
|
||||||
|
val liste = mutableListOf<Question>()
|
||||||
|
for (question in this) {
|
||||||
|
liste.add(question.ToModel())
|
||||||
|
}
|
||||||
|
return liste
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Question.ToQuestionWithSimpleReponse(): QuestionWithSimpleReponse {
|
||||||
|
return QuestionWithSimpleReponse(
|
||||||
|
this.id,
|
||||||
|
this.question,
|
||||||
|
this.reponses.ToSimpleReponses()
|
||||||
|
)
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package fr.iut.sciencequest.model.dto.extensions
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.dto.question.QuestionWithSimpleResponseDTO
|
||||||
|
import fr.iut.sciencequest.model.metier.question.QuestionWithSimpleReponse
|
||||||
|
|
||||||
|
fun QuestionWithSimpleResponseDTO.ToModel(): QuestionWithSimpleReponse {
|
||||||
|
return QuestionWithSimpleReponse(
|
||||||
|
this.id,
|
||||||
|
this.question,
|
||||||
|
this.reponses.ToModel()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<QuestionWithSimpleResponseDTO>.ToModel(): List<QuestionWithSimpleReponse> {
|
||||||
|
val liste = ArrayList<QuestionWithSimpleReponse>()
|
||||||
|
for (question in this) {
|
||||||
|
liste.add(question.ToModel())
|
||||||
|
}
|
||||||
|
return liste
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package fr.iut.sciencequest.model.dto.extensions
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.dto.reponse.ReponseDTO
|
||||||
|
import fr.iut.sciencequest.model.metier.reponse.Reponse
|
||||||
|
import fr.iut.sciencequest.model.metier.reponse.ReponseSimple
|
||||||
|
|
||||||
|
fun ReponseDTO.ToModel(): Reponse {
|
||||||
|
return Reponse(
|
||||||
|
this.id,
|
||||||
|
this.reponse,
|
||||||
|
this.question.ToModel(),
|
||||||
|
this.scientifique.ToModel()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<ReponseDTO>.ToModel(): List<Reponse> {
|
||||||
|
val liste = ArrayList<Reponse>()
|
||||||
|
for (reponse in this) {
|
||||||
|
liste.add(reponse.ToModel())
|
||||||
|
}
|
||||||
|
return liste
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Reponse.ToSimpleReponse(): ReponseSimple {
|
||||||
|
return ReponseSimple(
|
||||||
|
this.id,
|
||||||
|
this.reponse
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<Reponse>.ToSimpleReponses(): List<ReponseSimple> {
|
||||||
|
val liste = ArrayList<ReponseSimple>()
|
||||||
|
for (reponse in this) {
|
||||||
|
liste.add(reponse.ToSimpleReponse())
|
||||||
|
}
|
||||||
|
return liste
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package fr.iut.sciencequest.model.dto.extensions
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.dto.reponse.ReponseSimpleDTO
|
||||||
|
import fr.iut.sciencequest.model.metier.reponse.ReponseSimple
|
||||||
|
|
||||||
|
fun ReponseSimpleDTO.ToModel(): ReponseSimple {
|
||||||
|
return ReponseSimple(
|
||||||
|
this.id,
|
||||||
|
this.reponse
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<ReponseSimpleDTO>.ToModel(): List<ReponseSimple> {
|
||||||
|
val liste = ArrayList<ReponseSimple>()
|
||||||
|
for (reponse in this) {
|
||||||
|
liste.add(reponse.ToModel())
|
||||||
|
}
|
||||||
|
return liste
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
package fr.iut.sciencequest.model.metier
|
|
||||||
class Question(
|
|
||||||
val id: Int,
|
|
||||||
val question: String,
|
|
||||||
val reponses: List<Reponse>
|
|
||||||
) {}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
|||||||
package fr.iut.sciencequest.model.metier
|
|
||||||
|
|
||||||
class Reponse (
|
|
||||||
val id: Int,
|
|
||||||
val reponse: String,
|
|
||||||
val question: Question,
|
|
||||||
val scientifique: Scientifique
|
|
||||||
) {}
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package fr.iut.sciencequest.model.metier.question
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.metier.reponse.Reponse
|
||||||
|
|
||||||
|
class Question(
|
||||||
|
val id: Int,
|
||||||
|
val question: String,
|
||||||
|
val reponses: List<Reponse>
|
||||||
|
) {}
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package fr.iut.sciencequest.model.metier.question
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.metier.reponse.Reponse
|
||||||
|
import fr.iut.sciencequest.model.metier.reponse.ReponseSimple
|
||||||
|
|
||||||
|
class QuestionWithSimpleReponse (
|
||||||
|
val id: Int,
|
||||||
|
val question: String,
|
||||||
|
val reponses: List<ReponseSimple>
|
||||||
|
)
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut.sciencequest.model.metier.reponse
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.metier.question.Question
|
||||||
|
import fr.iut.sciencequest.model.metier.Scientifique
|
||||||
|
|
||||||
|
class Reponse (
|
||||||
|
val id: Int,
|
||||||
|
val reponse: String,
|
||||||
|
val question: Question,
|
||||||
|
val scientifique: Scientifique
|
||||||
|
) {}
|
||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut.sciencequest.model.metier.reponse
|
||||||
|
|
||||||
|
class ReponseSimple {
|
||||||
|
val id: Int
|
||||||
|
val reponse: String
|
||||||
|
|
||||||
|
constructor(id: Int, reponse: String) {
|
||||||
|
this.id = id
|
||||||
|
this.reponse = reponse
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut.sciencequest.model.repositories.question
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.metier.question.Question
|
||||||
|
import fr.iut.sciencequest.model.metier.question.QuestionWithSimpleReponse
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
|
interface IQuestionRepository {
|
||||||
|
val questions: StateFlow<List<QuestionWithSimpleReponse>>
|
||||||
|
suspend fun fetchQuestions(index: Int)
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package fr.iut.sciencequest.model.repositories.question
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import fr.iut.sciencequest.model.buisness.Question.QuestionRequestService
|
||||||
|
import fr.iut.sciencequest.model.buisness.createRequestService
|
||||||
|
import fr.iut.sciencequest.model.dto.extensions.ToModel
|
||||||
|
import fr.iut.sciencequest.model.metier.question.Question
|
||||||
|
import fr.iut.sciencequest.model.metier.question.QuestionWithSimpleReponse
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import retrofit2.create
|
||||||
|
|
||||||
|
class QuestionAPIRepository : IQuestionRepository {
|
||||||
|
|
||||||
|
private val _questions = MutableStateFlow<List<QuestionWithSimpleReponse>>(emptyList())
|
||||||
|
override val questions: StateFlow<List<QuestionWithSimpleReponse>>
|
||||||
|
get() = _questions.asStateFlow()
|
||||||
|
|
||||||
|
override suspend fun fetchQuestions(index: Int) {
|
||||||
|
val serviceClient = createRequestService().create<QuestionRequestService>()
|
||||||
|
try {
|
||||||
|
_questions.value = serviceClient.getQuestions(index).questions.ToModel()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("Requete API Question", e.message.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package fr.iut.sciencequest.model.repositories.scientifique
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.metier.Scientifique
|
||||||
|
import fr.iut.sciencequest.model.metier.question.QuestionWithSimpleReponse
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
|
interface IScientifiqueRepository {
|
||||||
|
|
||||||
|
val scientifique: StateFlow<Scientifique>
|
||||||
|
val scientifiques: StateFlow<List<Scientifique>>
|
||||||
|
suspend fun fetchScientifiques(index: Int)
|
||||||
|
suspend fun fetchScientifiqueById(id: Int)
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package fr.iut.sciencequest.model.repositories.scientifique
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import fr.iut.sciencequest.model.buisness.Question.QuestionRequestService
|
||||||
|
import fr.iut.sciencequest.model.buisness.Scientifique.ScientifiqueRequestService
|
||||||
|
import fr.iut.sciencequest.model.buisness.createRequestService
|
||||||
|
import fr.iut.sciencequest.model.dto.extensions.ToModel
|
||||||
|
import fr.iut.sciencequest.model.metier.Scientifique
|
||||||
|
import fr.iut.sciencequest.stub.StubScientifique1
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import retrofit2.create
|
||||||
|
|
||||||
|
class ScientifiqueAPIRepository: IScientifiqueRepository {
|
||||||
|
|
||||||
|
private val _scientifique = MutableStateFlow(StubScientifique1.ToModel())
|
||||||
|
override val scientifique: StateFlow<Scientifique>
|
||||||
|
get() = _scientifique.asStateFlow()
|
||||||
|
|
||||||
|
private val _scientifiques = MutableStateFlow<List<Scientifique>>(emptyList())
|
||||||
|
override val scientifiques: StateFlow<List<Scientifique>>
|
||||||
|
get() = _scientifiques.asStateFlow()
|
||||||
|
|
||||||
|
override suspend fun fetchScientifiqueById(id: Int) {
|
||||||
|
val serviceClient = createRequestService().create<ScientifiqueRequestService>()
|
||||||
|
try {
|
||||||
|
_scientifique.value = serviceClient.getScientifique(id).ToModel()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("Requete API Scientifiqu", e.message.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun fetchScientifiques(index: Int) {
|
||||||
|
val serviceClient = createRequestService().create<ScientifiqueRequestService>()
|
||||||
|
try {
|
||||||
|
_scientifiques.value = serviceClient.getScientifiques(index).scientifiques.ToModel()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("Requete API Scientifiqu", e.message.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package fr.iut.sciencequest.model.repositories.scientifique
|
||||||
|
|
||||||
|
import android.content.res.Resources.NotFoundException
|
||||||
|
import fr.iut.sciencequest.model.dto.extensions.ToModel
|
||||||
|
import fr.iut.sciencequest.model.metier.Scientifique
|
||||||
|
import fr.iut.sciencequest.stub.StubScientifique1
|
||||||
|
import fr.iut.sciencequest.stub.StubScientifique2
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
|
||||||
|
class ScientifiqueStubRepostory : IScientifiqueRepository {
|
||||||
|
|
||||||
|
private var listeStub : MutableList<Scientifique> = mutableListOf(
|
||||||
|
StubScientifique1.ToModel(),
|
||||||
|
StubScientifique2.ToModel()
|
||||||
|
)
|
||||||
|
private val _scientifique = MutableStateFlow(StubScientifique1.ToModel())
|
||||||
|
override val scientifique: StateFlow<Scientifique>
|
||||||
|
get() = _scientifique.asStateFlow()
|
||||||
|
private val _scientifiques = MutableStateFlow<List<Scientifique>>(emptyList())
|
||||||
|
override val scientifiques: StateFlow<List<Scientifique>>
|
||||||
|
get() = _scientifiques.asStateFlow()
|
||||||
|
|
||||||
|
override suspend fun fetchScientifiques(index: Int) {
|
||||||
|
_scientifiques.value = listeStub
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun fetchScientifiqueById(id: Int) {
|
||||||
|
val retrieved = listeStub.find {
|
||||||
|
it.id == id
|
||||||
|
} ?: throw NotFoundException("Scientifique introuvable dans le stub")
|
||||||
|
_scientifique.value = retrieved
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setScientifiqueStubList(scientifiques: MutableList<Scientifique>) {
|
||||||
|
listeStub = scientifiques
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package fr.iut.sciencequest.viewModels
|
||||||
|
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import fr.iut.sciencequest.model.repositories.question.IQuestionRepository
|
||||||
|
import fr.iut.sciencequest.model.repositories.question.QuestionAPIRepository
|
||||||
|
import fr.iut.sciencequest.viewModels.uixStates.KahootUIState
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
class KahootViewModel(
|
||||||
|
val questionRepo: IQuestionRepository
|
||||||
|
): ViewModel() {
|
||||||
|
var uiState = MutableStateFlow(KahootUIState())
|
||||||
|
|
||||||
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
|
fun lancerPartie() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
questionRepo.fetchQuestions(2)
|
||||||
|
Log.d("KahootViewModel","J'ai trouvé ${questionRepo.questions.value.size} questions")
|
||||||
|
var count = 1
|
||||||
|
for (question in questionRepo.questions.value) {
|
||||||
|
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 * count
|
||||||
|
)
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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}")
|
||||||
|
}
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
val ApiFactory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
override fun <T : ViewModel> create(
|
||||||
|
modelClass: Class<T>
|
||||||
|
): T {
|
||||||
|
return KahootViewModel(
|
||||||
|
QuestionAPIRepository()
|
||||||
|
) as T
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package fr.iut.sciencequest.ViewModels
|
package fr.iut.sciencequest.viewModels
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import fr.iut.sciencequest.ViewModels.UiStates.LoginUIState
|
import fr.iut.sciencequest.viewModels.uiStates.LoginUIState
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
|
||||||
class LoginViewModel: ViewModel() {
|
class LoginViewModel: ViewModel() {
|
@ -0,0 +1,40 @@
|
|||||||
|
package fr.iut.sciencequest.viewModels
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import fr.iut.sciencequest.viewModels.uiStates.ScientifiqueDecouvertsUIState
|
||||||
|
import fr.iut.sciencequest.model.repositories.scientifique.IScientifiqueRepository
|
||||||
|
import fr.iut.sciencequest.model.repositories.scientifique.ScientifiqueAPIRepository
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
class ScientifiquesDecouvertsVM(
|
||||||
|
val repository: IScientifiqueRepository
|
||||||
|
) : ViewModel() {
|
||||||
|
private val _listeScientifique: MutableStateFlow<ScientifiqueDecouvertsUIState> = MutableStateFlow(ScientifiqueDecouvertsUIState())
|
||||||
|
val listeScientifique = _listeScientifique.asStateFlow()
|
||||||
|
|
||||||
|
fun getScientifiques(page: Int) {
|
||||||
|
Log.d("ViewModelScientifique","Recup la liste de scientifiques")
|
||||||
|
viewModelScope.launch {
|
||||||
|
repository.fetchScientifiques(page)
|
||||||
|
_listeScientifique.value = ScientifiqueDecouvertsUIState(repository.scientifiques.value.toMutableList())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
val ApiFactory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
override fun <T : ViewModel> create(
|
||||||
|
modelClass: Class<T>
|
||||||
|
): T {
|
||||||
|
return ScientifiquesDecouvertsVM(
|
||||||
|
ScientifiqueAPIRepository()
|
||||||
|
) as T
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package fr.iut.sciencequest.viewModels.uixStates
|
||||||
|
|
||||||
|
import fr.iut.sciencequest.model.dto.extensions.ToModel
|
||||||
|
import fr.iut.sciencequest.model.metier.question.QuestionWithSimpleReponse
|
||||||
|
import fr.iut.sciencequest.stub.StubQuestionWithReponses
|
||||||
|
|
||||||
|
data class KahootUIState (
|
||||||
|
val question: QuestionWithSimpleReponse = StubQuestionWithReponses.ToModel(),
|
||||||
|
val reponseChoisie: Boolean = false,
|
||||||
|
// NOTE : Supposé en millisecondes
|
||||||
|
val duréePartie: Long = 10_000,
|
||||||
|
val nbPoints: Int = 0
|
||||||
|
)
|
@ -1,4 +1,4 @@
|
|||||||
package fr.iut.sciencequest.ViewModels.UiStates
|
package fr.iut.sciencequest.viewModels.uiStates
|
||||||
|
|
||||||
data class LoginUIState (
|
data class LoginUIState (
|
||||||
val pseudo: String = "Pseudo",
|
val pseudo: String = "Pseudo",
|
@ -1,4 +1,4 @@
|
|||||||
package fr.iut.sciencequest.ViewModels.UiStates
|
package fr.iut.sciencequest.viewModels.uiStates
|
||||||
|
|
||||||
data class PenduUIState(
|
data class PenduUIState(
|
||||||
val isWon: Boolean = false,
|
val isWon: Boolean = false,
|
@ -1,4 +1,4 @@
|
|||||||
package fr.iut.sciencequest.ViewModels.UiStates
|
package fr.iut.sciencequest.viewModels.uiStates
|
||||||
|
|
||||||
import fr.iut.sciencequest.model.metier.Scientifique
|
import fr.iut.sciencequest.model.metier.Scientifique
|
||||||
|
|
Loading…
Reference in new issue