androidCompose
Maxence GUITARD 1 year ago
commit d7a1267223

@ -28,11 +28,8 @@
<activity android:name=".MultiActivity" /> <activity android:name=".MultiActivity" />
<activity android:name=".ConnexionPlayerActivity" /> <activity android:name=".ConnexionPlayerActivity" />
<activity android:name=".CreateLobbyActivity" /> <activity android:name=".CreateLobbyActivity" />
<!--
<activity android:name=".ServerDetailsActivity" /> <activity android:name=".ServerDetailsActivity" />
<activity android:name=".QuizMultiActivity" /> <activity android:name=".QuizMultiActivity" />
-->
</application> </application>
</manifest> </manifest>

@ -1,5 +1,6 @@
package com.example.mathseduc package com.example.mathseduc
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
@ -12,6 +13,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -39,9 +41,9 @@ class ConnexionPlayerActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun ConnexionPlayerContent(activity: ConnexionPlayerActivity) { fun ConnexionPlayerContent(activity: ConnexionPlayerActivity) {
var nickname by remember { mutableStateOf("") } var nickname by rememberSaveable { mutableStateOf("") }
var password by remember { mutableStateOf("") } var password by rememberSaveable { mutableStateOf("") }
var showDialog by remember { mutableStateOf(false) } var showDialog by rememberSaveable { mutableStateOf(false) }
val context = LocalContext.current val context = LocalContext.current
@ -121,22 +123,22 @@ fun ConnexionPlayerContent(activity: ConnexionPlayerActivity) {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( Button(
onClick = { onClick = {
val isAuthenticated = ControllerPlayer.authenticateUser(nickname, password) val isAuthenticated = ControllerPlayer.authenticateUser(nickname, password)
if (isAuthenticated != -1) { if (isAuthenticated != -1) {
MainActivity.idPlayerConnected = isAuthenticated MainActivity.idPlayerConnected = isAuthenticated
Toast.makeText(context, "Connexion réussie, bienvenue $nickname !", Toast.LENGTH_SHORT).show() Toast.makeText(context, "Connexion réussie, bienvenue $nickname !", Toast.LENGTH_SHORT).show()
} else { } else {
Toast.makeText(context, "Connexion échouée. Veuillez réessayer.", Toast.LENGTH_SHORT).show() Toast.makeText(context, "Connexion échouée. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
} }
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(48.dp) .height(48.dp)
) { ) {
Text("Login") Text("Login")
} }
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))

@ -1,6 +1,7 @@
package com.example.mathseduc package com.example.mathseduc
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable

@ -0,0 +1,35 @@
package com.example.mathseduc
import android.os.Bundle
import android.os.CountDownTimer
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.example.mathseduc.ui.QuizMultiScreen
import com.example.mathseduc.ui.theme.MathsEducTheme
class QuizMultiActivity : ComponentActivity() {
companion object {
var countDownTimer: CountDownTimer? = null
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MathsEducTheme {
val lobbyId = intent.getIntExtra("lobbyId",-1)
QuizMultiScreen(lobbyId) //TODO sus
}
}
}
override fun onDestroy() {
super.onDestroy()
// Stop the CountDownTimer to avoid memory leaks
countDownTimer?.cancel()
}
}

@ -0,0 +1,230 @@
package com.example.mathseduc
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerPlayer
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.models.Player
import com.example.mathseduc.ui.theme.Colors
import okhttp3.MultipartBody
class ServerDetailsActivity : ComponentActivity() {
private var playerList: List<Player> = emptyList()
private val handler = Handler(Looper.getMainLooper())
private val refreshInterval: Long = 2000
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
myBackPressed()
}
}
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
setContent {
ServerDetailPage()
}
/*
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
//setContentView(R.layout.activity_server_details)
val serverName = intent.getStringExtra("serverName")
val serverNameTextView = findViewById<TextView>(R.id.titleServerDetails)
serverNameTextView.text = serverName
val lobbyId = intent.getIntExtra("lobbyId", -1)
if (savedInstanceState != null) {
playerList = savedInstanceState?.getParcelableArrayList("playerList") ?: emptyList()
} else {
val playerId = ControllerPlayer.getPlayersIdFromLobbyId(lobbyId)
if (playerId != null) {
playerList = playerId.mapNotNull { playerId ->
ControllerPlayer.getPlayerInfoById(playerId.toString())
}
}
}
val listViewPlayers = findViewById<ListView>(R.id.listViewPlayers)
playerAdapter = PlayerAdapter(this, playerList)
listViewPlayers.adapter = playerAdapter
handler.postDelayed(refreshRunnable, refreshInterval)
val btnLaunchQuiz = findViewById<Button>(R.id.btnLaunchQuiz)
if (ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) {
btnLaunchQuiz.visibility = View.VISIBLE
btnLaunchQuiz.setOnClickListener {
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilder.addFormDataPart("launched", "1")
ControllerLobby.updateLobbyLauched(lobbyId,formDataBuilder)
val intent = Intent(this, QuizMultiActivity::class.java)
intent.putExtra("lobbyId", lobbyId)
startActivity(intent)
}
} else {
btnLaunchQuiz.visibility = View.GONE
}
*/
}
private fun myBackPressed() {
val lobbyId = intent.getIntExtra("lobbyId", -1)
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)
}
}
//handler.removeCallbacks(refreshRunnable)
finish()
}
@Composable
fun ServerDetailPage() {
val context = LocalContext.current
val lobbyName = intent.getStringExtra("serverName")
val lobbyId = intent.getIntExtra("lobbyId",-1)
var isCreator by rememberSaveable { mutableStateOf(false) }
var playerListInfos: List<Player> by rememberSaveable { mutableStateOf(emptyList()) }
var playerList by rememberSaveable { mutableStateOf(ControllerPlayer.getPlayersIdFromLobbyId(lobbyId.toString()) ?: emptyList()) }
var refreshState by rememberSaveable { mutableStateOf(true) }
DisposableEffect(refreshState) {
val handler = Handler(Looper.getMainLooper())
val refreshRunnable = object : Runnable {
override fun run() {
playerList = ControllerPlayer.getPlayersIdFromLobbyId(lobbyId.toString())!!
playerListInfos = playerList.mapNotNull { playerId ->
ControllerPlayer.getPlayerInfoById(playerId.toString())
}
isCreator = ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)
if(ControllerLobby.lobbyIsLaunched(lobbyId)){
val intent = Intent(context, QuizMultiActivity::class.java)
intent.putExtra("lobbyId", lobbyId)
context.startActivity(intent)
refreshState = false
}
if (refreshState){
handler.postDelayed(this,3000)
Log.e("MainActivity", "Refresh ServerDetails")
}
}
}
handler.post(refreshRunnable)
onDispose {
refreshState = false
handler.removeCallbacks(refreshRunnable)
}
}
val modifier = Modifier
.fillMaxSize()
.padding(16.dp)
Column(
modifier = modifier,
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = lobbyName!!,
color = Color.White,
fontWeight = FontWeight.Bold
)
LazyColumn(
modifier = Modifier
.fillMaxSize()
.weight(0.75f)
) {
items(playerListInfos) { player ->
Text(
text = player.nickname,
fontSize = 18.sp,
color = Colors.White,
modifier = Modifier.weight(1f)
)
}
}
if (isCreator) {
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilder.addFormDataPart("launched", "1")
Button(
onClick = {
ControllerLobby.updateLobbyLauched(lobbyId,formDataBuilder)
},
shape = RoundedCornerShape(15),
enabled = isCreator,
colors = ButtonDefaults.buttonColors(Colors.Green),
modifier = Modifier
.fillMaxWidth()
.height(48.dp)
) {
Text(
text = "LAUNCH",
color = Color.White,
fontWeight = FontWeight.Bold
)
}
}
}
}
}

@ -13,7 +13,7 @@ import java.io.IOException
class ControllerPlayer { class ControllerPlayer {
companion object { companion object {
fun getPlayersIdFromLobbyId(lobbyId: Int): List<Int>? { fun getPlayersIdFromLobbyId(lobbyId: String?): List<Int>? {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy) StrictMode.setThreadPolicy(policy)

@ -1,5 +1,8 @@
package com.example.mathseduc.models package com.example.mathseduc.models
import android.os.Parcel
import android.os.Parcelable
data class Lobby( data class Lobby(
val id: Int, val id: Int,
val name: String, val name: String,
@ -9,4 +12,40 @@ data class Lobby(
val idchapter: Int, val idchapter: Int,
val difficulty: Int, val difficulty: Int,
val launched: Int val launched: Int
) ) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readString() ?: "",
parcel.readString() ?: "",
parcel.readInt(),
parcel.readInt(),
parcel.readInt(),
parcel.readInt(),
parcel.readInt()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(id)
parcel.writeString(name)
parcel.writeString(password)
parcel.writeInt(nbplayers)
parcel.writeInt(idplayercreator)
parcel.writeInt(idchapter)
parcel.writeInt(difficulty)
parcel.writeInt(launched)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Lobby> {
override fun createFromParcel(parcel: Parcel): Lobby {
return Lobby(parcel)
}
override fun newArray(size: Int): Array<Lobby?> {
return arrayOfNulls(size)
}
}
}

@ -1,6 +1,9 @@
package com.example.mathseduc.ui package com.example.mathseduc.ui
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
@ -34,8 +37,13 @@ import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.example.mathseduc.MainActivity
import com.example.mathseduc.ServerDetailsActivity
import com.example.mathseduc.controllers.ControllerChapter import com.example.mathseduc.controllers.ControllerChapter
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.ui.theme.Colors import com.example.mathseduc.ui.theme.Colors
import okhttp3.MultipartBody
@Composable @Composable
fun CreateLobbyPage() { fun CreateLobbyPage() {
@ -46,8 +54,9 @@ fun CreateLobbyPage() {
var chapter by remember { mutableStateOf("") } var chapter by remember { mutableStateOf("") }
var expandedDifficulty by remember { mutableStateOf(false) } var expandedDifficulty by remember { mutableStateOf(false) }
var expandedChapter by remember { mutableStateOf(false) } var expandedChapter by remember { mutableStateOf(false) }
var sizeDifficulty by remember { mutableStateOf(IntSize.Zero) } var size by remember { mutableStateOf(IntSize.Zero) }
var sizeChapter by remember { mutableStateOf(IntSize.Zero) }
val difficultyOptions = listOf("Facile", "Moyen", "Difficile")
val context = LocalContext.current val context = LocalContext.current
val keyboardController = LocalSoftwareKeyboardController.current val keyboardController = LocalSoftwareKeyboardController.current
@ -96,7 +105,6 @@ fun CreateLobbyPage() {
Box { Box {
val difficultyOptions = listOf("Facile", "Moyen", "Difficile")
TextField( TextField(
value = difficulty, value = difficulty,
onValueChange = { difficulty = it }, onValueChange = { difficulty = it },
@ -113,7 +121,7 @@ fun CreateLobbyPage() {
expandedDifficulty = !expandedDifficulty expandedDifficulty = !expandedDifficulty
}) })
.onGloballyPositioned { .onGloballyPositioned {
sizeDifficulty = it.size size = it.size
} }
.fillMaxWidth() .fillMaxWidth()
.background(Colors.White) .background(Colors.White)
@ -133,7 +141,7 @@ fun CreateLobbyPage() {
expanded = expandedDifficulty, expanded = expandedDifficulty,
onDismissRequest = { expandedDifficulty = false }, onDismissRequest = { expandedDifficulty = false },
modifier = Modifier modifier = Modifier
.width(with(LocalDensity.current) { sizeDifficulty.width.toDp() }) .width(with(LocalDensity.current) { size.width.toDp() })
.background(Colors.White) .background(Colors.White)
.padding(8.dp) .padding(8.dp)
) { ) {
@ -169,7 +177,7 @@ fun CreateLobbyPage() {
expandedChapter = !expandedChapter expandedChapter = !expandedChapter
}) })
.onGloballyPositioned { .onGloballyPositioned {
sizeChapter = it.size size = it.size
} }
.fillMaxWidth() .fillMaxWidth()
.background(Colors.White) .background(Colors.White)
@ -209,7 +217,50 @@ fun CreateLobbyPage() {
Button( Button(
onClick = { onClick = {
// Handle button click (Create Lobby) // Handle button click (Create Lobby)
// Use the collected data: lobbyName, password, nbPlayers, difficulty, chapter val selectedChapter = ControllerChapter.getChapters()?.find { it.name == chapter }
val difficultyNum = difficultyOptions.indexOf(difficulty) + 1
if (lobbyName.isBlank() || password.isBlank() || nbPlayers.isBlank() || nbPlayers.toInt() <= 0 || difficulty.isBlank() || selectedChapter == null) {
// Show error message if any field is invalid
Toast.makeText(context, "Invalid input. Please check all fields.", Toast.LENGTH_SHORT).show()
} else {
try {
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
// Add fields to form data
formDataBuilder.addFormDataPart("name", lobbyName)
formDataBuilder.addFormDataPart("password", password)
formDataBuilder.addFormDataPart("nbplayers", nbPlayers)
formDataBuilder.addFormDataPart("idplayercreator", MainActivity.idPlayerConnected.toString())
formDataBuilder.addFormDataPart("idchapter", selectedChapter.id)
formDataBuilder.addFormDataPart("difficulty", difficultyNum.toString())
Log.e("formData : ",formDataBuilder.toString())
// Call createLobby function from ControllerLobby
val lobbyId = ControllerLobby.createLobby(formDataBuilder)
// Check if lobby creation is successful
if (lobbyId != -1) {
val formDataBuilderConnexion = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilderConnexion.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
formDataBuilderConnexion.addFormDataPart("idlobby", lobbyId.toString())
formDataBuilderConnexion.addFormDataPart("playertime", "0")
ControllerUtiliser.createUtiliserByIdLobby(formDataBuilderConnexion)
Toast.makeText(context, "Lobby created successfully!", Toast.LENGTH_SHORT).show()
val intent = Intent(context, ServerDetailsActivity::class.java)
intent.putExtra("lobbyId", lobbyId)
intent.putExtra("serverName", lobbyName)
context.startActivity(intent)
} else {
Toast.makeText(context, "Failed to create lobby. Please try again.", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
// Log en cas d'erreur
Log.e("CreateLobby", "Error creating lobby", e)
Toast.makeText(context, "Failed to create lobby. Please try again.", Toast.LENGTH_SHORT).show()
}
}
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -218,7 +269,6 @@ fun CreateLobbyPage() {
Text(text = "Create Lobby", color = Colors.White) Text(text = "Create Lobby", color = Colors.White)
} }
} }
} }

@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp
import com.example.mathseduc.ConnexionPlayerActivity import com.example.mathseduc.ConnexionPlayerActivity
import com.example.mathseduc.MainActivity import com.example.mathseduc.MainActivity
import com.example.mathseduc.MultiActivity import com.example.mathseduc.MultiActivity
import com.example.mathseduc.QuizMultiActivity
import com.example.mathseduc.R import com.example.mathseduc.R
import com.example.mathseduc.ui.theme.Colors import com.example.mathseduc.ui.theme.Colors
@ -54,7 +55,10 @@ fun HomePage() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( Button(
onClick = { /* Handle solo button click */ }, onClick = {
val intent = Intent(context, QuizMultiActivity::class.java)
context.startActivity(intent)
},
shape = RoundedCornerShape(15), shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Green), colors = ButtonDefaults.buttonColors(Colors.Green),
modifier = Modifier modifier = Modifier

@ -2,6 +2,8 @@ package com.example.mathseduc.ui
import android.content.Intent import android.content.Intent
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
@ -24,7 +26,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -34,33 +36,42 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.example.mathseduc.CreateLobbyActivity import com.example.mathseduc.CreateLobbyActivity
import com.example.mathseduc.MainActivity import com.example.mathseduc.MainActivity
import com.example.mathseduc.ServerDetailsActivity
import com.example.mathseduc.controllers.ControllerChapter import com.example.mathseduc.controllers.ControllerChapter
import com.example.mathseduc.controllers.ControllerLobby import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.models.Lobby import com.example.mathseduc.models.Lobby
import com.example.mathseduc.ui.theme.Colors import com.example.mathseduc.ui.theme.Colors
import okhttp3.MultipartBody
@Composable @Composable
fun MultiPage() { fun MultiPage() {
val context = LocalContext.current val context = LocalContext.current
var selectedItem by remember { mutableStateOf<Lobby?>(null) } var selectedItem by rememberSaveable { mutableStateOf<Lobby?>(null) }
var lobbyList by remember { mutableStateOf(ControllerLobby.getLobbies() ?: emptyList()) } var lobbyList by rememberSaveable { mutableStateOf(ControllerLobby.getLobbies() ?: emptyList()) }
var refreshState by remember { mutableStateOf(true) } var refreshState by rememberSaveable { mutableStateOf(true) }
DisposableEffect(refreshState) { DisposableEffect(refreshState) {
val handler = Handler(Looper.getMainLooper()) val handler = Handler(Looper.getMainLooper())
val refreshRunnable = object : Runnable { val refreshRunnable = object : Runnable {
override fun run() { override fun run() {
lobbyList = emptyList()
lobbyList = ControllerLobby.getLobbies()!! lobbyList = ControllerLobby.getLobbies()!!
selectedItem = null selectedItem = null
handler.postDelayed(this, 3000) if (refreshState){
handler.postDelayed(this,3000)
Log.e("MainActivity", "Refresh Multi")
}
} }
} }
handler.postDelayed(refreshRunnable, 3000) handler.post(refreshRunnable)
onDispose { onDispose {
Toast.makeText(context, "Au REVOIR UwU", Toast.LENGTH_SHORT).show()
refreshState = false
handler.removeCallbacks(refreshRunnable) handler.removeCallbacks(refreshRunnable)
} }
} }
@ -103,9 +114,10 @@ fun MultiPage() {
.weight(1f) .weight(1f)
.padding(start = 20.dp) .padding(start = 20.dp)
) )
Button( Button(
onClick = { onClick = {
refreshState = false
val intent = Intent(context, CreateLobbyActivity::class.java) val intent = Intent(context, CreateLobbyActivity::class.java)
context.startActivity(intent) context.startActivity(intent)
}, },
@ -127,6 +139,24 @@ fun MultiPage() {
items(lobbyList ?: emptyList()) { lobby -> items(lobbyList ?: emptyList()) { lobby ->
LobbyItem(lobby, selectedItem == lobby) { LobbyItem(lobby, selectedItem == lobby) {
selectedItem = it selectedItem = it
refreshState = false
if (ControllerLobby.getNbPlayerInLobby(selectedItem!!.id) < selectedItem!!.nbplayers){
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilder.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
formDataBuilder.addFormDataPart("idlobby", selectedItem!!.id.toString())
formDataBuilder.addFormDataPart("playertime", "0")
ControllerUtiliser.createUtiliserByIdLobby(formDataBuilder)
val intent = Intent(context, ServerDetailsActivity::class.java)
intent.putExtra("serverName", selectedItem!!.name)
intent.putExtra("lobbyId", selectedItem!!.id)
context.startActivity(intent)
} else {
Toast.makeText(context, "Oh nan, le serveur est déjà plein ! Réessayer plus tard.", Toast.LENGTH_SHORT).show()
}
} }
} }
} }
@ -136,6 +166,8 @@ fun MultiPage() {
@Composable @Composable
fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) { fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) {
val nbCurrentPlayer = ControllerLobby.getNbPlayerInLobby(lobby.id)
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

@ -0,0 +1,207 @@
package com.example.mathseduc.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerQuestion
import com.example.mathseduc.ui.theme.Colors
import kotlinx.coroutines.channels.ticker
@Composable
fun QuizMultiScreen(lobbyId: Int) {
var progressBar1Value by remember { mutableStateOf(0.0f) }
var chronoValue by remember { mutableStateOf(0.0f) }
var listQuestion by remember { mutableStateOf(ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId))) }
LaunchedEffect(Unit) {
val timer = ticker(delayMillis = 100) // Update every 100 milliseconds
for (tick in timer) {
progressBar1Value += 0.1f // Increment ProgressBar1 value every 100ms
chronoValue += 0.1f // Increment Chrono value every 100ms
if (chronoValue >= 30f) {
// Time's up, do something
break
}
}
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.SpaceBetween
) {
// ProgressBar1
CustomProgressBar(progressBar1Value)
// Chrono ProgressBar
ChronoProgressBar(chronoValue)
Row(
modifier = Modifier
.weight(2f)
.padding(vertical = 20.dp)
) {
Spacer(modifier = Modifier.weight(1f))
Box(
modifier = Modifier
.weight(5f)
.background(color = Colors.Grey, shape = RoundedCornerShape(8.dp))
) {
Text(
text = "test",
modifier = Modifier
.padding(16.dp),
color = Colors.White,
fontSize = 20.sp,
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.weight(1f))
}
Column(modifier = Modifier.weight(1f)) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 10.dp)
) {
Spacer(modifier = Modifier.weight(0.25f))
Button(
onClick = { /* Handle button click */ },
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Blue),
) {
Text(text = "Answer1")
}
Spacer(modifier = Modifier.weight(0.5f))
Button(
onClick = { /* Handle button click */ },
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Green),
) {
Text(text = "Answer2")
}
Spacer(modifier = Modifier.weight(0.25f))
}
Row(
modifier = Modifier
.fillMaxWidth()
) {
Spacer(modifier = Modifier.weight(0.25f))
Button(
onClick = { /* Handle button click */ },
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Orange),
) {
Text(text = "Answer3")
}
Spacer(modifier = Modifier.weight(0.5f))
Button(
onClick = { /* Handle button click */ },
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Purple500),
) {
Text(text = "Answer4")
}
Spacer(modifier = Modifier.weight(0.25f))
}
}
Row(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
.padding(vertical = 10.dp)
) {
Spacer(modifier = Modifier.weight(1f))
Button(
onClick = { /* Handle button click (Passer) */ },
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Red),
) {
Text(text = "Passer")
}
Spacer(modifier = Modifier.weight(0.15f))
Button(
onClick = {
progressBar1Value += 3f
},
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Green),
) {
Text(text = "Valider")
}
Spacer(modifier = Modifier.weight(1f))
}
}
}
@Composable
fun CustomProgressBar(progressBarValue: Float) {
LinearProgressIndicator(
progress = progressBarValue / 100f,
modifier = Modifier.fillMaxWidth()
)
}
@Composable
fun ChronoProgressBar(chronoValue: Float) {
CircularProgressIndicator(
progress = chronoValue / 30f,
modifier = Modifier.padding(horizontal = 10.dp)
)
}
Loading…
Cancel
Save