Jetpack Compose Navigation Deeplink handling for Files

Viewed 527

I've been playing with Compose Navigation for a while and stumbled across this use case:

When a user clicks on a File in his FileManager which my App can open, i want to jump to the specific screen where the user can view this file:

I have a working solution but I'm not sure if this is the best way...

My Intent Filter looks like this:

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:scheme="content" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\\.custom-file-extension" />
            </intent-filter>

In the NavGraph I handle the deeplink like this:

        composable(
            route = NavigationDestination.BackupRestore,
            deepLinks = listOf(
                navDeepLink {
                    uriPattern = "content://"
                    mimeType = "*/*"
                    action = Intent.ACTION_VIEW
                }
            )
        ) { backstackEntry ->
            val intent = (backstackEntry.arguments?.get(NavController.KEY_DEEP_LINK_INTENT) as? Intent)
            val uri = if (intent?.action != null) intent.data else null
            val viewModel = hiltViewModel<BackupRestoreViewModel>()
            BackupRestoreScreen(
                viewModel = viewModel,
                onBack = navController::popBackStack,
                data = uri
            )
        }

Is it really necessary to use backstackEntry.arguments?.get(NavController.KEY_DEEP_LINK_INTENT) to get the Intent first, then check if the action is not null and after that read the data to make sure the user is coming from a deeplink?

Or Is it possible to read the File Uri directly from the arguments? If yes, how can i achieve this?

Would be really nice if anyone can help me find a decent solution

0 Answers
Related