-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(tab bar coordinator): add support for transition action when …
…starting the flow
- Loading branch information
1 parent
a7374f5
commit 2a87d7c
Showing
4 changed files
with
90 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Sources/SwiftUICoordinator/TabBarCoordinator/TabBarCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import SwiftUI | ||
|
||
public typealias TabBarRouting = Coordinator & TabBarCoordinator | ||
|
||
/// A protocol for managing a tab bar interface in an application. | ||
@MainActor | ||
public protocol TabBarCoordinator: ObservableObject { | ||
associatedtype Route: TabBarNavigationRoute | ||
|
||
var navigationController: NavigationController { get } | ||
var tabBarController: UITabBarController { get } | ||
var tabs: [Route] { get } | ||
/// This method should be called to show the `tabBarController` | ||
func start(with action: TransitionAction) | ||
} | ||
|
||
public extension TabBarCoordinator where Self: RouterViewFactory { | ||
func start(with action: TransitionAction = .push(animated: true)) { | ||
tabBarController.viewControllers = views(for: tabs) | ||
|
||
switch action { | ||
case .push(let animated): | ||
navigationController.pushViewController(tabBarController, animated: animated) | ||
case .present(let animated, let modalPresentationStyle, let delegate, let completion): | ||
present( | ||
viewController: tabBarController, | ||
animated: animated, | ||
modalPresentationStyle: modalPresentationStyle, | ||
delegate: delegate, | ||
completion: completion | ||
) | ||
} | ||
} | ||
|
||
// MARK: - Private methods | ||
|
||
private func present( | ||
viewController: UITabBarController, | ||
animated: Bool, | ||
modalPresentationStyle: UIModalPresentationStyle, | ||
delegate: UIViewControllerTransitioningDelegate?, | ||
completion: (() -> Void)? | ||
) { | ||
if let delegate { | ||
viewController.modalPresentationStyle = .custom | ||
viewController.transitioningDelegate = delegate | ||
} else { | ||
viewController.modalPresentationStyle = modalPresentationStyle | ||
} | ||
|
||
navigationController.present(viewController, animated: animated, completion: completion) | ||
} | ||
} |