From 5e26cc7fd665e86c494cd33ca0dbf9e2b2d9d181 Mon Sep 17 00:00:00 2001 From: cemal Date: Tue, 19 Dec 2023 11:23:54 +0100 Subject: [PATCH] Added confirm dialog for import export --- src/main/kotlin/main/Glossary.kt | 51 +++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/main/Glossary.kt b/src/main/kotlin/main/Glossary.kt index 97ce7ea..75cc537 100644 --- a/src/main/kotlin/main/Glossary.kt +++ b/src/main/kotlin/main/Glossary.kt @@ -1,10 +1,7 @@ package main import androidx.compose.foundation.layout.* -import androidx.compose.material.Button -import androidx.compose.material.ButtonDefaults -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Text +import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -23,6 +20,7 @@ import java.io.IOException data class Glossary(val name: String, val jsonFilePath: String) +@OptIn(ExperimentalMaterialApi::class) @Composable fun glossaryPage( languageManager: LanguageManager, @@ -76,6 +74,25 @@ fun glossaryPage( Text(languageManager.getImportText()) } + if (importedSuccessfully.value) { + AlertDialog( + onDismissRequest = { importedSuccessfully.value = false }, + title = { Text("Importation réussie") }, + text = { Text("Le fichier a été importé avec succès") }, + confirmButton = { + Button( + onClick = { importedSuccessfully.value = false }, + colors = ButtonDefaults.buttonColors( + backgroundColor = customRedColor, + contentColor = Color.White + ) + ) { + Text("OK") + } + } + ) + } + Button( onClick = onExportClick, modifier = Modifier @@ -88,6 +105,25 @@ fun glossaryPage( Text(languageManager.getExportText()) } + if (exportedSuccessfully.value) { + AlertDialog( + onDismissRequest = { exportedSuccessfully.value = false }, + title = { Text("Exportation réussie") }, + text = { Text("Le fichier a été exporté avec succès") }, + confirmButton = { + Button( + onClick = { exportedSuccessfully.value = false }, + colors = ButtonDefaults.buttonColors( + backgroundColor = customRedColor, + contentColor = Color.White + ) + ) { + Text("OK") + } + } + ) + } + Button( onClick = onSeeGlossaryClick, modifier = Modifier @@ -118,6 +154,8 @@ fun glossaryPage( } } +var importedSuccessfully = mutableStateOf(false) +var exportedSuccessfully = mutableStateOf(false) fun selectFile(extensions: Set, onFileSelected: (String) -> Unit) { val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) fileDialog.isMultipleMode = true // To enable selecting only one file @@ -159,6 +197,7 @@ fun exportToCSV(glossary: Glossary, csvFilePath: String) { FileWriter(csvFilePath).use { fileWriter -> fileWriter.write(csvContent) } + exportedSuccessfully.value = true } catch (e: IOException) { e.printStackTrace() } @@ -182,6 +221,7 @@ fun importFile(glossary: Glossary, filePath: String) { } } + fun importCSVFile(glossary : Glossary, filePath: String) { val file = File(filePath).readText(Charsets.UTF_8) @@ -219,9 +259,12 @@ fun importCSVFile(glossary : Glossary, filePath: String) { } addWordToGlossary(glossary.jsonFilePath, nouveauWord) } + importedSuccessfully.value = true } + } + private fun importXLSXFile(glossary: Glossary, cheminFichier: String) { val workbook: Workbook try {