This repository contains a library implementing the Coordinator pattern, which is a design pattern used in iOS app development to manage app navigation flows. The library provides a set of classes and protocols that can be used to implement the Coordinator pattern in an iOS app. It works either UIKit or SwiftUI apps Its core navigation has created with UINavigationController (UIKit) with the aim to get profit about navigation stack.
To use the Coordinator pattern library in your iOS project, you'll need to add the library files to your project and set up a Coordinator object. Here are the basic steps:
First let's define our paths and its views.
NOTE: If you want to create a UIKit-compatible coordinator, you must
import UIKCoordinator
otherwiseimport SUICoordinator
. Next we are going to write an example of SwiftUI
import SUICoordinator
import SwiftUI
enum OnboardingRoute: NavigationRoute {
case firstStep(viewModel: FirstViewModel)
case secondStep(viewModel: SecondViewModel)
// MARK: NavigationRouter
var transition: NavigationTransitionStyle {
switch self {
case .firstStep:
return .push
case .secondStep:
return .modal
}
}
func view() -> any View {
switch self {
case .firstStep(let vm):
return FirstView()
.environmentObject(vm)
case .secondStep(let vm):
return SecondView(viewModel: vm)
}
}
}
Second let's create our first Coordinator. All coordinator should to implement the start()
function and then starts the flow (mandatory). Finally add additional flows
import SUICoordinator
class OnboardingCoordinator: NavigationCoordinator<OnboardingRoute> {
// MARK: Coordinator
override func start(animated: Bool) {
let vm = FirstViewModel(coordinator: self)
router.startFlow(
route: .firstStep(viewModel: vm),
animated: animated
)
}
// MARK: Helper funcs
func showStep2() {
let vm = SecondViewModel(coordinator: self)
router.navigate(to: .secondStep(viewModel: vm))
}
func showHomeCoordinator() {
let coordinator = HomeCoordinatorSUI(currentPage: .settings)
router.navigate(to: coordinator)
}
}
import SUICoordinator
enum HomeRoute: CaseIterable, TabbarPage {
case marketplace
case settings
// MARK: NavigationRouter
func coordinator() -> Coordinator {
switch self {
case .settings:
return SettingsCoordinator()
case .marketplace:
return MarketplaceCoordinator()
}
}
// MARK: TabbarPageDataSource
public var title: String {
switch self {
case .marketplace:
return "Marketplace"
case .settings:
return "Settings"
}
}
public var icon: String {
switch self {
case .marketplace:
return "house"
case .settings:
return "gearshape"
}
}
public var position: Int {
switch self {
case .marketplace:
return 0
case .settings:
return 1
}
}
}
- Default tabbar build with UIKIT (It also works with SwiftUI)
import UIKCoordinator
import UIKit
class HomeCoordinatorUIKit: TabbarCoordinator<HomeRoute> {
// MARK: Constructor
public init() {
super.init(
pages: [.marketplace, .settings],
currentPage: .marketplace
)
}
}
- Custom view (SwiftUI)
import SUICoordinator
import SwiftUI
import Combine
class HomeCoordinatorSUI: TabbarCoordinator<HomeRoute> {
// MARK: Properties
var cancelables = Set<AnyCancellable>()
// Custom Tabbar view
public init(currentPage: HomeRoute) {
let viewModel = HomeTabbarViewModel()
let view = HomeTabbarView(viewModel: viewModel)
viewModel.currentPage = currentPage
super.init(
customView: view,
pages: [.marketplace, .settings],
currentPage: currentPage
)
viewModel.$currentPage
.sink { [weak self] page in
self?.currentPage = page
}.store(in: &cancelables)
UITabBar.appearance().isHidden = true
}
// Default Tabbar view
public init(default: Bool ) {
super.init(pages: [.marketplace, .settings])
}
}
import SUICoordinator
class MainCoordinator: NavigationCoordinator<MainRoute> {
// MARK: Constructor
init() {
super.init(parent: nil)
router.startFlow(route: .splash, animated: false)
}
// MARK: Coordinator
override func start(animated: Bool = false) {
router.navigate(to: OnboardingCoordinator(presentationStyle: .fullScreen), animated: animated)
}
}
import SUICoordinator
import SwiftUI
enum MainRoute: NavigationRoute {
case splash
// MARK: NavigationRoute
var transition: NavigationTransitionStyle { .push }
func view() -> any View { SplashScreenView() }
}
- Create a SceneDelegate class if your app supports scenes:
import SwiftUI
import SUICoordinator
final class SceneDelegate: NSObject, UIWindowSceneDelegate {
var mainCoordinator: MainCoordinator?
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
setupCoordinator(window: window, animated: true)
}
private func setupCoordinator(window: UIWindow?, animated: Bool = false) {
mainCoordinator = .init()
setupWindow(controller: mainCoordinator?.root)
BaseCoordinator.mainCoordinator = mainCoordinator
mainCoordinator?.start(animated: animated)
}
private func setupWindow(controller: UIViewController?) {
window?.rootViewController = controller
window?.makeKeyAndVisible()
}
}
- In your app's AppDelegate file, set the SceneDelegate class as the windowScene delegate:
import UIKit
@main
final class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
return true
}
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let sessionRole = connectingSceneSession.role
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: sessionRole)
sceneConfig.delegateClass = SceneDelegate.self
return sceneConfig
}
}
You can find an example here https://github.com/felilo/TestCoordinatorLibrary
These are the most important features and actions that you can perform:
Name | Parametes | Description | ||||
---|---|---|---|---|---|---|
navigate(_) |
|
Allows you to navigate among the views that were defined in the Route. The types of presentation are Push, Modal, ModalFullScreen and Custom. | ||||
navigate(_) |
|
Allows you to navigate among the Coordinators. It calls the start() function. |
||||
startFlow(_) |
|
Cleans the navigation stack and runs the navigation flow. | ||||
present(_) |
|
Presents a view modally. | ||||
pop(_) |
|
Pops the top view from the navigation stack and updates the display. | ||||
popToRoot(_) |
|
Pops all the views on the stack except the root view and updates the display. | ||||
dismiss(_) |
|
Dismisses the view that was presented modally by the view. | ||||
popToView(_) |
|
Pops views until the specified view is at the top of the navigation stack. Example: router.popToView(MyView.self) |
||||
finishFlow(_) |
|
Pops all the views on the stack including the root view, dismisses all the modal view and remove the current coordinator from the coordinator stack. |
Name | Parametes | Description | ||||
---|---|---|---|---|---|---|
router |
Variable of Route type which allow performs action router. | |||||
forcePresentation(_) |
|
Puts the current coordinator at the top of the coordinator stack, making it the active and visible coordinator. This feature is very useful to start the navigation flow from push notifications, notification center, atypical flows, etc. | ||||
getTopCoordinator(_) |
|
Returns the coordinator that is at the top of the coordinator stack. | ||||
restartApp(_) |
|
Cleans the navigation stack and runs the main coordinator navigation flow. |
Name | Parametes | Description | ||
---|---|---|---|---|
currentPage |
Returns the current page selected. | |||
getCoordinatorSelected() |
|
Returns the coordinator selected that is associated to the selected tab | ||
setPages(_) |
|
Updates the page set. | ||
forcePresentation(_) |
|
Puts the current coordinator at the top of the coordinator stack, making it the active and visible coordinator. This feature is very useful to start the navigation flow from push notifications, notification center, atypical flows, etc. |
SPM
Open Xcode and your project, click File / Swift Packages / Add package dependency... . In the textfield "Enter package repository URL", write https://github.com/felilo/ALCoordinator and press Next twice
Contributions to the ALCoordinator library are welcome! To contribute, simply fork this repository and make your changes in a new branch. When your changes are ready, submit a pull request to this repository for review.
License
The ALCoordinator library is released under the MIT license. See the LICENSE file for more information.
<style> table { border: none; } table .c1 { border: none !important; } </style>