mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-14 20:39:59 -05:00
cleanup code
This commit is contained in:
parent
82ece10ba0
commit
a5cc1a2586
5 changed files with 24 additions and 21 deletions
|
@ -41,7 +41,7 @@ fun CategoryScreen(
|
|||
onClickSortAlphabetically: () -> Unit,
|
||||
onClickRename: (Category) -> Unit,
|
||||
onClickDelete: (Category) -> Unit,
|
||||
moveTo: (Category, Int) -> Unit,
|
||||
changeOrder: (Category, Int) -> Unit,
|
||||
navigateUp: () -> Unit,
|
||||
) {
|
||||
val lazyListState = rememberLazyListState()
|
||||
|
@ -87,7 +87,7 @@ fun CategoryScreen(
|
|||
PaddingValues(horizontal = MaterialTheme.padding.medium),
|
||||
onClickRename = onClickRename,
|
||||
onClickDelete = onClickDelete,
|
||||
moveTo = moveTo,
|
||||
changeOrder = changeOrder,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -99,12 +99,12 @@ private fun CategoryContent(
|
|||
paddingValues: PaddingValues,
|
||||
onClickRename: (Category) -> Unit,
|
||||
onClickDelete: (Category) -> Unit,
|
||||
moveTo: (Category, Int) -> Unit,
|
||||
changeOrder: (Category, Int) -> Unit,
|
||||
) {
|
||||
var reorderableList by remember { mutableStateOf(categories.toList()) }
|
||||
val reorderableLazyColumnState = rememberReorderableLazyListState(lazyListState) { from, to ->
|
||||
reorderableList = reorderableList.toMutableList().apply {
|
||||
moveTo(reorderableList[from.index], to.index - from.index)
|
||||
changeOrder(reorderableList[from.index], to.index - from.index)
|
||||
add(to.index, removeAt(from.index))
|
||||
}
|
||||
}
|
||||
|
@ -122,9 +122,9 @@ private fun CategoryContent(
|
|||
) {
|
||||
items(
|
||||
items = reorderableList,
|
||||
key = { category -> category.key() },
|
||||
key = { category -> category.key },
|
||||
) { category ->
|
||||
ReorderableItem(reorderableLazyColumnState, category.key()) {
|
||||
ReorderableItem(reorderableLazyColumnState, category.key) {
|
||||
CategoryListItem(
|
||||
modifier = Modifier.animateItem(),
|
||||
category = category,
|
||||
|
@ -136,4 +136,4 @@ private fun CategoryContent(
|
|||
}
|
||||
}
|
||||
|
||||
fun Category.key() = "category-$id"
|
||||
private val Category.key get() = "category-$id"
|
||||
|
|
|
@ -6,8 +6,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
|||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Delete
|
||||
import androidx.compose.material.icons.outlined.DragHandle
|
||||
import androidx.compose.material.icons.outlined.Edit
|
||||
import androidx.compose.material.icons.rounded.DragHandle
|
||||
import androidx.compose.material3.ElevatedCard
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
|
@ -36,21 +36,24 @@ fun ReorderableCollectionItemScope.CategoryListItem(
|
|||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onRename() }
|
||||
.padding(MaterialTheme.padding.small),
|
||||
.padding(vertical = MaterialTheme.padding.small)
|
||||
.padding(
|
||||
start = MaterialTheme.padding.small,
|
||||
end = MaterialTheme.padding.medium,
|
||||
),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
IconButton(
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.DragHandle,
|
||||
contentDescription = null,
|
||||
modifier = Modifier
|
||||
.padding(MaterialTheme.padding.medium)
|
||||
.draggableHandle(),
|
||||
onClick = {},
|
||||
) {
|
||||
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
|
||||
}
|
||||
)
|
||||
Text(
|
||||
text = category.name,
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.padding(start = MaterialTheme.padding.small),
|
||||
.weight(1f),
|
||||
)
|
||||
IconButton(onClick = onRename) {
|
||||
Icon(
|
||||
|
|
|
@ -43,7 +43,7 @@ class CategoryScreen : Screen() {
|
|||
onClickSortAlphabetically = { screenModel.showDialog(CategoryDialog.SortAlphabetically) },
|
||||
onClickRename = { screenModel.showDialog(CategoryDialog.Rename(it)) },
|
||||
onClickDelete = { screenModel.showDialog(CategoryDialog.Delete(it)) },
|
||||
moveTo = screenModel::moveTo,
|
||||
changeOrder = screenModel::changeOrder,
|
||||
navigateUp = navigator::pop,
|
||||
)
|
||||
|
||||
|
|
|
@ -74,9 +74,9 @@ class CategoryScreenModel(
|
|||
}
|
||||
}
|
||||
|
||||
fun moveTo(category: Category, offset: Int) {
|
||||
fun changeOrder(category: Category, newOrder: Int) {
|
||||
screenModelScope.launch {
|
||||
when (reorderCategory.await(category, offset)) {
|
||||
when (reorderCategory.changeOrder(category, newOrder)) {
|
||||
is ReorderCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError)
|
||||
else -> {}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ class ReorderCategory(
|
|||
|
||||
private val mutex = Mutex()
|
||||
|
||||
suspend fun await(category: Category, offset: Int) = withNonCancellableContext {
|
||||
suspend fun changeOrder(category: Category, newOrder: Int) = withNonCancellableContext {
|
||||
mutex.withLock {
|
||||
val categories = categoryRepository.getAll()
|
||||
.filterNot(Category::isSystemCategory)
|
||||
|
@ -26,7 +26,7 @@ class ReorderCategory(
|
|||
return@withNonCancellableContext Result.Unchanged
|
||||
}
|
||||
|
||||
val newPosition = currentIndex + offset
|
||||
val newPosition = currentIndex + newOrder
|
||||
|
||||
try {
|
||||
categories.add(newPosition, categories.removeAt(currentIndex))
|
||||
|
|
Loading…
Reference in a new issue