Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Reactive bindings for UIBarButtonItem #7

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ReactiveUIKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
782B49D81C36D52100E5979D /* UIBarButtonItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 782B49D71C36D52100E5979D /* UIBarButtonItem.swift */; };
EC9549321BF1D8A7000FC2BF /* UICollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC9549311BF1D8A7000FC2BF /* UICollectionView.swift */; };
EC9549341BF1DC52000FC2BF /* UIControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC9549331BF1DC52000FC2BF /* UIControl.swift */; };
EC9549361BF1DE78000FC2BF /* UIButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC9549351BF1DE78000FC2BF /* UIButton.swift */; };
Expand Down Expand Up @@ -66,6 +67,7 @@
/* Begin PBXFileReference section */
16E125A11BF5056C00543EFB /* ReactiveUIKit.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = ReactiveUIKit.podspec; sourceTree = "<group>"; };
16E125A21BF5056C00543EFB /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
782B49D71C36D52100E5979D /* UIBarButtonItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIBarButtonItem.swift; sourceTree = "<group>"; };
EC9549311BF1D8A7000FC2BF /* UICollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UICollectionView.swift; sourceTree = "<group>"; };
EC9549331BF1DC52000FC2BF /* UIControl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIControl.swift; sourceTree = "<group>"; };
EC9549351BF1DE78000FC2BF /* UIButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +167,7 @@
children = (
ECB7A5441BEB71E10034053A /* ReactiveUIKit.h */,
ECB7A5631BEB72380034053A /* UIView.swift */,
782B49D71C36D52100E5979D /* UIBarButtonItem.swift */,
ECB7A55C1BEB72380034053A /* UIBarItem.swift */,
ECB7A55D1BEB72380034053A /* UIImageView.swift */,
ECB7A55E1BEB72380034053A /* UILabel.swift */,
Expand Down Expand Up @@ -377,6 +380,7 @@
EC95493A1BF1E12E000FC2BF /* UISegmentedControl.swift in Sources */,
EC9549341BF1DC52000FC2BF /* UIControl.swift in Sources */,
EC95493E1BF1E286000FC2BF /* UISwitch.swift in Sources */,
782B49D81C36D52100E5979D /* UIBarButtonItem.swift in Sources */,
ECB7A56C1BEB72380034053A /* UIView.swift in Sources */,
ECB7A5651BEB72380034053A /* UIBarItem.swift in Sources */,
ECB7A5691BEB72380034053A /* UIProgressView.swift in Sources */,
Expand Down
63 changes: 63 additions & 0 deletions ReactiveUIKit/UIBarButtonItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// UIBarButtonItem.swift
// ReactiveUIKit
//
// Created by Ivan Misuno on 01/01/16.
// Copyright © 2016 Srdan Rasic. All rights reserved.
//

import UIKit
import ReactiveKit
import ReactiveFoundation

@objc class RKUIBarButtonItemHelper: NSObject
{
weak var barButtonItem: UIBarButtonItem?
let observer: Void -> Void

init(barButtonItem: UIBarButtonItem, observer: Void -> Void) {
self.barButtonItem = barButtonItem
self.observer = observer
super.init()
if barButtonItem.target != nil {
NSLog("Hijacking existing UIBarButtonItem's target and action: \(barButtonItem)")
}
barButtonItem.target = self
barButtonItem.action = Selector("eventHandler:")
}

func eventHandler(sender: AnyObject) {
observer()
}

deinit {
barButtonItem?.target = nil
}
}

extension UIBarButtonItem {

private struct AssociatedKeys {
static var BarButtonItemActionKey = "r_BarButtonItemActionKey"
static var BarButtonItemBondHelperKey = "r_BarButtonItemBondHelperKey"
}

public var rAction: ActiveStream<Void> {
if let rAction: AnyObject = objc_getAssociatedObject(self, &AssociatedKeys.BarButtonItemActionKey) {
return rAction as! ActiveStream<Void>
} else {
var capturedObserver: (Void -> Void)! = nil

let rAction = ActiveStream<Void> { observer in
capturedObserver = observer
return nil
}

let barButtonItemHelper = RKUIBarButtonItemHelper(barButtonItem: self, observer: capturedObserver)

objc_setAssociatedObject(self, &AssociatedKeys.BarButtonItemBondHelperKey, barButtonItemHelper, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
objc_setAssociatedObject(self, &AssociatedKeys.BarButtonItemActionKey, rAction, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return rAction
}
}
}