update dialog profile

This commit is contained in:
DimazzP 2025-05-26 00:16:19 +07:00
parent a2ff149f23
commit 038ba6b207
2 changed files with 50 additions and 7 deletions

View File

@ -13,7 +13,6 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.example.lexilearn.data.model.UserDataModel import com.example.lexilearn.data.model.UserDataModel
import com.example.lexilearn.data.repository.UserRepository import com.example.lexilearn.data.repository.UserRepository
@Composable @Composable
fun DialogProfile(showDialog: Boolean, repository: UserRepository, userData: UserDataModel, onDismiss: () -> Unit) { fun DialogProfile(showDialog: Boolean, repository: UserRepository, userData: UserDataModel, onDismiss: () -> Unit) {
val context = LocalContext.current val context = LocalContext.current
@ -29,24 +28,68 @@ fun DialogProfile(showDialog: Boolean, repository: UserRepository, userData: Use
Text("Masukkan Nama") Text("Masukkan Nama")
TextField( TextField(
value = name, value = name,
onValueChange = { name = it }, onValueChange = {
if (it.text.length <= 30) {
name = it
}
},
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(8.dp) shape = RoundedCornerShape(8.dp),
singleLine = true,
) )
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
Text("Masukkan Usia") Text("Masukkan Usia")
TextField( TextField(
value = age, 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(), modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(8.dp), shape = RoundedCornerShape(8.dp),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number) keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
singleLine = true,
) )
} }
}, },
confirmButton = { confirmButton = {
Button(onClick = { 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() onDismiss()
Toast.makeText(context, "Data Pengguna Diperbarui", Toast.LENGTH_LONG).show() Toast.makeText(context, "Data Pengguna Diperbarui", Toast.LENGTH_LONG).show()
} }

View File

@ -423,7 +423,7 @@ fun HomeScreen(navController: NavController, viewModel: HomeViewModel = viewMode
DialogProfile( DialogProfile(
showDialog = showDialogProfile.value, showDialog = showDialogProfile.value,
repository = viewModel.userRepository, repository = viewModel.userRepository,
userData = userData.value ?: UserDataModel(name = "User", age = 6) userData = userData.value ?: UserDataModel(name = "", age = 6)
) { ) {
viewModel.showHiddenDialog() viewModel.showHiddenDialog()
viewModel.getUserData() viewModel.getUserData()