Add compare in table

main
CAPEL Maxime 2023-12-19 11:26:29 +01:00
parent 5e26cc7fd6
commit c65c90b0a1
6 changed files with 100 additions and 77 deletions

View File

@ -1,17 +1,18 @@
package main package main
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.border
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.MaterialTheme import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
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.unit.dp import androidx.compose.ui.unit.dp
@Composable @Composable
@ -19,38 +20,28 @@ fun compareResults(
glossaryWords: List<Word>, glossaryWords: List<Word>,
codeWords: List<String>, codeWords: List<String>,
onBackClick: () -> Unit onBackClick: () -> Unit
) { ) {
val commonWords = findCommonWords(glossaryWords, codeWords)
Column( Column(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Top, // Align content at the top verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
Text( 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 style = MaterialTheme.typography.h3
) )
}
Column( Spacer(modifier = Modifier.height(16.dp))
modifier = Modifier
.fillMaxSize() // Fills the maximum available width
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) { commonWordsTable(glossaryWords, codeWords)
Text(text = "Le code contient ${compareWords(glossaryWords, codeWords)}% des mots du glossaire")
} Spacer(modifier = Modifier.height(16.dp))
Column(
modifier = Modifier.fillMaxSize().padding(20.dp),
verticalArrangement = Arrangement.Bottom,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button( Button(
onClick = onBackClick, onClick = onBackClick,
colors = ButtonDefaults.buttonColors( colors = androidx.compose.material.ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
) )
@ -58,18 +49,79 @@ fun compareResults(
Text("Retour") Text("Retour")
} }
} }
} }
fun compareWords(glossaryWord: List<Word>, wordsCode: List<String>): Int { @Composable
var foundWords = 0 fun commonWordsTable(
wordsCode.forEach { wordCode -> glossaryWords: List<Word>,
glossaryWord.forEach { word -> codeWords: List<String>
if (wordCode.equals(word.name, ignoreCase = true)) { ) {
foundWords++ 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<String>,
glossaryWordsSet: Set<String>
) {
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<Word>, codeWords: List<String>): List<String> {
val glossaryWordsSet = glossaryWords.map { it.name }.toSet()
return codeWords.filter { it in glossaryWordsSet }
} }

View File

@ -93,7 +93,7 @@ fun headerRow() {
} }
@Composable @Composable
fun CellContent(text: String, modifier: Modifier = Modifier) { fun cellContent(text: String, modifier: Modifier = Modifier) {
Text( Text(
text = text, text = text,
modifier = modifier modifier = modifier
@ -111,12 +111,12 @@ fun glossaryDataRow(word: Word) {
) { ) {
val modifier = Modifier.weight(1f).padding(8.dp) val modifier = Modifier.weight(1f).padding(8.dp)
// Contenu de chaque cellule // Contenu de chaque cellule
CellContent(word.name, modifier) cellContent(word.name, modifier)
CellContent(word.description, modifier) cellContent(word.description, modifier)
CellContent(word.mainContext, modifier) cellContent(word.mainContext, modifier)
CellContent(word.secondaryContext, modifier) cellContent(word.secondaryContext, modifier)
CellContent(word.relatedTo, modifier) cellContent(word.relatedTo, modifier)
CellContent(word.synonymous, modifier) cellContent(word.synonymous, modifier)
CellContent(word.antonym, modifier) cellContent(word.antonym, modifier)
} }
} }

View File

@ -28,8 +28,7 @@ fun glossaryPage(
onImportClick: () -> Unit, onImportClick: () -> Unit,
onExportClick: () -> Unit, onExportClick: () -> Unit,
onSeeGlossaryClick: () -> Unit, onSeeGlossaryClick: () -> Unit,
onBackClick: () -> Unit, onBackClick: () -> Unit
selectedGlossary: Glossary
) { ) {

View File

@ -1,8 +1,8 @@
package main package main
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@ -10,7 +10,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import java.util.*
var mostUsedWordList = mutableMapOf<String, Int>() var mostUsedWordList = mutableMapOf<String, Int>()
@ -129,28 +128,3 @@ 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"
}
}

View File

@ -246,10 +246,8 @@ fun app() {
onSeeGlossaryClick = { onSeeGlossaryClick = {
glossaryDetail = selectedGlossary?.let { loadDatasFromFile(it.jsonFilePath) }!! glossaryDetail = selectedGlossary?.let { loadDatasFromFile(it.jsonFilePath) }!!
currentPage.value = "glossaireDetail" currentPage.value = "glossaireDetail"
}, }
onBackClick = { currentPage.value = "glossaires" }, ) { currentPage.value = "glossaires" }
selectedGlossary = selectedGlossary!!
)
} }
"glossaireDetail" -> { "glossaireDetail" -> {
glossaryDetailedPage( glossaryDetailedPage(

View File

@ -80,7 +80,7 @@ fun parsedWordsDataRow(word: String, count: Int) {
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
val modifier = Modifier.weight(1f).padding(8.dp) val modifier = Modifier.weight(1f).padding(8.dp)
CellContent(word, modifier) cellContent(word, modifier)
CellContent(count.toString(), modifier) cellContent(count.toString(), modifier)
} }
} }