From e2b1ce560b2d990ebeace6e61132cd47458e2fc4 Mon Sep 17 00:00:00 2001 From: Jeremy DUCOURTHIAL Date: Thu, 14 Mar 2024 17:53:40 +0100 Subject: [PATCH] feat : recuperation des question + enchainement sur la page --- .../controllers/ControllerQuestion.kt | 41 ++-- .../com/example/mathseduc/models/Answer.kt | 4 + .../com/example/mathseduc/models/Question.kt | 3 + .../mathseduc/ui/activity_quiz_multi.kt | 176 +++++++----------- 4 files changed, 107 insertions(+), 117 deletions(-) create mode 100644 Android/app/src/main/java/com/example/mathseduc/models/Answer.kt diff --git a/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerQuestion.kt b/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerQuestion.kt index cae5b68..cbe1f53 100644 --- a/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerQuestion.kt +++ b/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerQuestion.kt @@ -1,18 +1,13 @@ package com.example.mathseduc.controllers +import Answer +import android.annotation.SuppressLint import android.os.StrictMode -import android.util.Log -import com.example.mathseduc.models.Lobby import com.example.mathseduc.models.Question -import com.example.mathseduc.models.Utiliser import com.google.gson.Gson -import com.google.gson.reflect.TypeToken -import okhttp3.MultipartBody import okhttp3.OkHttpClient import okhttp3.Request -import okhttp3.Response -import org.json.JSONObject -import java.io.IOException +import org.json.JSONArray class ControllerQuestion { @@ -30,6 +25,7 @@ class ControllerQuestion { return if (questions.isNotEmpty()) questions else null } + @SuppressLint("SuspiciousIndentation") private fun getQuestionById(questionId: String): Question? { val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() StrictMode.setThreadPolicy(policy) @@ -51,12 +47,33 @@ class ControllerQuestion { // Gson deserialization val gson = Gson() - val typeTokenProduct = object : TypeToken>() {}.type + val responseData = response.body?.string() ?: return null + val jsonArray = JSONArray(responseData) - val question: ArrayList = gson.fromJson(response.body!!.string(), typeTokenProduct) + val answers = mutableListOf() + for (i in 0 until jsonArray.length()) { + val jsonObject = jsonArray.getJSONObject(i) + val answer = Answer( + id = jsonObject.getInt("a_id"), + content = jsonObject.getString("a_content") + ) + answers.add(answer) + } + + val questions = mutableListOf() + val jsonObject = jsonArray.getJSONObject(0) + val question = Question( + id = jsonObject.getInt("q_id"), + content = jsonObject.getString("q_content"), + nbfails = jsonObject.getInt("nbfails"), + difficulty = jsonObject.getInt("difficulty"), + idchapter = jsonObject.getInt("idchapter"), + idanswergood = jsonObject.getInt("idanswergood"), + answers = answers + ) + questions.add(question) - // Assuming the API returns a list with a single question - return if (question.isNotEmpty()) question[0] else null + return if (questions.isNotEmpty()) questions[0] else null } } } diff --git a/Android/app/src/main/java/com/example/mathseduc/models/Answer.kt b/Android/app/src/main/java/com/example/mathseduc/models/Answer.kt new file mode 100644 index 0000000..c109968 --- /dev/null +++ b/Android/app/src/main/java/com/example/mathseduc/models/Answer.kt @@ -0,0 +1,4 @@ +data class Answer( + val id: Int, + val content: String +) diff --git a/Android/app/src/main/java/com/example/mathseduc/models/Question.kt b/Android/app/src/main/java/com/example/mathseduc/models/Question.kt index 52cfde9..3295f39 100644 --- a/Android/app/src/main/java/com/example/mathseduc/models/Question.kt +++ b/Android/app/src/main/java/com/example/mathseduc/models/Question.kt @@ -1,5 +1,7 @@ package com.example.mathseduc.models +import Answer + data class Question( val id: Int, val content: String, @@ -7,4 +9,5 @@ data class Question( val difficulty: Int, val idchapter: Int, val idanswergood: Int, + val answers: List ) \ No newline at end of file diff --git a/Android/app/src/main/java/com/example/mathseduc/ui/activity_quiz_multi.kt b/Android/app/src/main/java/com/example/mathseduc/ui/activity_quiz_multi.kt index 48d2f84..743fba6 100644 --- a/Android/app/src/main/java/com/example/mathseduc/ui/activity_quiz_multi.kt +++ b/Android/app/src/main/java/com/example/mathseduc/ui/activity_quiz_multi.kt @@ -1,5 +1,6 @@ package com.example.mathseduc.ui +import android.widget.Toast import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -18,10 +19,12 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect 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.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp @@ -33,17 +36,20 @@ import kotlinx.coroutines.channels.ticker @Composable fun QuizMultiScreen(lobbyId: Int) { + val context = LocalContext.current + var progressBar1Value by remember { mutableStateOf(0.0f) } var chronoValue by remember { mutableStateOf(0.0f) } var listQuestion by remember { mutableStateOf(ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId))) } + var currentQuestionIndex by remember { mutableIntStateOf(0) } 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 + progressBar1Value += 0.1f + chronoValue += 0.1f if (chronoValue >= 30f) { - // Time's up, do something + currentQuestionIndex++ break } } @@ -62,130 +68,90 @@ fun QuizMultiScreen(lobbyId: Int) { // Chrono ProgressBar ChronoProgressBar(chronoValue) - Row( - modifier = Modifier - .weight(2f) - .padding(vertical = 20.dp) - ) { - Spacer(modifier = Modifier.weight(1f)) + if (currentQuestionIndex < listQuestion!!.size) { + val currentQuestion = listQuestion!![currentQuestionIndex] - Box( + Column( modifier = Modifier - .weight(5f) - .background(color = Colors.Grey, shape = RoundedCornerShape(8.dp)) + .fillMaxSize() + .padding(8.dp), + verticalArrangement = Arrangement.SpaceBetween ) { - 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 */ }, + Row( modifier = Modifier - .weight(2f), - shape = RoundedCornerShape(15), - colors = ButtonDefaults.buttonColors(Colors.Blue), + .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 = "Answer1") + Text( + text = currentQuestion.content, + modifier = Modifier.padding(16.dp), + color = Colors.White, + fontSize = 20.sp, + textAlign = TextAlign.Center + ) + } + + Spacer(modifier = Modifier.weight(1f)) } - 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") + Column(modifier = Modifier.weight(1f)) { + currentQuestion.answers.forEachIndexed { index, answer -> + val buttonBackgroundColor = when (index) { + 0 -> Colors.Red + 1 -> Colors.Blue + 2 -> Colors.Green + else -> Colors.Cyan + } + + Button( + onClick = { + if (answer.id == currentQuestion.idanswergood) { + Toast.makeText(context, "Oh ouii !!", Toast.LENGTH_SHORT).show() + progressBar1Value += 10f + } else { + Toast.makeText(context, "Oh nan !!", Toast.LENGTH_SHORT).show() + } + currentQuestionIndex++ + }, + modifier = Modifier + .fillMaxWidth() + .padding(bottom = 10.dp), + shape = RoundedCornerShape(15), + colors = ButtonDefaults.buttonColors(buttonBackgroundColor) + ) { + Text(text = answer.content) + } + } } - - Spacer(modifier = Modifier.weight(0.25f)) } Row( modifier = Modifier .fillMaxWidth() + .weight(1f) + .padding(vertical = 10.dp) ) { - Spacer(modifier = Modifier.weight(0.25f)) - Button( - onClick = { /* Handle button click */ }, + onClick = { + currentQuestionIndex++ + }, modifier = Modifier .weight(2f), shape = RoundedCornerShape(15), - colors = ButtonDefaults.buttonColors(Colors.Orange), + colors = ButtonDefaults.buttonColors(Colors.Grey), ) { - Text(text = "Answer3") + Text(text = "Passer") } - - 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)) + } else { + Toast.makeText(context, "Fini !!", Toast.LENGTH_SHORT).show() } } }