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):
_3plugins {_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_12fun myCompose (vm: myFirstViewModel){_12_12}_12_12@AtlasScreen(mySecondViewModel::class)_12@Composable_12fun 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)_17struct ContentViewFirst: View {_17 let vm : myFirstViewModel?_17_17 var body: some View {_17 Text("")_17 }_17}_17_17//@AtlasScreen(viewModel: mySecondViewModel.self)_17struct 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_9class myFirstViewModel : ViewModel(){_9_9}_9_9@ViewModels_9class mySecondViewModel : ViewModel(){_9_9}
To navigate from myFirstViewModel to mySecondViewModel, inside your myFirstViewModel:
_6@ViewModels_6class 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):
_2Pushable<T> // T can be String, or Int, or any enum or class_2Poppable<T> // sets the return type (passed from the screen returned from)
_27@ViewModels_27class 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_27class 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}