diff --git a/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenu.kt b/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenu.kt index 08ecf3d3..7bd2d502 100644 --- a/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenu.kt +++ b/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenu.kt @@ -12,6 +12,8 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed @@ -23,6 +25,7 @@ import androidx.compose.material.IconButton import androidx.compose.material.LocalContentAlpha import androidx.compose.material.LocalContentColor import androidx.compose.material.MaterialTheme +import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add @@ -42,53 +45,61 @@ import androidx.compose.ui.unit.dp import ca.gosyer.ui.base.vm.viewModel import ca.gosyer.util.compose.ThemedWindow -fun openCategoriesMenu() { +fun openCategoriesMenu(notifyFinished: (() -> Unit)? = null) { val windowEvents = WindowEvents() ThemedWindow("TachideskJUI - Categories", events = windowEvents) { - CategoriesMenu(windowEvents) + CategoriesMenu(notifyFinished, windowEvents) } } @Composable -fun CategoriesMenu(windowEvents: WindowEvents) { +fun CategoriesMenu(notifyFinished: (() -> Unit)? = null, windowEvents: WindowEvents) { val vm = viewModel() val categories by vm.categories.collectAsState() remember { - windowEvents.onClose = { vm.updateCategories() } + windowEvents.onClose = { + vm.updateCategories() + notifyFinished?.invoke() + } } - Box { - LazyColumn(modifier = Modifier.fillMaxSize()) { - itemsIndexed(categories) { i, category -> - CategoryRow( - category = category, - moveUpEnabled = i != 0, - moveDownEnabled = i != categories.lastIndex, - onMoveUp = { vm.moveUp(category) }, - onMoveDown = { vm.moveDown(category) }, - onRename = { - openRenameDialog(category) { - vm.renameCategory(category, it) - } - }, - onDelete = { - openDeleteDialog(category) { - vm.deleteCategory(category) - } - }, - ) - } - } - ExtendedFloatingActionButton( - text = { Text(text = "Add") }, - icon = { Icon(imageVector = Icons.Default.Add, contentDescription = null) }, - modifier = Modifier.align(Alignment.BottomEnd).padding(16.dp), - onClick = { - openCreateDialog { - vm.createCategory(it) + Surface { + Box { + LazyColumn(modifier = Modifier.fillMaxSize()) { + itemsIndexed(categories) { i, category -> + CategoryRow( + category = category, + moveUpEnabled = i != 0, + moveDownEnabled = i != categories.lastIndex, + onMoveUp = { vm.moveUp(category) }, + onMoveDown = { vm.moveDown(category) }, + onRename = { + openRenameDialog(category) { + vm.renameCategory(category, it) + } + }, + onDelete = { + openDeleteDialog(category) { + vm.deleteCategory(category) + } + }, + ) + } + item { + Spacer(Modifier.height(80.dp).fillMaxWidth()) } } - ) + ExtendedFloatingActionButton( + text = { Text(text = "Add") }, + icon = { Icon(imageVector = Icons.Default.Add, contentDescription = null) }, + modifier = Modifier.align(Alignment.BottomEnd).padding(16.dp), + onClick = { + openCreateDialog { + vm.createCategory(it) + } + } + ) + } } } diff --git a/src/main/kotlin/ca/gosyer/ui/settings/SettingsLibraryScreen.kt b/src/main/kotlin/ca/gosyer/ui/settings/SettingsLibraryScreen.kt index 78d654de..a5974718 100644 --- a/src/main/kotlin/ca/gosyer/ui/settings/SettingsLibraryScreen.kt +++ b/src/main/kotlin/ca/gosyer/ui/settings/SettingsLibraryScreen.kt @@ -9,20 +9,40 @@ package ca.gosyer.ui.settings import androidx.compose.foundation.layout.Column import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import ca.gosyer.data.library.LibraryPreferences +import ca.gosyer.data.server.interactions.CategoryInteractionHandler import ca.gosyer.ui.base.components.Toolbar +import ca.gosyer.ui.base.prefs.PreferenceRow import ca.gosyer.ui.base.prefs.SwitchPreference import ca.gosyer.ui.base.vm.ViewModel import ca.gosyer.ui.base.vm.viewModel +import ca.gosyer.ui.categories.openCategoriesMenu import ca.gosyer.ui.main.Route import com.github.zsoltk.compose.router.BackStack +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch import javax.inject.Inject class SettingsLibraryViewModel @Inject constructor( - libraryPreferences: LibraryPreferences + libraryPreferences: LibraryPreferences, + private val categoryHandler: CategoryInteractionHandler ) : ViewModel() { val showAllCategory = libraryPreferences.showAllCategory().asStateFlow() + private val _categories = MutableStateFlow(0) + val categories = _categories.asStateFlow() + + init { + refreshCategoryCount() + } + + fun refreshCategoryCount() { + scope.launch { + _categories.value = categoryHandler.getCategories().size + } + } } @Composable @@ -35,6 +55,13 @@ fun SettingsLibraryScreen(navController: BackStack) { item { SwitchPreference(preference = vm.showAllCategory, title = "Show all category") } + item { + PreferenceRow( + "Categories", + onClick = { openCategoriesMenu(vm::refreshCategoryCount) }, + subtitle = vm.categories.collectAsState().value.toString() + ) + } } } }