Added structure for Mot and json file read

main
Cemal Odabasioglu 2023-12-05 16:05:16 +01:00
parent eb764277dd
commit 61b70f176b
3 changed files with 75 additions and 45 deletions

View File

@ -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")

View File

@ -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<JsonObject>()
@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<String, String>): Boolean {
}
fun ajouterMotAuGlossaire(mot: Map<String, String>) {
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<Mot> {
//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<List<Mot>>(content)
} catch (e: IOException) {
e.printStackTrace()
return emptyList() // Handle the exception as needed
}
}
fun sauvegarderDonneesDansFichier(listeMots: List<Mot>) {
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,

View File

@ -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
)