From d46785a99760c06b9fd31a61c594b4934d75ea45 Mon Sep 17 00:00:00 2001 From: christianrowlands Date: Tue, 29 Oct 2024 07:35:36 -0400 Subject: [PATCH] WIP: Adds back in support for GNSS filtering in the UI --- .../networksurvey/ui/main/AlertDialogs.kt | 80 +++++++++++++++++++ .../networksurvey/ui/main/HomeScreen.kt | 56 +++++++++++-- 2 files changed, 131 insertions(+), 5 deletions(-) diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AlertDialogs.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AlertDialogs.kt index 868b12af..22d04053 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AlertDialogs.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/AlertDialogs.kt @@ -1,13 +1,27 @@ package com.craxiom.networksurvey.ui.main +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.width import androidx.compose.material3.AlertDialog import androidx.compose.material3.Checkbox import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.unit.dp +import com.craxiom.networksurvey.Application +import com.craxiom.networksurvey.R +import com.craxiom.networksurvey.model.GnssType +import com.craxiom.networksurvey.util.PreferenceUtils @Composable fun GnssFailureDialog(onDismiss: () -> Unit, onConfirm: (Boolean) -> Unit) { @@ -32,3 +46,69 @@ fun GnssFailureDialog(onDismiss: () -> Unit, onConfirm: (Boolean) -> Unit) { } ) } + +@Composable +fun GnssFilterDialog( + initialItems: Array, + initialChecks: BooleanArray, + onDismissRequest: () -> Unit, + onSave: () -> Unit +) { + val context = LocalContext.current + + val selectedChecks = remember { mutableStateListOf(*initialChecks.toTypedArray()) } + + + AlertDialog( + onDismissRequest = onDismissRequest, + title = { Text(text = context.getString(R.string.filter_dialog_title)) }, + text = { + Column { + initialItems.forEachIndexed { index, item -> + Row( + modifier = Modifier + .fillMaxWidth() + .clickable { + // Toggle the checkbox state when the row is clicked + selectedChecks[index] = !selectedChecks[index] + }, + verticalAlignment = Alignment.CenterVertically + ) { + Checkbox( + checked = selectedChecks[index], + onCheckedChange = { isChecked -> + selectedChecks[index] = isChecked + } + ) + Spacer(modifier = Modifier.width(8.dp)) + Text(text = item) + } + } + } + }, + confirmButton = { + TextButton( + onClick = { + // Save selections to SharedPreferences using PreferenceUtils + val selectedGnssTypes = GnssType.entries.toTypedArray() + .filterIndexed { index, _ -> selectedChecks[index] } + .toSet() + PreferenceUtils.saveGnssFilter( + Application.get(), + selectedGnssTypes, + Application.getPrefs() + ) + onSave() + } + ) { + Text(text = context.getString(R.string.save)) + } + }, + dismissButton = { + TextButton(onClick = onDismissRequest) { + Text(text = context.getString(android.R.string.cancel)) + } + } + ) +} + diff --git a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/HomeScreen.kt b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/HomeScreen.kt index e3640089..d06e7e85 100644 --- a/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/HomeScreen.kt +++ b/networksurvey/src/main/java/com/craxiom/networksurvey/ui/main/HomeScreen.kt @@ -15,6 +15,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.viewinterop.AndroidViewBinding import androidx.navigation.NavController @@ -23,6 +24,7 @@ import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController +import com.craxiom.networksurvey.Application import com.craxiom.networksurvey.R import com.craxiom.networksurvey.databinding.ContainerBluetoothFragmentBinding import com.craxiom.networksurvey.databinding.ContainerCellularFragmentBinding @@ -34,8 +36,11 @@ import com.craxiom.networksurvey.fragments.DashboardFragment import com.craxiom.networksurvey.fragments.MainCellularFragment import com.craxiom.networksurvey.fragments.MainGnssFragment import com.craxiom.networksurvey.fragments.WifiNetworksFragment +import com.craxiom.networksurvey.model.GnssType import com.craxiom.networksurvey.ui.main.appbar.AppBar import com.craxiom.networksurvey.ui.main.appbar.AppBarAction +import com.craxiom.networksurvey.util.LibUIUtils +import com.craxiom.networksurvey.util.PreferenceUtils @Composable fun HomeScreen( @@ -44,13 +49,17 @@ fun HomeScreen( ) { val bottomNavController: NavHostController = rememberNavController() var currentScreen by remember { mutableStateOf(MainScreens.Dashboard) } + var showGnssFilterDialog by remember { mutableStateOf(false) } Scaffold( topBar = { AppBar( drawerState = drawerState, title = getAppBarTitle(currentScreen), - appBarActions = getAppBarActions(currentScreen, mainNavController) + appBarActions = getAppBarActions( + currentScreen, + mainNavController, + showGnssFilterDialog = { showGnssFilterDialog = it }) ) }, bottomBar = { @@ -84,6 +93,13 @@ fun HomeScreen( } } } + + if (showGnssFilterDialog) { + ShowSatsFilterDialog( + onDismissRequest = { showGnssFilterDialog = false }, + onSave = { showGnssFilterDialog = false } + ) + } } @Composable @@ -137,7 +153,11 @@ fun getAppBarTitle(currentScreen: MainScreens): Int { } @Composable -fun getAppBarActions(currentScreen: MainScreens, navController: NavController): List { +fun getAppBarActions( + currentScreen: MainScreens, + navController: NavController, + showGnssFilterDialog: (Boolean) -> Unit +): List { return when (currentScreen) { MainScreens.Cellular -> listOf( AppBarAction( @@ -170,9 +190,7 @@ fun getAppBarActions(currentScreen: MainScreens, navController: NavController): AppBarAction( icon = R.drawable.ic_filter, description = R.string.menu_option_filter_content_description, - onClick = { - // TODO Finish me - } + onClick = { showGnssFilterDialog(true) } ) ) @@ -180,6 +198,34 @@ fun getAppBarActions(currentScreen: MainScreens, navController: NavController): } } +@Composable +fun ShowSatsFilterDialog( + onDismissRequest: () -> Unit, + onSave: () -> Unit +) { + val context = LocalContext.current + val gnssTypes = GnssType.values() + val len = gnssTypes.size + + // Retrieve the current filter from SharedPreferences + val filter = PreferenceUtils.gnssFilter(context, Application.getPrefs()) + + val items = Array(len) { index -> + LibUIUtils.getGnssDisplayName(context, gnssTypes[index]) + } + val checks = BooleanArray(len) { index -> + filter.contains(gnssTypes[index]) + } + + // Display the GnssFilterDialog with the prepared items and initial checks + GnssFilterDialog( + initialItems = items, + initialChecks = checks, + onDismissRequest = onDismissRequest, + onSave = onSave + ) +} + sealed class MainScreens(val route: String) { data object Dashboard : MainScreens("dashboard_route") data object Cellular : MainScreens("cellular_route")