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

Expose signal for .primaryActionTriggered (on tvOS) #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 35 additions & 15 deletions Signals/ios/UIControl+Signals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,42 @@ public extension UIControl {
var onEditingDidEndOnExit: Signal<Void> {
return getOrCreateSignalForUIControlEvent(.editingDidEndOnExit)
}

#if os(tvOS)
/// A signal that fires for the primary action triggered (used in tvOS) control event.
var onPrimaryActionTriggered: Signal<Void> {
return getOrCreateSignalForUIControlEvent(.primaryActionTriggered)
}
#endif

// MARK: - Private interface

private struct AssociatedKeys {
static var SignalDictionaryKey = "signals_signalKey"
}

private static let eventToKey: [UIControl.Event: NSString] = [
.touchDown: "TouchDown",
.touchDownRepeat: "TouchDownRepeat",
.touchDragInside: "TouchDragInside",
.touchDragOutside: "TouchDragOutside",
.touchDragEnter: "TouchDragEnter",
.touchDragExit: "TouchDragExit",
.touchUpInside: "TouchUpInside",
.touchUpOutside: "TouchUpOutside",
.touchCancel: "TouchCancel",
.valueChanged: "ValueChanged",
.editingDidBegin: "EditingDidBegin",
.editingChanged: "EditingChanged",
.editingDidEnd: "EditingDidEnd",
.editingDidEndOnExit: "EditingDidEndOnExit"]
private static let eventToKey: [UIControl.Event: NSString] = {
var mapping: [UIControl.Event: NSString] = [
.touchDown: "TouchDown",
.touchDownRepeat: "TouchDownRepeat",
.touchDragInside: "TouchDragInside",
.touchDragOutside: "TouchDragOutside",
.touchDragEnter: "TouchDragEnter",
.touchDragExit: "TouchDragExit",
.touchUpInside: "TouchUpInside",
.touchUpOutside: "TouchUpOutside",
.touchCancel: "TouchCancel",
.valueChanged: "ValueChanged",
.editingDidBegin: "EditingDidBegin",
.editingChanged: "EditingChanged",
.editingDidEnd: "EditingDidEnd",
.editingDidEndOnExit: "EditingDidEndOnExit"
]
#if os(tvOS)
mapping[.primaryActionTriggered] = "PrimaryActionTriggered"
#endif
return mapping
}()

private func getOrCreateSignalForUIControlEvent(_ event: UIControl.Event) -> Signal<Void> {
guard let key = UIControl.eventToKey[event] else {
Expand Down Expand Up @@ -176,6 +190,12 @@ public extension UIControl {
@objc private dynamic func eventHandlerEditingDidEndOnExit() {
handleUIControlEvent(.editingDidEndOnExit)
}

#if os(tvOS)
@objc private dynamic func eventHandlerPrimaryActionTriggered() {
handleUIControlEvent(.primaryActionTriggered)
}
#endif
}

extension UIControl.Event: Hashable {
Expand Down
21 changes: 20 additions & 1 deletion SignalsTests/UIControl+SignalsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class UIControl_SignalsTests: XCTestCase {
var onEditingChangedCount = 0
var onEditingDidEndCount = 0
var onEditingDidEndOnExitCount = 0
#if os(tvOS)
var onPrimaryActionTriggeredCount = 0
#endif

button.onTouchDown.subscribe(with: self) { _ in
onTouchDownCount += 1
Expand Down Expand Up @@ -68,7 +71,13 @@ class UIControl_SignalsTests: XCTestCase {
button.onEditingDidEndOnExit.subscribe(with: self) { _ in
onEditingDidEndOnExitCount += 1
}


#if os(tvOS)
button.onPrimaryActionTriggered.subscribe(with: self) { _ in
onPrimaryActionTriggeredCount += 1
}
#endif

let events: [UIControl.Event] = [.touchDown, .touchDownRepeat, .touchDragInside, .touchDragOutside,
.touchDragEnter, .touchDragExit, .touchUpInside, .touchUpOutside,
.touchCancel, .valueChanged, .editingDidBegin, .editingChanged,
Expand All @@ -80,6 +89,12 @@ class UIControl_SignalsTests: XCTestCase {
button.perform(Selector(action))
}
}

#if os(tvOS)
for action in button.actions(forTarget: button, forControlEvent: .primaryActionTriggered)! {
button.perform(Selector(action))
}
#endif

XCTAssertEqual(onTouchDownCount, 1, "Should have triggered once")
XCTAssertEqual(onTouchDownRepeatCount, 1, "Should have triggered once")
Expand All @@ -95,6 +110,10 @@ class UIControl_SignalsTests: XCTestCase {
XCTAssertEqual(onEditingChangedCount, 1, "Should have triggered once")
XCTAssertEqual(onEditingDidEndCount, 1, "Should have triggered once")
XCTAssertEqual(onEditingDidEndOnExitCount, 1, "Should have triggered once")

#if os(tvOS)
XCTAssertEqual(onPrimaryActionTriggeredCount, 1, "Should have triggered once")
#endif
}
}

Expand Down