Skip to content

muukii/Presenter

Repository files navigation

Presenter

CI Status Version License Platform Carthage compatible

Screen transition with safe and clean code.

With Presenter, you can…

  • Assure that the ViewController's requirements are met, such as a ViewModel to be injected.
  • Constrain transition types (push or present modal or both)

This library is recommended to be used together with Instantiatable.

Usage

Clean Screen Transition

MyViewController.Presenter(userID: "muukii").push(self.navigationController)
MyViewController.Presenter(userID: "muukii").present(self)

Advanced

MyViewController.Presenter(userID: "muukii").push(self.navigationController) { (transaction: PushTransaction<MyViewController> in

    // Pop    
    transaction.pop()

    // Get
    transaction.viewController
}

MyViewController.Presenter(userID: "muukii").present(self) { (transaction: ModalTransaction<MyViewController>) in

    // Pop    
    transaction.dismiss()

    // Get
    transaction.viewController
}

Create Presenter

Push

extension MyViewController {

    final class Presenter: PushPresenter {

        let userID: String

        init(userID: String) {
            self.userID = userID
        }

        func createViewController() -> MyViewController {
            let controller = MessagesViewController() // Init from Stroyboard or XIB
            controller.userID = userID
            return controller
        }  
        
        // Optional:
        
        public func willPush(viewController: MyViewController) {
        
        }
    
        public func didPush(viewController: MyViewController) {
        
        }
    }
}

Present

extension MyViewController {

    final class Presenter: ModalPresenter {

        let userID: String

        init(userID: String) {
            self.userID = userID
        }

        func parentController(viewController: UIViewController) -> UIViewController? {
            return UINavigationController(rootViewController: viewController)
        }

        func createViewController() -> MyViewController {
            let controller = MessagesViewController() // Init from Stroyboard or XIB
            controller.userID = userID
            return controller
        }   
        
        // Optional
        
        public func willPresent(viewController: MyViewController) {
        
        }
    
        public func didPresent(viewController: MyViewController) {
        
        }
    }
}

Present or Push

extension MyViewController {

    final class Presenter: PushPresenter, ModalPresenter {

        let userID: String

        init(userID: String) {
            self.userID = userID
        }

        func parentController(viewController: UIViewController) -> UIViewController? {
            // Call Present() only
            return UINavigationController(rootViewController: viewController)
        }

        func createViewController() -> MyViewController {
            let controller = MessagesViewController() // Init from Stroyboard or XIB
            controller.userID = userID
            return controller
        }    
        
        // Optional
        
        public func willPresent(viewController: MyViewController) {
        
        }
    
        public func didPresent(viewController: MyViewController) {
        
        }
        
        public func willPush(viewController: MyViewController) {
        
        }
    
        public func didPush(viewController: MyViewController) {
        
        }
    }
}

Requirements

Installation

Presenter is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Presenter"

Author

muukii, [email protected]

License

Presenter is available under the MIT license. See the LICENSE file for more info.