Crex 0.1.0

Crex 0.1.0

Maintained by Marek Rogowski.



Crex 0.1.0

  • By
  • rogowskimark

Crex

CI Status

Version

License

Platform

Crex is an library for simple and type safe usage of Notification Center. Posting extra information is so handy due to code completion.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.3+

  • Swift 4.1

Installation

Crex is available through CocoaPods. To install

it, simply add the following line to your Podfile:

pod 'Crex'

Usage

Definig Event

import Crex

struct QueueStateChanged: CrexEvent {

    typealias PayloadType = QueueStateChanged.Payload
    static let notificationName = NSNotification.Name(rawValue: "MyApp.QueueStateChanged")
    static let transformation: CrexTransformation = nil

    struct Payload {
        let updatedAt = Date()
        let itemsLeft: Int
    }
}

Registering observer

Crex<QueueStateChanged>.addObserver { (_, queueState) in
    guard let queueState = queueState else {
        return
    }

    print("Queue updated: \(queueState.updatedAt), items left: \(queueState.itemsLeft)")
}

Posting event

Crex<QueueStateChanged>.post(QueueStateChanged.Payload(itemsLeft: 42))

Using system notifications

struct ApplicationDidBecomeActive: CrexEvent {
    typealias PayloadType = Void
    static let notificationName: NSNotification.Name = .UIApplicationDidBecomeActive
    static let transformation: CrexTransformation = nil
}

Crex<ApplicationDidBecomeActive>.addObserver { (_, _) in
    print("Application did become active")
}

Using transformation

Sometimes it is enough to just take some bits of data passed iside notification sent from system. Also in this approach we are not creating duplication when fetching data from userInfo dictionary.

struct KeyboardWillShow: CrexEvent {
    typealias PayloadType = KeyboardWillShow.KeyboardPayload
    static let notificationName: NSNotification.Name = .UIKeyboardWillShow

    static let transformation: CrexTransformation = { (userInfo: CrexUserInfo) in
        guard let keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect,
        let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval else {
        return nil
        }
        return KeyboardPayload(height: keyboardSize.height, animationDuration: animationDuration)
    }

    struct KeyboardPayload {
        let height: CGFloat
        let animationDuration: TimeInterval
    }
}

Crex<KeyboardWillShow>.addObserver { (_, keyboard) in
    guard let keyboard = keyboard else {
        return
    }

    print("Will show keyboard height: \(keyboard.height), duration: \(keyboard.animationDuration)")
}

Author

Marek Rogowski, [email protected]

License

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