Swiftpack.co -  RomainRoche/Coordinator as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
RomainRoche/Coordinator
Navigation pattern for iOS apps.
.package(url: "https://github.com/RomainRoche/Coordinator.git", from: "0.0.1")

Coordinator

The Coordinator package proposes an alternative to using segues to navigate between screens in iOS apps. The coordinator also provides a way to share data and logic between view controllers of the same flow.

This is a very early draft. Don't hesitate to give your feedbacks!

Use a Coordinator

Implement a Coordinator

class MyCoordinator: Coordinator {
    
    let storyboardName = "my-storyboard"
    var navigationController = UINavigationController()
    var parent: Coordinator?
    
}

Implement a Coordinated view controller

A view controller subclass needs to implement the Coordinated protocol to be used with a Coordinator implementation.

class MyViewController: UIViewController, Coordinated {

    typealias CoordinatorType = MyCoordinator
    var coordinator = MyCoordinator()

}

A Coordinated view controller will also implement the Storyboarded protocol of the framework. This is a shortcut allowing to instantiate a view controller from a storyboard.

To do so:

  • In the "my-storyboard" storyboard used by MyCoordinator add a View Controller
  • Set its class to MyViewController in the identity inspector
  • Set its Storyboard ID to MyViewController too

Operations from a Coordinated view controller.

In the following example MainViewController and OtherViewController both implement Coordinated with the same CoordinatorType typealias.

class MainViewController: Coordinated {

    typealias CoordinatorType = MyCoordinator
    var coordinator = MyCoordinator()
    
    func goToOther() {
        let other = self.coordinator.push(OtherViewController.self)
    }
    
    func presentOther() {
        let other = self.coordinator.present(OtherViewController.self)
    }

}

MainViewController and OtherViewController will then have the same instance of MyCoordinator and can then share properties through it.

Customizing view controller

When pushing or presenting, the Coordinated view controller created is returned, allowing you to set what you need on it.

let other = self.coordinator.present(OtherViewController.self)
other.doSetup()

NOTE The returned view controller can be ignored.

// no reference to the returned view controller
self.coordinator.present(OtherViewController.self)

Use another Coordinator

A Coordinator can push another coordinator, or present it modally.

Push in the same navigation controller

class MainViewController: Coordinated {

    typealias CoordinatorType = MyCoordinator
    var coordinator = MyCoordinator()
    
    func goToNextCoordinator() {
        self.coordinator.push(
            coordinator: NextCoordinator(),
            coordinated: NextViewController.self
        )
    }

}

Present another coordinator

class MainViewController: Coordinated {

    typealias CoordinatorType = MyCoordinator
    var coordinator = MyCoordinator()
    
    func presentNextCoordinator() {
        self.coordinator.present(
            coordinator: NextCoordinator(),
            coordinated: NextViewController.self
        )
    }

}

The presented coordinator can the be dismissed.

nextCoordinator.dismiss()

GitHub

link
Stars: 1
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

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