Created
November 29, 2020 11:33
-
-
Save mayuce/de5a91d96421a1219f009e52a25424cd to your computer and use it in GitHub Desktop.
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
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