Repository navigation
feat: add deep links to every screen #1119
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 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
54fce34
feat: add deep links to every screen
guzino ec09077
refactor: drop explanatory comment
guzino 1862da0
feat: add deep links to bottom sheet screens
guzino a31add7
chore: rename changelog fragment
guzino 2846c61
Merge remote-tracking branch 'origin/master' into feat/deep-link-screens
guzino 511250c
fix: deny sheet routes that commit flow state
guzino 09239fb
fix: gate cold-start links and deny external routes
guzino ad4ec80
fix: deny late transfer routes and dismiss sheet on root link
guzino 1ff7743
test: cover screen intent detachment at navhost boundary
guzino ec39dfb
Merge remote-tracking branch 'origin/master' into feat/deep-link-screens
guzino 4362cc9
fix: keep sheet open when a screen link is rejected
guzino c4f4bca
refactor: let routes own direct entry eligibility
guzino 0e0a16b
refactor: let sheet families own their entry points
guzino 0a19f0f
Merge remote-tracking branch 'origin/master' into feat/deep-link-screens
guzino c755819
refactor: derive sheet ids from their class names
guzino ce7f8a8
fix: restore external connection deep link and drop doc
guzino e420f42
Merge branch 'master' into feat/deep-link-screens
guzino 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package to.bitkit.ui.utils | ||
|
|
||
| import android.net.Uri | ||
| import androidx.navigation.NavDeepLink | ||
| import androidx.navigation.navDeepLink | ||
| import to.bitkit.ui.Routes | ||
| import kotlin.reflect.KClass | ||
|
|
||
| object ScreenDeepLinks { | ||
| const val SCHEME = "bitkit" | ||
| const val HOST = "screen" | ||
|
|
||
| private const val BASE_URI = "$SCHEME://$HOST" | ||
|
|
||
| private val CAMEL_HUMP = Regex("(?<=[a-z0-9])(?=[A-Z])") | ||
|
|
||
| private val DENIED: Set<KClass<out Routes>> = setOf( | ||
|
ovitrif marked this conversation as resolved.
Outdated
ovitrif marked this conversation as resolved.
Outdated
ovitrif marked this conversation as resolved.
Outdated
|
||
| Routes.AuthCheck::class, | ||
| Routes.CriticalUpdate::class, | ||
| Routes.LegacyRnRecovery::class, | ||
| Routes.LnurlChannel::class, | ||
| Routes.RecoveryMnemonic::class, | ||
| Routes.RecoveryMode::class, | ||
| ) | ||
|
|
||
| fun isDenied(route: KClass<*>): Boolean = route in DENIED | ||
|
|
||
| fun screenId(route: KClass<*>): String? { | ||
| if (!isScreenRoute(route)) return null | ||
| if (isDenied(route)) return null | ||
|
|
||
| return kebabId(route) | ||
| } | ||
|
|
||
| fun kebabId(route: KClass<*>): String? { | ||
| val name = route.simpleName ?: return null | ||
| return CAMEL_HUMP.split(name).joinToString("-") { it.lowercase() } | ||
| } | ||
|
|
||
| fun basePath(route: KClass<*>): String? = screenId(route)?.let { "$BASE_URI/$it" } | ||
|
|
||
| fun <T : Any> linksFor(route: KClass<T>): List<NavDeepLink> { | ||
| val basePath = basePath(route) ?: return emptyList() | ||
| return listOf(navDeepLink(route = route, basePath = basePath) {}) | ||
| } | ||
|
|
||
| fun isScreenDeepLink(uri: Uri): Boolean = | ||
| uri.scheme?.lowercase() == SCHEME && uri.host?.lowercase() == HOST | ||
|
|
||
| private fun isScreenRoute(route: KClass<*>): Boolean = Routes::class.java.isAssignableFrom(route.java) | ||
| } | ||
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,81 @@ | ||
| package to.bitkit.ui.utils | ||
|
|
||
| import android.net.Uri | ||
| import to.bitkit.ui.components.Sheet | ||
| import to.bitkit.ui.screens.wallets.receive.ReceiveRoute | ||
| import to.bitkit.ui.sheets.BackupRoute | ||
| import to.bitkit.ui.sheets.SendRoute | ||
| import to.bitkit.ui.sheets.WidgetsRoute | ||
| import to.bitkit.ui.sheets.hardware.HardwareRoute | ||
|
|
||
| object SheetDeepLinks { | ||
| private val SHEETS: List<Sheet> = listOf( | ||
|
ovitrif marked this conversation as resolved.
Outdated
|
||
| Sheet.Send(SendRoute.Recipient), | ||
| Sheet.Send(SendRoute.Address), | ||
| Sheet.Send(SendRoute.ContactSelect), | ||
| Sheet.Send(SendRoute.Amount), | ||
| Sheet.Send(SendRoute.QrScanner), | ||
| Sheet.Send(SendRoute.CoinSelection), | ||
| Sheet.Send(SendRoute.AddTag), | ||
| Sheet.Send(SendRoute.ComingSoon), | ||
| Sheet.Send(SendRoute.Support), | ||
|
|
||
| Sheet.Receive(ReceiveRoute.QR), | ||
| Sheet.Receive(ReceiveRoute.Amount), | ||
| Sheet.Receive(ReceiveRoute.EditInvoice), | ||
| Sheet.Receive(ReceiveRoute.AddTag), | ||
| Sheet.Receive(ReceiveRoute.GeoBlock), | ||
|
|
||
| Sheet.Backup(BackupRoute.Intro), | ||
| Sheet.Backup(BackupRoute.MultipleDevices), | ||
| Sheet.Backup(BackupRoute.Metadata), | ||
|
|
||
| Sheet.Widgets(WidgetsRoute.Gallery), | ||
| Sheet.Widgets(WidgetsRoute.PricePreview), | ||
| Sheet.Widgets(WidgetsRoute.PriceEdit), | ||
| Sheet.Widgets(WidgetsRoute.WeatherPreview), | ||
| Sheet.Widgets(WidgetsRoute.WeatherEdit), | ||
| Sheet.Widgets(WidgetsRoute.BlocksPreview), | ||
| Sheet.Widgets(WidgetsRoute.BlocksEdit), | ||
| Sheet.Widgets(WidgetsRoute.HeadlinesPreview), | ||
| Sheet.Widgets(WidgetsRoute.HeadlinesEdit), | ||
| Sheet.Widgets(WidgetsRoute.FactsPreview), | ||
| Sheet.Widgets(WidgetsRoute.CalculatorPreview), | ||
| Sheet.Widgets(WidgetsRoute.SuggestionsPreview), | ||
|
|
||
| Sheet.Hardware(HardwareRoute.Intro), | ||
|
|
||
| Sheet.ActivityDateRangeSelector, | ||
| Sheet.ActivityTagSelector, | ||
| Sheet.QrScanner, | ||
| ) | ||
|
|
||
| private val BY_PATH: Map<String, Sheet> = buildMap { | ||
| SHEETS.forEach { sheet -> | ||
| val sheetId = ScreenDeepLinks.kebabId(sheet::class) ?: return@forEach | ||
| putIfAbsent(sheetId, sheet) | ||
|
|
||
| val route = routeOf(sheet) ?: return@forEach | ||
| val routeId = ScreenDeepLinks.kebabId(route::class) ?: return@forEach | ||
| put("$sheetId/$routeId", sheet) | ||
| } | ||
| } | ||
|
|
||
| val paths: Set<String> get() = BY_PATH.keys | ||
|
|
||
| fun sheetFor(uri: Uri): Sheet? { | ||
| if (!ScreenDeepLinks.isScreenDeepLink(uri)) return null | ||
|
|
||
| val path = uri.pathSegments.orEmpty().joinToString("/").lowercase() | ||
| return BY_PATH[path] | ||
| } | ||
|
|
||
| private fun routeOf(sheet: Sheet): Any? = when (sheet) { | ||
| is Sheet.Send -> sheet.route | ||
| is Sheet.Receive -> sheet.route | ||
| is Sheet.Backup -> sheet.route | ||
| is Sheet.Widgets -> sheet.route | ||
| is Sheet.Hardware -> sheet.route | ||
| else -> null | ||
| } | ||
| } | ||
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
140 changes: 140 additions & 0 deletions
140
app/src/test/java/to/bitkit/ui/utils/ScreenDeepLinksTest.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,140 @@ | ||
| package to.bitkit.ui.utils | ||
|
|
||
| import android.net.Uri | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
| import to.bitkit.test.BaseUnitTest | ||
| import to.bitkit.ui.Routes | ||
| import to.bitkit.ui.sheets.SendRoute | ||
| import kotlin.reflect.KClass | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertNull | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @Config(sdk = [34]) | ||
| @RunWith(RobolectricTestRunner::class) | ||
| class ScreenDeepLinksTest : BaseUnitTest() { | ||
| private companion object { | ||
| val SENSITIVE_ROUTES: List<KClass<out Routes>> = listOf( | ||
| Routes.AuthCheck::class, | ||
| Routes.CriticalUpdate::class, | ||
| Routes.LegacyRnRecovery::class, | ||
| Routes.LnurlChannel::class, | ||
| Routes.RecoveryMnemonic::class, | ||
| Routes.RecoveryMode::class, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `screen id is derived from the route name in kebab-case`() { | ||
| val home = ScreenDeepLinks.screenId(Routes.Home::class) | ||
| val activityDetail = ScreenDeepLinks.screenId(Routes.ActivityDetail::class) | ||
| val rgsServer = ScreenDeepLinks.screenId(Routes.RgsServer::class) | ||
|
|
||
| assertEquals("home", home) | ||
| assertEquals("activity-detail", activityDetail) | ||
| assertEquals("rgs-server", rgsServer) | ||
| } | ||
|
|
||
| @Test | ||
| fun `routes without arguments produce a bare pattern`() { | ||
| val links = ScreenDeepLinks.linksFor(Routes.Settings::class) | ||
|
|
||
| assertEquals(1, links.size) | ||
| assertEquals("bitkit://screen/settings", links.single().uriPattern) | ||
| } | ||
|
|
||
| @Test | ||
| fun `required arguments are appended as path segments`() { | ||
| val links = ScreenDeepLinks.linksFor(Routes.ActivityAssignContact::class) | ||
|
|
||
| assertEquals("bitkit://screen/activity-assign-contact/{id}", links.single().uriPattern) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a route with both argument kinds keeps the required one in the path`() { | ||
| val links = ScreenDeepLinks.linksFor(Routes.ActivityDetail::class) | ||
|
|
||
| assertEquals("bitkit://screen/activity-detail/{id}?walletId={walletId}", links.single().uriPattern) | ||
| } | ||
|
|
||
| @Test | ||
| fun `arguments with defaults are appended as query parameters`() { | ||
| val links = ScreenDeepLinks.linksFor(Routes.Contacts::class) | ||
|
|
||
| assertEquals( | ||
| "bitkit://screen/contacts?showAddContactSheet={showAddContactSheet}", | ||
| links.single().uriPattern, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sensitive routes are denied`() { | ||
| SENSITIVE_ROUTES.forEach { route -> | ||
| assertTrue(ScreenDeepLinks.isDenied(route), "expected ${route.simpleName} to be denied") | ||
| assertNull(ScreenDeepLinks.screenId(route)) | ||
| assertTrue(ScreenDeepLinks.linksFor(route).isEmpty()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `every route is either denied or reachable by a unique screen id`() { | ||
| val ids = mutableMapOf<String, String>() | ||
|
|
||
| Routes::class.sealedSubclasses.forEach { route -> | ||
| val name = route.simpleName.orEmpty() | ||
| val id = ScreenDeepLinks.screenId(route) | ||
|
|
||
| if (ScreenDeepLinks.isDenied(route)) { | ||
| assertNull(id, "denied route $name must not expose a screen id") | ||
| return@forEach | ||
| } | ||
|
|
||
| assertNotNull(id, "route $name has no screen id") | ||
| val clash = ids.put(id, name) | ||
| assertNull(clash, "screen id '$id' is used by both $clash and $name") | ||
| assertEquals(1, ScreenDeepLinks.linksFor(route).size, "route $name has no deep link") | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `denied set contains only routes that still exist`() { | ||
| val declared = Routes::class.sealedSubclasses.toSet() | ||
| val denied = SENSITIVE_ROUTES.filter { it in declared } | ||
|
|
||
| assertEquals(SENSITIVE_ROUTES.size, denied.size) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sheet routes are not exposed as screen deeplinks`() { | ||
| val id = ScreenDeepLinks.screenId(SendRoute.Amount::class) | ||
| val links = ScreenDeepLinks.linksFor(SendRoute.Amount::class) | ||
|
|
||
| assertNull(id) | ||
| assertTrue(links.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `screen deeplinks are recognised regardless of case`() { | ||
| val lowercase = ScreenDeepLinks.isScreenDeepLink(Uri.parse("bitkit://screen/settings")) | ||
| val uppercase = ScreenDeepLinks.isScreenDeepLink(Uri.parse("BITKIT://SCREEN/settings")) | ||
|
|
||
| assertTrue(lowercase) | ||
| assertTrue(uppercase) | ||
| } | ||
|
|
||
| @Test | ||
| fun `other bitkit hosts are not screen deeplinks`() { | ||
| val recoveryMode = ScreenDeepLinks.isScreenDeepLink(Uri.parse("bitkit://recovery-mode")) | ||
| val pubkyAuth = ScreenDeepLinks.isScreenDeepLink(Uri.parse("bitkit://pubky-auth/callback")) | ||
| val lightning = ScreenDeepLinks.isScreenDeepLink(Uri.parse("lightning:lnbc1")) | ||
|
|
||
| assertFalse(recoveryMode) | ||
| assertFalse(pubkyAuth) | ||
| assertFalse(lightning) | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.