From 200cb8e0135728e3105306abe9671f652b974b4f Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Thu, 18 Jan 2024 17:06:05 +0100 Subject: [PATCH] Remove complexity from Glossary.kt --- src/main/kotlin/main/Compare.kt | 20 ++-- src/main/kotlin/main/Glossary.kt | 135 +++++++++++++++----------- src/main/kotlin/main/nb_occurrence.kt | 2 - 3 files changed, 84 insertions(+), 73 deletions(-) diff --git a/src/main/kotlin/main/Compare.kt b/src/main/kotlin/main/Compare.kt index d7172eb..5627f90 100644 --- a/src/main/kotlin/main/Compare.kt +++ b/src/main/kotlin/main/Compare.kt @@ -8,14 +8,15 @@ import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.Button import androidx.compose.material.MaterialTheme import androidx.compose.material.Text -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import main.component.dropdownButtonComponent -import org.apache.poi.ss.usermodel.HorizontalAlignment @Composable fun compareResults( @@ -27,8 +28,6 @@ fun compareResults( onCodeToVerifyClick: () -> Unit, ) { println(glossaryWords) - val commonWords = findCommonWords(glossaryWords, codeWords) - var isCompareClicked by remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) } Column( @@ -50,9 +49,9 @@ fun compareResults( // Espace entre les éléments horizontalArrangement = Arrangement.spacedBy(16.dp) ) { - LegendItem(color = customGreenColor, label = "Mot dans le glossaire et dans le code") - LegendItem(color = Color.White, label = "Mot seulement dans le glossaire") - LegendItem(color = customYellowColor, label = "Mot seulement dans le code") + legendItem(color = customGreenColor, label = "Mot dans le glossaire et dans le code") + legendItem(color = Color.White, label = "Mot seulement dans le glossaire") + legendItem(color = customYellowColor, label = "Mot seulement dans le code") } @@ -127,7 +126,7 @@ fun commonWordsTable( } @Composable -fun LegendItem(color: Color, label: String) { +fun legendItem(color: Color, label: String) { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp) @@ -193,8 +192,3 @@ fun commonWordsDataRow( cellContent(if (isCommon) "Oui" else "Non", modifier) } } - -fun findCommonWords(glossaryWords: List, codeWords: List): List { - val glossaryWordsSet = glossaryWords.map { it.name }.toSet() - return codeWords.filter { it in glossaryWordsSet } -} \ No newline at end of file diff --git a/src/main/kotlin/main/Glossary.kt b/src/main/kotlin/main/Glossary.kt index 93669a9..84148af 100644 --- a/src/main/kotlin/main/Glossary.kt +++ b/src/main/kotlin/main/Glossary.kt @@ -9,19 +9,6 @@ import androidx.compose.foundation.rememberScrollbarAdapter import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Delete -import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import org.apache.poi.ss.usermodel.CellType -import org.apache.poi.ss.usermodel.Workbook -import org.apache.poi.xssf.usermodel.XSSFWorkbook -import java.awt.FileDialog -import java.awt.Frame -import java.io.FileInputStream -import java.io.FileWriter -import java.io.IOException -import java.io.File import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.ExperimentalComposeUiApi @@ -30,12 +17,57 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.key.* import androidx.compose.ui.unit.dp import main.component.dropdownButtonComponent +import org.apache.poi.ss.usermodel.CellType +import org.apache.poi.ss.usermodel.Workbook +import org.apache.poi.xssf.usermodel.XSSFWorkbook +import java.awt.FileDialog +import java.awt.Frame +import java.io.File +import java.io.FileInputStream +import java.io.FileWriter +import java.io.IOException data class Glossary(val name: String, val jsonFilePath: String) val frame = Frame() +@Composable +fun glossaryItem( + glossary: Glossary, + onGlossarySelected: (Glossary) -> Unit, + onGlossaryDelete: (Glossary) -> Unit +) { + Row( + modifier = Modifier.width(200.dp).fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween + ) { + Button( + onClick = { + onGlossarySelected(glossary) + }, + modifier = Modifier.width(150.dp), + colors = ButtonDefaults.buttonColors( + backgroundColor = customRedColor, + contentColor = Color.White + ) + ) { + Text(glossary.name) + } + + IconButton( + onClick = { + onGlossaryDelete(glossary) + } + ) { + Icon( + imageVector = Icons.Default.Delete, + contentDescription = "Delete Glossary" + ) + } + } +} + @Composable fun glossariesPage( languageManager: LanguageManager, @@ -43,9 +75,7 @@ fun glossariesPage( onProjectClick: () -> Unit, onCodeToVerifyClick: () -> Unit ) { - var glossaries by remember { mutableStateOf(emptyList()) } - var isCompareClicked by remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) } Column( @@ -74,42 +104,18 @@ fun glossariesPage( modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp) ) { - println(glossaries) items(glossaries) { glossary -> - Row( - modifier = Modifier.width(200.dp).fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween - ) { - Button( - onClick = { - appState.selectedGlossary = glossary - currentPage.value = "glossaireDetail" - }, - modifier = Modifier - .width(150.dp), - colors = ButtonDefaults.buttonColors( - backgroundColor = customRedColor, - contentColor = Color.White - ) - ) { - Text(glossary.name) + glossaryItem( + glossary = glossary, + onGlossarySelected = { + appState.selectedGlossary = it + currentPage.value = "glossaireDetail" + }, + onGlossaryDelete = { + glossaries = glossaries.filterNot { g -> g == it } + handleGlossaryDeletion(it) } - - IconButton( - onClick = { - // Handle delete glossary action - glossaries = glossaries.filterNot { it == glossary } - val file = - File(glossaryPath + (appState.selectedProject?.name) + "/" + glossary.jsonFilePath) - file.delete() - } - ) { - Icon( - imageVector = Icons.Default.Delete, - contentDescription = "Delete Glossary" - ) - } - } + ) } } } @@ -124,13 +130,11 @@ fun glossariesPage( Text(languageManager.getOuText(), style = MaterialTheme.typography.h5) Spacer(modifier = Modifier.height(16.dp)) - Button( onClick = { currentPage.value = "nouveauGlossaire" }, - modifier = Modifier - .width(300.dp), + modifier = Modifier.width(300.dp), colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White @@ -145,8 +149,7 @@ fun glossariesPage( onClick = { currentPage.value = "projects" }, - modifier = Modifier - .width(250.dp), + modifier = Modifier.width(250.dp), colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White @@ -157,7 +160,11 @@ fun glossariesPage( } dropdownButtonComponent( languageManager = languageManager, - items = listOf(languageManager.getGlossaryText(), languageManager.getCodeToVerifyText(), languageManager.getCompareText()) + items = listOf( + languageManager.getGlossaryText(), + languageManager.getCodeToVerifyText(), + languageManager.getCompareText() + ) ) { selectedOption -> // Handle the selected option when (selectedOption) { @@ -175,7 +182,19 @@ fun glossariesPage( } } -@OptIn(ExperimentalStdlibApi::class, ExperimentalComposeUiApi::class) +private fun handleGlossaryDeletion(glossary: Glossary) { + val file = + File(glossaryPath + (appState.selectedProject?.name) + "/" + glossary.jsonFilePath) + val deletionResult = file.delete() + + if (deletionResult) { + println("File deleted successfully.") + } else { + println("Error while deleting file.") + } +} + +@OptIn(ExperimentalComposeUiApi::class) @Composable fun newGlossary( languageManager: LanguageManager, @@ -195,7 +214,7 @@ fun newGlossary( verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { - Text("Nom du glossaire", style = MaterialTheme.typography.h5) + Text(languageManager.getNameGlossaryText(), style = MaterialTheme.typography.h5) Spacer(modifier = Modifier.height(16.dp)) @@ -348,7 +367,7 @@ fun exportToCSV(glossary: Glossary, csvFilePath: String) { } } try { - FileWriter(csvFilePath + ".csv").use { fileWriter -> + FileWriter("$csvFilePath.csv").use { fileWriter -> fileWriter.write(csvContent) } exportedSuccessfully.value = true diff --git a/src/main/kotlin/main/nb_occurrence.kt b/src/main/kotlin/main/nb_occurrence.kt index 6f5bfff..6548d7c 100644 --- a/src/main/kotlin/main/nb_occurrence.kt +++ b/src/main/kotlin/main/nb_occurrence.kt @@ -6,7 +6,6 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material.Button import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.material.icons.Icons @@ -29,7 +28,6 @@ fun parsedWordsTable( onCodeToVerifyClick: () -> Unit, onCompareClick: () -> Unit, ) { - var isCompareClicked by remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) } // State pour stocker la liste triée des mots