All Projects → marty-suzuki → Noticeobservekit

marty-suzuki / Noticeobservekit

Licence: mit
NoticeObserveKit is type-safe NotificationCenter wrapper.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Noticeobservekit

Alley
Essential `URLSessionDataTask` micro-wrapper for communication with HTTP(S) web services, with built-in automatic request retries.
Stars: ✭ 137 (-6.8%)
Mutual labels:  tvos, watchos
Contentful.swift
A delightful Swift interface to Contentful's content delivery API.
Stars: ✭ 132 (-10.2%)
Mutual labels:  tvos, watchos
Ios Samples
Xamarin.iOS sample apps
Stars: ✭ 1,501 (+921.09%)
Mutual labels:  tvos, watchos
Iconic
🎨 Auto-generated icon font library for iOS, watchOS and tvOS
Stars: ✭ 1,567 (+965.99%)
Mutual labels:  tvos, watchos
Ducttape
📦 KeyPath dynamicMemberLookup based syntax sugar for Swift.
Stars: ✭ 138 (-6.12%)
Mutual labels:  tvos, watchos
Diff
Simple diff library in pure Swift
Stars: ✭ 110 (-25.17%)
Mutual labels:  tvos, watchos
Mapbox Directions Swift
Traffic-aware directions and map matching in Swift on iOS, macOS, tvOS, watchOS, and Linux
Stars: ✭ 115 (-21.77%)
Mutual labels:  tvos, watchos
Conbini
Publishers, operators, and subscribers to supplement Combine.
Stars: ✭ 109 (-25.85%)
Mutual labels:  tvos, watchos
Surmagic
🚀 The better way to deal with Binary Frameworks on iOS, Mac Catalyst, tvOS, macOS, and watchOS. Create XCFrameworks with ease.
Stars: ✭ 119 (-19.05%)
Mutual labels:  tvos, watchos
Swiftui Kit
A SwiftUI system components and interactions demo app
Stars: ✭ 1,733 (+1078.91%)
Mutual labels:  tvos, watchos
Cloudkitgdpr
Framework for allowing users to manage data stored in iCloud
Stars: ✭ 126 (-14.29%)
Mutual labels:  tvos, watchos
Color
Color utilities for macOS, iOS, tvOS, and watchOS
Stars: ✭ 145 (-1.36%)
Mutual labels:  tvos, watchos
Swiftui Shapes
🚀 Collection of SwiftUI shapes
Stars: ✭ 137 (-6.8%)
Mutual labels:  tvos, watchos
Persistencekit
Store and retrieve Codable objects to various persistence layers, in a couple lines of code!
Stars: ✭ 121 (-17.69%)
Mutual labels:  tvos, watchos
Swift Sdk
LeanCloud Swift SDK
Stars: ✭ 110 (-25.17%)
Mutual labels:  tvos, watchos
Articles Zh Hans
Articles for NSHipster.cn
Stars: ✭ 113 (-23.13%)
Mutual labels:  tvos, watchos
Swifterswift
A handy collection of more than 500 native Swift extensions to boost your productivity.
Stars: ✭ 10,706 (+7182.99%)
Mutual labels:  tvos, watchos
Sniffer
Networking activity logger for Swift
Stars: ✭ 108 (-26.53%)
Mutual labels:  tvos, watchos
Mapboxgeocoder.swift
Address search and reverse geocoding in Swift or Objective-C on iOS, macOS, tvOS, and watchOS
Stars: ✭ 115 (-21.77%)
Mutual labels:  tvos, watchos
Sqift
Powerful Swift wrapper for SQLite
Stars: ✭ 119 (-19.05%)
Mutual labels:  tvos, watchos

NoticeObserveKit

Version License Carthage compatible Platform

NoticeObserveKit is type-safe NotificationCenter wrapper.

// .keyboardWillShow is a static property.
Notice.Center.default.observe(name: .keyboardWillShow) { keyboardInfo in
    // In this case, keyboardInfo is UIKeyboardInfo type.
    // It is inferred from a generic parameter of Notice.Name<Value>.
    print(keyboardInfo)
}
// pool is Notice.ObserverPool.
// If pool is released, Notice.Observes are automatically removed.
.invalidated(by: pool)

Usage

First of all, you need to implement Notice.Name<T> like this. T is type of value in notification.userInfo.

extension Notice.Names {
    static let keyboardWillShow = Notice.Name<UIKeyboardInfo>(UIResponder.keyboardWillShowNotification)
}

If you define custom object, you need to implement that with NoticeUserInfoDecodable protocol. To confirm this protocol, you must implement init?(info: [AnyHashable : Any]) and func dictionaryRepresentation() -> [AnyHashable : Any].

struct UIKeyboardInfo: NoticeUserInfoDecodable {
    let frame: CGRect
    let animationDuration: TimeInterval
    let animationCurve: UIViewAnimationOptions

    init?(info: [AnyHashable : Any]) {
        guard
            let frame = (info[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
            let duration = info[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval,
            let curve = info[UIKeyboardAnimationCurveUserInfoKey] as? UInt
        else {
            return nil
        }
        self.frame = frame
        self.animationDuration = duration
        self.animationCurve = UIViewAnimationOptions(rawValue: curve)
    }
}

Usage for under v0.4.0 is documents/v0_4_0.

Customization

If you can post custom Notification like this.

extension Notice.Names {
    static let navigationControllerDidShow = Notice.Name<NavigationControllerContent>(name: "navigationControllerDidShow")
}

let content = NavigationControllerContent(viewController: viewController, animated: animated)
Notice.Center.default.post(name: .navigationControllerDidShow, value: content)

You can invalidate manually like this.

let observer = Notice.Center.default.observe(name: .keyboardWillShow) { keyboardInfo in
    print(keyboardInfo)
}
observer.invalidate()

You can use vi NotificationCenter.

NotificationCenter.default.nok.observe(name: .keyboardWillShow) { keyboardInfo in
    print(keyboardInfo)
}
.invalidated(by: pool)

Sample

import UIKit
import NoticeObserveKit

class ViewController: UIViewController {
    private let searchBar = UISearchBar(frame: .zero)
    private var pool = Notice.ObserverPool()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        navigationItem.titleView = searchBar

        configureObservers()
    }

    private func configureObservers() {
        Notice.Center.default.observe(name: .keyboardWillShow) {
            print("UIKeyboard will show = \($0)")
        }.invalidated(by: pool)

        Notice.Center.default.observe(name: .keyboardWillHide) {
            print("UIKeyboard will hide = \($0)")
        }.invalidated(by: pool)
    }
}

Requirements

  • Swift 5
  • Xcode 10.2 or greater
  • iOS 10.0 or greater
  • tvOS 10.0 or greater
  • macOS 10.10 or greater
  • watchOS 3.0 or greater

Installation

CocoaPods

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

pod "NoticeObserveKit"

Carthage

If you’re using Carthage, simply add NoticeObserveKit to your Cartfile:

github "marty-suzuki/NoticeObserveKit"

Make sure to add NoticeObserveKit.framework to "Linked Frameworks and Libraries" and "copy-frameworks" Build Phases.

Author

marty-suzuki, [email protected]

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].