Skip to content

Instantly share code, notes, and snippets.

@virendersran01
Forked from mayuce/ModularRoutable.kt
Created April 9, 2021 04:28
Show Gist options
  • Select an option

  • Save virendersran01/a109424f7a77c6808d7571529100ae65 to your computer and use it in GitHub Desktop.

Select an option

Save virendersran01/a109424f7a77c6808d7571529100ae65 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