update dialog profile
This commit is contained in:
parent
a2ff149f23
commit
038ba6b207
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue