From c65c90b0a18bd835940d7d4b4358b8f4d34c55cd Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:26:29 +0100 Subject: [PATCH] Add compare in table --- src/main/kotlin/main/Compare.kt | 118 +++++++++++++++++++------- src/main/kotlin/main/Detail.kt | 16 ++-- src/main/kotlin/main/Glossary.kt | 3 +- src/main/kotlin/main/Home.kt | 30 +------ src/main/kotlin/main/Main.kt | 6 +- src/main/kotlin/main/nb_occurrence.kt | 4 +- 6 files changed, 100 insertions(+), 77 deletions(-) diff --git a/src/main/kotlin/main/Compare.kt b/src/main/kotlin/main/Compare.kt index 260de71..cd23238 100644 --- a/src/main/kotlin/main/Compare.kt +++ b/src/main/kotlin/main/Compare.kt @@ -1,17 +1,18 @@ package main -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.background +import androidx.compose.foundation.border +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.ButtonDefaults import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.Composable 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 @Composable @@ -19,38 +20,28 @@ fun compareResults( glossaryWords: List, codeWords: List, onBackClick: () -> Unit - ) { + val commonWords = findCommonWords(glossaryWords, codeWords) + Column( modifier = Modifier.fillMaxSize(), - verticalArrangement = Arrangement.Top, // Align content at the top + verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally ) { Text( - text = "Le code contient ${compareWords(glossaryWords, codeWords)}% des mots du glossaire", + text = "Le code contient ${commonWords.size}% des mots du glossaire", style = MaterialTheme.typography.h3 ) - } - Column( - modifier = Modifier - .fillMaxSize() // Fills the maximum available width - .padding(16.dp), - verticalArrangement = Arrangement.Center, - horizontalAlignment = Alignment.CenterHorizontally + Spacer(modifier = Modifier.height(16.dp)) - ) { - Text(text = "Le code contient ${compareWords(glossaryWords, codeWords)}% des mots du glossaire") - } + commonWordsTable(glossaryWords, codeWords) + + Spacer(modifier = Modifier.height(16.dp)) - Column( - modifier = Modifier.fillMaxSize().padding(20.dp), - verticalArrangement = Arrangement.Bottom, - horizontalAlignment = Alignment.CenterHorizontally - ) { Button( onClick = onBackClick, - colors = ButtonDefaults.buttonColors( + colors = androidx.compose.material.ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White ) @@ -58,18 +49,79 @@ fun compareResults( Text("Retour") } } - } -fun compareWords(glossaryWord: List, wordsCode: List): Int { - var foundWords = 0 - wordsCode.forEach { wordCode -> - glossaryWord.forEach { word -> - if (wordCode.equals(word.name, ignoreCase = true)) { - foundWords++ +@Composable +fun commonWordsTable( + glossaryWords: List, + codeWords: List +) { + val glossaryWordsSet = glossaryWords.map { it.name }.toSet() + + LazyColumn( + modifier = Modifier.fillMaxSize(), + content = { + item { + commonWordsHeaderRow() + } + items(glossaryWords) { word -> + commonWordsDataRow(word, codeWords, glossaryWordsSet) } } + ) +} + +@Composable +fun commonWordsHeaderRow() { + Row( + modifier = Modifier + .fillMaxWidth() + .background(Color.Gray) + .border(1.dp, Color.Black), + verticalAlignment = Alignment.CenterVertically + ) { + listOf("Mot du glossaire", "Mot du code", "Présence commune").forEach { header -> + Text( + text = header, + fontWeight = FontWeight.Bold, + modifier = Modifier.weight(1f).padding(8.dp), + color = Color.White + ) + } } - println(foundWords * 100 / wordsCode.size) - return foundWords * 100 / wordsCode.size +} + +@Composable +fun commonWordsDataRow( + word: Word, + codeWords: List, + glossaryWordsSet: Set +) { + val modifier = Modifier + .fillMaxWidth() + .border(1.dp, Color.Black) + .padding(8.dp) + + val isCommon = word.name in codeWords && word.name in glossaryWordsSet + + val backgroundColor = when { + isCommon -> Color.Yellow + word.name in codeWords -> Color.Blue // Couleur pour les mots du code + word.name in glossaryWordsSet -> Color.Green // Couleur pour les mots du glossaire + else -> Color.White + } + + Row( + modifier = modifier.background(backgroundColor), + verticalAlignment = Alignment.CenterVertically + ) { + cellContent(word.name, modifier) + cellContent(if (word.name in codeWords) "Oui" else "Non", modifier) + 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/Detail.kt b/src/main/kotlin/main/Detail.kt index f33dcbf..f4c0775 100644 --- a/src/main/kotlin/main/Detail.kt +++ b/src/main/kotlin/main/Detail.kt @@ -93,7 +93,7 @@ fun headerRow() { } @Composable -fun CellContent(text: String, modifier: Modifier = Modifier) { +fun cellContent(text: String, modifier: Modifier = Modifier) { Text( text = text, modifier = modifier @@ -111,12 +111,12 @@ fun glossaryDataRow(word: Word) { ) { val modifier = Modifier.weight(1f).padding(8.dp) // Contenu de chaque cellule - CellContent(word.name, modifier) - CellContent(word.description, modifier) - CellContent(word.mainContext, modifier) - CellContent(word.secondaryContext, modifier) - CellContent(word.relatedTo, modifier) - CellContent(word.synonymous, modifier) - CellContent(word.antonym, modifier) + cellContent(word.name, modifier) + cellContent(word.description, modifier) + cellContent(word.mainContext, modifier) + cellContent(word.secondaryContext, modifier) + cellContent(word.relatedTo, modifier) + cellContent(word.synonymous, modifier) + cellContent(word.antonym, modifier) } } \ No newline at end of file diff --git a/src/main/kotlin/main/Glossary.kt b/src/main/kotlin/main/Glossary.kt index 75cc537..666ecda 100644 --- a/src/main/kotlin/main/Glossary.kt +++ b/src/main/kotlin/main/Glossary.kt @@ -28,8 +28,7 @@ fun glossaryPage( onImportClick: () -> Unit, onExportClick: () -> Unit, onSeeGlossaryClick: () -> Unit, - onBackClick: () -> Unit, - selectedGlossary: Glossary + onBackClick: () -> Unit ) { diff --git a/src/main/kotlin/main/Home.kt b/src/main/kotlin/main/Home.kt index 3bf712a..cbadc6c 100644 --- a/src/main/kotlin/main/Home.kt +++ b/src/main/kotlin/main/Home.kt @@ -1,8 +1,8 @@ package main import androidx.compose.foundation.Image -import androidx.compose.foundation.layout.* import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment @@ -10,7 +10,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp -import java.util.* var mostUsedWordList = mutableMapOf() @@ -128,29 +127,4 @@ fun homePage( ) } } -} - -@Composable -fun getGlossaryButtonText(language: Language): String { - return when (language) { - Language.FRENCH -> "Glossaire" - Language.ENGLISH -> "Glossary" - } -} - -@Composable -fun getCodeToVerifyButtonText(language: Language): String { - return when (language) { - Language.FRENCH -> "Code à Vérifier" - Language.ENGLISH -> "Code to Verify" - } -} - -@Composable -fun getCompareButtonText(language: Language): String { - return when (language) { - Language.FRENCH -> "Comparer" - Language.ENGLISH -> "Compare" - } -} - +} \ No newline at end of file diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 8adadc5..f19661a 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -246,10 +246,8 @@ fun app() { onSeeGlossaryClick = { glossaryDetail = selectedGlossary?.let { loadDatasFromFile(it.jsonFilePath) }!! currentPage.value = "glossaireDetail" - }, - onBackClick = { currentPage.value = "glossaires" }, - selectedGlossary = selectedGlossary!! - ) + } + ) { currentPage.value = "glossaires" } } "glossaireDetail" -> { glossaryDetailedPage( diff --git a/src/main/kotlin/main/nb_occurrence.kt b/src/main/kotlin/main/nb_occurrence.kt index 116dbb4..5a955f9 100644 --- a/src/main/kotlin/main/nb_occurrence.kt +++ b/src/main/kotlin/main/nb_occurrence.kt @@ -80,7 +80,7 @@ fun parsedWordsDataRow(word: String, count: Int) { verticalAlignment = Alignment.CenterVertically ) { val modifier = Modifier.weight(1f).padding(8.dp) - CellContent(word, modifier) - CellContent(count.toString(), modifier) + cellContent(word, modifier) + cellContent(count.toString(), modifier) } } \ No newline at end of file