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

Loading…
Cancel
Save