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 #97 from UrbanCompass/tap-multiple-subscriptions-fix
Browse files Browse the repository at this point in the history
Fix Tap Multiple Subscriptions
  • Loading branch information
russellbstephens authored Aug 29, 2018
2 parents 8d78c95 + 329906b commit 91dd2bf
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Snail.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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 */; };
24CF1FA81EF875A400F34234 /* URLSessionExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24CF1FA71EF875A400F34234 /* URLSessionExtension.swift */; };
24CF1FA81EF875A400F34234 /* URLSessionExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24CF1FA71EF875A400F34234 /* URLSessionExtensions.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 @@ -49,7 +49,7 @@
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>"; };
24CF1FA71EF875A400F34234 /* URLSessionExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionExtension.swift; sourceTree = "<group>"; };
24CF1FA71EF875A400F34234 /* URLSessionExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionExtensions.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 @@ -163,7 +163,7 @@
CBE54E6C1DFB6A910008DD64 /* UIControlExtensions.swift */,
2421BA711E09801000EA9064 /* UIGestureRecognizerExtensions.swift */,
CBE54E6E1DFB797F0008DD64 /* UIViewExtensions.swift */,
24CF1FA71EF875A400F34234 /* URLSessionExtension.swift */,
24CF1FA71EF875A400F34234 /* URLSessionExtensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -306,7 +306,7 @@
2421BA741E0992D400EA9064 /* NotificationCenterExtensions.swift in Sources */,
CB2936771DFE151B00792E6B /* Just.swift in Sources */,
241F15581E03124600DD70A2 /* UIBarButtonItemExtensions.swift in Sources */,
24CF1FA81EF875A400F34234 /* URLSessionExtension.swift in Sources */,
24CF1FA81EF875A400F34234 /* URLSessionExtensions.swift in Sources */,
2408FA902112A15900B9F59E /* Scheduler.swift in Sources */,
CBE54E601DFB39510008DD64 /* Event.swift in Sources */,
CBE54E631DFB39510008DD64 /* Variable.swift in Sources */,
Expand Down
14 changes: 8 additions & 6 deletions Snail/Extensions/UIBarButtonItemExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
import UIKit

extension UIBarButtonItem {
private static var observableKey = "ObservableKey"
private static var observableKey = "com.compass.Snail.UIBarButtonItem.ObservableKey"

public var tap: Observable<Void> {
private var tapObservable: Observable<Void> {
if let observable = objc_getAssociatedObject(self, &UIBarButtonItem.observableKey) as? Observable<Void> {
return observable
}
let observable = Observable<Void>()
objc_setAssociatedObject(self, &UIBarButtonItem.observableKey, observable, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return observable
}

public var tap: Observable<Void> {
target = self
action = #selector(observableHandler(_:))
return observable
return tapObservable
}

@objc private func observableHandler(_ sender: UIBarButtonItem) {
if let observable = objc_getAssociatedObject(self, &UIBarButtonItem.observableKey) as? Observable<Void> {
observable.on(.next(()))
}
tapObservable.on(.next(()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Snail/Extensions/UIControlExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import UIKit

public extension UIControl {
private static var observableKey = "ObservableKey"
private static var observableKey = "com.compass.Snail.UIControl.ObservableKey"

public func controlEvent(_ controlEvents: UIControlEvents) -> Observable<Void> {
if let observable = objc_getAssociatedObject(self, &UIControl.observableKey) as? Observable<Void> {
Expand Down
2 changes: 1 addition & 1 deletion Snail/Extensions/UIGestureRecognizerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import UIKit

extension UIGestureRecognizer {
private static var observableKey = "ObservableKey"
private static var observableKey = "com.compass.Snail.UIGestureRecognizer.ObservableKey"

public func asObservable() -> Observable<UIGestureRecognizerState> {
if let observable = objc_getAssociatedObject(self, &UIGestureRecognizer.observableKey) as? Observable<UIGestureRecognizerState> {
Expand Down
16 changes: 13 additions & 3 deletions Snail/Extensions/UIViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@
import UIKit

public extension UIView {
private static var observableKey = "com.compass.Snail.UIView.ObservableKey"

public func observe(event: Notification.Name) -> Observable<Notification> {
return NotificationCenter.default.observeEvent(event)
}

private var tapObservable: Observable<Void> {
if let observable = objc_getAssociatedObject(self, &UIView.observableKey) as? Observable<Void> {
return observable
}
let observable = Observable<Void>()
objc_setAssociatedObject(self, &UIView.observableKey, observable, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return observable
}

public var tap: Observable<Void> {
if let control = self as? UIControl {
return control.controlEvent(.touchUpInside)
}
let tap = UITapGestureRecognizer()
addGestureRecognizer(tap)
let result = Observable<Void>()
tap.asObservable().subscribe(onNext: { _ in result.on(.next(())) })
return result
tap.asObservable().subscribe(onNext: { [weak self] _ in self?.tapObservable.on(.next(())) })
return tapObservable
}

public var keyboardHeightWillChange: Observable<(height: CGFloat, duration: Double)> {
Expand Down
4 changes: 2 additions & 2 deletions SnailTests/Extensions/NotificationCenterExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import XCTest
@testable import Snail

class NotificationCenterTests: XCTestCase {
func testNotificaiton() {
func testNotification() {
let exp = expectation(description: "notification")
let notificationName = Notification.Name.UIKeyboardWillShow
var notifcation: Notification?
Expand All @@ -22,7 +22,7 @@ class NotificationCenterTests: XCTestCase {
}
}

func testMultipleNotificaitons() {
func testMultipleNotifications() {
let willHide = expectation(description: "notification")
var gotWillShow = false
var gotWillHide = false
Expand Down

0 comments on commit 91dd2bf

Please sign in to comment.