Merge branch 'androidCompose' of https://codefirst.iut.uca.fr/git/jade.van_brabandt/3.01-QCM_MuscuMaths into androidCompose
continuous-integration/drone/push Build is passing Details

androidCompose
Maxence GUITARD 8 months ago
commit 58a7c00376

@ -19,6 +19,7 @@ import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
@ -33,47 +34,53 @@ import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import com.example.mathseduc.MainActivity
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerQuestion
import com.example.mathseduc.ui.theme.Colors
import com.example.mathseduc.viewModel.QuizMultiViewModel
import kotlinx.coroutines.channels.ticker
import okhttp3.MultipartBody
@Composable
fun QuizMultiScreen(navController: NavController, lobbyId: Int?) {
val context = LocalContext.current
val activity = LocalView.current.context as Activity
var chronoValue by remember { mutableStateOf(0.0f) }
var listQuestion by remember { mutableStateOf(ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId!!))) }
var listPlayer by remember { mutableStateOf(ControllerLobby.getPlayerInLobby(lobbyId!!)) }
var currentQuestionIndex by remember { mutableIntStateOf(0) }
val progressBarValues by remember { mutableStateOf(Array(listPlayer.size) { 0.0f }) }
val progressBarTotalValues by remember { mutableStateOf(Array(listPlayer.size) { 0.0f }) }
var quizFinished by remember { mutableStateOf(false) }
val viewModel = ViewModelProvider(navController.getViewModelStoreOwner(navController.graph.id)).get(QuizMultiViewModel::class.java)
val windowInsetsController = remember {
WindowCompat.getInsetsController(activity.window, activity.window.decorView)
}
val chronoValue by viewModel.chronoValue.collectAsState(0.0f)
val listQuestion by viewModel.listQuestion.collectAsState(ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId!!)))
val listPlayer by viewModel.listPlayer.collectAsState(ControllerLobby.getPlayerInLobby(lobbyId!!))
val currentQuestionIndex by viewModel.currentQuestionIndex.collectAsState(0)
val progressBarValues by viewModel.progressBarValues.collectAsState(List(listPlayer.size) { 0.0f })
val progressBarTotalValues by viewModel.progressBarTotalValues.collectAsState(List(listPlayer.size) { 0.0f })
val quizFinished by viewModel.quizFinished.collectAsState(false)
val windowInsetsController by viewModel.windowInsetsController.collectAsState(WindowCompat.getInsetsController(activity.window, activity.window.decorView))
windowInsetsController?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController?.hide(WindowInsetsCompat.Type.systemBars())
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
viewModel.updateListQuestion(lobbyId)
viewModel.updateListPlayer(lobbyId)
viewModel.initializeProgressBar()
viewModel.initializeProgressBarTotal()
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
LaunchedEffect(Unit) {
val timer = ticker(delayMillis = 100) // Update every 100 milliseconds
val timer = ticker(delayMillis = 1000) // Update every 1000 milliseconds
for (tick in timer) {
if (quizFinished) break
for ((index, player) in listPlayer.withIndex()) {
progressBarValues[index] += 0.1f
viewModel.updateProgressBarValues(index)
}
chronoValue += 0.1f
viewModel.updateChronoValue(1f)
if (chronoValue >= 30f) {
currentQuestionIndex++
chronoValue = 0.0f
viewModel.updateCurrentQuestionIndex()
viewModel.resetChronoValue()
}
}
}
@ -84,7 +91,7 @@ fun QuizMultiScreen(navController: NavController, lobbyId: Int?) {
if (quizFinished) break
var valueBD = ControllerLobby.getPlayerInLobby(lobbyId!!)
for ((index, player) in listPlayer.withIndex()) {
progressBarTotalValues[index] = progressBarValues[index] + valueBD[index].playertime
viewModel.updateProgressBarTotalValues(index,valueBD)
}
}
}
@ -97,7 +104,7 @@ fun QuizMultiScreen(navController: NavController, lobbyId: Int?) {
) {
for ((index, player) in listPlayer.withIndex()) {
CustomProgressBar(progressBarTotalValues[index])
CustomProgressBar(progressBarTotalValues.toMutableList()[index])
}
// Chrono ProgressBar
@ -158,7 +165,8 @@ fun QuizMultiScreen(navController: NavController, lobbyId: Int?) {
} else {
Toast.makeText(context, "Oh nan !!", Toast.LENGTH_SHORT).show()
}
currentQuestionIndex++
viewModel.resetChronoValue()
viewModel.updateCurrentQuestionIndex()
},
modifier = Modifier
.fillMaxWidth()
@ -180,7 +188,7 @@ fun QuizMultiScreen(navController: NavController, lobbyId: Int?) {
) {
Button(
onClick = {
currentQuestionIndex++
viewModel.updateCurrentQuestionIndex()
},
modifier = Modifier
.weight(2f),
@ -193,7 +201,7 @@ fun QuizMultiScreen(navController: NavController, lobbyId: Int?) {
} else {
Toast.makeText(context, "Fini !!", Toast.LENGTH_SHORT).show()
navController.navigate("home")
quizFinished = true
viewModel.updateQuizFinished(true)
}
}
}

@ -1,7 +1,6 @@
package com.example.mathseduc.ui
import android.app.Activity
import android.content.Intent
import android.content.res.Configuration
import android.os.Handler
import android.os.Looper
@ -52,13 +51,15 @@ import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.navigation.NavController
import com.example.mathseduc.QuizMultiActivity
import com.example.mathseduc.MainActivity
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.ui.theme.Colors
import com.example.mathseduc.viewModel.ServerDetailsViewModel
import okhttp3.MultipartBody
@Composable
fun LeaveLobbyDialog(namelobby: String, onConfirmLeave: () -> Unit, onCancelLeave: () -> Unit) {
fun LeaveLobbyDialog(namelobby: String,lobbyId : Int?, onConfirmLeave: () -> Unit, onCancelLeave: () -> Unit) {
val context = LocalContext.current
AlertDialog(
@ -67,6 +68,9 @@ fun LeaveLobbyDialog(namelobby: String, onConfirmLeave: () -> Unit, onCancelLeav
confirmButton = {
Button(
onClick = {
if (lobbyId != null) {
myBackPressed(lobbyId)
}
onConfirmLeave()
}
) {
@ -121,6 +125,7 @@ fun ServerDetailPage(navController: NavController, serverName: String?, lobbyId:
viewModel.updateIsCreator(lobbyId)
if(viewModel.Launchedlobby(lobbyId)){
/*
val intent = Intent(context, QuizMultiActivity::class.java)
intent.putExtra("serverName", serverName)
intent.putExtra("lobbyId", lobbyId)
@ -128,6 +133,8 @@ fun ServerDetailPage(navController: NavController, serverName: String?, lobbyId:
intent.putExtra("nbPlayers", nbPlayers)
intent.putExtra("lobbyDifficulty", lobbyDifficulty)
context.startActivity(intent)
*/
navController.navigate("quizMultiScreen/${lobbyId}")
viewModel.updateRefresh(false)
}
@ -262,7 +269,23 @@ fun ServerDetailPage(navController: NavController, serverName: String?, lobbyId:
}
}
if (showDialog) {
LeaveLobbyDialog(serverName, onConfirmLeave = {navController.navigate("multiplayer")},onCancelLeave = { viewModel.updateShowDialog(false) })
LeaveLobbyDialog(serverName,lobbyId, onConfirmLeave = {navController.navigate("multiplayer")},onCancelLeave = { viewModel.updateShowDialog(false) })
}
}
}
private fun myBackPressed(lobbyId: Int) {
ControllerUtiliser.DeleteUtiliserForLobby(MainActivity.idPlayerConnected, lobbyId)
if (ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) {
val idNextPlayerCreator = ControllerUtiliser.getIdNextPlayerInLobby(lobbyId)
if (idNextPlayerCreator == -1) {
ControllerLobby.deleteLobby(lobbyId)
} else {
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilder.addFormDataPart("idplayercreator", idNextPlayerCreator.toString())
ControllerLobby.updateLobbyIdCreatorLobby(lobbyId, formDataBuilder)
}
}
}

@ -0,0 +1,93 @@
package com.example.mathseduc.viewModel
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.ViewModel
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerQuestion
import com.example.mathseduc.models.Lobby
import com.example.mathseduc.models.Player
import com.example.mathseduc.models.Question
import com.example.mathseduc.models.Utiliser
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.withIndex
import kotlin.coroutines.CoroutineContext
class QuizMultiViewModel : ViewModel() {
private var chronoValueState = MutableStateFlow(0.0f)
val chronoValue : StateFlow<Float> = chronoValueState
private var listQuestionState = MutableStateFlow<List<Question>?>(emptyList())
val listQuestion : StateFlow<List<Question>?> = listQuestionState
private var listPlayerState = MutableStateFlow<List<Utiliser>>(emptyList())
val listPlayer : StateFlow<List<Utiliser>> = listPlayerState
private var currentQuestionIndexState = MutableStateFlow(0)
val currentQuestionIndex : StateFlow<Int> = currentQuestionIndexState
private var progressBarValuesState = MutableStateFlow<List<Float>>(emptyList())
val progressBarValues : StateFlow<List<Float>> = progressBarValuesState
private var progressBarTotalValuesState = MutableStateFlow<List<Float>>(emptyList())
val progressBarTotalValues : StateFlow<List<Float>> = progressBarTotalValuesState
private var quizFinishedState = MutableStateFlow(false)
val quizFinished : StateFlow<Boolean> = quizFinishedState
private var windowInsetsControllerState = MutableStateFlow(null)
val windowInsetsController : StateFlow<WindowInsetsControllerCompat?> = windowInsetsControllerState
fun updateListQuestion(lobbyId : Int?){
this.listQuestionState.update{ ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId!!)) }
}
fun updateListPlayer(lobbyId : Int?){
this.listPlayerState.update{ ControllerLobby.getPlayerInLobby(lobbyId!!) }
}
fun initializeProgressBar(){
this.progressBarValuesState.update{ List(this.listPlayer.value.size) { 0.0f } }
}
fun initializeProgressBarTotal(){
this.progressBarTotalValuesState.update{ List(this.listPlayer.value.size) { 0.0f } }
}
fun updateChronoValue(time : Float){
this.chronoValueState.update { this.chronoValue.value + time }
}
fun resetChronoValue(){
this.chronoValueState.update { 0.0f }
}
fun updateCurrentQuestionIndex(){
this.currentQuestionIndexState.update { this.currentQuestionIndex.value + 1 }
}
fun updateQuizFinished(bool : Boolean){
this.quizFinishedState.update { bool }
}
fun updateProgressBarValues(index : Int){
this.progressBarValuesState.update {
val newList = it.toMutableList()
newList[index] += 1f
newList
}
}
fun updateProgressBarTotalValues(index : Int,valueBD : List<Utiliser>){
this.progressBarTotalValuesState.update {
val newList = it.toMutableList()
newList[index] += this.progressBarValues.value[index] + valueBD[index].playertime.toFloat()
newList
}
}
}
Loading…
Cancel
Save