From 61b70f176b9ace4ddb6b0c8368133b461a067df6 Mon Sep 17 00:00:00 2001 From: cemal Date: Tue, 5 Dec 2023 16:05:16 +0100 Subject: [PATCH] Added structure for Mot and json file read --- build.gradle.kts | 3 +- src/main/kotlin/main/Main.kt | 102 ++++++++++++++++++++--------------- src/main/kotlin/main/Mot.kt | 15 ++++++ 3 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 src/main/kotlin/main/Mot.kt diff --git a/build.gradle.kts b/build.gradle.kts index bfe29bb..47eb8b9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,7 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat plugins { kotlin("jvm") id("org.jetbrains.compose") + kotlin("plugin.serialization") version "1.5.30" } group = "com.example" @@ -21,7 +22,6 @@ 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("com.github.doyaaaaaken:kotlin-csv-jvm:1.9.2") implementation("org.jetbrains.compose.material:material:1.0.0-beta-02") @@ -33,6 +33,7 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30") implementation("com.almworks.sqlite4java:sqlite4java:1.0.392") implementation("io.ktor:ktor-server-netty:1.6.3") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2") implementation("io.ktor:ktor-html-builder:1.6.3") diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 2ae0630..959cfa3 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -21,16 +21,24 @@ import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.window.* +import com.mysql.cj.xdevapi.JsonParser import database.DatabaseManager +import kotlinx.serialization.json.JsonArray +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 +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json +import kotlinx.serialization.encodeToString + val connection = DatabaseManager.getConnection() val customRedColor = Color(0xFFB70D1B) +val glossaireList = mutableListOf() @OptIn(ExperimentalComposeUiApi::class) @Composable @@ -38,7 +46,6 @@ fun HomePage( onGlossaireClick: () -> Unit, onCodeAVerifierClick: () -> Unit ) { - var isHover by remember { mutableStateOf(false) } Column( modifier = Modifier.fillMaxSize(), @@ -201,6 +208,15 @@ fun importCSV(cheminFichier: String) { println(value) } val mot = header.zip(values).toMap().toMutableMap() + val nouveauMot = Mot( + nom = mot["Mot"]!!, + description = mot["Description"]!!, + contextePrincipal = mot["Contexte principal"]!!, + contexte2 = mot["Contexte 2"]!!, + lieA = mot["Lié à"]!!, + synonyme = mot["Synonyme"]!!, + antonyme = mot["Antonyme"]!! + ) if(mot["Antonyme"] == "\r") { mot["Antonyme"] = "" } @@ -210,7 +226,7 @@ fun importCSV(cheminFichier: String) { if (!verifierChampsRequis(mot)) { return } - ajouterMotAuGlossaire(mot) + ajouterMotAuGlossaire(nouveauMot) } } @@ -240,31 +256,6 @@ fun verifierChampsRequis(mot: Map): Boolean { } -fun ajouterMotAuGlossaire(mot: Map) { - println("Ajout du mot :" + mot["Mot"]) - - - val motJson = buildJsonObject { - put("nom", mot["Mot"]) - put("description", mot["Description"]) - put("contextePrincipal", mot["Contexte principal"]) - put("contexte2", mot["Contexte 2"]) - put("lieA", mot["Lié à"]) - put("synonyme", mot["Synonyme"]) - put("antonyme", mot["Antonyme"]) - } - - - try { - FileWriter("glossaire.json", true).use { fileWriter -> - fileWriter.appendLine(motJson.toString()) - } - } catch (e: IOException) { - e.printStackTrace() - } - -} - @Composable fun ChoixLangagePage( onRetourClick: () -> Unit, @@ -517,24 +508,17 @@ fun FormulairePage(onAnnulerClick: () -> Unit) { // 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) - } + val nouveauMot = Mot( + nom=nom.value, + description=description.value, + contextePrincipal=contextePrincipal.value, + contexte2=contexte2.value, + lieA=lieA.value, + synonyme=synonyme.value, + 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() - } + ajouterMotAuGlossaire(nouveauMot) }, colors = ButtonDefaults.buttonColors( @@ -551,6 +535,36 @@ fun FormulairePage(onAnnulerClick: () -> Unit) { } } +fun chargerDonneesDepuisFichier(): List { + //if file is empty, return empty list + if (!File("glossaire.json").exists() || File("glossaire.json").length() == 0L) { + return emptyList() + } + try { + val content = File("glossaire.json").readText() + return Json.decodeFromString>(content) + } catch (e: IOException) { + e.printStackTrace() + return emptyList() // Handle the exception as needed + } +} + +fun sauvegarderDonneesDansFichier(listeMots: List) { + try { + val content = Json.encodeToString(listeMots) + File("glossaire.json").writeText(content) + } catch (e: IOException) { + e.printStackTrace() + // Handle the exception as needed + } +} + +fun ajouterMotAuGlossaire(nouveauMot: Mot) { + val listeMots = chargerDonneesDepuisFichier().toMutableList() + listeMots.add(nouveauMot) + sauvegarderDonneesDansFichier(listeMots) +} + fun main() = application { val state = rememberWindowState( placement = WindowPlacement.Floating, diff --git a/src/main/kotlin/main/Mot.kt b/src/main/kotlin/main/Mot.kt new file mode 100644 index 0000000..8af2d7c --- /dev/null +++ b/src/main/kotlin/main/Mot.kt @@ -0,0 +1,15 @@ +package main + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class Mot( + val nom: String, + val description: String, + val contextePrincipal: String, + val contexte2: String, + val lieA: String, + val synonyme: String, + val antonyme: String +)