diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt index d337e1c..ee92acc 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt @@ -44,6 +44,7 @@ import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem +import com.zaneschepke.wireguardautotunnel.ui.common.navigation.isCurrentRoute import com.zaneschepke.wireguardautotunnel.ui.common.prompt.CustomSnackBar import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen @@ -115,9 +116,10 @@ class MainActivity : AppCompatActivity() { Modifier .focusable() .focusProperties { - when (navBackStackEntry?.toRoute()) { - is Screens.Lock -> Unit - else -> up = focusRequester + if (navBackStackEntry?.isCurrentRoute(Route.Lock) == true) { + Unit + } else { + up = focusRequester } }, bottomBar = { @@ -126,17 +128,17 @@ class MainActivity : AppCompatActivity() { listOf( BottomNavItem( name = stringResource(R.string.tunnels), - route = Screens.Main, + route = Route.Main, icon = Icons.Rounded.Home, ), BottomNavItem( name = stringResource(R.string.settings), - route = Screens.Settings, + route = Route.Settings, icon = Icons.Rounded.Settings, ), BottomNavItem( name = stringResource(R.string.support), - route = Screens.Support, + route = Route.Support, icon = Icons.Rounded.QuestionMark, ), ), @@ -148,16 +150,16 @@ class MainActivity : AppCompatActivity() { navController, enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) }, exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) }, - startDestination = (if (isPinLockEnabled == true) Screens.Lock else Screens.Main), + startDestination = (if (isPinLockEnabled == true) Route.Lock else Route.Main), ) { - composable { + composable { MainScreen( focusRequester = focusRequester, uiState = appUiState, navController = navController, ) } - composable { + composable { SettingsScreen( appViewModel = appViewModel, uiState = appUiState, @@ -165,25 +167,25 @@ class MainActivity : AppCompatActivity() { focusRequester = focusRequester, ) } - composable { + composable { SupportScreen( focusRequester = focusRequester, navController = navController, appUiState = appUiState, ) } - composable { + composable { LogsScreen() } - composable { - val args = it.toRoute() + composable { + val args = it.toRoute() ConfigScreen( focusRequester = focusRequester, tunnelId = args.id, ) } - composable { - val args = it.toRoute() + composable { + val args = it.toRoute() OptionsScreen( navController = navController, tunnelId = args.id, @@ -191,7 +193,7 @@ class MainActivity : AppCompatActivity() { appUiState = appUiState, ) } - composable { + composable { PinLockScreen( navController = navController, appViewModel = appViewModel, diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/Screens.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/Route.kt similarity index 56% rename from app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/Screens.kt rename to app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/Route.kt index be56164..f570341 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/Screens.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/Route.kt @@ -2,29 +2,29 @@ package com.zaneschepke.wireguardautotunnel.ui import kotlinx.serialization.Serializable -sealed class Screens { +sealed class Route { @Serializable - data object Support : Screens() + data object Support : Route() @Serializable - data object Settings : Screens() + data object Settings : Route() @Serializable - data object Main : Screens() + data object Main : Route() @Serializable data class Option( val id: Int, - ) : Screens() + ) : Route() @Serializable - data object Lock : Screens() + data object Lock : Route() @Serializable data class Config( val id: Int, - ) : Screens() + ) : Route() @Serializable - data object Logs : Screens() + data object Logs : Route() } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavBar.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavBar.kt index 6ca5523..6f4798f 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavBar.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavBar.kt @@ -33,9 +33,7 @@ fun BottomNavBar(navController: NavController, bottomNavItems: List - val selected = navBackStackEntry?.destination?.hierarchy?.any { - it.hasRoute(route = item.route::class) - } == true + val selected = navBackStackEntry.isCurrentRoute(item.route) NavigationBarItem( selected = selected, onClick = { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavItem.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavItem.kt index a586b77..643633a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavItem.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/BottomNavItem.kt @@ -1,10 +1,10 @@ package com.zaneschepke.wireguardautotunnel.ui.common.navigation import androidx.compose.ui.graphics.vector.ImageVector -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route data class BottomNavItem( val name: String, - val route: Screens, + val route: Route, val icon: ImageVector, ) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/Extensions.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/Extensions.kt new file mode 100644 index 0000000..8cb9547 --- /dev/null +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/common/navigation/Extensions.kt @@ -0,0 +1,12 @@ +package com.zaneschepke.wireguardautotunnel.ui.common.navigation + +import androidx.navigation.NavBackStackEntry +import androidx.navigation.NavDestination.Companion.hasRoute +import androidx.navigation.NavDestination.Companion.hierarchy +import com.zaneschepke.wireguardautotunnel.ui.Route + +fun NavBackStackEntry?.isCurrentRoute(route: Route): Boolean { + return this?.destination?.hierarchy?.any { + it.hasRoute(route = route::class) + } == true +} diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/config/ConfigViewModel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/config/ConfigViewModel.kt index 4cbdee6..40a113f 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/config/ConfigViewModel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/config/ConfigViewModel.kt @@ -17,7 +17,7 @@ import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.module.IoDispatcher -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy import com.zaneschepke.wireguardautotunnel.util.Constants @@ -335,14 +335,15 @@ constructor( SnackbarController.showMessage( StringValue.StringResource(R.string.config_changes_saved), ) - navController.navigate(Screens.Main) + navController.navigate(Route.Main) }.onFailure { Timber.e(it) val message = it.message?.substringAfter(":", missingDelimiterValue = "") - val stringValue = - message?.let { - StringValue.DynamicString(message) - } ?: StringValue.StringResource(R.string.unknown_error) + val stringValue = if (message.isNullOrBlank()) { + StringValue.StringResource(R.string.unknown_error) + } else { + StringValue.DynamicString(message) + } SnackbarController.showMessage(stringValue) } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt index e6b4d88..29998e7 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt @@ -68,7 +68,7 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState import com.zaneschepke.wireguardautotunnel.ui.AppUiState -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.common.RowListItem import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult @@ -235,7 +235,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState, onQrClick = { launchQrScanner() }, onManualImportClick = { navController.navigate( - Screens.Config(Constants.MANUAL_TUNNEL_CONFIG_ID), + Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID), ) }, ) @@ -409,7 +409,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState, onClick = { selectedTunnel?.let { navController.navigate( - Screens.Option(it.id), + Route.Option(it.id), ) } }, @@ -456,7 +456,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState, selectedTunnel = tunnel selectedTunnel?.let { navController.navigate( - Screens.Option(it.id), + Route.Option(it.id), ) } }, diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/options/OptionsScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/options/OptionsScreen.kt index fc2caa7..3e05be4 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/options/OptionsScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/options/OptionsScreen.kt @@ -50,7 +50,7 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavController import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.ui.AppUiState -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle import com.zaneschepke.wireguardautotunnel.ui.common.config.SubmitConfigurationTextBox @@ -114,7 +114,7 @@ fun OptionsScreen( ) }, focusRequester, isVisible = true, onClick = { navController.navigate( - Screens.Config(config.id), + Route.Config(config.id), ) }) }, diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/pinlock/PinLockScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/pinlock/PinLockScreen.kt index f03dc82..d4e1d99 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/pinlock/PinLockScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/pinlock/PinLockScreen.kt @@ -8,7 +8,7 @@ import androidx.compose.ui.res.stringResource import androidx.navigation.NavController import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.ui.AppViewModel -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController import com.zaneschepke.wireguardautotunnel.util.StringValue import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv @@ -36,11 +36,11 @@ fun PinLockScreen(navController: NavController, appViewModel: AppViewModel) { onPinCorrect = { // pin is correct, navigate or hide pin lock if (context.isRunningOnTv()) { - navController.navigate(Screens.Main) + navController.navigate(Route.Main) } else { val isPopped = navController.popBackStack() if (!isPopped) { - navController.navigate(Screens.Main) + navController.navigate(Route.Main) } } }, diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/SettingsScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/SettingsScreen.kt index 9da776e..4593d2a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/SettingsScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/SettingsScreen.kt @@ -69,7 +69,7 @@ import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState import com.zaneschepke.wireguardautotunnel.ui.AppUiState import com.zaneschepke.wireguardautotunnel.ui.AppViewModel -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt @@ -581,7 +581,7 @@ fun SettingsScreen( } else { // TODO may want to show a dialog before proceeding in the future PinManager.initialize(WireGuardAutoTunnel.instance) - navController.navigate(Screens.Lock) + navController.navigate(Route.Lock) } }, ) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/support/SupportScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/support/SupportScreen.kt index 7fabf37..3b3377c 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/support/SupportScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/support/SupportScreen.kt @@ -46,7 +46,7 @@ import androidx.navigation.NavController import com.zaneschepke.wireguardautotunnel.BuildConfig import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.ui.AppUiState -import com.zaneschepke.wireguardautotunnel.ui.Screens +import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv import com.zaneschepke.wireguardautotunnel.util.extensions.launchSupportEmail import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl @@ -244,7 +244,7 @@ fun SupportScreen(navController: NavController, focusRequester: FocusRequester, color = MaterialTheme.colorScheme.onBackground, ) TextButton( - onClick = { navController.navigate(Screens.Logs) }, + onClick = { navController.navigate(Route.Logs) }, modifier = Modifier.padding(vertical = 5.dp), ) { Row(