A library of Coordinator Patterns that are easy to use for dependency injections in applications written on pure Swift for iOS. Coordinator pattern This is an easy way to inject a presenter (UINavigationController) into each Navigator, rather than a complex child structure like other open sources.
FlowInject is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'FlowInject'
To use Navigator from the launch of the app, make sure to create the app’s window programmatically in AppDelegate.swift
Then, set the Navigator as the root of the window’s view hierarchy in the AppDelegate.didFinishLaunching
.
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let appNavigator = AppNavigator(in: self.window)
return true
}
}
First, create an enum of the Route
type.
The cases of this Enum are used as paths.
Then, the Navigator
is inherited from the Navigator class
and the Route
type is designated as the generic type.
After inheriting Navigator, inject the received presenter
into initializer, Using navigate(to destination:)
, insert the service and the presenter to enable scene movement.
In the case of screen movement, it did not implement logic such as transition (push, present, pop, disiss) because each developer has different custom needs. :]
I'll leave it up to you.
enum MainRoute: Route {
case detail(content: String)
}
class MainNavigator: Navigator<MainRoute> {
override init(with presenter: UINavigationController?) {
super.init()
self.presenter = presenter
}
func navigate(to destination: MainRoute) {
switch destination {
case .detail(let content):
let viewController = DetailViewController(contentText: content,
detailNavigator:DetailNavigator(with: presenter))
presenter?.pushViewController(viewController, animated: true)
}
}
}
To get more information about FlowInject, Check out the example project
seungjin, [email protected]
FlowInject is available under the MIT license. See the LICENSE file for more info.