Readded structure for Mot and json file read
parent
a629fc393b
commit
d4914fc830
|
@ -22,15 +22,17 @@ import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.window.*
|
import androidx.compose.ui.window.*
|
||||||
import database.DatabaseManager
|
import database.DatabaseManager
|
||||||
import kotlinx.serialization.json.buildJsonObject
|
|
||||||
import kotlinx.serialization.json.put
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileWriter
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
val connection = DatabaseManager.getConnection()
|
val connection = DatabaseManager.getConnection()
|
||||||
val customRedColor = Color(0xFFB70D1B)
|
val customRedColor = Color(0xFFB70D1B)
|
||||||
|
val glossaireList = mutableListOf<JsonObject>()
|
||||||
|
|
||||||
@OptIn(ExperimentalComposeUiApi::class)
|
@OptIn(ExperimentalComposeUiApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -201,6 +203,16 @@ fun importCSV(cheminFichier: String) {
|
||||||
println(value)
|
println(value)
|
||||||
}
|
}
|
||||||
val mot = header.zip(values).toMap().toMutableMap()
|
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") {
|
if(mot["Antonyme"] == "\r") {
|
||||||
mot["Antonyme"] = ""
|
mot["Antonyme"] = ""
|
||||||
}
|
}
|
||||||
|
@ -210,7 +222,7 @@ fun importCSV(cheminFichier: String) {
|
||||||
if (!verifierChampsRequis(mot)) {
|
if (!verifierChampsRequis(mot)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ajouterMotAuGlossaire(mot)
|
ajouterMotAuGlossaire(nouveauMot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,31 +252,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
|
@Composable
|
||||||
fun ChoixLangagePage(
|
fun ChoixLangagePage(
|
||||||
onRetourClick: () -> Unit,
|
onRetourClick: () -> Unit,
|
||||||
|
@ -516,25 +503,18 @@ fun FormulairePage(onAnnulerClick: () -> Unit) {
|
||||||
onClick = {
|
onClick = {
|
||||||
// Validation du formulaire
|
// Validation du formulaire
|
||||||
|
|
||||||
// Create a JsonObject to represent your Mot
|
val nouveauMot = Mot(
|
||||||
val motJson = buildJsonObject {
|
nom=nom.value,
|
||||||
put("nom", nom.value)
|
description=description.value,
|
||||||
put("description", description.value)
|
contextePrincipal=contextePrincipal.value,
|
||||||
put("contextePrincipal", contextePrincipal.value)
|
contexte2=contexte2.value,
|
||||||
put("contexte2", contexte2.value)
|
lieA=lieA.value,
|
||||||
put("lieA", lieA.value)
|
synonyme=synonyme.value,
|
||||||
put("synonyme", synonyme.value)
|
antonyme=antonyme.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(
|
colors = ButtonDefaults.buttonColors(
|
||||||
|
@ -551,6 +531,37 @@ 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 {
|
fun main() = application {
|
||||||
val state = rememberWindowState(
|
val state = rememberWindowState(
|
||||||
placement = WindowPlacement.Floating,
|
placement = WindowPlacement.Floating,
|
||||||
|
|
Loading…
Reference in New Issue