Swiftpack.co - difomiry/Coordinator as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by difomiry.
difomiry/Coordinator 0.1.4
A package that provides helpful stuff for coordinator pattern.
⭐️ 1
🕓 3 years ago
.package(url: "https://github.com/difomiry/Coordinator.git", from: "0.1.4")

Coordinator

A package that provides helpful stuff for coordinator pattern.

Usage

See sources or examples for more usage information.

Factory of Coordinators

You can use a type-erased AnyCoordinatable for factories.

protocol CoordinatorFactoring {
  func makeAppCoordinator(router: Router) -> AnyCoordinatable<Void, Void>
  func makeFooCoordinator(router: Router) -> AnyCoordinatable<FooCoordinatorInput, FooCoordinatorOutput>
  func makeBarCoordinator(router: Router) -> AnyCoordinatable<BarCoordinatorInput, BarCoordinatorOutput>
}

final class CoordinatorFactory {
  // Fields with services here.
  // Initializer for services is here too.
}

extension CoordinatorFactory: CoordinatorFactoring {

  func makeAppCoordinator(router: Router) -> AnyCoordinatable<Void, Void> {
    .init(AppCoordinator(router: Router, factory: self))
  }

  func makeFooCoordinator(router: Router) -> AnyCoordinatable<FooCoordinatorInput, FooCoordinatorOutput> {
    .init(FooCoordinator(router: router, factory: self, aService: aService))
  }

  func makeBarCoordinator(router: Router) -> AnyCoordinatable<BarCoordinatorInput, BarCoordinatorOutput> {
    .init(BarCoordinator(router: router, factory: self, bService: bService))
  }

}

Coordinators

Use coordinate method inside coordinator to coordinate child coordinators.

final class AppCoordinator: Coordinator {
  // Fields with services here.
  // Initializer for services is here too.
}

extension AppCoordinator: Coordinatable {

  func start(with _: Void, completion: CoordinatorCompletion<Void>?) {
    if authService.isAuthorized {
      startMain()
    } else {
      startAuth()
    }
  }

  private func startMain() {

    // Define the tabbar controller.
    let tabBarController = UITabBarController()

    // Replace the root view controller with the tabbar controller.
    router.set(tabBarController, animated: true)

    // Start coordination of the main screen.
    coordinate(to: factory.makeMainCoordinator(router: tabBarController))
  }

  private func startAuth() {

    // Define the navigation controller.
    let navigationController = UINavigationController()

    // Replace the root view controller with the navigation controller.
    router.set(navigationController, animated: true)

    // Start coordination of the auth screen.
    coordinate(to: factory.makeAuthCoordinator(router: navigationController)) { [unowned self] _ in
      self.startMain()
    }
  }

}

Requirements

  • Xcode 11.4
  • Swift 5.2

Installation

Coming soon.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics