As simple as only possible. 3 Steps are:
enum AppRoute: Route {
case splash
case onboarding
case login
case home
}final class AppCoordinator: ViewCoordinator {
var initialRoute: AppRoute
init(initialRoute: AppRoute) {
self.initialRoute = initialRoute
}
@MainActor
func prepareTransition(for route: AppRoute) -> ViewTransitionType {
switch route {
case .splash: .root
case .onboarding: .fullScreen
case .login: .sheet
case .home: .multiple(.root, .dismiss)
}
}
@MainActor
@ViewBuilder
func prepareView(for route: AppRoute, router: any Router<AppRoute>) -> some View {
switch route {
case .splash:
let viewModel = TemplateViewModel(color: .red, nextStep: .onboarding, router: router)
TemplateView(viewModel: viewModel)
case .onboarding:
let coordinator = OnboardingCoordinator(initialRoute: .screen1, parentRouter: router)
OnboardingFlow(coordinator: coordinator)
....
}
}
}struct AppFlow: DefaultViewFlow {
var coordinator: AppCoordinator
}@main
struct CoordinatorX_ExampleApp: App {
private let coordinator = AppCoordinator(initialRoute: .splash)
var body: some Scene {
WindowGroup {
AppFlow(coordinator: coordinator)
}
}
}var initialRoute: Route from which route a Flow should be started.
func prepareTransition(for route: RouteType) -> TransitionType notify Coordinator how to show a view for route.
func prepareView(for route: RouteType, router: any Router<RouteType>) -> some View prepare View to be showed.
There are 3 types of Coordinators prepared for your app:
ViewCoordinatorRedirectionViewCoordinatorNavigationCoordinator
fullScreen, overlay, sheet has own Context. That means that you can call from them appearing another part of fullScreen, overlay, sheet. And it will be handled by the same Coordinator
It is to present a single root View and the root View can be covered with sheet, fullscreen, overlay or replaced by another View.
ViewCoordinator supports next Transition types:
case dismiss
case fullScreen
case none
case overlay
case root
case set
case sheetTransition actions:
dismiss can be applied to dismiss fullScreen, overlay, sheet
fullScreen is applied to cover root View with modal View
none just do nothing
overlay is applied to cover root View with overlay View
root is applied to replace root View with another one. It was created to be used from fullScreen, overlay, sheet
set should be applied to replace View which is presented as fullScreen, overlay, sheet
sheet is applied to cover root View with sheet View
It is similar to ViewCoordinator, but can be understood as children coordinator. It has identical Transition types plus one additional:
case parent(ParentRouteType)Which is used to triger action on parent flow.
It is to present Navigation Flow. It has identical Transition types as ViewCoordinator plus few additional:
case pop
case popToRoot
case pushThanks for reading until the end! 🫡