feat : recuperation des question + enchainement sur la page

androidCompose
Jeremy DUCOURTHIAL 1 year ago
parent 27f83b26d4
commit e2b1ce560b

@ -1,18 +1,13 @@
package com.example.mathseduc.controllers package com.example.mathseduc.controllers
import Answer
import android.annotation.SuppressLint
import android.os.StrictMode 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.Question
import com.example.mathseduc.models.Utiliser
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.MultipartBody
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
class ControllerQuestion { class ControllerQuestion {
@ -30,6 +25,7 @@ class ControllerQuestion {
return if (questions.isNotEmpty()) questions else null return if (questions.isNotEmpty()) questions else null
} }
@SuppressLint("SuspiciousIndentation")
private fun getQuestionById(questionId: String): Question? { private fun getQuestionById(questionId: String): Question? {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy) StrictMode.setThreadPolicy(policy)
@ -51,12 +47,33 @@ class ControllerQuestion {
// Gson deserialization // Gson deserialization
val gson = Gson() val gson = Gson()
val typeTokenProduct = object : TypeToken<ArrayList<Question>>() {}.type val responseData = response.body?.string() ?: return null
val jsonArray = JSONArray(responseData)
val question: ArrayList<Question> = gson.fromJson(response.body!!.string(), typeTokenProduct) val answers = mutableListOf<Answer>()
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<Question>()
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 (questions.isNotEmpty()) questions[0] else null
return if (question.isNotEmpty()) question[0] else null
} }
} }
} }

@ -0,0 +1,4 @@
data class Answer(
val id: Int,
val content: String
)

@ -1,5 +1,7 @@
package com.example.mathseduc.models package com.example.mathseduc.models
import Answer
data class Question( data class Question(
val id: Int, val id: Int,
val content: String, val content: String,
@ -7,4 +9,5 @@ data class Question(
val difficulty: Int, val difficulty: Int,
val idchapter: Int, val idchapter: Int,
val idanswergood: Int, val idanswergood: Int,
val answers: List<Answer>
) )

@ -1,5 +1,6 @@
package com.example.mathseduc.ui package com.example.mathseduc.ui
import android.widget.Toast
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,10 +19,12 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
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.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
@ -33,17 +36,20 @@ import kotlinx.coroutines.channels.ticker
@Composable @Composable
fun QuizMultiScreen(lobbyId: Int) { fun QuizMultiScreen(lobbyId: Int) {
val context = LocalContext.current
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 by remember { mutableStateOf(ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId))) } var listQuestion by remember { mutableStateOf(ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId))) }
var currentQuestionIndex by remember { mutableIntStateOf(0) }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
val timer = ticker(delayMillis = 100) // Update every 100 milliseconds val timer = ticker(delayMillis = 100) // Update every 100 milliseconds
for (tick in timer) { for (tick in timer) {
progressBar1Value += 0.1f // Increment ProgressBar1 value every 100ms progressBar1Value += 0.1f
chronoValue += 0.1f // Increment Chrono value every 100ms chronoValue += 0.1f
if (chronoValue >= 30f) { if (chronoValue >= 30f) {
// Time's up, do something currentQuestionIndex++
break break
} }
} }
@ -62,130 +68,90 @@ fun QuizMultiScreen(lobbyId: Int) {
// Chrono ProgressBar // Chrono ProgressBar
ChronoProgressBar(chronoValue) ChronoProgressBar(chronoValue)
Row( if (currentQuestionIndex < listQuestion!!.size) {
modifier = Modifier val currentQuestion = listQuestion!![currentQuestionIndex]
.weight(2f)
.padding(vertical = 20.dp)
) {
Spacer(modifier = Modifier.weight(1f))
Box( Column(
modifier = Modifier modifier = Modifier
.weight(5f) .fillMaxSize()
.background(color = Colors.Grey, shape = RoundedCornerShape(8.dp)) .padding(8.dp),
verticalArrangement = Arrangement.SpaceBetween
) { ) {
Text( Row(
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 modifier = Modifier
.weight(2f), .weight(2f)
shape = RoundedCornerShape(15), .padding(vertical = 20.dp)
colors = ButtonDefaults.buttonColors(Colors.Blue), ) {
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)) Column(modifier = Modifier.weight(1f)) {
currentQuestion.answers.forEachIndexed { index, answer ->
Button( val buttonBackgroundColor = when (index) {
onClick = { /* Handle button click */ }, 0 -> Colors.Red
modifier = Modifier 1 -> Colors.Blue
.weight(2f), 2 -> Colors.Green
shape = RoundedCornerShape(15), else -> Colors.Cyan
colors = ButtonDefaults.buttonColors(Colors.Green), }
) {
Text(text = "Answer2") 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( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.weight(1f)
.padding(vertical = 10.dp)
) { ) {
Spacer(modifier = Modifier.weight(0.25f))
Button( Button(
onClick = { /* Handle button click */ }, onClick = {
currentQuestionIndex++
},
modifier = Modifier modifier = Modifier
.weight(2f), .weight(2f),
shape = RoundedCornerShape(15), 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")
} }
} else {
Spacer(modifier = Modifier.weight(0.15f)) Toast.makeText(context, "Fini !!", Toast.LENGTH_SHORT).show()
Button(
onClick = {
progressBar1Value += 3f
},
modifier = Modifier
.weight(2f),
shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Green),
) {
Text(text = "Valider")
}
Spacer(modifier = Modifier.weight(1f))
} }
} }
} }

Loading…
Cancel
Save