UI: remove code duplication in sort menu

This commit is contained in:
Han Yin 2025-04-17 20:51:59 -07:00
parent 41615be5ae
commit 481ba6e9d3
1 changed files with 28 additions and 67 deletions

View File

@ -83,7 +83,7 @@ fun ModelsManagementBottomBar(
BottomAppBar(
actions = {
if (selection.isActive) {
// Multi-selection mode actions
/* Multi-selection mode actions */
IconButton(onClick = { selection.toggleAllSelection(true) }) {
Icon(
imageVector = Icons.Default.SelectAll,
@ -111,8 +111,11 @@ fun ModelsManagementBottomBar(
MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.3f)
)
}
} else {
// Default mode actions
/* Default mode actions */
// Sorting action
IconButton(onClick = { sorting.toggleMenu(true) }) {
Icon(
imageVector = Icons.AutoMirrored.Filled.Sort,
@ -120,78 +123,35 @@ fun ModelsManagementBottomBar(
)
}
// Sort dropdown menu
// Sorting dropdown menu
DropdownMenu(
expanded = sorting.isMenuVisible,
onDismissRequest = { sorting.toggleMenu(false) }
) {
val sortOptions = listOf(
Triple(ModelSortOrder.NAME_ASC, "Name (A-Z)", "Sort by name in ascending order"),
Triple(ModelSortOrder.NAME_DESC, "Name (Z-A)", "Sort by name in descending order"),
Triple(ModelSortOrder.SIZE_ASC, "Size (Smallest first)", "Sort by size in ascending order"),
Triple(ModelSortOrder.SIZE_DESC, "Size (Largest first)", "Sort by size in descending order"),
Triple(ModelSortOrder.LAST_USED, "Last used", "Sort by last used")
)
sortOptions.forEach { (order, label, description) ->
DropdownMenuItem(
text = { Text("Name (A-Z)") },
text = { Text(label) },
trailingIcon = {
if (sorting.currentOrder == ModelSortOrder.NAME_ASC)
if (sorting.currentOrder == order)
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Sort by name in ascending order, selected"
contentDescription = "$description, selected"
)
},
onClick = {
sorting.selectOrder(ModelSortOrder.NAME_ASC)
onClick = { sorting.selectOrder(order) }
)
}
)
DropdownMenuItem(
text = { Text("Name (Z-A)") },
trailingIcon = {
if (sorting.currentOrder == ModelSortOrder.NAME_DESC)
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Sort by name in descending order, selected"
)
},
onClick = {
sorting.selectOrder(ModelSortOrder.NAME_DESC)
}
)
DropdownMenuItem(
text = { Text("Size (Smallest first)") },
trailingIcon = {
if (sorting.currentOrder == ModelSortOrder.SIZE_ASC)
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Sort by size in ascending order, selected"
)
},
onClick = {
sorting.selectOrder(ModelSortOrder.SIZE_ASC)
}
)
DropdownMenuItem(
text = { Text("Size (Largest first)") },
trailingIcon = {
if (sorting.currentOrder == ModelSortOrder.SIZE_DESC)
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Sort by size in descending order, selected"
)
},
onClick = {
sorting.selectOrder(ModelSortOrder.SIZE_DESC)
}
)
DropdownMenuItem(
text = { Text("Last used") },
trailingIcon = {
if (sorting.currentOrder == ModelSortOrder.LAST_USED)
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Sort by last used, selected"
)
},
onClick = {
sorting.selectOrder(ModelSortOrder.LAST_USED)
}
)
}
// Filtering action
IconButton(
onClick = filtering.onClick
) {
@ -201,6 +161,7 @@ fun ModelsManagementBottomBar(
)
}
// Selection action
IconButton(onClick = { selection.toggleMode(true) }) {
Icon(
imageVector = Icons.Default.DeleteSweep,