Created
February 11, 2020 14:26
-
-
Save alizeec/8f37b636ba31a0cbc60af504603f8291 to your computer and use it in GitHub Desktop.
Old navigation system at Aircall
This file contains 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
interface IRouteParser { | |
fun parseRoute(route: String, clearTask: Boolean): RouteOptions? | |
} |
This file contains 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
interface IRouter { | |
var currentBaseActivity: BaseActivity? | |
fun navigateTo(route: String, clearTask: Boolean = false, extras: Bundle? = null) | |
fun navigateForResult(route: String, requestCode: Int, extras: Bundle? = null) | |
fun openExternalApplication( | |
appType: ExternalAppType, | |
toEmails: Array<out String> = emptyArray(), | |
subject: String? = null, | |
text: String? = null, | |
title: String | |
) | |
fun navigateBack() | |
fun navigateUpForResultOk(intent: Intent?) | |
fun getCurrentView(): Fragment? | |
fun setCurrentActivity(activity: BaseActivity) | |
fun unSetCurrentActivity(activity: BaseActivity) | |
} |
This file contains 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
MyPresenter { | |
... | |
router.navigateTo(Routes.SIGN_IN_FORM, clearTask = true) | |
... | |
} |
This file contains 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
class RouteParser @Inject constructor() : IRouteParser { | |
override fun parseRoute(route: String, clearTask: Boolean): RouteOptions? { | |
return when (route) { | |
Routes.SPLASH -> RouteOptions( | |
RouteType(route, R.id.fragment_splash_screen), | |
SplashScreenActivity::class.java, | |
clearTask | |
) | |
... | |
} | |
} | |
} | |
data class RouteOptions( | |
val routeType: RouteType, | |
val destinationActivityClass: Class<out Activity>, | |
val clearTask: Boolean = false | |
) |
This file contains 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
object Router : IRouter { | |
override var currentActivity: BaseActivity? = null | |
@set:Inject | |
lateinit var routeParser: IRouteParser | |
@set:Inject | |
lateinit var navigatorProvider: INavigatorProvider | |
@set:Inject | |
lateinit var applicationContext: Context | |
override fun navigateTo(route: String, clearTask: Boolean, extras: Bundle?) { | |
routeParser.parseRoute(route, clearTask)?.let { routeOptions -> | |
currentActivity?.also { activity -> | |
navigatorProvider.of(activity).navigate(routeOptions, extras) | |
} ?: run { | |
with(navigatorProvider.of(applicationContext)) { | |
startActivity(buildIntent(routeOptions, extras)) | |
} | |
} | |
} | |
} | |
override fun navigateForResult(route: String, requestCode: Int, extras: Bundle?) { | |
routeParser.parseRoute(route, false)?.let { routeOptions -> | |
currentActivity?.also { activity -> | |
navigatorProvider.of(activity).navigateForResult(routeOptions, requestCode, extras) | |
} | |
} | |
} | |
override fun openExternalApplication(...) { | |
when (appType) { | |
ExternalAppType.EMAIL -> openEmailApp(toEmails, subject, text, title) | |
ExternalAppType.SHARE -> openSharingTextPanel(text) | |
} | |
} | |
private fun openEmailApp(...) { ... } | |
private fun openSharingTextPanel(...) { ... } | |
override fun navigateBack() { | |
currentActivity?.let { | |
if (!navigatorProvider.of(it).navigateUp()) { | |
it.finish() | |
} | |
} | |
} | |
override fun navigateUpForResultOk(intent: Intent?) { | |
currentActivity?.let { | |
navigatorProvider.of(it).navigateUpForResultOk(intent) | |
it.finish() | |
} | |
} | |
override fun getCurrentView(): Fragment? { | |
currentActivity?.let { activity -> | |
return navigatorProvider.of(activity).findCurrentFragment() | |
} | |
return null | |
} | |
override fun setCurrentActivity(activity: BaseActivity) { ... } | |
override fun unSetCurrentActivity(activity: BaseActivity) { ... } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment