I have a table using LazyColumn, there I have a button for sorting Ascending,Descending,Not sorted.
data class ToolListState(
val tools: List<GetToolsWithService> = emptyList()
)
val stateList: MutableStateFlow<ToolListState> = MutableStateFlow(ToolListState())
class ViewComponent {
fun sort(sortColumn: SortColumn) {
componentScope.launch {
println("Sort Start")
stateList.value = stateList.value.copy(tools = sortTable(sortColumn, stateList.value.tools))
stateList.value.tools.forEach {
println(it)
}
}
println("Sort end")
}
}
fun injectListFromDatabase(list: List<GetToolsWithService>) {
stateList.value = stateList.value.copy(tools = list)
}
}
@Composable
fun View(component: ViewComponent) {
val stateList by component.stateList.collectAsState()
val sortOrder by component.sortingOrderData
LazyColumn {
stickyHeader {
Row {... header ...}
}
items(stateList.value.tools) {
Row {... tableRow ...}
println(row.toString()) // here it will print twice one sorted list and immediately unsorted
}
item {
Button(onclick = { component.sort(sortColumn) }) {
Icon(
imageVector = when (sortOrder) {
SortOrder.ASCENDING -> Icons.Default.KeyboardArrowUp
SortOrder.DESCENDING -> Icons.Default.KeyboardArrowDown
SortOrder.NOSORT -> Icons.Default.UnfoldMore
},
contentDescription = "Sort",
tint = MaterialTheme.colorScheme.onPrimaryContainer
)
}
}
}
}
After search result and before the view is called a list is injected using function injectListFromDatabase from searchView and a table is created.
After I push my sort button, icon will change according to sorting order, my function sort will be called once (used println for test as in code), function sortTable will return my sorted list (tested with println as in code), LazyColumn will recompose new changes but immediately recomposes back to not sorted (tested with println at the end of items). Variable sortColumn only selects by which table column to sort by.
But I don’t know why stateList.value.tools changes back to not sorted I don’t understand how.
I tried to use mutableStateOf on data class but it didn’t seem to change anything.