Set sort for parsed words

main
CAPEL Maxime 2024-01-17 14:29:46 +01:00
parent 04f263dbb3
commit 1fa9c904bd
1 changed files with 54 additions and 3 deletions

View File

@ -14,6 +14,7 @@ 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.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import main.component.buttonComponent
import main.component.dropdownButtonComponent import main.component.dropdownButtonComponent
@Composable @Composable
@ -24,10 +25,36 @@ fun parsedWordsTable(
onProjectClick: () -> Unit, onProjectClick: () -> Unit,
onCodeToVerifyClick: () -> Unit, onCodeToVerifyClick: () -> Unit,
) { ) {
var isCompareClicked by remember { mutableStateOf(false) } var isCompareClicked by remember { mutableStateOf(false) }
val noFileSnackbarVisibleState = remember { mutableStateOf(false) } val noFileSnackbarVisibleState = remember { mutableStateOf(false) }
// State pour stocker la liste triée des mots
var sortedWords by remember { mutableStateOf(parsedWords.toList()) }
// State pour stocker le sens du tri pour chaque bouton
var isAscendingOrderByWord by remember { mutableStateOf(true) }
var isAscendingOrderByOccurrences by remember { mutableStateOf(true) }
// Fonction pour trier par ordre alphabétique ou nombre d'occurrences
fun sortList(comparator: Comparator<Pair<String, Int>>, isAscendingOrder: Boolean) {
sortedWords = if (isAscendingOrder) {
parsedWords.toList().sortedWith(comparator)
} else {
parsedWords.toList().sortedWith(comparator.reversed())
}
}
// Fonction pour trier par ordre alphabétique
fun sortByAlphabet(isAscendingOrder: Boolean) {
sortList(compareBy { it.first }, isAscendingOrder)
}
// Fonction pour trier par nombre d'occurrences
fun sortByOccurrences(isAscendingOrder: Boolean) {
sortList(compareByDescending { it.second }, isAscendingOrder)
}
Column( Column(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Top, verticalArrangement = Arrangement.Top,
@ -37,13 +64,37 @@ fun parsedWordsTable(
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
// Ajoutez des boutons de tri
Row(
modifier = Modifier
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
buttonComponent(
text = "Trier par mot ${if (isAscendingOrderByWord) "▲" else "▼"}",
onClick = {
sortByAlphabet(!isAscendingOrderByWord)
isAscendingOrderByWord = !isAscendingOrderByWord
},
width = 250,
)
buttonComponent(
text = "Trier par occurrences ${if (isAscendingOrderByOccurrences) "▲" else "▼"}",
onClick = {
sortByOccurrences(!isAscendingOrderByOccurrences)
isAscendingOrderByOccurrences = !isAscendingOrderByOccurrences
},
width = 250,
)
}
// Tableau des mots parsés // Tableau des mots parsés
LazyColumn(modifier = Modifier.padding(16.dp)) { LazyColumn(modifier = Modifier.padding(16.dp)) {
item { item {
parsedWordsHeaderRow() parsedWordsHeaderRow()
} }
items(parsedWords.toList()) { word -> items(sortedWords) { word ->
val backgroundColor = if (parsedWords.toList().indexOf(word) % 2 == 0) { val backgroundColor = if (sortedWords.indexOf(word) % 2 == 0) {
Color.White Color.White
} else { } else {
Color.LightGray Color.LightGray