Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/felilo/alcoordinator
Coordinators either UIKit or SWiftUI. Simple, powerful and elegant.
https://github.com/felilo/alcoordinator
coordinator-pattern mvvm mvvm-c navigation-stack swift swiftui viper
Last synced: 3 months ago
JSON representation
Coordinators either UIKit or SWiftUI. Simple, powerful and elegant.
- Host: GitHub
- URL: https://github.com/felilo/alcoordinator
- Owner: felilo
- Created: 2023-03-02T23:00:53.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-02T17:32:14.000Z (over 1 year ago)
- Last Synced: 2024-04-24T16:37:31.937Z (9 months ago)
- Topics: coordinator-pattern, mvvm, mvvm-c, navigation-stack, swift, swiftui, viper
- Language: Swift
- Homepage:
- Size: 91.8 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ALCoodinator
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.
_____## Getting Started
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:
_____## Defining the coordinator
First let's define our paths and its views.
> **_NOTE:_** If you want to create a UIKit-compatible coordinator, you must **`import UIKCoordinator`** otherwise **`import SUICoordinator`**.
> Next we are going to write an example of SwiftUI
```swift
import SUICoordinator
import SwiftUIenum 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
```swift
import SUICoordinatorclass OnboardingCoordinator: NavigationCoordinator {
// 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)
}
}
```_____
## Create a TabbarCoordinator
### 1. Create a router
```swift
import SUICoordinatorenum 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: Image {
switch self {
case .marketplace:
return Image(systemName: "house")
case .settings:
return Image(systemName: "gearshape")
}
}
public var position: Int {
switch self {
case .marketplace:
return 0
case .settings:
return 1
}
}
}
```### 2. Create a TabbarCoordinator
* Default tabbar build with UIKIT (It also works with SwiftUI)
```swift
import UIKCoordinator
import UIKitclass HomeCoordinatorUIKit: TabbarCoordinator {
// MARK: Constructor
public init() {
super.init(
pages: [.marketplace, .settings],
currentPage: .marketplace
)
}
}
```* Custom view (SwiftUI)
```swift
import SUICoordinator
import SwiftUI
import Combineclass HomeCoordinatorSUI: TabbarCoordinator {
// MARK: Properties
var cancelables = Set()// Custom Tabbar view
public init(currentPage: HomeRoute) {
let viewModel = HomeTabbarViewModel()
let view = HomeTabbarView(viewModel: viewModel)
viewModel.currentPage = currentPagesuper.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])
}
}
```### 3. Create MainCoordinator
```swift
import SUICoordinatorclass MainCoordinator: NavigationCoordinator {
// MARK: Constructor
init() {
super.init(parent: nil)
router.startFlow(route: .splash, animated: false)
}
// MARK: Coordinator
override func start(animated: Bool = false) {
let coordinator = OnboardingCoordinator(presentationStyle: .fullScreen)
router.navigate(to: coordinator, animated: animated)
}
}
``````swift
import SUICoordinator
import SwiftUIenum MainRoute: NavigationRoute {
case splash// MARK: NavigationRoute
var transition: NavigationTransitionStyle { .push }
func view() -> any View { SplashScreenView() }
}
```
_____### Setup project
1. Create a SceneDelegate class if your app supports scenes:
```swift
import SwiftUI
import SUICoordinatorfinal 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(animated: true)
}
private func setupCoordinator(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()
}
}
```
2. In your app's AppDelegate file, set the SceneDelegate class as the windowScene delegate:
```swift
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
_____
### Features
These are the most important features and actions that you can perform:
#### Router
The router is encharge to manage the navigation stack and coordinate the transitions between different views. It abstracts away the navigation details from the views, allowing them to focus on their specific features such as:
Name
Parametes
Description
navigate(_)
-
to:Route
, -
transitionStyle:NavigationTransitionStyle?
, default:automatic
, -
animated:Bool?
, defaulttrue
, -
completion:(() -> Void)?
, default:nil
Allows you to navigate among the views that were defined in the Route. The types of presentation are Push, Modal, ModalFullScreen and Custom.
navigate(_)
-
to:Coordinator
-
animated:Bool?
, defaulttrue
,
Allows you to navigate among the Coordinators. It calls the
start()
function.
startFlow(_)
-
to:Route
-
transitionStyle:NavigationTransitionStyle?
, default:automatic
, -
animated:Bool?
, defaulttrue
Cleans the navigation stack and runs the navigation flow.
present(_)
-
_ view:ViewType
-
animated:Bool?
, defaulttrue
, -
completion:(() -> Void)?
, default:nil
Presents a view modally.
pop(_)
-
animated:Bool?
, defaulttrue
,
Pops the top view from the navigation stack and updates the display.
popToRoot(_)
-
animated:Bool?
, defaulttrue
, -
completion:(() -> Void)?
, default:nil
Pops all the views on the stack except the root view and updates the display.
dismiss(_)
-
animated:Bool?
, defaulttrue
,
Dismisses the view that was presented modally by the view.
popToView(_)
-
_ view:T
-
animated:Bool?
, defaulttrue
,
Pops views until the specified view is at the top of the navigation stack. Example:
router.popToView(MyView.self)
finishFlow(_)
-
animated:Bool?
, defaulttrue
, -
completion:(() -> Void)?
, default:nil
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.
#### NavigationCoordinator
Acts as a separate entity from the views, decoupling the navigation logic from the presentation logic. This separation of concerns allows the views to focus solely on their specific functionalities, while the Navigation Coordinator takes charge of the app's overall navigation flow. Some features are:
Name
Parametes
Description
router
Variable of Route type which allow performs action router.
forcePresentation(_)
-
route:Route
-
transitionStyle:NavigationTransitionStyle?
, default:automatic
, -
animated:Bool?
, defaulttrue
, -
mainCoordinator:Coordinator?
, default:mainCoordinator
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(_)
-
mainCoordinator:Coordinator?
, defaultmainCoordinator
,
Returns the coordinator that is at the top of the coordinator stack.
restartApp(_)
-
mainCoordinator:Coordinator?
, defaultmainCoordinator
, -
animated:Bool?
, defaulttrue
, -
completion:(() -> Void)?
, default:nil
Cleans the navigation stack and runs the main coordinator navigation flow.
#### TabbarCoordinator
Acts as a separate entity from the views, decoupling the navigation logic from the presentation logic. This separation of concerns allows the views to focus solely on their specific functionalities, while the Navigation Coordinator takes charge of the app's overall navigation flow. It is encharge if build the tab bar (UITabbarController) with the coordinators that were defined in its route, some features are:
Name
Parametes
Description
currentPage
Returns the current page selected.
getCoordinatorSelected()
-
mainCoordinator:Coordinator?
, defaultmainCoordinator
,
Returns the coordinator selected that is associated to the selected tab
setPages(_)
-
_values:[PAGE]?
, defaultmainCoordinator
, -
completion:(() -> Void)?
, default:nil
Updates the page set.
forcePresentation(_)
-
animated:Bool?
, defaulttrue
, -
mainCoordinator:Coordinator?
, default:mainCoordinator
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.
_____
### Installation 💾
SPM
Open Xcode and your project, click File / Swift Packages / Add package dependency... . In the textfield "Enter package repository URL", write and press Next twice
_____
## Contributing
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.