Skip to content

Instantly share code, notes, and snippets.

@kingsleyadio
Last active August 30, 2022 17:47
Show Gist options
  • Save kingsleyadio/3c6a3a264ccf7944863f27a925a6e043 to your computer and use it in GitHub Desktop.
Save kingsleyadio/3c6a3a264ccf7944863f27a925a6e043 to your computer and use it in GitHub Desktop.
Deeplinks POC
package com.hellofresh.deeplinker
// This will map relative/absolute URLs with respective activities
interface SimpleDeepLinkInterface {
@DeepLink("user/login", "user/register")
fun provideLoginActivity(): LoginActivity
// we can request that some specific query parameters be included in the extras bundle
// we can even describe the type we'd like the extra presented as. Int in this case
@DeepLink("about")
fun provideAboutActivity(@Query("v") versionToLoad: Int): AboutActivity
}
// If we need more control as to building the intent. We could use this pattern. Possibly
// provide this class instance via injection
class SimpleDeepLinkIntentProvider(private val context: Context, private val someUserDataManager: Any) {
@DeepLink("recipes")
fun provideExploreIntent(options: DeepLinkOptions): Intent {
return Intent(options.appContext, MainActivity::class.java)
.putExtra("TabID", 1)
}
@DeepLink("tasty/food-boxes/{id}")
fun providePlanIntent(@Path("id") id: String, options: DeepLinkOptions): Intent {
return Intent(options.appContext, PlansActivity::class.java)
.putExtra("PlanID", id)
.putExtra("TabID", 2)
}
@DeepLink("customer/new")
fun provideSignUpIntent(@Query("action") action: String, @Query("social") showSocialLogin: Boolean): Intent {
return Intent(context, LoginActivity::class.java)
.putExtra("Action", action)
.putExtra("ShowSocialLogin", showSocialLogin)
}
@DeepLink("https://hellofresh.com/tos")
fun provideTosIntent(extras: Bundle, options: DeepLinkOptions): Intent {
return Intent()
.setClass(options.appContext, AboutActivity::class.java)
.putExtra("Link", options.rawUrl)
.putExtras(extras)
}
@Authenticated
@DeepLink("account", "account/settings")
fun provideAccountSettingsIntent(options: DeepLinkOptions): Intent {
return Intent(options.appContext, AccountActivity::class.java)
}
@Authenticated
@DeepLink("account/subscriptions")
fun provideSubscriptionsTask(options: DeepLinkOptions): TaskStackBuilder {
return TaskStackBuilder.create(options.appContext)
.addNextIntentWithParentStack(provideExploreIntent(options))
.addNextIntent(Intent(options.appContext, SubscriptionsActivity::class.java))
}
}
private fun checkIsAuthenticated() = true
fun main(args: Array<String>) {
// Build a DeepLinker instance using the application context
val app = Application()
val deepLinker = DeepLinker.Builder(app)
.schemes("hellofresh")
.baseUrls("https://hellofresh.de/", "https://hellofresh.co.uk/")
.registerInterface(SimpleDeepLinkInterface::class.java)
.registerInstance(SimpleDeepLinkIntentProvider(Application(), Any()))
.authenticator(Intent(app, LoginActivity::class.java), ::checkIsAuthenticated)
.build()
// Execute deep link request from the DeepLinkReceiverActivity
deepLinker.execute(deeplinkReceiverIntent)
// If coming from the authenticator, then instead, we do this
deepLinker.afterAuth(authenticatorActivityIntent)
}
@eyedol
Copy link

eyedol commented Oct 30, 2017

I like the thinking behind this. I would like if we could annotate the URIs on the Activities themselves. Then generate the intent for them. A similar approach to how DeepLinkDispatcher library does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment