Skip to content

Commit

Permalink
Created StackNavigationHandler and Misc other updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed Feb 27, 2022
1 parent 61c4865 commit d77e032
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal actual fun <Context, Key> InternalNavContainer(
}
}

BackHandler { navigator.goBack() }
BackHandler(enabled = navigator.canGoBack()) { navigator.goBack() }
}

@Composable
Expand All @@ -44,5 +44,5 @@ internal actual fun <Context, Key, NavigationScope : ComposeNavigationKeyScope<K
}
}

BackHandler { navigator.goBack() }
BackHandler(enabled = navigator.canGoBack()) { navigator.goBack() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.chrynan.navigation
/**
* A [NavigationHandler] used on the Android platform that uses an [AndroidNavigationScope].
*/
fun interface AndroidNavigationHandler<I : NavigationIntent> :
NavigationHandler<NavigationEvent<I>, AndroidNavigationScope>
interface AndroidNavigationHandler<Intent : NavigationIntent> :
NavigationEventHandler<Intent, AndroidNavigationScope>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package com.chrynan.navigation

import android.app.Activity

internal class AndroidNavigator<I : NavigationIntent>(
private val handler: AndroidNavigationHandler<I>,
internal class AndroidNavigator<Intent : NavigationIntent>(
private val handler: AndroidNavigationHandler<Intent>,
private val scope: AndroidNavigationScope
) : NavigationEventNavigator<I> {
) : NavigationEventNavigator<Intent> {

override fun navigate(event: NavigationEvent<I>) {
override fun navigate(event: NavigationEvent<Intent>) {
handler.apply {
scope.onNavigate(event = event)
}
Expand All @@ -32,10 +32,10 @@ internal class AndroidNavigator<I : NavigationIntent>(
* navigator.goBack()
* ```
*/
fun <I : NavigationIntent> navigator(
fun <Intent : NavigationIntent> navigator(
activity: Activity,
handler: AndroidNavigationHandler<I>
): NavigationEventNavigator<I> {
handler: AndroidNavigationHandler<Intent>
): NavigationEventNavigator<Intent> {
val scope = AndroidNavigationScope(activity = activity)

return AndroidNavigator(handler = handler, scope = scope)
Expand All @@ -54,21 +54,25 @@ fun <I : NavigationIntent> navigator(
* navigator.goBack()
* ```
*/
fun <I : NavigationIntent> navigator(
fun <Intent : NavigationIntent> navigator(
activity: Activity,
canGoBack: () -> Boolean = { false },
onGoBack: () -> Unit = { activity.onBackPressed() },
onGoUp: () -> Unit = { activity.onBackPressed() },
onGoTo: (intent: I) -> Unit
): NavigationEventNavigator<I> {
onGoTo: (intent: Intent) -> Unit
): NavigationEventNavigator<Intent> {
val scope = AndroidNavigationScope(activity = activity)

return AndroidNavigator(
handler = { event ->
when (event) {
is NavigationEvent.Back -> onGoBack()
is NavigationEvent.Up -> onGoUp()
is NavigationEvent.To -> onGoTo(event.intent)
}
handler = object : AndroidNavigationHandler<Intent> {

override fun AndroidNavigationScope.onGoBack() = onGoBack()

override fun AndroidNavigationScope.onGoUp() = onGoUp()

override fun AndroidNavigationScope.onGoTo(intent: Intent) = onGoTo(intent)

override fun AndroidNavigationScope.canGoBack(): Boolean = canGoBack()
},
scope = scope
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ package com.chrynan.navigation
* @see [Navigator.navigate]
* @see [NavigationHandler.onNavigate]
*/
sealed class NavigationEvent<I : NavigationIntent> {
sealed class NavigationEvent<Intent : NavigationIntent> {

class Back<I : NavigationIntent> internal constructor() : NavigationEvent<I>()
class Back<Intent : NavigationIntent> internal constructor() : NavigationEvent<Intent>()

class Up<I : NavigationIntent> internal constructor() : NavigationEvent<I>()
class Up<Intent : NavigationIntent> internal constructor() : NavigationEvent<Intent>()

data class To<I : NavigationIntent> internal constructor(val intent: I) : NavigationEvent<I>()
data class To<Intent : NavigationIntent> internal constructor(val intent: Intent) : NavigationEvent<Intent>()

companion object
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ package com.chrynan.navigation
* A [NavigationHandler] that provides distinct functions for each of the possible
* [NavigationEvent]s.
*/
interface NavigationEventHandler<I : NavigationIntent, S : NavigationScope> :
NavigationHandler<NavigationEvent<I>, S> {
interface NavigationEventHandler<Intent : NavigationIntent, Scope : NavigationScope> :
NavigationHandler<NavigationEvent<Intent>, Scope>,
StackNavigationHandler<NavigationEvent<Intent>, Scope> {

fun S.onGoBack()
fun Scope.onGoBack()

fun S.onGoUp()
fun Scope.onGoUp()

fun S.onGoTo(intent: I)
fun Scope.onGoTo(intent: Intent)

override fun S.onNavigate(event: NavigationEvent<I>) =
override fun Scope.onNavigate(event: NavigationEvent<Intent>) =
when (event) {
is NavigationEvent.Back -> onGoBack()
is NavigationEvent.Up -> onGoUp()
is NavigationEvent.To -> onGoTo(intent = event.intent)
}

override fun Scope.canGoBack(): Boolean = false

companion object
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ package com.chrynan.navigation
* A [Navigator] that navigates using [NavigationEvent]s. This is typically the base [Navigator] but not every UI
* framework can use it, such as Jetpack Compose, so it is separate from the [Navigator] interface.
*/
interface NavigationEventNavigator<I : NavigationIntent> : Navigator {
interface NavigationEventNavigator<Intent : NavigationIntent> : Navigator {

/**
* Navigates to the provided [event].
*/
fun navigate(event: NavigationEvent<I>)
fun navigate(event: NavigationEvent<Intent>)
}

/**
* A convenience function for calling [NavigationEventNavigator.navigate] with [NavigationEvent.Back].
*/
fun <I : NavigationIntent> NavigationEventNavigator<I>.goBack() =
fun <Intent : NavigationIntent> NavigationEventNavigator<Intent>.goBack() =
navigate(event = NavigationEvent.Back())

/**
* A convenience function for calling [NavigationEventNavigator.navigate] with [NavigationEvent.Up].
*/
fun <I : NavigationIntent> NavigationEventNavigator<I>.goUp() =
fun <Intent : NavigationIntent> NavigationEventNavigator<Intent>.goUp() =
navigate(event = NavigationEvent.Up())

/**
* A convenience function for calling [NavigationEventNavigator.navigate] with [NavigationEvent.To].
*/
fun <I : NavigationIntent> NavigationEventNavigator<I>.goTo(intent: I) =
fun <Intent : NavigationIntent> NavigationEventNavigator<Intent>.goTo(intent: Intent) =
navigate(event = NavigationEvent.To(intent = intent))
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.chrynan.navigation

/**
* An extension on the [NavigationHandler] that provides functions to determine back navigation capability.
*/
interface StackNavigationHandler<Event : Any, Scope : NavigationScope> : NavigationHandler<Event, Scope> {

/**
* Determines whether a back navigation can be handled.
*/
fun Scope.canGoBack(): Boolean

companion object
}

0 comments on commit d77e032

Please sign in to comment.