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.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 com.mysql.cj.xdevapi.JsonParser
|
|
||||||
import database.DatabaseManager
|
import database.DatabaseManager
|
||||||
import kotlinx.serialization.json.JsonArray
|
|
||||||
import kotlinx.serialization.json.JsonObject
|
|
||||||
import kotlinx.serialization.json.buildJsonObject
|
import kotlinx.serialization.json.buildJsonObject
|
||||||
import kotlinx.serialization.json.put
|
import kotlinx.serialization.json.put
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileWriter
|
import java.io.FileWriter
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -46,6 +38,7 @@ fun HomePage(
|
||||||
onGlossaireClick: () -> Unit,
|
onGlossaireClick: () -> Unit,
|
||||||
onCodeAVerifierClick: () -> Unit
|
onCodeAVerifierClick: () -> Unit
|
||||||
) {
|
) {
|
||||||
|
var isHover by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
@ -208,15 +201,6 @@ 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"] = ""
|
||||||
}
|
}
|
||||||
|
@ -226,7 +210,7 @@ fun importCSV(cheminFichier: String) {
|
||||||
if (!verifierChampsRequis(mot)) {
|
if (!verifierChampsRequis(mot)) {
|
||||||
return
|
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
|
@Composable
|
||||||
fun ChoixLangagePage(
|
fun ChoixLangagePage(
|
||||||
onRetourClick: () -> Unit,
|
onRetourClick: () -> Unit,
|
||||||
|
@ -508,17 +517,24 @@ fun FormulairePage(onAnnulerClick: () -> Unit) {
|
||||||
// Validation du formulaire
|
// Validation du formulaire
|
||||||
|
|
||||||
// Create a JsonObject to represent your Mot
|
// 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(
|
||||||
|
@ -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 {
|
fun main() = application {
|
||||||
val state = rememberWindowState(
|
val state = rememberWindowState(
|
||||||
placement = WindowPlacement.Floating,
|
placement = WindowPlacement.Floating,
|
||||||
|
|
Loading…
Reference in New Issue