Skip to content

Commit

Permalink
WIP: Adds back in support for GNSS filtering in the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrowlands committed Oct 29, 2024
1 parent c93634f commit d46785a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -32,3 +46,69 @@ fun GnssFailureDialog(onDismiss: () -> Unit, onConfirm: (Boolean) -> Unit) {
}
)
}

@Composable
fun GnssFilterDialog(
initialItems: Array<String>,
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))
}
}
)
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -44,13 +49,17 @@ fun HomeScreen(
) {
val bottomNavController: NavHostController = rememberNavController()
var currentScreen by remember { mutableStateOf<MainScreens>(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 = {
Expand Down Expand Up @@ -84,6 +93,13 @@ fun HomeScreen(
}
}
}

if (showGnssFilterDialog) {
ShowSatsFilterDialog(
onDismissRequest = { showGnssFilterDialog = false },
onSave = { showGnssFilterDialog = false }
)
}
}

@Composable
Expand Down Expand Up @@ -137,7 +153,11 @@ fun getAppBarTitle(currentScreen: MainScreens): Int {
}

@Composable
fun getAppBarActions(currentScreen: MainScreens, navController: NavController): List<AppBarAction> {
fun getAppBarActions(
currentScreen: MainScreens,
navController: NavController,
showGnssFilterDialog: (Boolean) -> Unit
): List<AppBarAction> {
return when (currentScreen) {
MainScreens.Cellular -> listOf(
AppBarAction(
Expand Down Expand Up @@ -170,16 +190,42 @@ 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) }
)
)

else -> emptyList()
}
}

@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")
Expand Down

0 comments on commit d46785a

Please sign in to comment.