Skip to content

Instantly share code, notes, and snippets.

@mayuce
Created November 29, 2020 12:07
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/12c971e7e22a1758a057e537b3b82e31 to your computer and use it in GitHub Desktop.
Save mayuce/12c971e7e22a1758a057e537b3b82e31 to your computer and use it in GitHub Desktop.
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