From afaa22dfe25c6d9b9bc7a03db1ad30c531813f7f Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:49:59 +0100 Subject: [PATCH] Set sort for glossary view --- src/main/kotlin/main/Detail.kt | 143 ++++++++++++++++++++------ src/main/kotlin/main/nb_occurrence.kt | 15 ++- 2 files changed, 120 insertions(+), 38 deletions(-) diff --git a/src/main/kotlin/main/Detail.kt b/src/main/kotlin/main/Detail.kt index b967462..697d34d 100644 --- a/src/main/kotlin/main/Detail.kt +++ b/src/main/kotlin/main/Detail.kt @@ -2,6 +2,7 @@ package main import androidx.compose.foundation.background import androidx.compose.foundation.border +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items @@ -158,6 +159,31 @@ var exportedSuccessfully = mutableStateOf(false) fun glossaryTable(glossary: List) { val listState = rememberLazyListState() + var sortedWords by remember { mutableStateOf(glossary.toList()) } + + // State pour stocker le sens du tri pour chaque bouton + var isAscendingOrderByWord by remember { mutableStateOf(true) } + var isAscendingOrderByMainContext by remember { mutableStateOf(true) } + + // Fonction pour trier par ordre alphabétique ou nombre d'occurrences + fun sortList(comparator: Comparator, isAscendingOrder: Boolean) { + sortedWords = if (isAscendingOrder) { + sortedWords.sortedWith(comparator) + } else { + sortedWords.sortedWith(comparator.reversed()) + } + } + + // Fonction pour trier par mot + fun sortByWord(isAscendingOrder: Boolean) { + sortList(compareBy { it.name }, isAscendingOrder) + } + + // Fonction pour trier par contexte principal + fun sortByMainContext(isAscendingOrder: Boolean) { + sortList(compareBy { it.mainContext }, isAscendingOrder) + } + Box( modifier = Modifier .height(500.dp) @@ -165,10 +191,21 @@ fun glossaryTable(glossary: List) { ) { LazyColumn(state = listState, modifier = Modifier.padding(16.dp)) { item { - headerRow() + headerRow( + onWordHeaderClick = { + sortByWord(!isAscendingOrderByWord) + isAscendingOrderByWord = !isAscendingOrderByWord + }, + onMainContextHeaderClick = { + sortByMainContext(!isAscendingOrderByMainContext) + isAscendingOrderByMainContext = !isAscendingOrderByMainContext + }, + isAscendingOrderByWord = isAscendingOrderByWord, + isAscendingOrderByMainContext = isAscendingOrderByMainContext + ) } - items(glossary) { word -> - val backgroundColor = if (glossary.indexOf(word) % 2 == 0) { + items(sortedWords) { word -> + val backgroundColor = if (sortedWords.indexOf(word) % 2 == 0) { Color.White } else { Color.LightGray @@ -181,32 +218,78 @@ fun glossaryTable(glossary: List) { @Composable -fun headerRow() { - val headers = listOf( - "Mot", - "Définition", - "Contexte principal", - "Contexte secondaire", - "Mot lié", - "Synonyme", - "Antonyme" - ) - +fun headerRow( + onWordHeaderClick: () -> Unit = {}, + onMainContextHeaderClick: () -> Unit = {}, + isAscendingOrderByWord: Boolean, + isAscendingOrderByMainContext: Boolean +) { Row( modifier = Modifier .fillMaxWidth() .background(customRedColor) .border(1.dp, Color.Black), + verticalAlignment = Alignment.CenterVertically ) { - headers.forEach { header -> - Text( - text = header, - fontWeight = FontWeight.Bold, - modifier = Modifier.weight(1f).padding(8.dp), - color = Color.White - ) - } + Text( + text = "Mot ${if (isAscendingOrderByWord) "▲" else "▼"}", + modifier = Modifier + .weight(1f) + .padding(8.dp) + .clickable { onWordHeaderClick() }, + fontWeight = FontWeight.Bold, + color = Color.White + ) + Text( + text = "Description", + modifier = Modifier + .weight(1f) + .padding(8.dp), + fontWeight = FontWeight.Bold, + color = Color.White + ) + Text( + text = "Contexte principal ${if (isAscendingOrderByMainContext) "▲" else "▼"}", + modifier = Modifier + .weight(1f) + .padding(8.dp) + .clickable { onMainContextHeaderClick() }, + fontWeight = FontWeight.Bold, + color = Color.White + ) + Text( + text = "Contexte secondaire", + modifier = Modifier + .weight(1f) + .padding(8.dp), + fontWeight = FontWeight.Bold, + color = Color.White + ) + Text( + text = "Lié à", + modifier = Modifier + .weight(0.5f) + .padding(4.dp), + fontWeight = FontWeight.Bold, + color = Color.White + ) + Text( + text = "Synonyme", + modifier = Modifier + .weight(0.5f) + .padding(4.dp), + fontWeight = FontWeight.Bold, + color = Color.White + ) + Text( + text = "Antonyme", + modifier = Modifier + .weight(0.5f) + .padding(4.dp), + fontWeight = FontWeight.Bold, + color = Color.White + ) } } @@ -227,14 +310,14 @@ fun glossaryDataRow(word: Word, backgroundColor: Color) { .border(1.dp, Color.Black), verticalAlignment = Alignment.CenterVertically ) { - val modifier = Modifier.weight(1f).padding(8.dp) + val modifier = Modifier.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.weight(1f)) + cellContent(word.description, modifier.weight(1f)) + cellContent(word.mainContext, modifier.weight(1f)) + cellContent(word.secondaryContext, modifier.weight(1f)) + cellContent(word.relatedTo, modifier.weight(0.5f)) + cellContent(word.synonymous, modifier.weight(0.5f)) + cellContent(word.antonym, modifier.weight(0.5f)) } } \ No newline at end of file diff --git a/src/main/kotlin/main/nb_occurrence.kt b/src/main/kotlin/main/nb_occurrence.kt index fc42ca4..0f4deb8 100644 --- a/src/main/kotlin/main/nb_occurrence.kt +++ b/src/main/kotlin/main/nb_occurrence.kt @@ -9,6 +9,8 @@ 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 +import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -93,15 +95,12 @@ fun parsedWordsTable( Spacer(modifier = Modifier.height(16.dp)) - Button( + buttonComponent( + text = languageManager.getBackText(), onClick = { onBackClick() }, - colors = androidx.compose.material.ButtonDefaults.buttonColors( - backgroundColor = customRedColor, - contentColor = Color.White - ) - ) { - Text("Retour") - } + icon = Icons.Filled.ArrowBack, + modifier = Modifier.padding(10.dp) + ) } dropdownButtonComponent( languageManager = languageManager,