Skip to content

Instantly share code, notes, and snippets.

@mayuce
Created November 7, 2020 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mayuce/7a4d30be7786acc43b91e631a6902a36 to your computer and use it in GitHub Desktop.
Save mayuce/7a4d30be7786acc43b91e631a6902a36 to your computer and use it in GitHub Desktop.
Routable Gist
package com.some.package.routing
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Parcelable
import com.some.package.ui.BaseActivity
/**
* Routable implementation provides usage of navigation between activities
* also provides deeplink implementation
*/
interface Routable<Route : BaseActivity<*, *>, DATA : Parcelable> {
companion object {
const val ROUTE_DATA = "RouteData"
const val DEEP_LINK_DATA = "DeepLinkData"
}
/**
* Defines the activity class which will going to be navigated
*/
val route: Class<Route>
/**
* Defines the deep link codes which can be navigate to
* this function, might be null if there is no deep link navigation
*/
val deepLinkCode: Array<Int>?
/**
* Triggers when context comes from deep link navigation
*/
fun startDeepLink(context: Context?, data: Uri?) {
buildIntent(context).putExtra(DEEP_LINK_DATA, data).also {
context?.startActivity(it)
}
}
/**
* Triggers the block which is developer called
* Puts the bundle to the intent and calls the activity
*/
fun startActivity(context: Context?, data: DATA?) {
buildIntent(context).putExtra(ROUTE_DATA, data).also {
context?.startActivity(it)
}
}
/**
* Builds default intent
* Override it when you need to custom intent
*/
fun buildIntent(context: Context?) = Intent(context, route)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment