Stimulator 2.0.0

Stimulator 2.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0.2
SPMSupports SPM

Maintained by ukitaka.



  • By
  • yuki.takahashi

Custom event handling based on Responder Chain.

Usage

Create custom event and handler protocol

import Stimulator

struct ShowAlertEvent : Stimulator.Event {

    typealias Responder = ShowAlertResponder

        let title: String
        let message: String

        init(_ title: String, _ message: String) {
            self.title = title
                self.message = message
        }

    func stimulate(responder: Responder) {
        responder.showAlert(event: self)
    }
}

protocol ShowAlertResponder {

    func showAlert(event: ShowAlertEvent)

}

Generate event

Generates event in UIResponer subclass (e.g. UIView, UIViewController)

self.stimulate(event: ShowAlertEvent("title", "message"))

Handle event

class MyViewController: UIViewController, ShowAlertResponder {

    func showAlert(event: ShowAlertEvent) {
        let alert = UIAlertController(title: event.title, message: event.message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { _ in }))
        self.show(alert, sender: nil)
    }

}

You can use protocol extensions to provide a default implementation.

extension ShowAlertResponder where Self : UIViewController {

    func showActionSheet(event: ShowActionSheetEvent) {
        let alert = UIAlertController(title: event.title, message: event.message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { _ in }))
        self.showViewController(alert, sender: nil)
    }

}

Requirements

  • iOS 9.0+
  • Xcode8+

Installation

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

pod "Stimulator"

Author

yuki.takahashi, [email protected]

License

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