Created
November 29, 2020 12:07
-
-
Save mayuce/12c971e7e22a1758a057e537b3b82e31 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.net.Uri | |
import android.util.SparseArray | |
import androidx.core.util.forEach | |
import javax.inject.Inject | |
class Router @Inject constructor( | |
private val routes: SparseArray<Routable<*, *>> | |
) { | |
/** | |
* Routes with given code & bundle to the route | |
* which contains the given code as deep link code | |
*/ | |
fun routeToLink(context: Context?, code: Int, data: Uri?) { | |
routes.get(code)?.startDeepLink(context, data) | |
} | |
/** | |
* Routes with given code & data to the route | |
* which contains the given code as deep link code | |
* with given rotuer | |
* O(1) time complexity way | |
*/ | |
fun routeToActivity(context: Context?, code: Int, data: RouterData?) { | |
routes.get(code)?.startActivity(context, data) | |
} | |
/** | |
* Routes with given data | |
* which contains the given data class type as router data | |
* O(n) time complexity way | |
*/ | |
fun routeToActivity(context: Context?, data: RouterData?) { | |
routes.forEach { _, routable -> | |
routable.takeIf { it.routerDataClass == data?.javaClass }?.startActivity(context, data) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment