From 9a911e93529b6cafec0ff939277da038fc413bd8 Mon Sep 17 00:00:00 2001 From: cemal Date: Tue, 28 Nov 2023 11:55:16 +0100 Subject: [PATCH] Added JSON saving of Glossaire --- .gitignore | 4 ++++ build.gradle.kts | 1 + src/main/kotlin/main/Main.kt | 43 ++++++++++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 041b1da..2f09d41 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,7 @@ bin/ ### Mac OS ### .DS_Store .run/* + +/glossaire.json +``` + diff --git a/build.gradle.kts b/build.gradle.kts index a734b83..9ce2983 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,6 +21,7 @@ dependencies { // With compose.desktop.common you will also lose @Preview functionality implementation(compose.desktop.currentOs) implementation(kotlin("stdlib")) + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0") implementation("mysql:mysql-connector-java:8.0.23") implementation("org.jetbrains.compose.material:material:1.0.0-beta-02") implementation("org.jetbrains.compose.ui:ui:1.0.0-beta-02") diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 5c7810a..a44c27d 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -23,6 +23,13 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.window.* import database.DatabaseManager +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.put +import java.io.File +import java.io.FileWriter +import java.io.IOException + val connection = DatabaseManager.getConnection() val customRedColor = Color(0xFFB70D1B) @@ -326,9 +333,9 @@ fun glossairePage( } - @Composable fun FormulairePage(onAnnulerClick: () -> Unit) { + MaterialTheme { Column( modifier = Modifier.padding(16.dp), @@ -423,19 +430,47 @@ fun FormulairePage(onAnnulerClick: () -> Unit) { colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Icon(imageVector = Icons.Default.Close, contentDescription = null) Text("Annuler") } - Button(onClick = { /* Action de validation */ }, + Button( + onClick = { + // Validation du formulaire + + // Create a JsonObject to represent your Mot + val motJson = buildJsonObject { + put("nom", nom.value) + put("description", description.value) + put("contextePrincipal", contextePrincipal.value) + put("contexte2", contexte2.value) + put("lieA", lieA.value) + put("synonyme", synonyme.value) + put("antonyme", antonyme.value) + } + + // Append the JsonObject to the glossaire.json file + try { + FileWriter("glossaire.json", true).use { fileWriter -> + fileWriter.appendLine(motJson.toString()) + } + } catch (e: IOException) { + e.printStackTrace() + } + + // Reste de la logique de validation ou de navigation + }, colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White - )) { + ) + ) { Icon(imageVector = Icons.Default.Check, contentDescription = null) Text("Valider") } } + } } }