cleanup code

This commit is contained in:
Cuong-Tran 2024-11-07 23:46:44 +07:00
parent 82ece10ba0
commit a5cc1a2586
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2
5 changed files with 24 additions and 21 deletions

View file

@ -41,7 +41,7 @@ fun CategoryScreen(
onClickSortAlphabetically: () -> Unit, onClickSortAlphabetically: () -> Unit,
onClickRename: (Category) -> Unit, onClickRename: (Category) -> Unit,
onClickDelete: (Category) -> Unit, onClickDelete: (Category) -> Unit,
moveTo: (Category, Int) -> Unit, changeOrder: (Category, Int) -> Unit,
navigateUp: () -> Unit, navigateUp: () -> Unit,
) { ) {
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
@ -87,7 +87,7 @@ fun CategoryScreen(
PaddingValues(horizontal = MaterialTheme.padding.medium), PaddingValues(horizontal = MaterialTheme.padding.medium),
onClickRename = onClickRename, onClickRename = onClickRename,
onClickDelete = onClickDelete, onClickDelete = onClickDelete,
moveTo = moveTo, changeOrder = changeOrder,
) )
} }
} }
@ -99,12 +99,12 @@ private fun CategoryContent(
paddingValues: PaddingValues, paddingValues: PaddingValues,
onClickRename: (Category) -> Unit, onClickRename: (Category) -> Unit,
onClickDelete: (Category) -> Unit, onClickDelete: (Category) -> Unit,
moveTo: (Category, Int) -> Unit, changeOrder: (Category, Int) -> Unit,
) { ) {
var reorderableList by remember { mutableStateOf(categories.toList()) } var reorderableList by remember { mutableStateOf(categories.toList()) }
val reorderableLazyColumnState = rememberReorderableLazyListState(lazyListState) { from, to -> val reorderableLazyColumnState = rememberReorderableLazyListState(lazyListState) { from, to ->
reorderableList = reorderableList.toMutableList().apply { 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)) add(to.index, removeAt(from.index))
} }
} }
@ -122,9 +122,9 @@ private fun CategoryContent(
) { ) {
items( items(
items = reorderableList, items = reorderableList,
key = { category -> category.key() }, key = { category -> category.key },
) { category -> ) { category ->
ReorderableItem(reorderableLazyColumnState, category.key()) { ReorderableItem(reorderableLazyColumnState, category.key) {
CategoryListItem( CategoryListItem(
modifier = Modifier.animateItem(), modifier = Modifier.animateItem(),
category = category, category = category,
@ -136,4 +136,4 @@ private fun CategoryContent(
} }
} }
fun Category.key() = "category-$id" private val Category.key get() = "category-$id"

View file

@ -6,8 +6,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete 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.outlined.Edit
import androidx.compose.material.icons.rounded.DragHandle
import androidx.compose.material3.ElevatedCard import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton import androidx.compose.material3.IconButton
@ -36,21 +36,24 @@ fun ReorderableCollectionItemScope.CategoryListItem(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.clickable { onRename() } .clickable { onRename() }
.padding(MaterialTheme.padding.small), .padding(vertical = MaterialTheme.padding.small)
.padding(
start = MaterialTheme.padding.small,
end = MaterialTheme.padding.medium,
),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
) { ) {
IconButton( Icon(
imageVector = Icons.Outlined.DragHandle,
contentDescription = null,
modifier = Modifier modifier = Modifier
.padding(MaterialTheme.padding.medium)
.draggableHandle(), .draggableHandle(),
onClick = {}, )
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
Text( Text(
text = category.name, text = category.name,
modifier = Modifier modifier = Modifier
.weight(1f) .weight(1f),
.padding(start = MaterialTheme.padding.small),
) )
IconButton(onClick = onRename) { IconButton(onClick = onRename) {
Icon( Icon(

View file

@ -43,7 +43,7 @@ class CategoryScreen : Screen() {
onClickSortAlphabetically = { screenModel.showDialog(CategoryDialog.SortAlphabetically) }, onClickSortAlphabetically = { screenModel.showDialog(CategoryDialog.SortAlphabetically) },
onClickRename = { screenModel.showDialog(CategoryDialog.Rename(it)) }, onClickRename = { screenModel.showDialog(CategoryDialog.Rename(it)) },
onClickDelete = { screenModel.showDialog(CategoryDialog.Delete(it)) }, onClickDelete = { screenModel.showDialog(CategoryDialog.Delete(it)) },
moveTo = screenModel::moveTo, changeOrder = screenModel::changeOrder,
navigateUp = navigator::pop, navigateUp = navigator::pop,
) )

View file

@ -74,9 +74,9 @@ class CategoryScreenModel(
} }
} }
fun moveTo(category: Category, offset: Int) { fun changeOrder(category: Category, newOrder: Int) {
screenModelScope.launch { screenModelScope.launch {
when (reorderCategory.await(category, offset)) { when (reorderCategory.changeOrder(category, newOrder)) {
is ReorderCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError) is ReorderCategory.Result.InternalError -> _events.send(CategoryEvent.InternalError)
else -> {} else -> {}
} }

View file

@ -15,7 +15,7 @@ class ReorderCategory(
private val mutex = Mutex() private val mutex = Mutex()
suspend fun await(category: Category, offset: Int) = withNonCancellableContext { suspend fun changeOrder(category: Category, newOrder: Int) = withNonCancellableContext {
mutex.withLock { mutex.withLock {
val categories = categoryRepository.getAll() val categories = categoryRepository.getAll()
.filterNot(Category::isSystemCategory) .filterNot(Category::isSystemCategory)
@ -26,7 +26,7 @@ class ReorderCategory(
return@withNonCancellableContext Result.Unchanged return@withNonCancellableContext Result.Unchanged
} }
val newPosition = currentIndex + offset val newPosition = currentIndex + newOrder
try { try {
categories.add(newPosition, categories.removeAt(currentIndex)) categories.add(newPosition, categories.removeAt(currentIndex))