Added confirm dialog for import export

main
Cemal Odabasioglu 2023-12-19 11:23:54 +01:00
parent 7edd584d7e
commit 5e26cc7fd6
1 changed files with 47 additions and 4 deletions

View File

@ -1,10 +1,7 @@
package main package main
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material.Button import androidx.compose.material.*
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -23,6 +20,7 @@ import java.io.IOException
data class Glossary(val name: String, val jsonFilePath: String) data class Glossary(val name: String, val jsonFilePath: String)
@OptIn(ExperimentalMaterialApi::class)
@Composable @Composable
fun glossaryPage( fun glossaryPage(
languageManager: LanguageManager, languageManager: LanguageManager,
@ -76,6 +74,25 @@ fun glossaryPage(
Text(languageManager.getImportText()) 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( Button(
onClick = onExportClick, onClick = onExportClick,
modifier = Modifier modifier = Modifier
@ -88,6 +105,25 @@ fun glossaryPage(
Text(languageManager.getExportText()) 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( Button(
onClick = onSeeGlossaryClick, onClick = onSeeGlossaryClick,
modifier = Modifier modifier = Modifier
@ -118,6 +154,8 @@ fun glossaryPage(
} }
} }
var importedSuccessfully = mutableStateOf(false)
var exportedSuccessfully = mutableStateOf(false)
fun selectFile(extensions: Set<String>, onFileSelected: (String) -> Unit) { fun selectFile(extensions: Set<String>, onFileSelected: (String) -> Unit) {
val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD)
fileDialog.isMultipleMode = true // To enable selecting only one file fileDialog.isMultipleMode = true // To enable selecting only one file
@ -159,6 +197,7 @@ fun exportToCSV(glossary: Glossary, csvFilePath: String) {
FileWriter(csvFilePath).use { fileWriter -> FileWriter(csvFilePath).use { fileWriter ->
fileWriter.write(csvContent) fileWriter.write(csvContent)
} }
exportedSuccessfully.value = true
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
} }
@ -182,6 +221,7 @@ fun importFile(glossary: Glossary, filePath: String) {
} }
} }
fun importCSVFile(glossary : Glossary, filePath: String) { fun importCSVFile(glossary : Glossary, filePath: String) {
val file = File(filePath).readText(Charsets.UTF_8) val file = File(filePath).readText(Charsets.UTF_8)
@ -219,9 +259,12 @@ fun importCSVFile(glossary : Glossary, filePath: String) {
} }
addWordToGlossary(glossary.jsonFilePath, nouveauWord) addWordToGlossary(glossary.jsonFilePath, nouveauWord)
} }
importedSuccessfully.value = true
} }
} }
private fun importXLSXFile(glossary: Glossary, cheminFichier: String) { private fun importXLSXFile(glossary: Glossary, cheminFichier: String) {
val workbook: Workbook val workbook: Workbook
try { try {