Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open a stack of screens by a deeplink #1617

Merged
merged 2 commits into from
Oct 7, 2024
Merged

Open a stack of screens by a deeplink #1617

merged 2 commits into from
Oct 7, 2024

Conversation

terrakok
Copy link
Member

@terrakok terrakok commented Oct 4, 2024

The PR adds a common navController.navigate(uri: Uri) for a plain navigation by a deeplink.

navController.navigate(UriUtils.parse("https://www.example.com"))

Also the PR adds new non-android handleDeepLink(request: NavDeepLinkRequest): Boolean function which enables a stack build when navigating by a deep link.

On the android target it should be handled by a system Intent for now.
Androidx CL is on the way: https://android-review.googlesource.com/c/platform/frameworks/support/+/3293773

Screen.Recording.2024-10-04.at.15.36.38.mov
@Serializable data object Main
@Serializable data object User
@Serializable data class UserProfile(@SerialName("userId") val id: String = "0")
@Serializable data object Gallery
@Serializable data class Photo(@SerialName("photoId") val id: String = "0")

@Composable
internal fun App() = AppTheme {
    val navController = rememberNavController()
    Column(
        modifier = Modifier.fillMaxSize().windowInsetsPadding(WindowInsets.systemBars),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        val deeplink = "compose://app.io/user/42/photo?id=777"
        OutlinedButton(
            onClick = { navController.handleDeepLink(deeplink)}
        ) { Text(deeplink) }

        NavHost(navController, startDestination = Main) {
            composable<Main> { MainScreen() }
            navigation<User>(startDestination = UserProfile()) {
                composable<UserProfile> {
                    UserProfileScreen(it.toRoute<UserProfile>().id.toInt()) { navController.popBackStack() }
                }
                navigation<Gallery>(startDestination = Photo()) {
                    composable<Photo>(
                        deepLinks = listOf(
                            navDeepLink { uriPattern = "compose://app.io/user/{userId}/photo?id={photoId}" }
                        ),
                    ) {
                        PhotoScreen(it.toRoute<Photo>().id.toInt()) { navController.popBackStack() }
                    }
                }
            }
        }
    }
}

expect fun NavController.handleDeepLink(uri: String)
//android
actual fun NavController.handleDeepLink(uri: String) {
    handleDeepLink(
        Intent.parseUri(uri, Intent.URI_INTENT_SCHEME).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
    )
}
//non-android
actual fun NavController.handleDeepLink(uri: String) {
    handleDeepLink(NavDeepLinkRequest.Builder.fromUri(UriUtils.parse(uri)).build())
}

Fixes CMP-5003 Deep Linking Support - iOS

Testing

Unit tests and demo app

Release Notes

Features - Navigation

  • Commonize navController.navigate(Uri) method
  • Implemented non-android navController.handleDeepLink(NavDeepLinkRequest) method

@terrakok terrakok requested a review from MatkovIvan October 4, 2024 13:42
@terrakok terrakok force-pushed the k.tskh/deep-stack branch 2 times, most recently from 5c63741 to 14ca269 Compare October 5, 2024 11:47
…r better compatibility with android in the future and added navigation by uri methods.
Copy link
Member

@MatkovIvan MatkovIvan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change "Added new common" to "Commonize"/"Port from Android"/"Make available in common" in the release notes

@terrakok terrakok merged commit 87bbb6f into jb-main Oct 7, 2024
6 checks passed
@terrakok terrakok deleted the k.tskh/deep-stack branch October 7, 2024 08:29
@vanniktech
Copy link

@terrakok thanks for making so many improvements on the non-android side. With Android we can also get proper deep link support such when launching from outside of the app: https://developer.android.com/develop/ui/compose/navigation#deeplinks - for instance when clicking on a deeplink on a website. I noticed that this does not work with iOS yet using 1.7.3 which does not have this PR included. Is this expected to work with the 1.8.0-alpha1?

@JetBrains JetBrains locked and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants