feat : reduction lags QuizMulti
continuous-integration/drone/push Build is passing Details

androidCompose
Yvan CALATAYUD 1 year ago
parent 9f40a5a5a2
commit 27f83b26d4

@ -1,7 +1,5 @@
package com.example.mathseduc.ui
import android.os.CountDownTimer
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@ -18,32 +16,38 @@ import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
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.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mathseduc.QuizMultiActivity
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerQuestion
import com.example.mathseduc.ui.theme.Colors
import com.example.mathseduc.ui.theme.MathsEducTheme
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 = ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId))
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
@ -51,39 +55,12 @@ fun QuizMultiScreen(lobbyId: Int) {
.padding(16.dp),
verticalArrangement = Arrangement.SpaceBetween
) {
// ProgressBar1
LinearProgressIndicator(
progress = progressBar1Value/100f,
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.weight(0.025f))
LinearProgressIndicator(
progress = 0.05f,
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.weight(0.025f))
LinearProgressIndicator(
progress = 0.05f,
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.weight(0.025f))
LinearProgressIndicator(
progress = 1f,
modifier = Modifier.fillMaxWidth()
)
// ProgressBar1
CustomProgressBar(progressBar1Value)
// Chrono ProgressBar
CircularProgressIndicator(
progress = chronoValue/30f,
modifier = Modifier
.padding(horizontal = 10.dp)
)
ChronoProgressBar(chronoValue)
Row(
modifier = Modifier
@ -95,13 +72,13 @@ fun QuizMultiScreen(lobbyId: Int) {
Box(
modifier = Modifier
.weight(5f)
.background(color = Color.Gray, shape = RoundedCornerShape(8.dp))
.background(color = Colors.Grey, shape = RoundedCornerShape(8.dp))
) {
Text(
text = "test",
modifier = Modifier
.padding(16.dp),
color = Color.White,
color = Colors.White,
fontSize = 20.sp,
textAlign = TextAlign.Center
)
@ -125,7 +102,7 @@ fun QuizMultiScreen(lobbyId: Int) {
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Blue),
) {
) {
Text(text = "Answer1")
}
@ -211,24 +188,20 @@ fun QuizMultiScreen(lobbyId: Int) {
Spacer(modifier = Modifier.weight(1f))
}
}
}
// CountDownTimer
DisposableEffect(Unit) {
QuizMultiActivity.countDownTimer = object : CountDownTimer(30 * 1000, 1000) {
override fun onTick(millisUntilFinished: Long) {
progressBar1Value += 1f // Increment ProgressBar1 value every second
chronoValue += 1.0f // Increment Chrono value every second
}
override fun onFinish() {
// Code to execute when the timer is finished
}
}
QuizMultiActivity.countDownTimer?.start()
@Composable
fun CustomProgressBar(progressBarValue: Float) {
LinearProgressIndicator(
progress = progressBarValue / 100f,
modifier = Modifier.fillMaxWidth()
)
}
onDispose {
// Stop the CountDownTimer to avoid memory leaks
QuizMultiActivity.countDownTimer?.cancel()
}
}
@Composable
fun ChronoProgressBar(chronoValue: Float) {
CircularProgressIndicator(
progress = chronoValue / 30f,
modifier = Modifier.padding(horizontal = 10.dp)
)
}

Loading…
Cancel
Save