Skip to main content

App Navigation

Atlas is designed for ViewModel to ViewModel navigation.

To get started using it please import the plugin into your project (shared.gradle file):


_3
plugins {
_3
id("io.github.thearchitect123.atlasNavigationEngineGenerator") // generates the navigation graphs for each platform
_3
}

How to use

Currently Atlas supports only Compose, UIKit and SwiftUI.

First each screen/composable needs to be registered with the annotation.

If your screen/composable is the first instance in the navigation graph, please mark it with isInital=true


_1
@AtlasScreen(myViewModel::class, isInitial=true)

Otherwise empty/false by default for all other screens.


_1
@AtlasScreen(myViewModel::class)

In your compose project (android side), register your composable


_12
_12
@AtlasScreen(myFirstViewModel::class, isInitial=true)
_12
@Composable
_12
fun myCompose (vm: myFirstViewModel){
_12
_12
}
_12
_12
@AtlasScreen(mySecondViewModel::class)
_12
@Composable
_12
fun mySecondScreen (vm: mySecondViewModel){
_12
_12
}

And in your SwiftUI project. The viewmodels are injected into your widgets automatically by the navigation engine.


_17
//@AtlasScreen(viewModel: myFirstViewModel.self, initial: true)
_17
struct ContentViewFirst: View {
_17
let vm : myFirstViewModel?
_17
_17
var body: some View {
_17
Text("")
_17
}
_17
}
_17
_17
//@AtlasScreen(viewModel: mySecondViewModel.self)
_17
struct ContentViewSecond: View {
_17
let vm : mySecondViewModel?
_17
_17
var body: some View {
_17
Text("")
_17
}
_17
}

How to configure the ViewModels?

Inside your shared module:


_9
@ViewModels
_9
class myFirstViewModel : ViewModel(){
_9
_9
}
_9
_9
@ViewModels
_9
class mySecondViewModel : ViewModel(){
_9
_9
}

To navigate from myFirstViewModel to mySecondViewModel, inside your myFirstViewModel:


_6
@ViewModels
_6
class myFirstViewModel : ViewModel(){
_6
fun goToNextPage(){ // invoke this function in your UI (button press for example)
_6
AtlasDI.resolveService<AtlasNavigationService>().navigateToPage(mySecondViewModel::class)
_6
}
_6
}

AtlasNavigationService is a navigation service that is generated inside each of your platforms main folders (iOSMain, androidMain, etc). You will need to register this service with the IOC container


_1
AtlasDI.registerInterfaceToInstance(AtlasNavigationService::class, AtlasNavigation)

Please run this somewhere on AppStartup.

How to pass parameters between screens?

To allow your viewmodels to be able to handle pushing/popping parameters, you will need to add these interfaces (you can configure any data types you want to be able to pass):


_2
Pushable<T> // T can be String, or Int, or any enum or class
_2
Poppable<T> // sets the return type (passed from the screen returned from)


_27
@ViewModels
_27
class myFirstViewModel : ViewModel(), Pushable<String>, Poppable<Int>{
_27
fun goToNextPage(){ // triggers mySecondViewModel onPushParams, and assigns "My Sample VM" in params
_27
AtlasDI.resolveService<AtlasNavigationService>().navigateToPage(mySecondViewModel::class, "My Sample VM")
_27
}
_27
_27
override fun onPushParams(params: String) {
_27
_27
}
_27
_27
override fun onPopParams(params: Int) {
_27
_27
}
_27
}
_27
_27
@ViewModels
_27
class mySecondViewModel : ViewModel(), Pushable<String>, Poppable<Int>{
_27
fun popScreen(){ // triggers myFirstViewModel onPopParams, and assigns 55 in params
_27
AtlasDI.resolveService<AtlasNavigationService>().popPage(55)
_27
}
_27
_27
override fun onPushParams(params: String) {
_27
}
_27
_27
override fun onPopParams(params: Int) {
_27
}
_27
}