Skip to content

Instantly share code, notes, and snippets.

@mayuce
Created November 29, 2020 11:33
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/de5a91d96421a1219f009e52a25424cd to your computer and use it in GitHub Desktop.
Save mayuce/de5a91d96421a1219f009e52a25424cd to your computer and use it in GitHub Desktop.
package com.some.thing.routing
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Parcelable
import android.util.Log
import com.some.thing.ui.BaseActivity
/**
* Routable implementation provides usage of navigation between activities
* also provides deeplink implementation
*/
interface Routable<Route : BaseActivity, DATA : RouterData> {
companion object {
const val ROUTE_DATA = "RouteData"
const val DEEP_LINK_DATA = "DeepLinkData"
}
val route: Class<Route>
val routerDataClass: Class<DATA>
val deepLinkCode: 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)
}
}
/**
* Calling by developer from the code when navigating between activities
*/
fun startActivity(context: Context?, data: RouterData?) {
data?.takeIf { it.javaClass == routerDataClass }?.let { routerData ->
buildIntent(context).putExtra(ROUTE_DATA, routerData).also {
context?.startActivity(it)
}
} ?: kotlin.run {
Log.e(javaClass.simpleName, "router data is not valid!")
}
}
/**
* Build default intent
* Override it when you need a 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