-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f238507
commit 32e55c4
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |