Skip to content

Commit

Permalink
Creates a framework for having custom actions on the app bar for each…
Browse files Browse the repository at this point in the history
… bottom nav route
  • Loading branch information
christianrowlands committed Oct 25, 2024
1 parent 086b540 commit e39efbb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;

Expand Down Expand Up @@ -96,9 +94,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
{
binding = FragmentBluetoothListBinding.inflate(inflater);

final ViewModelStoreOwner viewModelStoreOwner = NavHostFragment.findNavController(this).getViewModelStoreOwner(R.id.nav_graph);
final ViewModelProvider viewModelProvider = new ViewModelProvider(viewModelStoreOwner);
viewModel = viewModelProvider.get(getClass().getName(), BluetoothViewModel.class);
viewModel = new ViewModelProvider(requireActivity()).get(BluetoothViewModel.class);

binding.setVm(viewModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.SortedList;
Expand Down Expand Up @@ -97,9 +95,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
{
binding = FragmentWifiNetworksListBinding.inflate(inflater);

final ViewModelStoreOwner viewModelStoreOwner = NavHostFragment.findNavController(this).getViewModelStoreOwner(R.id.nav_graph);
final ViewModelProvider viewModelProvider = new ViewModelProvider(viewModelStoreOwner);
viewModel = viewModelProvider.get(getClass().getName(), WifiViewModel.class);
viewModel = new ViewModelProvider(requireActivity()).get(WifiViewModel.class);

binding.setVm(viewModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,51 @@ import com.craxiom.networksurvey.fragments.MainCellularFragment
import com.craxiom.networksurvey.fragments.MainGnssFragment
import com.craxiom.networksurvey.fragments.WifiNetworksFragment
import com.craxiom.networksurvey.ui.main.appbar.AppBar
import com.craxiom.networksurvey.ui.main.appbar.AppBarAction
import com.craxiom.networksurvey.ui.main.appdrawer.NavOption

@Composable
fun HomeScreen(
drawerState: DrawerState
drawerState: DrawerState,
mainNavController: NavHostController
) {
val navController: NavHostController = rememberNavController()
val bottomNavController: NavHostController = rememberNavController()
var currentScreen by remember { mutableStateOf<MainScreens>(MainScreens.Dashboard) }

Scaffold(
topBar = { AppBar(drawerState = drawerState) },
topBar = {
AppBar(
drawerState = drawerState,
appBarActions = getAppBarActions(currentScreen, mainNavController)
)
},
bottomBar = {
BottomNavigationBar(navController)
BottomNavigationBar(bottomNavController)
},
) { padding ->
NavHost(
navController,
bottomNavController,
startDestination = MainScreens.Dashboard.route,
modifier = Modifier.padding(paddingValues = padding)
) {
composable(MainScreens.Dashboard.route) {
currentScreen = MainScreens.Dashboard
DashboardFragmentInCompose()
}
composable(MainScreens.Cellular.route) {
currentScreen = MainScreens.Cellular
CellularFragmentInCompose()
}
composable(MainScreens.Wifi.route) {
currentScreen = MainScreens.Wifi
WifiFragmentInCompose()
}
composable(MainScreens.Bluetooth.route) {
currentScreen = MainScreens.Bluetooth
BluetoothFragmentInCompose()
}
composable(MainScreens.Gnss.route) {
currentScreen = MainScreens.Gnss
GnssFragmentInCompose()
}
}
Expand Down Expand Up @@ -108,6 +122,50 @@ fun BottomNavigationBar(navController: NavController) {
}
}

@Composable
fun getAppBarActions(currentScreen: MainScreens, navController: NavController): List<AppBarAction> {
return when (currentScreen) {
MainScreens.Cellular -> listOf(
AppBarAction(
icon = android.R.drawable.ic_dialog_map,
description = R.string.open_tower_map,
onClick = {
navController.navigate(NavOption.TowerMap.name)
}
)
)

MainScreens.Wifi -> listOf(
AppBarAction(
icon = R.drawable.ic_spectrum_chart,
description = R.string.open_wifi_spectrum,
onClick = {
// TODO Navigate to WiFi Spectrum Fragment
}
)
)

MainScreens.Gnss -> listOf(
AppBarAction(
icon = R.drawable.ic_sort,
description = R.string.menu_option_sort_by,
onClick = {
// TODO Finish me
}
),
AppBarAction(
icon = R.drawable.ic_filter,
description = R.string.menu_option_filter_content_description,
onClick = {
// TODO Finish me
}
)
)

else -> emptyList()
}
}

sealed class MainScreens(val route: String) {
data object Dashboard : MainScreens("dashboard_route")
data object Cellular : MainScreens("cellular_route")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.craxiom.networksurvey.util.NsTheme

@Composable
fun MainCompose(
navController: NavHostController = rememberNavController(),
mainNavController: NavHostController = rememberNavController(),
drawerState: DrawerState = rememberDrawerState(initialValue = DrawerValue.Closed),
appVersion: String
) {
Expand All @@ -41,7 +41,7 @@ fun MainCompose(
LaunchedEffect(viewModel.navigateToQrCodeScanner) {
viewModel.navigateToQrCodeScanner.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.QrCodeScanner.name)
mainNavController.navigate(NavOption.QrCodeScanner.name)
viewModel.resetNavigationFlag()
}
}
Expand All @@ -50,7 +50,7 @@ fun MainCompose(
LaunchedEffect(viewModel.navigateToQrCodeShare) {
viewModel.navigateToQrCodeShare.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.QrCodeShare.name)
mainNavController.navigate(NavOption.QrCodeShare.name)
viewModel.resetNavigationFlag()
}
}
Expand All @@ -59,7 +59,7 @@ fun MainCompose(
LaunchedEffect(viewModel.navigateToTowerMap) {
viewModel.navigateToTowerMap.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.TowerMap.name)
mainNavController.navigate(NavOption.TowerMap.name)
viewModel.resetNavigationFlag()
}
}
Expand All @@ -68,7 +68,7 @@ fun MainCompose(
LaunchedEffect(viewModel.navigateToWifiSpectrum) {
viewModel.navigateToWifiSpectrum.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.WifiSpectrum.name)
mainNavController.navigate(NavOption.WifiSpectrum.name)
viewModel.resetNavigationFlag()
}
}
Expand All @@ -77,7 +77,7 @@ fun MainCompose(
LaunchedEffect(viewModel.navigateToWifiDetails) {
viewModel.navigateToWifiDetails.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.WifiDetails.name)
mainNavController.navigate(NavOption.WifiDetails.name)
viewModel.resetNavigationFlag()
}
}
Expand All @@ -86,7 +86,7 @@ fun MainCompose(
LaunchedEffect(viewModel.navigateToBluetooth) {
viewModel.navigateToBluetooth.observe(lifecycleOwner) { shouldNavigate ->
if (shouldNavigate) {
navController.navigate(NavOption.BluetoothDetails.name)
mainNavController.navigate(NavOption.BluetoothDetails.name)
viewModel.resetNavigationFlag()
}
}
Expand All @@ -106,31 +106,31 @@ fun MainCompose(
) { onUserPickedOption ->
when (onUserPickedOption) {
NavDrawerOption.None -> {
navController.navigate(onUserPickedOption.name) {
mainNavController.navigate(onUserPickedOption.name) {
popUpTo(NavDrawerOption.None.name)
}
}

NavDrawerOption.ServerConnection -> {
navController.navigate(onUserPickedOption.name) {
mainNavController.navigate(onUserPickedOption.name) {
popUpTo(NavDrawerOption.None.name)
}
}

NavDrawerOption.MqttBrokerConnection -> {
navController.navigate(onUserPickedOption.name) {
mainNavController.navigate(onUserPickedOption.name) {
popUpTo(NavDrawerOption.None.name)
}
}

NavDrawerOption.CellularCalculators -> {
navController.navigate(onUserPickedOption.name) {
mainNavController.navigate(onUserPickedOption.name) {
popUpTo(NavDrawerOption.None.name)
}
}

NavDrawerOption.Settings -> {
navController.navigate(onUserPickedOption.name) {
mainNavController.navigate(onUserPickedOption.name) {
popUpTo(NavDrawerOption.None.name)
}
}
Expand Down Expand Up @@ -171,10 +171,13 @@ fun MainCompose(
}
) {
NavHost(
navController,
mainNavController,
startDestination = NavRoutes.MainRoute.name
) {
mainGraph(drawerState, paddingValues = paddingValues)
mainGraph(
drawerState, paddingValues = paddingValues,
mainNavController = mainNavController
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidViewBinding
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
Expand All @@ -31,17 +32,19 @@ import com.craxiom.networksurvey.ui.main.NavRoutes

fun NavGraphBuilder.mainGraph(
drawerState: DrawerState,
paddingValues: PaddingValues
paddingValues: PaddingValues,
mainNavController: NavHostController
) {
navigation(startDestination = NavDrawerOption.None.name, route = NavRoutes.MainRoute.name) {
// TODO Need to add a header like the old display for all of these
composable(NavDrawerOption.None.name) {
HomeScreen(drawerState)
HomeScreen(drawerState, mainNavController = mainNavController)
}

composable(NavDrawerOption.ServerConnection.name) {
GrpcFragmentInCompose(paddingValues)
}

composable(
route = "${NavDrawerOption.MqttBrokerConnection.name}?${MqttConnectionSettings.KEY}={mqttConnectionSettings}",
arguments = listOf(navArgument("mqttConnectionSettings") {
Expand Down
3 changes: 2 additions & 1 deletion networksurvey/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,9 @@ but work independently, so you have full control over how you handle your data.<
<string name="channel">Channel</string>
<string name="ssid">SSID\n</string>

<string name="open_tower_map">Tower Map</string>
<string name="open_tower_map">Open Tower Map</string>
<string name="open_tower_map_info">Tower Map Info</string>
<string name="mqtt_connection_settings">MQTT Connection Settings</string>
<string name="qr_code">QR Code</string>
<string name="open_wifi_spectrum">Open Wi-Fi Spectrum View</string>
</resources>

0 comments on commit e39efbb

Please sign in to comment.