Skip to content

Commit

Permalink
Add UISwitch on's property
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcrodrigues committed Apr 7, 2016
1 parent f238507 commit 32e55c4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Rex.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
4238D5961B4D5950008534C0 /* NSTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4238D5951B4D5950008534C0 /* NSTextField.swift */; };
7D2AA99B1CB6EFEB008AB5C9 /* UISwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2AA99A1CB6EFEB008AB5C9 /* UISwitch.swift */; };
7D2AA99D1CB6F275008AB5C9 /* UISwitchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2AA99C1CB6F275008AB5C9 /* UISwitchTests.swift */; };
8289A2E11BD7EF1F0097FB60 /* UIImageViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */; };
8289A2E31BD7EF740097FB60 /* UIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8289A2E21BD7EF740097FB60 /* UIImageView.swift */; };
8289A2E51BD7F6DD0097FB60 /* UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8289A2E41BD7F6DD0097FB60 /* UIView.swift */; };
Expand Down Expand Up @@ -173,6 +175,8 @@
4238D5951B4D5950008534C0 /* NSTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSTextField.swift; path = AppKit/NSTextField.swift; sourceTree = "<group>"; };
5173EBC51B625A2600C9B48E /* UIBarItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIBarItem.swift; sourceTree = "<group>"; };
5173EBC71B625A6800C9B48E /* UIBarButtonItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIBarButtonItem.swift; sourceTree = "<group>"; };
7D2AA99A1CB6EFEB008AB5C9 /* UISwitch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UISwitch.swift; sourceTree = "<group>"; };
7D2AA99C1CB6F275008AB5C9 /* UISwitchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UISwitchTests.swift; sourceTree = "<group>"; };
8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewTests.swift; sourceTree = "<group>"; };
8289A2E21BD7EF740097FB60 /* UIImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageView.swift; sourceTree = "<group>"; };
8289A2E41BD7F6DD0097FB60 /* UIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -393,6 +397,7 @@
9DA915A31CA6301C003723B9 /* UIDatePicker.swift */,
8289A2E21BD7EF740097FB60 /* UIImageView.swift */,
D86FFBD71B34B242001A89B3 /* UILabel.swift */,
7D2AA99A1CB6EFEB008AB5C9 /* UISwitch.swift */,
C7932E811C4B3EDB00086F3C /* UITextField.swift */,
8289A2E41BD7F6DD0097FB60 /* UIView.swift */,
);
Expand Down Expand Up @@ -429,6 +434,7 @@
9DA915A51CA63046003723B9 /* UIDatePickerTests.swift */,
8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */,
D8F073141B861B3A0047D546 /* UILabelTests.swift */,
7D2AA99C1CB6F275008AB5C9 /* UISwitchTests.swift */,
C7932E851C4B420A00086F3C /* UITextFieldTests.swift */,
8289A2E61BD7F7730097FB60 /* UIViewTests.swift */,
);
Expand Down Expand Up @@ -770,6 +776,7 @@
D8F0973E1B17F30D002E15BA /* NSUserDefaults.swift in Sources */,
D834572D1AFEE45B0070616A /* Signal.swift in Sources */,
D8E4A6211B7BBB2100EAD8A8 /* UIBarItem.swift in Sources */,
7D2AA99B1CB6EFEB008AB5C9 /* UISwitch.swift in Sources */,
D8A454071BD26A1A00C9E790 /* Property.swift in Sources */,
D8E4A6201B7BBB1600EAD8A8 /* UIBarButtonItem.swift in Sources */,
D8F097451B17F3C8002E15BA /* NSObject.swift in Sources */,
Expand All @@ -789,6 +796,7 @@
D8F0974B1B17F5E2002E15BA /* NSObjectTests.swift in Sources */,
8289A2E81BD7F7900097FB60 /* UIViewTests.swift in Sources */,
D8F073161B863CE70047D546 /* UILabelTests.swift in Sources */,
7D2AA99D1CB6F275008AB5C9 /* UISwitchTests.swift in Sources */,
C7932E871C4B42F500086F3C /* UITextFieldTests.swift in Sources */,
8295FD8A1B87352D007C9000 /* UIButtonTests.swift in Sources */,
D8A4540A1BD2772700C9E790 /* PropertyTests.swift in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions Source/UIKit/UISwitch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UISwitch.swift
// Rex
//
// Created by David Rodrigues on 07/04/16.
// Copyright © 2016 Neil Pankey. All rights reserved.
//

import ReactiveCocoa
import UIKit

extension UISwitch {

/// Wraps a switch's `on` state in a bindable property.
public var rex_on: MutableProperty<Bool> {

let property = associatedProperty(self, key: &onKey, initial: { $0.on }, setter: { $0.on = $1 })

property <~ rex_controlEvents(.ValueChanged)
.map { $0 as? UISwitch }
.ignoreNil()
.map { $0.on }

return property
}

}

private var onKey: UInt8 = 0
28 changes: 28 additions & 0 deletions Tests/UIKit/UISwitchTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// UISwitchTests.swift
// Rex
//
// Created by David Rodrigues on 07/04/16.
// Copyright © 2016 Neil Pankey. All rights reserved.
//

import XCTest
import ReactiveCocoa
import Result

class UISwitchTests: XCTestCase {

func testOnProperty() {
let s = UISwitch(frame: CGRectZero)
s.on = false

let (pipeSignal, observer) = Signal<Bool, NoError>.pipe()
s.rex_on <~ SignalProducer(signal: pipeSignal)

observer.sendNext(true)
XCTAssertTrue(s.on)
observer.sendNext(false)
XCTAssertFalse(s.on)
}

}

0 comments on commit 32e55c4

Please sign in to comment.