Merge pull request #7189 from thundernest/use_deeplinks_in_navigation

Use deep links in navigation
This commit is contained in:
Wolf-Martell Montwé 2023-09-25 13:55:14 +00:00 committed by GitHub
commit cc39924087
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 17 deletions

View file

@ -0,0 +1,30 @@
package app.k9mail.core.ui.compose.common.navigation
import android.net.Uri
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.runtime.Composable
import androidx.core.net.toUri
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.navDeepLink
fun NavGraphBuilder.deepLinkComposable(
route: String,
arguments: List<NamedNavArgument> = emptyList(),
content: @Composable AnimatedContentScope.(NavBackStackEntry) -> Unit,
) {
composable(
route = route,
arguments = arguments,
deepLinks = listOf(
navDeepLink { uriPattern = route.toDeepLink() },
),
content = content,
)
}
fun String.toDeepLink(): String = "app://$this"
fun String.toDeepLinkUri(): Uri = toDeepLink().toUri()

View file

@ -3,7 +3,7 @@ package app.k9mail.feature.account.setup.navigation
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions import androidx.navigation.NavOptions
import androidx.navigation.compose.composable import app.k9mail.core.ui.compose.common.navigation.deepLinkComposable
import app.k9mail.feature.account.setup.ui.AccountSetupScreen import app.k9mail.feature.account.setup.ui.AccountSetupScreen
const val NAVIGATION_ROUTE_ACCOUNT_SETUP = "/account/setup" const val NAVIGATION_ROUTE_ACCOUNT_SETUP = "/account/setup"
@ -16,7 +16,7 @@ fun NavGraphBuilder.accountSetupRoute(
onBack: () -> Unit, onBack: () -> Unit,
onFinish: (String) -> Unit, onFinish: (String) -> Unit,
) { ) {
composable(route = NAVIGATION_ROUTE_ACCOUNT_SETUP) { deepLinkComposable(route = NAVIGATION_ROUTE_ACCOUNT_SETUP) {
AccountSetupScreen( AccountSetupScreen(
onBack = onBack, onBack = onBack,
onFinish = onFinish, onFinish = onFinish,

View file

@ -6,6 +6,7 @@ import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import app.k9mail.core.ui.compose.common.activity.setActivityContent import app.k9mail.core.ui.compose.common.activity.setActivityContent
import app.k9mail.core.ui.compose.common.navigation.toDeepLinkUri
import app.k9mail.feature.account.setup.navigation.NAVIGATION_ROUTE_ACCOUNT_SETUP import app.k9mail.feature.account.setup.navigation.NAVIGATION_ROUTE_ACCOUNT_SETUP
import app.k9mail.feature.launcher.ui.FeatureLauncherApp import app.k9mail.feature.launcher.ui.FeatureLauncherApp
import app.k9mail.feature.onboarding.navigation.NAVIGATION_ROUTE_ONBOARDING import app.k9mail.feature.onboarding.navigation.NAVIGATION_ROUTE_ONBOARDING
@ -17,22 +18,16 @@ class FeatureLauncherActivity : ComponentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
val destination = intent.getStringExtra(EXTRA_DESTINATION)
setActivityContent { setActivityContent {
FeatureLauncherApp(startDestination = destination) FeatureLauncherApp()
} }
} }
companion object { companion object {
private const val EXTRA_DESTINATION = "destination"
private const val DESTINATION_ONBOARDING = NAVIGATION_ROUTE_ONBOARDING
private const val DESTINATION_SETUP_ACCOUNT = NAVIGATION_ROUTE_ACCOUNT_SETUP
@JvmStatic @JvmStatic
fun launchOnboarding(context: Activity) { fun launchOnboarding(context: Activity) {
val intent = Intent(context, FeatureLauncherActivity::class.java).apply { val intent = Intent(context, FeatureLauncherActivity::class.java).apply {
putExtra(EXTRA_DESTINATION, DESTINATION_ONBOARDING) data = NAVIGATION_ROUTE_ONBOARDING.toDeepLinkUri()
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
} }
context.startActivity(intent) context.startActivity(intent)
@ -41,7 +36,7 @@ class FeatureLauncherActivity : ComponentActivity() {
@JvmStatic @JvmStatic
fun launchSetupAccount(context: Activity) { fun launchSetupAccount(context: Activity) {
val intent = Intent(context, FeatureLauncherActivity::class.java).apply { val intent = Intent(context, FeatureLauncherActivity::class.java).apply {
putExtra(EXTRA_DESTINATION, DESTINATION_SETUP_ACCOUNT) data = NAVIGATION_ROUTE_ACCOUNT_SETUP.toDeepLinkUri()
} }
context.startActivity(intent) context.startActivity(intent)
} }

View file

@ -15,7 +15,6 @@ import org.koin.compose.koinInject
@Composable @Composable
fun FeatureLauncherNavHost( fun FeatureLauncherNavHost(
navController: NavHostController, navController: NavHostController,
startDestination: String?,
onBack: () -> Unit, onBack: () -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
importSettingsLauncher: ImportSettingsLauncher = koinInject(), importSettingsLauncher: ImportSettingsLauncher = koinInject(),
@ -23,7 +22,7 @@ fun FeatureLauncherNavHost(
) { ) {
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = startDestination ?: NAVIGATION_ROUTE_ONBOARDING, startDestination = NAVIGATION_ROUTE_ONBOARDING,
modifier = modifier, modifier = modifier,
) { ) {
onboardingRoute( onboardingRoute(

View file

@ -12,7 +12,6 @@ import app.k9mail.feature.launcher.navigation.FeatureLauncherNavHost
@Composable @Composable
fun FeatureLauncherApp( fun FeatureLauncherApp(
startDestination: String?,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val navController = rememberNavController() val navController = rememberNavController()
@ -28,7 +27,6 @@ fun FeatureLauncherApp(
FeatureLauncherNavHost( FeatureLauncherNavHost(
navController = navController, navController = navController,
startDestination = startDestination,
onBack = { activity.finish() }, onBack = { activity.finish() },
) )
} }

View file

@ -3,7 +3,7 @@ package app.k9mail.feature.onboarding.navigation
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions import androidx.navigation.NavOptions
import androidx.navigation.compose.composable import app.k9mail.core.ui.compose.common.navigation.deepLinkComposable
import app.k9mail.feature.onboarding.ui.OnboardingScreen import app.k9mail.feature.onboarding.ui.OnboardingScreen
const val NAVIGATION_ROUTE_ONBOARDING = "/onboarding" const val NAVIGATION_ROUTE_ONBOARDING = "/onboarding"
@ -18,7 +18,7 @@ fun NavGraphBuilder.onboardingRoute(
onStart: () -> Unit, onStart: () -> Unit,
onImport: () -> Unit, onImport: () -> Unit,
) { ) {
composable(route = NAVIGATION_ROUTE_ONBOARDING) { deepLinkComposable(route = NAVIGATION_ROUTE_ONBOARDING) {
OnboardingScreen( OnboardingScreen(
onStartClick = onStart, onStartClick = onStart,
onImportClick = onImport, onImportClick = onImport,