Skip to content

Instantly share code, notes, and snippets.

@digitalbuddha digitalbuddha/Router.kt
Last active Dec 17, 2017

Embed
What would you like to do?
data class Transition(toAnim:Animation=null, fromAnim:Animation,
direction:String=Forward, shouldDispatch=true)
sealed class Screen : State() {
data class MyScreen(var data: Data, layoutID:Int, transition:Transition) : Screen()
data class Ar(var data: Data, layoutID:Int, transition:Transition) : Screen()
}
@ActivityScoped
class Dispatcher(val container:Container, val inflator:MyInflatorInterface){
val history: Stack<Screen> = Stack()
fun goTo(screen:Screen){
history.push(screen)
//a sticky view wants to remain in stack when nagivating
//a view stays sticky if they want to listen for dispatcher past being top view
container.removeNonSticky()
//when going back we might be sticky and next to show
//only add to container if it does not contain us on top already
if(container.currentlySticky(screen).not()) {
container.addView(inflator.inflate(screen.layoutID))
}
//now that view is attached and presenter is attached, let's show view if needed
dispatch(screen)
}
fun goBack() {
currentScreen = popScreen()//current screen is what we are currently showing
previousScreen = popScreen()
//we just left a sticky screen so its time to hide it from view
if(currentScreen.transition.isSticky()) container.hideTopView()
goTo(previousScreen, forward=false)
}
private fun popScreen(): State {
if(!history.empty()) Timber.d("popping "+history.peek())
return if (history.isEmpty()) Screen.BackStackEmpty else history.pop()
}
}
init(AR) -> history=[] container=AR[invisible=true,sticky=true]
dispatcher.goTo(Landing) -> history=[Landing] container=Landing.visible, AR.invisible
dispatcher.goTo(AR) -> history=[Landing,AR] container=AR[visible]
dispatcher.goBack() -> history=[Landing] container=AR[invisible],Landing
dispatcher.goTo(AR) -> history=[Landing,AR] container=Ar[Visible]
dispatcher.goTo(Success) -> history=Landing,AR,Success container=AR,Success
dispatcher.goTo(NoNetwork)->history=Landing,AR,Success,NoNetwork container=AR,NoNetwork
dispatcher.goBack() -> history=Landing,AR,Success container=AR,Success
dispatcher.goBack() -> history=Landing,AR container=AR
dispatcher.goBack() -> history=Landing container=AR,Landing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.