Update newGlossary View

main
Thomas Breil 2024-01-12 14:47:49 +01:00
parent e331578e01
commit 29368a1688
1 changed files with 33 additions and 40 deletions

View File

@ -136,7 +136,7 @@ fun glossariesPage(
Button(
onClick = {
currentPage.value = "accueil"
currentPage.value = "projects"
},
modifier = Modifier
.width(250.dp),
@ -160,64 +160,57 @@ fun newGlossary(
val glossaryAlreadyExistsSnackbarVisibleState = remember { mutableStateOf(false) }
val invalidCharacterSnackbarVisibleState = remember { mutableStateOf(false) }
var nouveauGlossaireName by remember { mutableStateOf("") }
var glossaries by remember { mutableStateOf(emptyList<Glossary>()) }
var nouveauGlossaireName by remember { mutableStateOf("") }
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Nom du glossaire", style = MaterialTheme.typography.h5)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = nouveauGlossaireName,
onValueChange = { nouveauGlossaireName = it },
singleLine = true,
label = { Text("Nom du nouveau glossaire") },
onValueChange = { nouveauGlossaireName = it },
label = { Text("Nom du glossaire") },
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray,
focusedLabelColor = customRedColor
),
modifier = Modifier
.padding(16.dp)
.width(300.dp)
.fillMaxWidth()
.onKeyEvent { event ->
if (event.key == Key.Enter && event.type == KeyEventType.KeyDown) {
// Handle the Enter key event by calling the common function
when {
nouveauGlossaireName.isEmpty() -> {
if (nouveauGlossaireName.isEmpty()) {
println("Veuillez saisir un nom pour le nouveau glossaire")
isEmptySnackbarVisibleState.value = true
}
nouveauGlossaireName.contains(" ") -> {
} else if (nouveauGlossaireName.contains(" ")) {
println("Le nom du glossaire ne doit pas contenir d'espace")
containsSpaceSnackbarVisibleState.value = true
}
glossaries.any { it.name == nouveauGlossaireName } -> {
} else if (glossaries.any { it.name == nouveauGlossaireName }) {
println("Le nom du glossaire existe déjà")
glossaryAlreadyExistsSnackbarVisibleState.value = true
}
!isValidFileName(nouveauGlossaireName) -> {
} else if (!isValidFileName(nouveauGlossaireName)) {
println("Le nom du glossaire contient des caractères non autorisés")
invalidCharacterSnackbarVisibleState.value = true
}
else -> {
val newGlossary = Glossary(nouveauGlossaireName, "$nouveauGlossaireName.json")
glossaries = glossaries + newGlossary
// create new json file
val newFile =
File(glossaryPath + (appState.selectedProject?.name) + "/" + newGlossary.jsonFilePath)
newFile.createNewFile()
// update glossaries list
glossaries = loadGlossaries(appState.selectedProject!!)
currentPage.value = "glossaires" // Revenir à la liste des glossaires
}
} else {
val directory = File("src/main/resources/projects/${appState.selectedProject}/$nouveauGlossaireName")
directory.mkdirs()
println("Project $nouveauGlossaireName created")
currentPage.value = "projects"
}
true
} else {
false
}
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.White,
focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray
)
}
)
Button(