Remove complexity from Glossary.kt

main
CAPEL Maxime 2024-01-18 17:06:05 +01:00
parent 8d28997ad7
commit 200cb8e013
3 changed files with 84 additions and 73 deletions

View File

@ -8,14 +8,15 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text 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.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import main.component.dropdownButtonComponent import main.component.dropdownButtonComponent
import org.apache.poi.ss.usermodel.HorizontalAlignment
@Composable @Composable
fun compareResults( fun compareResults(
@ -27,8 +28,6 @@ fun compareResults(
onCodeToVerifyClick: () -> Unit, onCodeToVerifyClick: () -> Unit,
) { ) {
println(glossaryWords) println(glossaryWords)
val commonWords = findCommonWords(glossaryWords, codeWords)
var isCompareClicked by remember { mutableStateOf(false) }
val noFileSnackbarVisibleState = remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) }
Column( Column(
@ -50,9 +49,9 @@ fun compareResults(
// Espace entre les éléments // Espace entre les éléments
horizontalArrangement = Arrangement.spacedBy(16.dp) horizontalArrangement = Arrangement.spacedBy(16.dp)
) { ) {
LegendItem(color = customGreenColor, label = "Mot dans le glossaire et 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 = Color.White, label = "Mot seulement dans le glossaire")
LegendItem(color = customYellowColor, label = "Mot seulement dans le code") legendItem(color = customYellowColor, label = "Mot seulement dans le code")
} }
@ -127,7 +126,7 @@ fun commonWordsTable(
} }
@Composable @Composable
fun LegendItem(color: Color, label: String) { fun legendItem(color: Color, label: String) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(4.dp) horizontalArrangement = Arrangement.spacedBy(4.dp)
@ -193,8 +192,3 @@ fun commonWordsDataRow(
cellContent(if (isCommon) "Oui" else "Non", modifier) cellContent(if (isCommon) "Oui" else "Non", modifier)
} }
} }
fun findCommonWords(glossaryWords: List<Word>, codeWords: List<String>): List<String> {
val glossaryWordsSet = glossaryWords.map { it.name }.toSet()
return codeWords.filter { it in glossaryWordsSet }
}

View File

@ -9,19 +9,6 @@ import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete 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.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi 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.input.key.*
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import main.component.dropdownButtonComponent 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) data class Glossary(val name: String, val jsonFilePath: String)
val frame = Frame() 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 @Composable
fun glossariesPage( fun glossariesPage(
languageManager: LanguageManager, languageManager: LanguageManager,
@ -43,9 +75,7 @@ fun glossariesPage(
onProjectClick: () -> Unit, onProjectClick: () -> Unit,
onCodeToVerifyClick: () -> Unit onCodeToVerifyClick: () -> Unit
) { ) {
var glossaries by remember { mutableStateOf(emptyList<Glossary>()) } var glossaries by remember { mutableStateOf(emptyList<Glossary>()) }
var isCompareClicked by remember { mutableStateOf(false) }
val noFileSnackbarVisibleState = remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) }
Column( Column(
@ -74,42 +104,18 @@ fun glossariesPage(
modifier = Modifier.padding(10.dp), modifier = Modifier.padding(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp) verticalArrangement = Arrangement.spacedBy(10.dp)
) { ) {
println(glossaries)
items(glossaries) { glossary -> items(glossaries) { glossary ->
Row( glossaryItem(
modifier = Modifier.width(200.dp).fillMaxWidth(), glossary = glossary,
horizontalArrangement = Arrangement.SpaceBetween onGlossarySelected = {
) { appState.selectedGlossary = it
Button(
onClick = {
appState.selectedGlossary = glossary
currentPage.value = "glossaireDetail" currentPage.value = "glossaireDetail"
}, },
modifier = Modifier onGlossaryDelete = {
.width(150.dp), glossaries = glossaries.filterNot { g -> g == it }
colors = ButtonDefaults.buttonColors( handleGlossaryDeletion(it)
backgroundColor = customRedColor, }
contentColor = Color.White
) )
) {
Text(glossary.name)
}
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) Text(languageManager.getOuText(), style = MaterialTheme.typography.h5)
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( Button(
onClick = { onClick = {
currentPage.value = "nouveauGlossaire" currentPage.value = "nouveauGlossaire"
}, },
modifier = Modifier modifier = Modifier.width(300.dp),
.width(300.dp),
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
@ -145,8 +149,7 @@ fun glossariesPage(
onClick = { onClick = {
currentPage.value = "projects" currentPage.value = "projects"
}, },
modifier = Modifier modifier = Modifier.width(250.dp),
.width(250.dp),
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
@ -157,7 +160,11 @@ fun glossariesPage(
} }
dropdownButtonComponent( dropdownButtonComponent(
languageManager = languageManager, languageManager = languageManager,
items = listOf(languageManager.getGlossaryText(), languageManager.getCodeToVerifyText(), languageManager.getCompareText()) items = listOf(
languageManager.getGlossaryText(),
languageManager.getCodeToVerifyText(),
languageManager.getCompareText()
)
) { selectedOption -> ) { selectedOption ->
// Handle the selected option // Handle the selected option
when (selectedOption) { 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 @Composable
fun newGlossary( fun newGlossary(
languageManager: LanguageManager, languageManager: LanguageManager,
@ -195,7 +214,7 @@ fun newGlossary(
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
Text("Nom du glossaire", style = MaterialTheme.typography.h5) Text(languageManager.getNameGlossaryText(), style = MaterialTheme.typography.h5)
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
@ -348,7 +367,7 @@ fun exportToCSV(glossary: Glossary, csvFilePath: String) {
} }
} }
try { try {
FileWriter(csvFilePath + ".csv").use { fileWriter -> FileWriter("$csvFilePath.csv").use { fileWriter ->
fileWriter.write(csvContent) fileWriter.write(csvContent)
} }
exportedSuccessfully.value = true exportedSuccessfully.value = true

View File

@ -6,7 +6,6 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
@ -29,7 +28,6 @@ fun parsedWordsTable(
onCodeToVerifyClick: () -> Unit, onCodeToVerifyClick: () -> Unit,
onCompareClick: () -> Unit, onCompareClick: () -> Unit,
) { ) {
var isCompareClicked by remember { mutableStateOf(false) }
val noFileSnackbarVisibleState = remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) }
// State pour stocker la liste triée des mots // State pour stocker la liste triée des mots