Added structure for Mot and json file read
commit
a629fc393b
|
@ -21,24 +21,16 @@ 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
|
||||
|
@ -46,6 +38,7 @@ fun HomePage(
|
|||
onGlossaireClick: () -> Unit,
|
||||
onCodeAVerifierClick: () -> Unit
|
||||
) {
|
||||
var isHover by remember { mutableStateOf(false) }
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
|
@ -208,15 +201,6 @@ 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"] = ""
|
||||
}
|
||||
|
@ -226,7 +210,7 @@ fun importCSV(cheminFichier: String) {
|
|||
if (!verifierChampsRequis(mot)) {
|
||||
return
|
||||
}
|
||||
ajouterMotAuGlossaire(nouveauMot)
|
||||
ajouterMotAuGlossaire(mot)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -256,6 +240,31 @@ 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,
|
||||
|
@ -508,17 +517,24 @@ fun FormulairePage(onAnnulerClick: () -> Unit) {
|
|||
// Validation du formulaire
|
||||
|
||||
// Create a JsonObject to represent your Mot
|
||||
val nouveauMot = Mot(
|
||||
nom=nom.value,
|
||||
description=description.value,
|
||||
contextePrincipal=contextePrincipal.value,
|
||||
contexte2=contexte2.value,
|
||||
lieA=lieA.value,
|
||||
synonyme=synonyme.value,
|
||||
antonyme=antonyme.value
|
||||
)
|
||||
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)
|
||||
}
|
||||
|
||||
ajouterMotAuGlossaire(nouveauMot)
|
||||
// Append the JsonObject to the glossaire.json file
|
||||
try {
|
||||
FileWriter("glossaire.json", true).use { fileWriter ->
|
||||
fileWriter.appendLine(motJson.toString())
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
|
@ -535,36 +551,6 @@ 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,
|
||||
|
|
Loading…
Reference in New Issue