Set sort for glossary view

main
CAPEL Maxime 2024-01-17 15:49:59 +01:00
parent 684bacd863
commit afaa22dfe2
2 changed files with 120 additions and 38 deletions

View File

@ -2,6 +2,7 @@ package main
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
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
@ -158,6 +159,31 @@ var exportedSuccessfully = mutableStateOf(false)
fun glossaryTable(glossary: List<Word>) { fun glossaryTable(glossary: List<Word>) {
val listState = rememberLazyListState() 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<Word>, 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( Box(
modifier = Modifier modifier = Modifier
.height(500.dp) .height(500.dp)
@ -165,10 +191,21 @@ fun glossaryTable(glossary: List<Word>) {
) { ) {
LazyColumn(state = listState, modifier = Modifier.padding(16.dp)) { LazyColumn(state = listState, modifier = Modifier.padding(16.dp)) {
item { item {
headerRow() headerRow(
onWordHeaderClick = {
sortByWord(!isAscendingOrderByWord)
isAscendingOrderByWord = !isAscendingOrderByWord
},
onMainContextHeaderClick = {
sortByMainContext(!isAscendingOrderByMainContext)
isAscendingOrderByMainContext = !isAscendingOrderByMainContext
},
isAscendingOrderByWord = isAscendingOrderByWord,
isAscendingOrderByMainContext = isAscendingOrderByMainContext
)
} }
items(glossary) { word -> items(sortedWords) { word ->
val backgroundColor = if (glossary.indexOf(word) % 2 == 0) { val backgroundColor = if (sortedWords.indexOf(word) % 2 == 0) {
Color.White Color.White
} else { } else {
Color.LightGray Color.LightGray
@ -181,32 +218,78 @@ fun glossaryTable(glossary: List<Word>) {
@Composable @Composable
fun headerRow() { fun headerRow(
val headers = listOf( onWordHeaderClick: () -> Unit = {},
"Mot", onMainContextHeaderClick: () -> Unit = {},
"Définition", isAscendingOrderByWord: Boolean,
"Contexte principal", isAscendingOrderByMainContext: Boolean
"Contexte secondaire", ) {
"Mot lié",
"Synonyme",
"Antonyme"
)
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.background(customRedColor) .background(customRedColor)
.border(1.dp, Color.Black), .border(1.dp, Color.Black),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
headers.forEach { header ->
Text( Text(
text = header, 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, fontWeight = FontWeight.Bold,
modifier = Modifier.weight(1f).padding(8.dp),
color = Color.White color = Color.White
) )
}
} }
} }
@ -227,14 +310,14 @@ fun glossaryDataRow(word: Word, backgroundColor: Color) {
.border(1.dp, Color.Black), .border(1.dp, Color.Black),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
val modifier = Modifier.weight(1f).padding(8.dp) val modifier = Modifier.padding(8.dp)
// Contenu de chaque cellule // Contenu de chaque cellule
cellContent(word.name, modifier) cellContent(word.name, modifier.weight(1f))
cellContent(word.description, modifier) cellContent(word.description, modifier.weight(1f))
cellContent(word.mainContext, modifier) cellContent(word.mainContext, modifier.weight(1f))
cellContent(word.secondaryContext, modifier) cellContent(word.secondaryContext, modifier.weight(1f))
cellContent(word.relatedTo, modifier) cellContent(word.relatedTo, modifier.weight(0.5f))
cellContent(word.synonymous, modifier) cellContent(word.synonymous, modifier.weight(0.5f))
cellContent(word.antonym, modifier) cellContent(word.antonym, modifier.weight(0.5f))
} }
} }

View File

@ -9,6 +9,8 @@ import androidx.compose.foundation.lazy.items
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.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -93,15 +95,12 @@ fun parsedWordsTable(
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Button( buttonComponent(
text = languageManager.getBackText(),
onClick = { onBackClick() }, onClick = { onBackClick() },
colors = androidx.compose.material.ButtonDefaults.buttonColors( icon = Icons.Filled.ArrowBack,
backgroundColor = customRedColor, modifier = Modifier.padding(10.dp)
contentColor = Color.White
) )
) {
Text("Retour")
}
} }
dropdownButtonComponent( dropdownButtonComponent(
languageManager = languageManager, languageManager = languageManager,