-
Notifications
You must be signed in to change notification settings - Fork 89
Implement advanced deep link recipe #167
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| plugins { | ||
| alias(libs.plugins.android.application) | ||
| alias(libs.plugins.kotlin.android) | ||
| alias(libs.plugins.kotlin.compose) | ||
| alias(libs.plugins.kotlin.serialization) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.nav3recipes.deeplink.advanced" | ||
| compileSdk = 36 | ||
|
|
||
| defaultConfig { | ||
| applicationId = "com.example.nav3recipes.deeplink.advanced" | ||
| minSdk = 24 | ||
| targetSdk = 36 | ||
| versionCode = 1 | ||
| versionName = "1.0" | ||
|
|
||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| isMinifyEnabled = false | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro" | ||
| ) | ||
| } | ||
| } | ||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_11 | ||
| targetCompatibility = JavaVersion.VERSION_11 | ||
| } | ||
| kotlinOptions { | ||
| jvmTarget = "11" | ||
| } | ||
| buildFeatures { | ||
| compose = true | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| implementation(libs.androidx.core.ktx) | ||
| implementation(libs.androidx.lifecycle.runtime.ktx) | ||
| implementation(libs.androidx.activity.compose) | ||
| implementation(platform(libs.androidx.compose.bom)) | ||
| implementation(libs.androidx.ui) | ||
| implementation(libs.androidx.ui.graphics) | ||
| implementation(libs.androidx.ui.tooling.preview) | ||
| implementation(libs.androidx.material3) | ||
|
|
||
| implementation(libs.kotlinx.serialization.core) | ||
| implementation(libs.kotlinx.serialization.json) | ||
| implementation(libs.androidx.navigation3.runtime) | ||
| implementation(libs.androidx.navigation3.ui) | ||
| implementation(project(":common")) | ||
|
|
||
| testImplementation(libs.junit) | ||
| androidTestImplementation(libs.androidx.junit) | ||
| androidTestImplementation(libs.androidx.espresso.core) | ||
| androidTestImplementation(platform(libs.androidx.compose.bom)) | ||
| androidTestImplementation(libs.androidx.ui.test.junit4) | ||
| debugImplementation(libs.androidx.ui.tooling) | ||
| debugImplementation(libs.androidx.ui.test.manifest) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Add project specific ProGuard rules here. | ||
| # You can control the set of applied configuration files using the | ||
| # proguardFiles setting in build.gradle. | ||
| # | ||
| # For more details, see | ||
| # http://developer.android.com/guide/developing/tools/proguard.html | ||
|
|
||
| # If your project uses WebView with JS, uncomment the following | ||
| # and specify the fully qualified class name to the JavaScript interface | ||
| # class: | ||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| # public *; | ||
| #} | ||
|
|
||
| # Uncomment this to preserve the line number information for | ||
| # debugging stack traces. | ||
| #-keepattributes SourceFile,LineNumberTable | ||
|
|
||
| # If you keep the line number information, uncomment this to | ||
| # hide the original source file name. | ||
| #-renamesourcefileattribute SourceFile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
|
||
| <application | ||
| android:allowBackup="true" | ||
| android:icon="@mipmap/ic_launcher" | ||
| android:label="@string/app_name" | ||
| android:roundIcon="@mipmap/ic_launcher_round" | ||
| android:supportsRtl="true" | ||
| android:theme="@style/Theme.Nav3Recipes" | ||
| android:enableOnBackInvokedCallback="true"> | ||
| <activity | ||
| android:name="AdvancedDeeplinkAppActivity" | ||
| android:exported="true" | ||
| android:label="@string/app_name" | ||
| android:launchMode="singleTop" | ||
| android:theme="@style/Theme.Nav3Recipes"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| <intent-filter android:autoVerify="true"> | ||
| <action android:name="android.intent.action.VIEW" /> | ||
| <category android:name="android.intent.category.DEFAULT" /> | ||
| <category android:name="android.intent.category.BROWSABLE" /> | ||
| <data android:scheme="https" | ||
| android:host="www.nav3deeplink.com"/> | ||
| </intent-filter> | ||
| </activity> | ||
| </application> | ||
|
|
||
| </manifest> |
111 changes: 111 additions & 0 deletions
111
...pp/src/main/java/com/example/nav3recipes/deeplink/advanced/AdvancedDeeplinkAppActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package com.example.nav3recipes.deeplink.advanced | ||
|
|
||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Scaffold | ||
| import androidx.compose.material3.TopAppBar | ||
| import androidx.compose.material3.TopAppBarDefaults | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.navigation3.runtime.NavBackStack | ||
| import androidx.navigation3.runtime.NavKey | ||
| import androidx.navigation3.runtime.entryProvider | ||
| import androidx.navigation3.runtime.rememberNavBackStack | ||
| import androidx.navigation3.ui.NavDisplay | ||
| import com.example.nav3recipes.deeplink.advanced.util.buildBackStack | ||
| import com.example.nav3recipes.deeplink.advanced.util.navigateUp | ||
| import com.example.nav3recipes.deeplink.advanced.util.toKey | ||
| import com.example.nav3recipes.deeplink.common.EntryScreen | ||
| import com.example.nav3recipes.deeplink.common.FriendsList | ||
| import com.example.nav3recipes.deeplink.common.LIST_USERS | ||
| import com.example.nav3recipes.deeplink.common.PaddedButton | ||
|
|
||
| class AdvancedDeeplinkAppActivity: ComponentActivity() { | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| val startKey = intent.data.toKey() | ||
|
|
||
| val flags = intent.flags | ||
| val isNewTask = flags and Intent.FLAG_ACTIVITY_NEW_TASK != 0 && | ||
| flags and Intent.FLAG_ACTIVITY_CLEAR_TASK != 0 | ||
|
|
||
| val syntheticBackStack = buildBackStack( | ||
| startKey = startKey, | ||
| buildFullPath = isNewTask | ||
| ) | ||
| setContent { | ||
| val backStack: NavBackStack<NavKey> = rememberNavBackStack(*(syntheticBackStack.toTypedArray())) | ||
|
|
||
| Scaffold( | ||
| topBar = { | ||
| // top app bar to display up button | ||
| TopAppBar( | ||
| colors = TopAppBarDefaults.topAppBarColors( | ||
| containerColor = MaterialTheme.colorScheme.primary, | ||
| titleContentColor = MaterialTheme.colorScheme.primary, | ||
| ), | ||
| title = { stringResource(R.string.app_name)}, | ||
| navigationIcon = { | ||
| /** | ||
| * Up button should never exit your app. Do not display it | ||
| * on the root Screen. | ||
| */ | ||
| if (backStack.last() != Home) { | ||
| IconButton(onClick = { | ||
| backStack.navigateUp( | ||
| this@AdvancedDeeplinkAppActivity, | ||
| this@AdvancedDeeplinkAppActivity | ||
| ) | ||
| }) { | ||
| Icon( | ||
| painter = painterResource(id = R.drawable.outline_arrow_upward_24), | ||
| contentDescription = "Up Button", | ||
| ) | ||
| } | ||
| } | ||
| }, | ||
| ) | ||
| }, | ||
| ) { innerPadding -> | ||
| NavDisplay( | ||
| backStack = backStack, | ||
| onBack = { backStack.removeLastOrNull()}, | ||
| modifier = Modifier.padding(innerPadding), | ||
| entryProvider = entryProvider { | ||
| entry<Home> { key -> | ||
| EntryScreen(key.screenTitle) { | ||
| PaddedButton("See Users") { | ||
| backStack.add(Users) | ||
| } | ||
| } | ||
| } | ||
| entry<Users> { key -> | ||
| EntryScreen(key.screenTitle) { | ||
| FriendsList(LIST_USERS) { user -> | ||
| backStack.add(UserDetail(user)) | ||
| } | ||
| } | ||
| } | ||
| entry<UserDetail> { result -> | ||
| EntryScreen(result.screenTitle) { | ||
| FriendsList(listOf(result.user)) | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
advanceddeeplinkapp/src/main/java/com/example/nav3recipes/deeplink/advanced/NavRecipeKey.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.example.nav3recipes.deeplink.advanced | ||
|
|
||
| import androidx.navigation3.runtime.NavKey | ||
| import com.example.nav3recipes.deeplink.common.User | ||
| import kotlinx.serialization.Serializable | ||
| import androidx.navigation3.runtime.NavBackStack | ||
| import com.example.nav3recipes.deeplink.advanced.util.navigateUp | ||
|
|
||
| internal const val PATH_BASE = "https://www.nav3deeplink.com" | ||
|
|
||
| /** | ||
| * Defines the NavKey used for this app. | ||
| * | ||
| * The keys are defined with this inheritance structure: | ||
| * [NavDeepLinkRecipeKey] extends [NavRecipeKey] extends [NavKey]. | ||
| * | ||
| * [NavKey] - the base Navigation 3 interface that can be used with [NavBackStack] | ||
| * | ||
| * [NavRecipeKey] - a sub-interface to supports member variables and functions | ||
| * specific to this app | ||
| * | ||
| * [NavDeepLinkRecipeKey] - a sub-interface to ensure that all keys | ||
| * that support deeplinking (or all keys that have children keys that can be deeplinked into) | ||
| * implement these two fields: | ||
| * 1. parent - the hierarchical parent of this key, required for building a synthetic backStack | ||
| * 2. deeplinkUrl - the deeplink url associated with this key, required for supporting the | ||
| * Up button (see [navigateUp] for more on this). | ||
| */ | ||
|
|
||
| internal interface NavRecipeKey: NavKey { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be better named |
||
| val screenTitle: String | ||
| } | ||
|
|
||
| internal interface NavDeepLinkRecipeKey: NavRecipeKey { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better as |
||
| val parent: NavKey | ||
| val deeplinkUrl: String | ||
| } | ||
|
|
||
| @Serializable | ||
| object Home: NavRecipeKey { | ||
| override val screenTitle: String = "Home" | ||
| } | ||
|
|
||
| @Serializable | ||
| object Users: NavDeepLinkRecipeKey { | ||
| override val screenTitle: String = "Users" | ||
| override val parent: NavKey = Home | ||
| override val deeplinkUrl: String | ||
| get() = "$PATH_BASE/$DEEPLINK_URL_TAG_USERS" | ||
| } | ||
|
|
||
| @Serializable | ||
| internal data class UserDetail( | ||
| val user: User | ||
| ): NavDeepLinkRecipeKey { | ||
| override val screenTitle: String = "User" | ||
| override val parent: NavKey = Users | ||
| override val deeplinkUrl: String | ||
| get() = "$PATH_BASE/$DEEPLINK_URL_TAG_USER/${user.firstName}/${user.location}" | ||
| } | ||
|
|
||
| internal const val DEEPLINK_URL_TAG_USER = "user" | ||
| internal const val DEEPLINK_URL_TAG_USERS = "users" | ||
11 changes: 11 additions & 0 deletions
11
...nceddeeplinkapp/src/main/java/com/example/nav3recipes/deeplink/advanced/ui/theme/Color.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.example.nav3recipes.deeplink.advanced.ui.theme | ||
|
|
||
| import androidx.compose.ui.graphics.Color | ||
|
|
||
| val Purple80 = Color(0xFFD0BCFF) | ||
| val PurpleGrey80 = Color(0xFFCCC2DC) | ||
| val Pink80 = Color(0xFFEFB8C8) | ||
|
|
||
| val Purple40 = Color(0xFF6650a4) | ||
| val PurpleGrey40 = Color(0xFF625b71) | ||
| val Pink40 = Color(0xFF7D5260) |
58 changes: 58 additions & 0 deletions
58
...nceddeeplinkapp/src/main/java/com/example/nav3recipes/deeplink/advanced/ui/theme/Theme.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.example.nav3recipes.deeplink.advanced.ui.theme | ||
|
|
||
| import android.app.Activity | ||
| import android.os.Build | ||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.darkColorScheme | ||
| import androidx.compose.material3.dynamicDarkColorScheme | ||
| import androidx.compose.material3.dynamicLightColorScheme | ||
| import androidx.compose.material3.lightColorScheme | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.platform.LocalContext | ||
|
|
||
| private val DarkColorScheme = darkColorScheme( | ||
| primary = Purple80, | ||
| secondary = PurpleGrey80, | ||
| tertiary = Pink80 | ||
| ) | ||
|
|
||
| private val LightColorScheme = lightColorScheme( | ||
| primary = Purple40, | ||
| secondary = PurpleGrey40, | ||
| tertiary = Pink40 | ||
|
|
||
| /* Other default colors to override | ||
| background = Color(0xFFFFFBFE), | ||
| surface = Color(0xFFFFFBFE), | ||
| onPrimary = Color.White, | ||
| onSecondary = Color.White, | ||
| onTertiary = Color.White, | ||
| onBackground = Color(0xFF1C1B1F), | ||
| onSurface = Color(0xFF1C1B1F), | ||
| */ | ||
| ) | ||
|
|
||
| @Composable | ||
| fun Nav3RecipesTheme( | ||
| darkTheme: Boolean = isSystemInDarkTheme(), | ||
| // Dynamic color is available on Android 12+ | ||
| dynamicColor: Boolean = true, | ||
| content: @Composable () -> Unit | ||
| ) { | ||
| val colorScheme = when { | ||
| dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { | ||
| val context = LocalContext.current | ||
| if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) | ||
| } | ||
|
|
||
| darkTheme -> DarkColorScheme | ||
| else -> LightColorScheme | ||
| } | ||
|
|
||
| MaterialTheme( | ||
| colorScheme = colorScheme, | ||
| typography = Typography, | ||
| content = content | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If at all possible, I would like to avoid creating separate modules in the recipes repo. Currently each recipe has its own activity - is it not possible to demonstrate the deep linking concepts by having separate activities for each deep link app?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've explored how to represent different Task stacks without a second app module. Separate activities is not enough - we need to show the difference between an app's Activity in another App's Task, and the an app's Activity in its own Task. We do need a second app for this :(
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you deep link to another Activity within the same module, the Activity would still be started in the original Task even if you flagged it with
Intent.FLAG_ACTIVITY_NEW_TASKThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK fair enough, thanks for clarifying.