Compare commits

..

No commits in common. '047cb8c34fb94587284645bf5ce2cee8cc7a36b1' and '7416798f95c06706a4ca20d900989d22ab0755dd' have entirely different histories.

@ -6,11 +6,9 @@ import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
@ -32,101 +30,68 @@ class ConnexionPlayerActivity : ComponentActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContent { setContent {
MathsEducTheme { MathsEducTheme {
ConnexionPlayerContent(activity = this@ConnexionPlayerActivity) ConnexionPlayerContent()
} }
} }
} }
} }
@OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun ConnexionPlayerContent(activity: ConnexionPlayerActivity) { fun ConnexionPlayerContent() {
var nickname by rememberSaveable { mutableStateOf("") } var nickname by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") } var password by rememberSaveable { mutableStateOf("") }
var showDialog by rememberSaveable { mutableStateOf(false) } var showDialog by rememberSaveable { mutableStateOf(false) }
val context = LocalContext.current val context = LocalContext.current
Column(modifier = Modifier.fillMaxSize()) { Column(
TopAppBar( modifier = Modifier
colors = TopAppBarDefaults.topAppBarColors( .fillMaxSize()
containerColor = Color.Transparent, .padding(16.dp),
), verticalArrangement = Arrangement.Center,
title = {}, horizontalAlignment = Alignment.CenterHorizontally
navigationIcon = { ) {
IconButton( Image(
onClick = { activity.finish() }, painter = painterResource(id = R.drawable.logo),
modifier = Modifier.size(60.dp) contentDescription = null,
) { modifier = Modifier
Icon( .size(160.dp, 130.dp)
imageVector = Icons.Filled.ArrowBack, .background(Color.Gray) // Replace with your background color
contentDescription = "Retour",
modifier = Modifier.size(36.dp),
tint = Color.White
)
}
},
) )
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
.weight(1f),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image( Spacer(modifier = Modifier.height(16.dp))
painter = painterResource(id = R.drawable.logo),
contentDescription = null,
modifier = Modifier
.size(160.dp, 130.dp)
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField( OutlinedTextField(
value = nickname, value = nickname,
onValueChange = { nickname = it }, onValueChange = { nickname = it },
label = { Text("Nickname") }, label = { Text("Nickname") },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(8.dp), .padding(8.dp)
colors = OutlinedTextFieldDefaults.colors( )
focusedContainerColor = Color.White,
unfocusedContainerColor = Color.White,
disabledContainerColor = Color.White,
focusedBorderColor = Color.Blue,
),
shape = RoundedCornerShape(8.dp)
)
OutlinedTextField( OutlinedTextField(
value = password, value = password,
onValueChange = { password = it }, onValueChange = { password = it },
label = { Text("Password") }, label = { Text("Password") },
visualTransformation = PasswordVisualTransformation(), visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions.Default.copy( keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Password keyboardType = KeyboardType.Password
), ),
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(8.dp), .padding(8.dp)
colors = OutlinedTextFieldDefaults.colors( )
focusedContainerColor = Color.White,
unfocusedContainerColor = Color.White,
disabledContainerColor = Color.White,
focusedBorderColor = Color.Blue,
),
shape = RoundedCornerShape(8.dp)
)
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( Button(
onClick = { onClick = {
val isAuthenticated = ControllerPlayer.authenticateUser(nickname, password) val isAuthenticated = ControllerPlayer.authenticateUser(nickname, password)
if (isAuthenticated != -1) { if (isAuthenticated != -1) {
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
MainActivity.idPlayerConnected = isAuthenticated MainActivity.idPlayerConnected = isAuthenticated
Toast.makeText(context, "Connexion réussie, bienvenue $nickname !", Toast.LENGTH_SHORT).show() Toast.makeText(context, "Connexion réussie, bienvenue $nickname !", Toast.LENGTH_SHORT).show()
} else { } else {
@ -140,22 +105,19 @@ fun ConnexionPlayerContent(activity: ConnexionPlayerActivity) {
Text("Login") Text("Login")
} }
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( Button(
onClick = { showDialog = true }, onClick = { showDialog = true },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(48.dp), .height(48.dp)
colors = ButtonDefaults.buttonColors( ) {
containerColor = Color(0xFF40E0D0)) Text("Register")
) { }
Text("Register")
}
if (showDialog) { if (showDialog) {
RegisterDialog(onDismiss = { showDialog = false }) RegisterDialog(onDismiss = { showDialog = false })
}
} }
} }
} }
@ -168,66 +130,65 @@ fun RegisterDialog(onDismiss: () -> Unit) {
var password by remember { mutableStateOf("") } var password by remember { mutableStateOf("") }
AlertDialog( AlertDialog(
onDismissRequest = { onDismissRequest = {
onDismiss() onDismiss()
}, },
title = { Text("Register") }, title = { Text("Register") },
confirmButton = { confirmButton = {
Button( Button(
onClick = { onClick = {
val playerId = ControllerPlayer.createPlayer(nickname, password) // Save the user information or perform registration
onDismiss() onDismiss()
}
) {
Text("Save")
}
},
dismissButton = {
Button(
onClick = {
onDismiss()
}
) {
Text("Dismiss")
} }
}, ) {
text = { Text("Save")
Column( }
modifier = Modifier },
.fillMaxWidth() dismissButton = {
.padding(8.dp) Button(
) { onClick = {
OutlinedTextField( onDismiss()
value = nickname,
onValueChange = { nickname = it },
label = { Text("Nickname") },
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)
)
} }
) {
Text("Dismiss")
}
},
text = {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
OutlinedTextField(
value = nickname,
onValueChange = { nickname = it },
label = { Text("Nickname") },
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)
)
} }
}
) )
} }
@Preview(showBackground = true) @Preview(showBackground = true)
@Composable @Composable
fun ConnexionPlayerPreview() { fun ConnexionPlayerPreview() {
val fakeActivity = ConnexionPlayerActivity()
MathsEducTheme { MathsEducTheme {
ConnexionPlayerContent(activity = fakeActivity) ConnexionPlayerContent()
} }
} }

@ -62,39 +62,6 @@ class ControllerPlayer {
return null return null
} }
fun createPlayer(nickname: String, password: String): Int {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val client = OkHttpClient()
val hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt())
val requestBody = FormBody.Builder()
.add("nickname", nickname)
.add("password", hashedPassword)
.build()
val request = Request.Builder()
.url("https://trusting-panini.87-106-126-109.plesk.page/api/add/player/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.post(requestBody)
.build()
client.newCall(request).execute().use { response ->
if (response.isSuccessful) {
val responseData = response.body?.string()
val gson = Gson()
val player: Player = gson.fromJson(responseData, Player::class.java)
return player.id
} else {
throw IOException("Failed to create player: ${response.code}")
}
}
}
fun authenticateUser(nickname: String, password: String): Int { fun authenticateUser(nickname: String, password: String): Int {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()

@ -103,8 +103,7 @@ fun HomePage() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( Button(
onClick = { val intent = Intent(context, ConnexionPlayerActivity::class.java) onClick = { /* Handle connexion button click */ },
context.startActivity(intent) },
shape = RoundedCornerShape(15), shape = RoundedCornerShape(15),
colors = ButtonDefaults.buttonColors(Colors.Grey), colors = ButtonDefaults.buttonColors(Colors.Grey),
modifier = Modifier modifier = Modifier

Loading…
Cancel
Save