From 038ba6b2070871863e632643ae41b18d6a99c864 Mon Sep 17 00:00:00 2001 From: DimazzP Date: Mon, 26 May 2025 00:16:19 +0700 Subject: [PATCH] update dialog profile --- .../lexilearn/ui/components/DialogProfile.kt | 55 +++++++++++++++++-- .../lexilearn/ui/views/pHome/HomeScreen.kt | 2 +- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/example/lexilearn/ui/components/DialogProfile.kt b/app/src/main/java/com/example/lexilearn/ui/components/DialogProfile.kt index 48e1200..7197bea 100644 --- a/app/src/main/java/com/example/lexilearn/ui/components/DialogProfile.kt +++ b/app/src/main/java/com/example/lexilearn/ui/components/DialogProfile.kt @@ -13,7 +13,6 @@ import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.unit.dp import com.example.lexilearn.data.model.UserDataModel import com.example.lexilearn.data.repository.UserRepository - @Composable fun DialogProfile(showDialog: Boolean, repository: UserRepository, userData: UserDataModel, onDismiss: () -> Unit) { val context = LocalContext.current @@ -29,24 +28,68 @@ fun DialogProfile(showDialog: Boolean, repository: UserRepository, userData: Use Text("Masukkan Nama") TextField( value = name, - onValueChange = { name = it }, + onValueChange = { + if (it.text.length <= 30) { + name = it + } + }, modifier = Modifier.fillMaxWidth(), - shape = RoundedCornerShape(8.dp) + shape = RoundedCornerShape(8.dp), + singleLine = true, ) Spacer(modifier = Modifier.height(8.dp)) Text("Masukkan Usia") TextField( value = age, - onValueChange = { age = it }, + onValueChange = { + // Batasi hanya angka dan maksimal 2 karakter + if (it.text.length <= 2 && it.text.all { c -> c.isDigit() }) { + age = it + } + }, modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(8.dp), - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number) + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + singleLine = true, ) } }, confirmButton = { Button(onClick = { - repository.updateUser(UserDataModel(name.text, age.text.toInt(), userData.unlock_data)){ + val nameText = name.text.trim() + val ageText = age.text.trim() + + // Validasi nama: tidak kosong, hanya huruf, maksimal 30 karakter + val nameRegex = Regex("^[a-zA-Z ]+$") + if (nameText.isEmpty()) { + Toast.makeText(context, "Nama tidak boleh kosong", Toast.LENGTH_LONG).show() + return@Button + } + if (!nameRegex.matches(nameText)) { + Toast.makeText(context, "Nama hanya boleh berisi huruf dan spasi", Toast.LENGTH_LONG).show() + return@Button + } + if (nameText.length > 30) { + Toast.makeText(context, "Nama maksimal 30 karakter", Toast.LENGTH_LONG).show() + return@Button + } + + // Validasi usia: tidak kosong, angka, maksimal 2 digit, > 0 + val ageInt = ageText.toIntOrNull() + if (ageText.isEmpty()) { + Toast.makeText(context, "Usia tidak boleh kosong", Toast.LENGTH_LONG).show() + return@Button + } + if (ageInt == null || ageInt <= 0) { + Toast.makeText(context, "Usia harus berupa angka positif", Toast.LENGTH_LONG).show() + return@Button + } + if (ageText.length > 2) { + Toast.makeText(context, "Usia maksimal 2 digit", Toast.LENGTH_LONG).show() + return@Button + } + + repository.updateUser(UserDataModel(nameText, ageInt, userData.unlock_data)) { onDismiss() Toast.makeText(context, "Data Pengguna Diperbarui", Toast.LENGTH_LONG).show() } diff --git a/app/src/main/java/com/example/lexilearn/ui/views/pHome/HomeScreen.kt b/app/src/main/java/com/example/lexilearn/ui/views/pHome/HomeScreen.kt index 447328b..064fcaf 100644 --- a/app/src/main/java/com/example/lexilearn/ui/views/pHome/HomeScreen.kt +++ b/app/src/main/java/com/example/lexilearn/ui/views/pHome/HomeScreen.kt @@ -423,7 +423,7 @@ fun HomeScreen(navController: NavController, viewModel: HomeViewModel = viewMode DialogProfile( showDialog = showDialogProfile.value, repository = viewModel.userRepository, - userData = userData.value ?: UserDataModel(name = "User", age = 6) + userData = userData.value ?: UserDataModel(name = "", age = 6) ) { viewModel.showHiddenDialog() viewModel.getUserData()