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.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<Word>) {
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(
modifier = Modifier
.height(500.dp)
@ -165,10 +191,21 @@ fun glossaryTable(glossary: List<Word>) {
) {
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<Word>) {
@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))
}
}

View File

@ -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,