From 684bacd86345b0b0f7903b1925b2c240c70ab9d8 Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:53:12 +0100 Subject: [PATCH] Update sort method --- src/main/kotlin/main/nb_occurrence.kt | 74 +++++++++++++++------------ 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/main/kotlin/main/nb_occurrence.kt b/src/main/kotlin/main/nb_occurrence.kt index 6ee4268..fc42ca4 100644 --- a/src/main/kotlin/main/nb_occurrence.kt +++ b/src/main/kotlin/main/nb_occurrence.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 @@ -64,34 +65,21 @@ fun parsedWordsTable( 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 LazyColumn(modifier = Modifier.padding(16.dp)) { item { - parsedWordsHeaderRow() + parsedWordsHeaderRow( + onWordHeaderClick = { + sortByAlphabet(!isAscendingOrderByWord) + isAscendingOrderByWord = !isAscendingOrderByWord + }, + onOccurrencesHeaderClick = { + sortByOccurrences(!isAscendingOrderByOccurrences) + isAscendingOrderByOccurrences = !isAscendingOrderByOccurrences + }, + isAscendingOrderByWord = isAscendingOrderByWord, + isAscendingOrderByOccurrences = isAscendingOrderByOccurrences + ) } items(sortedWords) { word -> val backgroundColor = if (sortedWords.indexOf(word) % 2 == 0) { @@ -136,7 +124,12 @@ fun parsedWordsTable( } @Composable -fun parsedWordsHeaderRow() { +fun parsedWordsHeaderRow( + onWordHeaderClick: () -> Unit, + onOccurrencesHeaderClick: () -> Unit, + isAscendingOrderByWord: Boolean, + isAscendingOrderByOccurrences: Boolean +) { Row( modifier = Modifier .fillMaxWidth() @@ -144,14 +137,27 @@ fun parsedWordsHeaderRow() { .border(1.dp, Color.Black), verticalAlignment = Alignment.CenterVertically ) { - listOf("Mot", "Occurrences").forEach { header -> - Text( - text = header, - fontWeight = FontWeight.Bold, - modifier = Modifier.weight(1f).padding(8.dp), - color = Color.White - ) - } + // En-tête pour le mot + Text( + text = "Mot ${if (isAscendingOrderByWord) "▲" else "▼"}", + fontWeight = FontWeight.Bold, + modifier = Modifier + .weight(1f) + .padding(8.dp) + .clickable { onWordHeaderClick() }, + color = Color.White + ) + + // En-tête pour les occurrences + Text( + text = "Occurrences ${if (isAscendingOrderByOccurrences) "▲" else "▼"}", + fontWeight = FontWeight.Bold, + modifier = Modifier + .weight(1f) + .padding(8.dp) + .clickable { onOccurrencesHeaderClick() }, + color = Color.White + ) } }