Skip to content

Instantly share code, notes, and snippets.

@attilakruchio
Created June 25, 2022 13:02
Show Gist options
  • Save attilakruchio/e951fd90baaefae47669f847589009e0 to your computer and use it in GitHub Desktop.
Save attilakruchio/e951fd90baaefae47669f847589009e0 to your computer and use it in GitHub Desktop.
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
@ActivityRetainedScoped
class Navigator @Inject constructor() {
private val _navEvents = Channel<Direction>(Channel.UNLIMITED)
val navEvents: Flow<Direction> = _navEvents.receiveAsFlow()
fun navigateTo(navDirections: NavDirections) {
_navEvents.trySend(Direction.NavigateTo(navDirections))
}
fun navigateBack() {
_navEvents.trySend(Direction.NavigateBack)
}
sealed interface Direction {
data class NavigateTo(
val navDirections: NavDirections,
) : Direction
object NavigateBack : Direction
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment