From 32e0c17e08c9af98c799c575fec4048e9dffe831 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Tue, 19 Mar 2024 22:35:12 +0100 Subject: [PATCH] Improve state hoisting in FilterBottomSheet --- .../app/compose/timelime/FilterBottomSheet.kt | 20 ++++++------------- .../app/compose/timelime/TimelineTab.kt | 13 +++++++++++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/appcompose/src/main/java/com/readrops/app/compose/timelime/FilterBottomSheet.kt b/appcompose/src/main/java/com/readrops/app/compose/timelime/FilterBottomSheet.kt index 62a6378c..d7717a9b 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/timelime/FilterBottomSheet.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/timelime/FilterBottomSheet.kt @@ -24,7 +24,8 @@ import com.readrops.db.queries.QueryFilters @OptIn(ExperimentalMaterial3Api::class) @Composable fun FilterBottomSheet( - viewModel: TimelineScreenModel, + onSetShowReadItemsState: () -> Unit, + onSetSortTypeState: () -> Unit, filters: QueryFilters, onDismiss: () -> Unit, ) { @@ -44,11 +45,11 @@ fun FilterBottomSheet( verticalAlignment = Alignment.CenterVertically, modifier = Modifier .fillMaxWidth() - .clickable { viewModel.setShowReadItemsState(!filters.showReadItems) } + .clickable(onClick = onSetShowReadItemsState) ) { Checkbox( checked = filters.showReadItems, - onCheckedChange = { viewModel.setShowReadItemsState(!filters.showReadItems) } + onCheckedChange = { onSetShowReadItemsState() } ) ShortSpacer() @@ -60,24 +61,15 @@ fun FilterBottomSheet( ShortSpacer() - fun setSortTypeState() { - viewModel.setSortTypeState( - if (filters.sortType == ListSortType.NEWEST_TO_OLDEST) - ListSortType.OLDEST_TO_NEWEST - else - ListSortType.NEWEST_TO_OLDEST - ) - } - Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier .fillMaxWidth() - .clickable { setSortTypeState() } + .clickable(onClick = onSetSortTypeState) ) { Checkbox( checked = filters.sortType == ListSortType.OLDEST_TO_NEWEST, - onCheckedChange = { setSortTypeState() } + onCheckedChange = { onSetSortTypeState() } ) ShortSpacer() diff --git a/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt b/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt index c6dfede7..fe4c2dbc 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/timelime/TimelineTab.kt @@ -50,6 +50,7 @@ import com.readrops.app.compose.util.components.CenteredProgressIndicator import com.readrops.app.compose.util.components.Placeholder import com.readrops.app.compose.util.components.TwoChoicesDialog import com.readrops.app.compose.util.theme.spacing +import com.readrops.db.filters.ListSortType import com.readrops.db.filters.MainFilter import com.readrops.db.filters.SubFilter @@ -137,8 +138,18 @@ object TimelineTab : Tab { DialogState.FilterSheet -> { FilterBottomSheet( - viewModel = viewModel, filters = state.filters, + onSetShowReadItemsState = { + viewModel.setShowReadItemsState(!state.filters.showReadItems) + }, + onSetSortTypeState = { + viewModel.setSortTypeState( + if (state.filters.sortType == ListSortType.NEWEST_TO_OLDEST) + ListSortType.OLDEST_TO_NEWEST + else + ListSortType.NEWEST_TO_OLDEST + ) + }, onDismiss = { viewModel.closeDialog() }