Skip to content

Commit 8fbb807

Browse files
authored
Merge pull request #578 from ooni/nav-type-safety
Migrate to navigation type safety
2 parents 084623c + 02ed041 commit 8fbb807

File tree

7 files changed

+133
-288
lines changed

7 files changed

+133
-288
lines changed

composeApp/src/commonMain/kotlin/org/ooni/probe/App.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.compose.runtime.getValue
1616
import androidx.compose.runtime.remember
1717
import androidx.compose.ui.Modifier
1818
import androidx.compose.ui.platform.LocalLayoutDirection
19+
import androidx.navigation.NavDestination.Companion.hasRoute
1920
import androidx.navigation.compose.currentBackStackEntryAsState
2021
import androidx.navigation.compose.rememberNavController
2122
import co.touchlab.kermit.Logger
@@ -43,13 +44,15 @@ fun App(
4344
val snackbarHostState = remember { SnackbarHostState() }
4445

4546
val currentNavEntry by navController.currentBackStackEntryAsState()
46-
val currentRoute = currentNavEntry?.destination?.route
47-
val isMainScreen = MAIN_NAVIGATION_SCREENS.map { it.route }.contains(currentRoute)
47+
val isMainScreen = MAIN_NAVIGATION_SCREENS.any {
48+
currentNavEntry?.destination?.hasRoute(it::class) == true
49+
}
4850

4951
CompositionLocalProvider(
50-
values = dependencies.localeDirection?.invoke()?.let { LocalLayoutDirection provides it }?.let {
51-
arrayOf(LocalSnackbarHostState provides snackbarHostState, it)
52-
} ?: arrayOf(LocalSnackbarHostState provides snackbarHostState),
52+
values = dependencies.localeDirection?.invoke()?.let { LocalLayoutDirection provides it }
53+
?.let {
54+
arrayOf(LocalSnackbarHostState provides snackbarHostState, it)
55+
} ?: arrayOf(LocalSnackbarHostState provides snackbarHostState),
5356
) {
5457
AppTheme {
5558
Surface(
@@ -113,17 +116,22 @@ fun App(
113116
LaunchedEffect(deepLink) {
114117
when (deepLink) {
115118
is DeepLink.AddDescriptor -> {
116-
navController.navigate("add-descriptor/${deepLink.id}")
119+
navController.navigate(
120+
Screen.AddDescriptor(deepLink.id.toLongOrNull() ?: return@LaunchedEffect),
121+
)
117122
onDeeplinkHandled()
118123
}
124+
119125
is DeepLink.RunUrls -> {
120-
navController.navigate(Screen.ChooseWebsites(deepLink.url).route)
126+
navController.navigate(Screen.ChooseWebsites(deepLink.url))
121127
onDeeplinkHandled()
122128
}
129+
123130
DeepLink.Error -> {
124131
snackbarHostState.showSnackbar(getString(Res.string.AddDescriptor_Toasts_Unsupported_Url))
125132
onDeeplinkHandled()
126133
}
134+
127135
null -> Unit
128136
}
129137
}

composeApp/src/commonMain/kotlin/org/ooni/probe/shared/StringExt.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

composeApp/src/commonMain/kotlin/org/ooni/probe/ui/navigation/BottomNavigationBar.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.compose.ui.platform.testTag
1414
import androidx.compose.ui.text.style.TextAlign
1515
import androidx.compose.ui.unit.dp
1616
import androidx.navigation.NavController
17+
import androidx.navigation.NavDestination.Companion.hasRoute
1718
import androidx.navigation.compose.currentBackStackEntryAsState
1819
import ooniprobe.composeapp.generated.resources.Dashboard_Tab_Label
1920
import ooniprobe.composeapp.generated.resources.Res
@@ -30,7 +31,6 @@ import org.ooni.probe.ui.shared.isHeightCompact
3031
@Composable
3132
fun BottomNavigationBar(navController: NavController) {
3233
val entry by navController.currentBackStackEntryAsState()
33-
val currentRoute = entry?.destination?.route ?: return
3434

3535
val customMinHeightModifier =
3636
Modifier.run { if (isHeightCompact()) defaultMinSize(minHeight = 64.dp) else this }
@@ -52,13 +52,14 @@ fun BottomNavigationBar(navController: NavController) {
5252
textAlign = TextAlign.Center,
5353
)
5454
},
55-
selected = currentRoute == screen.route,
55+
selected = entry?.destination?.hasRoute(screen::class) == true,
5656
onClick = { navController.navigateToMainScreen(screen) },
5757
colors = NavigationBarItemDefaults.colors(
5858
indicatorColor = MaterialTheme.colorScheme.primaryContainer,
5959
selectedIconColor = MaterialTheme.colorScheme.onPrimaryContainer,
6060
),
61-
modifier = customMinHeightModifier.testTag(screen.route),
61+
modifier = customMinHeightModifier
62+
.testTag(screen::class.simpleName.orEmpty().lowercase()),
6263
)
6364
}
6465
}

0 commit comments

Comments
 (0)