Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #37 from UrbanCompass/notification
Browse files Browse the repository at this point in the history
adding notification extension
  • Loading branch information
wesbillman authored Dec 20, 2016
2 parents b30cdf1 + 3dabf5e commit eefea40
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Snail.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
241F15581E03124600DD70A2 /* UIBarButtonItemExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241F15571E03124600DD70A2 /* UIBarButtonItemExtensions.swift */; };
2421BA721E09801000EA9064 /* UIGestureRecognizerExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2421BA711E09801000EA9064 /* UIGestureRecognizerExtensions.swift */; };
2421BA741E0992D400EA9064 /* NotificationCenterExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2421BA731E0992D400EA9064 /* NotificationCenterExtensions.swift */; };
2421BA771E09A78F00EA9064 /* NSNotificationCenterExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2421BA761E09A78F00EA9064 /* NSNotificationCenterExtensions.swift */; };
24FABD561DFEF670005CF84E /* Fail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24FABD551DFEF670005CF84E /* Fail.swift */; };
24FABD581DFEF7EC005CF84E /* FailTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24FABD571DFEF7EC005CF84E /* FailTests.swift */; };
24FABD5A1DFF0B48005CF84E /* Replay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24FABD591DFF0B48005CF84E /* Replay.swift */; };
Expand Down Expand Up @@ -40,6 +42,8 @@
/* Begin PBXFileReference section */
241F15571E03124600DD70A2 /* UIBarButtonItemExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIBarButtonItemExtensions.swift; sourceTree = "<group>"; };
2421BA711E09801000EA9064 /* UIGestureRecognizerExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIGestureRecognizerExtensions.swift; sourceTree = "<group>"; };
2421BA731E0992D400EA9064 /* NotificationCenterExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationCenterExtensions.swift; sourceTree = "<group>"; };
2421BA761E09A78F00EA9064 /* NSNotificationCenterExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSNotificationCenterExtensions.swift; path = Extensions/NSNotificationCenterExtensions.swift; sourceTree = "<group>"; };
24FABD551DFEF670005CF84E /* Fail.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fail.swift; sourceTree = "<group>"; };
24FABD571DFEF7EC005CF84E /* FailTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FailTests.swift; sourceTree = "<group>"; };
24FABD591DFF0B48005CF84E /* Replay.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Replay.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,6 +84,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
2421BA751E09A6D500EA9064 /* Extensions */ = {
isa = PBXGroup;
children = (
2421BA761E09A78F00EA9064 /* NSNotificationCenterExtensions.swift */,
);
name = Extensions;
sourceTree = "<group>";
};
CBE54E371DFB36DF0008DD64 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -124,6 +136,7 @@
CBE54E641DFB395A0008DD64 /* ObservableTests.swift */,
24FABD5B1DFF0BAF005CF84E /* ReplayTests.swift */,
CBE54E661DFB4F3F0008DD64 /* VariableTests.swift */,
2421BA751E09A6D500EA9064 /* Extensions */,
);
path = SnailTests;
sourceTree = "<group>";
Expand All @@ -135,6 +148,7 @@
CBE54E6E1DFB797F0008DD64 /* UIViewExtensions.swift */,
241F15571E03124600DD70A2 /* UIBarButtonItemExtensions.swift */,
2421BA711E09801000EA9064 /* UIGestureRecognizerExtensions.swift */,
2421BA731E0992D400EA9064 /* NotificationCenterExtensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -254,6 +268,7 @@
2421BA721E09801000EA9064 /* UIGestureRecognizerExtensions.swift in Sources */,
24FABD561DFEF670005CF84E /* Fail.swift in Sources */,
CBE54E611DFB39510008DD64 /* Observable.swift in Sources */,
2421BA741E0992D400EA9064 /* NotificationCenterExtensions.swift in Sources */,
CB2936771DFE151B00792E6B /* Just.swift in Sources */,
241F15581E03124600DD70A2 /* UIBarButtonItemExtensions.swift in Sources */,
CBE54E601DFB39510008DD64 /* Event.swift in Sources */,
Expand All @@ -272,6 +287,7 @@
CBE54E651DFB395A0008DD64 /* ObservableTests.swift in Sources */,
24FABD5C1DFF0BAF005CF84E /* ReplayTests.swift in Sources */,
CBE54E671DFB4F3F0008DD64 /* VariableTests.swift in Sources */,
2421BA771E09A78F00EA9064 /* NSNotificationCenterExtensions.swift in Sources */,
24FABD581DFEF7EC005CF84E /* FailTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
20 changes: 20 additions & 0 deletions Snail/Extensions/NotificationCenterExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright © 2016 Compass. All rights reserved.

import Foundation

extension NotificationCenter {
private static var observableKey = "ObservableKey"

func observableHandler(_ notification: NSNotification) {
if let observable = objc_getAssociatedObject(self, &NotificationCenter.observableKey) as? Observable<NSNotification> {
observable.on(.next(notification))
}
}

func observeEvent(_ name: NSNotification.Name?) -> Observable<NSNotification> {
let observable = Observable<NSNotification>()
objc_setAssociatedObject(self, &NotificationCenter.observableKey, observable, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
addObserver(self, selector: #selector(observableHandler(_:)), name: name, object: nil)
return observable
}
}
24 changes: 24 additions & 0 deletions SnailTests/Extensions/NSNotificationCenterExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2016 Compass. All rights reserved.

import UIKit
import XCTest
@testable import Snail

class NSNotificationCenterTests: XCTestCase {
func testNotificaiton() {
let exp = expectation(description: "notification")
let notificationName = NSNotification.Name.UIKeyboardWillShow
var notifcation: NSNotification?
let subject = NotificationCenter.default.observeEvent(notificationName)
subject.subscribe(onNext: { n in
notifcation = n
exp.fulfill()
})
NotificationCenter.default.post(name: notificationName, object: nil)
waitForExpectations(timeout: 3) { error in
XCTAssertNil(error)
XCTAssertNotNil(notifcation)
XCTAssert(notifcation?.name == notificationName)
}
}
}

0 comments on commit eefea40

Please sign in to comment.