From 9d024a0c6966a78fd70f75e14c2b71e1859ff8fd Mon Sep 17 00:00:00 2001 From: mezhendosina Date: Thu, 8 Jun 2023 18:32:24 +0500 Subject: [PATCH] =?UTF-8?q?Bottom=20Sheet'=D1=8B=20=D1=82=D0=B5=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D1=8C=20=D0=B6=D0=B8=D0=B2=D1=83=D1=82=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D0=B0=D1=85,=20=D1=87=D1=82=D0=BE=D0=B1?= =?UTF-8?q?=D1=8B=20Sensors.kt=20=D0=B1=D1=8B=D0=BB=20=D1=87=D0=B8=D1=82?= =?UTF-8?q?=D0=B0=D0=B1=D0=B5=D0=BB=D1=8C=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bottomSheets/FilterSensorsBottomSheet.kt | 53 ++++++++++ .../ui/bottomSheets/SortSensorsBottomSheet.kt | 97 +++++++++++++++++++ .../java/ru/nm17/narodmon/ui/pages/Sensors.kt | 82 ++-------------- 3 files changed, 160 insertions(+), 72 deletions(-) create mode 100644 app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/FilterSensorsBottomSheet.kt create mode 100644 app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/SortSensorsBottomSheet.kt diff --git a/app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/FilterSensorsBottomSheet.kt b/app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/FilterSensorsBottomSheet.kt new file mode 100644 index 0000000..92283bd --- /dev/null +++ b/app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/FilterSensorsBottomSheet.kt @@ -0,0 +1,53 @@ +package ru.nm17.narodmon.ui.bottomSheets + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ModalBottomSheet +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import ru.nm17.narodmon.R +import ru.nm17.narodmon.ui.pages.FilterCheckbox +import ru.nm17.narodmon.ui.pages.SensorFilter + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun FilterSensorsBottomSheet( + filterItems: List, + onDismissRequest: () -> Unit +) { + ModalBottomSheet(onDismissRequest = { onDismissRequest.invoke() }) { + Row( + horizontalArrangement = Arrangement.Center, + modifier = Modifier.fillMaxWidth(), + ) { + Text( + text = stringResource(R.string.sensors_filter_title), + fontSize = 24.sp, + fontWeight = FontWeight(500), + ) + } + + LazyColumn( + modifier = Modifier + .padding(horizontal = 4.dp) + .fillMaxWidth(), + ) { + items(filterItems) { + FilterCheckbox( + checked = it.enabled.value, + stringRes = it.stringRes, + ) { it.enabled.value = !it.enabled.value } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/SortSensorsBottomSheet.kt b/app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/SortSensorsBottomSheet.kt new file mode 100644 index 0000000..8287226 --- /dev/null +++ b/app/src/main/java/ru/nm17/narodmon/ui/bottomSheets/SortSensorsBottomSheet.kt @@ -0,0 +1,97 @@ +package ru.nm17.narodmon.ui.bottomSheets + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Button +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ModalBottomSheet +import androidx.compose.material3.SheetState +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import ru.nm17.narodmon.R +import ru.nm17.narodmon.ui.entities.SensorSortingUiEntity +import ru.nm17.narodmon.ui.entities.SortingTypes +import ru.nm17.narodmon.ui.pages.FilterRadioButton +import ru.nm17.narodmon.ui.theme.NarodMonTheme + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun SortSensorsBottomSheet( + onApply: (sortingType: SortingTypes) -> Unit, + onDismissRequest: () -> Unit +) { + var sortingType by remember { mutableStateOf(SortingTypes.DISTANCE) } + + val sortingTypes = remember { + listOf( + SensorSortingUiEntity(R.string.sort_by_name, SortingTypes.NAME), + SensorSortingUiEntity(R.string.sort_by_name_desc, SortingTypes.NAME_DESC), + SensorSortingUiEntity(R.string.sort_by_distance, SortingTypes.DISTANCE), + SensorSortingUiEntity(R.string.sort_by_distance_desc, SortingTypes.DISTANCE_DESC), + SensorSortingUiEntity(R.string.sort_by_type, SortingTypes.TYPE), + SensorSortingUiEntity(R.string.sort_by_type_desc, SortingTypes.TYPE_DESC), + SensorSortingUiEntity(R.string.sort_update_time, SortingTypes.UPD_TIME), + ) + } + + ModalBottomSheet( + onDismissRequest = { onDismissRequest.invoke() }, + sheetState = SheetState(true) + ) { + Row( + horizontalArrangement = Arrangement.Center, + modifier = Modifier.fillMaxWidth(), + ) { + Text( + text = stringResource(R.string.sensors_sort_title), + fontSize = 24.sp, + fontWeight = FontWeight(500), + ) + } + + LazyColumn( + modifier = Modifier + .padding(horizontal = 4.dp) + .fillMaxWidth(), + ) { + items(sortingTypes) { + FilterRadioButton( + selected = (sortingType == it.sortingType), + onClick = { sortingType = it.sortingType }, + stringRes = it.stringRes, + ) + } + } + Button( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 64.dp, vertical = 8.dp), + onClick = { + onApply.invoke(sortingType) + }) { + Text(text = stringResource(R.string.apply)) + } + } +} + +@Preview +@Composable +fun PreviewSortBottomSheet() { + NarodMonTheme { + SortSensorsBottomSheet(onApply = {}) {} + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/nm17/narodmon/ui/pages/Sensors.kt b/app/src/main/java/ru/nm17/narodmon/ui/pages/Sensors.kt index 566f394..aaac7dc 100644 --- a/app/src/main/java/ru/nm17/narodmon/ui/pages/Sensors.kt +++ b/app/src/main/java/ru/nm17/narodmon/ui/pages/Sensors.kt @@ -220,81 +220,19 @@ fun SensorsPage(navController: NavController) { } if (filterShow) { - ModalBottomSheet(onDismissRequest = { filterShow = false }) { - Row( - horizontalArrangement = Arrangement.Center, - modifier = Modifier.fillMaxWidth(), - ) { - Text( - text = stringResource(R.string.sensors_filter_title), - fontSize = 24.sp, - fontWeight = FontWeight(500), - ) - } - - LazyColumn( - modifier = Modifier - .padding(horizontal = 4.dp) - .fillMaxWidth(), - ) { - items(filterItems) { - FilterCheckbox( - checked = it.enabled.value, - stringRes = it.stringRes, - ) { it.enabled.value = !it.enabled.value } - } - } - } + FilterSensorsBottomSheet( + filterItems, + onDismissRequest = { filterShow = false } + ) } if (sortingShow) { - ModalBottomSheet(onDismissRequest = { sortingShow = false }) { - Row( - horizontalArrangement = Arrangement.Center, - modifier = Modifier.fillMaxWidth(), - ) { - Text( - text = stringResource(R.string.sensors_sort_title), - fontSize = 24.sp, - fontWeight = FontWeight(500), - ) - } - - LazyColumn( - modifier = Modifier - .padding(horizontal = 4.dp) - .fillMaxWidth(), - ) { - items(sortingTypes) { - FilterRadioButton( - selected = (sortingType == it.sortingType), - onClick = { sortingType = it.sortingType }, - stringRes = it.stringRes, - ) - } - - item { - FilterCheckbox( - checked = sortingDesc, - stringRes = R.string.sort_option_desc, - ) { sortingDesc = !sortingDesc } - } - - item { - FilterCheckbox( - checked = sortingFav, - stringRes = R.string.sort_option_fav, - ) { sortingFav = !sortingFav } - } - - item { - FilterCheckbox( - checked = sortingMine, - stringRes = R.string.sort_option_mine, - ) { sortingMine = !sortingMine } - } - } - } + SortSensorsBottomSheet( + onApply = { s -> + sortingType = s + sortingShow = false + }, + onDismissRequest = { sortingShow = false }) } }