Use buttonComponent on Form.kt

main
CAPEL Maxime 2023-12-21 15:39:53 +01:00
parent 2fa3ce4714
commit 1d031fe96c
2 changed files with 20 additions and 48 deletions

View File

@ -21,6 +21,7 @@ import androidx.compose.ui.input.key.*
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import main.component.buttonComponent
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@ -55,20 +56,14 @@ fun formPage(glossary: Glossary, onCancelClick: () -> Unit) {
Text("Le mot '${name.value}' a bien été ajouté au glossaire.") Text("Le mot '${name.value}' a bien été ajouté au glossaire.")
}, },
confirmButton = { confirmButton = {
Button( buttonComponent(
onClick = { onClick = {
openDialog.value = false openDialog.value = false
resetFields(name, description, mainContext, secondaryContext, relatedTo, synonymous, antonym) resetFields(name, description, mainContext, secondaryContext, relatedTo, synonymous, antonym)
}, },
colors = ButtonDefaults.buttonColors( width = 100,
backgroundColor = customRedColor, text = "Fermer"
contentColor = Color.White
) )
) {
Text("Fermer")
}
} }
) )
} }
@ -324,23 +319,14 @@ fun formPage(glossary: Glossary, onCancelClick: () -> Unit) {
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly horizontalArrangement = Arrangement.SpaceEvenly
) { ) {
Button( buttonComponent(onClick = onCancelClick, width = 150, text = "Retour", icon = Icons.Default.ArrowBack)
onClick = onCancelClick, buttonComponent(
colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor,
contentColor = Color.White
)
) {
Icon(imageVector = Icons.Default.ArrowBack, contentDescription = null)
Text("Retour")
}
Button(
onClick = { onClick = {
// Validate the fields // Validate the fields
if (name.value.isBlank() || mainContext.value.isBlank()) { if (name.value.isBlank() || mainContext.value.isBlank()) {
// Show the snackbar when validation fails // Show the snackbar when validation fails
requiredFieldsSnackbarVisibleState.value = true requiredFieldsSnackbarVisibleState.value = true
return@Button return@buttonComponent
} }
val wordsList = loadDatasFromFile(glossary.jsonFilePath).toMutableList() val wordsList = loadDatasFromFile(glossary.jsonFilePath).toMutableList()
@ -352,7 +338,7 @@ fun formPage(glossary: Glossary, onCancelClick: () -> Unit) {
if (wordsList.any { it.name.equals(name.value, ignoreCase = true) }) { if (wordsList.any { it.name.equals(name.value, ignoreCase = true) }) {
println("Le mot '${name.value}' existe déjà dans le glossaire. Ajout annulé.") println("Le mot '${name.value}' existe déjà dans le glossaire. Ajout annulé.")
alreadyExistSnackbarVisibleState.value = true alreadyExistSnackbarVisibleState.value = true
return@Button return@buttonComponent
} }
val newWord = Word( val newWord = Word(
@ -369,14 +355,10 @@ fun formPage(glossary: Glossary, onCancelClick: () -> Unit) {
openDialog.value = true openDialog.value = true
}, },
colors = ButtonDefaults.buttonColors( width = 150,
backgroundColor = customRedColor, text = "Valider",
contentColor = Color.White icon = Icons.Default.Check
) )
) {
Icon(imageVector = Icons.Default.Check, contentDescription = null)
Text("Valider")
}
} }
// Show the snackbar when the state is true // Show the snackbar when the state is true
@ -384,15 +366,10 @@ fun formPage(glossary: Glossary, onCancelClick: () -> Unit) {
Snackbar( Snackbar(
modifier = Modifier.padding(16.dp), modifier = Modifier.padding(16.dp),
action = { action = {
Button( buttonComponent(
onClick = { requiredFieldsSnackbarVisibleState.value = false }, onClick = { requiredFieldsSnackbarVisibleState.value = false },
colors = ButtonDefaults.buttonColors( width = 100,
backgroundColor = customRedColor, text = "OK")
contentColor = Color.White
)
) {
Text("OK")
}
} }
) { ) {
Text("Les champs 'Mot' et 'Contexte principal' sont obligatoires.") Text("Les champs 'Mot' et 'Contexte principal' sont obligatoires.")
@ -402,15 +379,10 @@ fun formPage(glossary: Glossary, onCancelClick: () -> Unit) {
Snackbar( Snackbar(
modifier = Modifier.padding(16.dp), modifier = Modifier.padding(16.dp),
action = { action = {
Button( buttonComponent(
onClick = { alreadyExistSnackbarVisibleState.value = false }, onClick = { alreadyExistSnackbarVisibleState.value = false },
colors = ButtonDefaults.buttonColors( width = 100,
backgroundColor = customRedColor, text = "OK")
contentColor = Color.White
)
) {
Text("OK")
}
} }
) { ) {
Text("Le mot '${name.value}' existe déjà dans le glossaire. Ajout annulé.") Text("Le mot '${name.value}' existe déjà dans le glossaire. Ajout annulé.")