-
Notifications
You must be signed in to change notification settings - Fork 69
**DO NOT MERGE** Add deeplink recipe for single module #97
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
claraf3
wants to merge
4
commits into
android:main
Choose a base branch
from
claraf3:basic-deeplink-new-design
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
189 changes: 189 additions & 0 deletions
189
app/src/main/java/com/example/nav3recipes/deeplink/basic/CreateDeepLinkActivity.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,189 @@ | ||
| package com.example.nav3recipes.deeplink.basic | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateMapOf | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import com.example.nav3recipes.deeplink.basic.ui.DeepLinkButton | ||
| import com.example.nav3recipes.deeplink.basic.ui.EMPTY | ||
| import com.example.nav3recipes.deeplink.basic.ui.EntryScreen | ||
| import com.example.nav3recipes.deeplink.basic.ui.FIRST_NAME_JOHN | ||
| import com.example.nav3recipes.deeplink.basic.ui.FIRST_NAME_JULIE | ||
| import com.example.nav3recipes.deeplink.basic.ui.FIRST_NAME_MARY | ||
| import com.example.nav3recipes.deeplink.basic.ui.FIRST_NAME_TOM | ||
| import com.example.nav3recipes.deeplink.basic.ui.LOCATION_BC | ||
| import com.example.nav3recipes.deeplink.basic.ui.LOCATION_BR | ||
| import com.example.nav3recipes.deeplink.basic.ui.LOCATION_CA | ||
| import com.example.nav3recipes.deeplink.basic.ui.LOCATION_US | ||
| import com.example.nav3recipes.deeplink.basic.ui.MenuDropDown | ||
| import com.example.nav3recipes.deeplink.basic.ui.MenuTextInput | ||
| import com.example.nav3recipes.deeplink.basic.ui.PATH_BASE | ||
| import com.example.nav3recipes.deeplink.basic.ui.PATH_INCLUDE | ||
| import com.example.nav3recipes.deeplink.basic.ui.PATH_SEARCH | ||
| import com.example.nav3recipes.deeplink.basic.ui.STRING_LITERAL_HOME | ||
| import com.example.nav3recipes.deeplink.basic.ui.SearchKey | ||
| import com.example.nav3recipes.deeplink.basic.ui.TextContent | ||
| import com.example.nav3recipes.deeplink.basic.ui.HomeKey | ||
| import com.example.nav3recipes.deeplink.basic.ui.UsersKey | ||
|
|
||
| /** | ||
| * This activity allows the user to create a deep link and make a request with it. | ||
| * | ||
| * **HOW THIS RECIPE WORKS** it consists of two activities - [CreateDeepLinkActivity] to construct | ||
| * and trigger the deeplink request, and the [MainActivity] to show how an app can handle | ||
| * that request. | ||
| * | ||
| * **DEMONSTRATED FORMS OF DEEPLINK** The [MainActivity] has a several backStack keys to | ||
| * demonstrate different types of supported deeplinks: | ||
| * 1. [HomeKey] - deeplink with an exact url (no deeplink arguments) | ||
| * 2. [UsersKey] - deeplink with path arguments | ||
| * 3. [SearchKey] - deeplink with query arguments | ||
| * See [MainActivity.deepLinkPatterns] for the actual url pattern of each. | ||
| * | ||
| * **RECIPE STRUCTURE** This recipe consists of three main packages: | ||
| * 1. basic.deeplink - Contains the two activities | ||
| * 2. basic.deeplink.ui - Contains the activity UI code, i.e. Screens, global string variables etc | ||
| * 3. basic.deeplink.deeplinkutil - Contains the classes and helper methods to parse and match | ||
| * the deeplinks | ||
| * | ||
| * See [MainActivity] for how the requested deeplink is handled. | ||
| */ | ||
| class CreateDeepLinkActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| setContent { | ||
| /** | ||
| * UI for deeplink sandbox | ||
| */ | ||
| EntryScreen("Sandbox - Build Your Deeplink") { | ||
| TextContent("Base url:\n${PATH_BASE}/") | ||
| var showFilterOptions by remember { mutableStateOf(false) } | ||
| val selectedPath = remember { mutableStateOf(MENU_OPTIONS_PATH[KEY_PATH]?.first()) } | ||
claraf3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var showQueryOptions by remember { mutableStateOf(false) } | ||
| var selectedFilter by remember { mutableStateOf("") } | ||
| val selectedSearchQuery = remember { mutableStateMapOf<String, String>() } | ||
|
|
||
| // manage path options | ||
| MenuDropDown( | ||
| menuOptions = MENU_OPTIONS_PATH, | ||
| ) { _, selection -> | ||
| selectedPath.value = selection | ||
| when (selection) { | ||
| PATH_SEARCH -> { | ||
| showQueryOptions = true | ||
| showFilterOptions = false | ||
| } | ||
|
|
||
| PATH_INCLUDE -> { | ||
| showQueryOptions = false | ||
| showFilterOptions = true | ||
| } | ||
|
|
||
| else -> { | ||
| showQueryOptions = false | ||
| showFilterOptions = false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // manage path filter options, reset state if menu is closed | ||
| LaunchedEffect(showFilterOptions) { | ||
| selectedFilter = if (showFilterOptions) { | ||
| MENU_OPTIONS_FILTER.values.first().first() | ||
| } else { | ||
| "" | ||
| } | ||
| } | ||
| if (showFilterOptions) { | ||
| MenuDropDown( | ||
| menuOptions = MENU_OPTIONS_FILTER, | ||
| ) { _, selected -> | ||
| selectedFilter = selected | ||
| } | ||
| } | ||
|
|
||
| // manage query options, reset state if menu is closed | ||
| LaunchedEffect(showQueryOptions) { | ||
| if (showQueryOptions) { | ||
| val initEntry = MENU_OPTIONS_SEARCH.entries.first() | ||
| selectedSearchQuery[initEntry.key] = initEntry.value.first() | ||
| } else { | ||
| selectedSearchQuery.clear() | ||
| } | ||
| } | ||
| if (showQueryOptions) { | ||
| MenuTextInput( | ||
| menuLabels = MENU_LABELS_SEARCH, | ||
| ) { label, selected -> | ||
| selectedSearchQuery[label] = selected | ||
| } | ||
| MenuDropDown( | ||
| menuOptions = MENU_OPTIONS_SEARCH, | ||
| ) { label, selected -> | ||
| selectedSearchQuery[label] = selected | ||
| } | ||
| } | ||
|
|
||
| // form final deeplink url | ||
| val arguments = when (selectedPath.value) { | ||
| PATH_INCLUDE -> "/${selectedFilter}" | ||
| PATH_SEARCH -> { | ||
| buildString { | ||
| selectedSearchQuery.forEach { entry -> | ||
| if (entry.value.isNotEmpty()) { | ||
| val prefix = if (isEmpty()) "?" else "&" | ||
| append("$prefix${entry.key}=${entry.value}") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| else -> "" | ||
| } | ||
| val finalUrl = "${PATH_BASE}/${selectedPath.value}$arguments" | ||
| TextContent("Final url:\n$finalUrl") | ||
| // deeplink to target | ||
| DeepLinkButton( | ||
| context = this@CreateDeepLinkActivity, | ||
| targetActivity = MainActivity::class.java, | ||
| deepLinkUrl = finalUrl | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private const val KEY_PATH = "path" | ||
| private val MENU_OPTIONS_PATH = mapOf( | ||
| KEY_PATH to listOf( | ||
| STRING_LITERAL_HOME, | ||
| PATH_INCLUDE, | ||
| PATH_SEARCH, | ||
| ), | ||
| ) | ||
|
|
||
| private val MENU_OPTIONS_FILTER = mapOf( | ||
| UsersKey.FILTER_KEY to listOf(UsersKey.FILTER_OPTION_RECENTLY_ADDED, UsersKey.FILTER_OPTION_ALL), | ||
| ) | ||
|
|
||
| private val MENU_OPTIONS_SEARCH = mapOf( | ||
| SearchKey::firstName.name to listOf( | ||
| EMPTY, | ||
| FIRST_NAME_JOHN, | ||
| FIRST_NAME_TOM, | ||
| FIRST_NAME_MARY, | ||
| FIRST_NAME_JULIE | ||
| ), | ||
| SearchKey::location.name to listOf(EMPTY, LOCATION_CA, LOCATION_BC, LOCATION_BR, LOCATION_US) | ||
| ) | ||
|
|
||
| private val MENU_LABELS_SEARCH = listOf(SearchKey::ageMin.name, SearchKey::ageMax.name) | ||
|
|
||
|
|
||
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.
This comment block will likely be the landing page from the README. It'd be good to have a more detailed description of:
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.
Added docs in
CreateDeepLinkActivity.ktstarting line#35