From ef784ff3d31c69b9b897d2b09fd113a647f0fb96 Mon Sep 17 00:00:00 2001 From: "maxence.guitard" Date: Sat, 16 Mar 2024 17:55:50 +0100 Subject: [PATCH] feat : Confirmdialog password lobby --- .../example/mathseduc/ui/activity_multi.kt | 120 +++++++++++++++--- 1 file changed, 100 insertions(+), 20 deletions(-) diff --git a/Android/app/src/main/java/com/example/mathseduc/ui/activity_multi.kt b/Android/app/src/main/java/com/example/mathseduc/ui/activity_multi.kt index 2dce33a..f326b9c 100644 --- a/Android/app/src/main/java/com/example/mathseduc/ui/activity_multi.kt +++ b/Android/app/src/main/java/com/example/mathseduc/ui/activity_multi.kt @@ -15,12 +15,15 @@ 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.foundation.text.KeyboardOptions import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.ArrowBack +import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Icon import androidx.compose.material3.IconButton +import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect @@ -34,6 +37,8 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.mathseduc.CreateLobbyActivity @@ -90,8 +95,8 @@ fun MultiPage() { Box( modifier = Modifier - .fillMaxSize() - .padding(16.dp) + .fillMaxSize() + .padding(16.dp) ) { IconButton( onClick = { @@ -110,21 +115,21 @@ fun MultiPage() { Column( modifier = Modifier - .fillMaxSize() - .padding(top = 48.dp) + .fillMaxSize() + .padding(top = 48.dp) ) { Row( modifier = Modifier - .fillMaxWidth() - .padding(vertical = 10.dp), + .fillMaxWidth() + .padding(vertical = 10.dp), verticalAlignment = Alignment.CenterVertically ) { Text( text = "Liste des lobbies", color = Colors.White, modifier = Modifier - .weight(1f) - .padding(start = 20.dp) + .weight(1f) + .padding(start = 20.dp) ) Button( onClick = { @@ -134,8 +139,8 @@ fun MultiPage() { }, shape = RoundedCornerShape(15), modifier = Modifier - .weight(1f) - .padding(end = 20.dp), + .weight(1f) + .padding(end = 20.dp), colors = ButtonDefaults.buttonColors(Colors.Blue) ) { Text(text = "Ajouter un lobby") @@ -144,8 +149,8 @@ fun MultiPage() { LazyColumn( modifier = Modifier - .fillMaxSize() - .weight(0.85f) + .fillMaxSize() + .weight(0.85f) ) { items(lobbyList) { lobby -> LobbyItem(lobby, selectedItem == lobby) { @@ -154,6 +159,7 @@ fun MultiPage() { if (ControllerLobby.getNbPlayerInLobby(selectedItem!!.id) < selectedItem!!.nbplayers) { // Créer un Utiliser si le lobby n'est pas plein + scope.launch { createUtiliserForLobby(context, selectedItem!!) } @@ -192,16 +198,22 @@ private suspend fun createUtiliserForLobby(context: Context, lobby: Lobby) { @Composable fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) { + val context = LocalContext.current + var showDialog by rememberSaveable { mutableStateOf(false) } Row( modifier = Modifier - .fillMaxWidth() - .padding(8.dp) - .clickable { - onItemClick(lobby) - } - .background(if (isSelected) Colors.Orange else Colors.Grey) - .padding(16.dp) - .clip(RoundedCornerShape(8.dp)) + .fillMaxWidth() + .padding(8.dp) + .clickable { + if (lobby.password.isNotEmpty()) { + showDialog = true + } else { + onItemClick(lobby) + } + } + .background(if (isSelected) Colors.Orange else Colors.Grey) + .padding(16.dp) + .clip(RoundedCornerShape(8.dp)) ) { Text( text = lobby.name, @@ -227,5 +239,73 @@ fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) { color = Colors.White, modifier = Modifier.weight(1f) ) + + if (showDialog) { + ConfirmDialog( + onDismiss = { showDialog = false }, + onConfirm = { password -> + println("Password input : $password") + if (password == lobby.password) { + showDialog = false + onItemClick(lobby) + } else { + Toast.makeText(context, "Wrong password!", Toast.LENGTH_SHORT).show() + } + } + ) + } } +} + +@Composable +fun ConfirmDialog(onDismiss: () -> Unit, onConfirm: (String) -> Unit) { + val context = LocalContext.current + + var password by remember { mutableStateOf("") } + + AlertDialog( + onDismissRequest = { + onDismiss() + }, + title = { Text("Enter Password") }, + confirmButton = { + Button( + onClick = { + onConfirm(password) + } + ) { + Text("Confirm") + } + }, + dismissButton = { + Button( + onClick = { + onDismiss() + } + ) { + Text("Dismiss") + } + }, + text = { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(8.dp) + ) { + + OutlinedTextField( + value = password, + onValueChange = { password = it }, + label = { Text("Password") }, + visualTransformation = PasswordVisualTransformation(), + keyboardOptions = KeyboardOptions.Default.copy( + keyboardType = KeyboardType.Password + ), + modifier = Modifier + .fillMaxWidth() + .padding(8.dp) + ) + } + } + ) } \ No newline at end of file