Merge branch 'androidCompose' of https://codefirst.iut.uca.fr/git/jade.van_brabandt/3.01-QCM_MuscuMaths into androidCompose
commit
d7a1267223
@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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…
Reference in new issue