Skip to content

Commit e7c17f0

Browse files
committed
Unified NSButton+Action and UIButton+Action
1 parent 42ad8b5 commit e7c17f0

File tree

3 files changed

+53
-114
lines changed

3 files changed

+53
-114
lines changed

Action.xcodeproj/project.pbxproj

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1FCDDA651EAC31EF006EB95B /* Action+Internal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E01DE28587007E1D0D /* Action+Internal.swift */; };
1313
1FCDDA661EAC31EF006EB95B /* AlertAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E51DE28587007E1D0D /* AlertAction.swift */; };
1414
1FCDDA671EAC31EF006EB95B /* UIBarButtonItem+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E61DE28587007E1D0D /* UIBarButtonItem+Action.swift */; };
15-
1FCDDA681EAC31EF006EB95B /* UIButton+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E71DE28587007E1D0D /* UIButton+Rx.swift */; };
1615
1FCDDA6C1EAC3221006EB95B /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FCDDA6A1EAC3221006EB95B /* RxCocoa.framework */; };
1716
1FCDDA6D1EAC3221006EB95B /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FCDDA6B1EAC3221006EB95B /* RxSwift.framework */; };
1817
1FCDDA8A1EAC329E006EB95B /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E21DE28587007E1D0D /* Action.swift */; };
@@ -24,10 +23,12 @@
2423
1FCDDAA11EAC33D3006EB95B /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FCDDA9F1EAC33D3006EB95B /* RxCocoa.framework */; };
2524
1FCDDAA21EAC33D3006EB95B /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FCDDAA01EAC33D3006EB95B /* RxSwift.framework */; };
2625
3D37A3961DB4A97C0028BC0E /* RxTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D9C98331DB4A87B004A9F7C /* RxTest.framework */; };
27-
3D3D42771F5DBACF008CF951 /* NSButton+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D3D42751F5DBAAE008CF951 /* NSButton+Action.swift */; };
2826
3D730DE31F673BE4008534D3 /* Control+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D730DE21F673BE4008534D3 /* Control+Action.swift */; };
2927
3D730DE41F673C64008534D3 /* Control+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D730DE21F673BE4008534D3 /* Control+Action.swift */; };
3028
3D730DE51F673C65008534D3 /* Control+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D730DE21F673BE4008534D3 /* Control+Action.swift */; };
29+
3D730DE91F673FD9008534D3 /* Button+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D730DE81F673FD9008534D3 /* Button+Action.swift */; };
30+
3D730DEA1F674043008534D3 /* Button+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D730DE81F673FD9008534D3 /* Button+Action.swift */; };
31+
3D730DEB1F674044008534D3 /* Button+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D730DE81F673FD9008534D3 /* Button+Action.swift */; };
3132
3DD965C01F5DC0E400C180FE /* Action.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FCDDA821EAC3295006EB95B /* Action.framework */; };
3233
3DD965C61F5DC2E100C180FE /* ActionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569F01DE288EB007E1D0D /* ActionTests.swift */; };
3334
3DD965C81F5DC2E700C180FE /* InputSubjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA2861C91ED6A41700BB327A /* InputSubjectTests.swift */; };
@@ -45,7 +46,6 @@
4546
7F0569EA1DE28587007E1D0D /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E21DE28587007E1D0D /* Action.swift */; };
4647
7F0569EC1DE28587007E1D0D /* AlertAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E51DE28587007E1D0D /* AlertAction.swift */; };
4748
7F0569ED1DE28587007E1D0D /* UIBarButtonItem+Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E61DE28587007E1D0D /* UIBarButtonItem+Action.swift */; };
48-
7F0569EE1DE28587007E1D0D /* UIButton+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569E71DE28587007E1D0D /* UIButton+Rx.swift */; };
4949
7F0569F51DE288EB007E1D0D /* ActionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569F01DE288EB007E1D0D /* ActionTests.swift */; };
5050
7F0569F61DE288EB007E1D0D /* AlertActionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569F11DE288EB007E1D0D /* AlertActionTests.swift */; };
5151
7F0569F71DE288EB007E1D0D /* BarButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F0569F21DE288EB007E1D0D /* BarButtonTests.swift */; };
@@ -124,8 +124,8 @@
124124
1FCDDA961EAC33B0006EB95B /* Action.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Action.framework; sourceTree = BUILT_PRODUCTS_DIR; };
125125
1FCDDA9F1EAC33D3006EB95B /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = "Carthage/Checkouts/RxSwift/build/Debug-watchos/RxCocoa.framework"; sourceTree = "<group>"; };
126126
1FCDDAA01EAC33D3006EB95B /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = "Carthage/Checkouts/RxSwift/build/Debug-watchos/RxSwift.framework"; sourceTree = "<group>"; };
127-
3D3D42751F5DBAAE008CF951 /* NSButton+Action.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSButton+Action.swift"; sourceTree = "<group>"; };
128127
3D730DE21F673BE4008534D3 /* Control+Action.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Control+Action.swift"; sourceTree = "<group>"; };
128+
3D730DE81F673FD9008534D3 /* Button+Action.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Button+Action.swift"; sourceTree = "<group>"; };
129129
3D9C98331DB4A87B004A9F7C /* RxTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxTest.framework; path = Carthage/Checkouts/RxSwift/build/Debug/RxTest.framework; sourceTree = "<group>"; };
130130
3DD965BB1F5DC0E400C180FE /* macOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "macOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
131131
3DD965CF1F5DC31D00C180FE /* Nimble.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -143,7 +143,6 @@
143143
7F0569E31DE28587007E1D0D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
144144
7F0569E51DE28587007E1D0D /* AlertAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertAction.swift; sourceTree = "<group>"; };
145145
7F0569E61DE28587007E1D0D /* UIBarButtonItem+Action.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIBarButtonItem+Action.swift"; sourceTree = "<group>"; };
146-
7F0569E71DE28587007E1D0D /* UIButton+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIButton+Rx.swift"; sourceTree = "<group>"; };
147146
7F0569F01DE288EB007E1D0D /* ActionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ActionTests.swift; path = ActionTests/ActionTests.swift; sourceTree = "<group>"; };
148147
7F0569F11DE288EB007E1D0D /* AlertActionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertActionTests.swift; sourceTree = "<group>"; };
149148
7F0569F21DE288EB007E1D0D /* BarButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BarButtonTests.swift; sourceTree = "<group>"; };
@@ -245,18 +244,11 @@
245244
/* End PBXFrameworksBuildPhase section */
246245

247246
/* Begin PBXGroup section */
248-
3D3D42741F5DBAAE008CF951 /* AppKitExtensions */ = {
249-
isa = PBXGroup;
250-
children = (
251-
3D3D42751F5DBAAE008CF951 /* NSButton+Action.swift */,
252-
);
253-
path = AppKitExtensions;
254-
sourceTree = "<group>";
255-
};
256247
3D730DE11F673BCB008534D3 /* CommonUI */ = {
257248
isa = PBXGroup;
258249
children = (
259250
3D730DE21F673BE4008534D3 /* Control+Action.swift */,
251+
3D730DE81F673FD9008534D3 /* Button+Action.swift */,
260252
);
261253
path = CommonUI;
262254
sourceTree = "<group>";
@@ -296,7 +288,6 @@
296288
7F0569E01DE28587007E1D0D /* Action+Internal.swift */,
297289
CA2861C71ED6979400BB327A /* InputSubject.swift */,
298290
3D730DE11F673BCB008534D3 /* CommonUI */,
299-
3D3D42741F5DBAAE008CF951 /* AppKitExtensions */,
300291
7F0569E41DE28587007E1D0D /* UIKitExtensions */,
301292
7F0569EF1DE28598007E1D0D /* Supporting Files */,
302293
);
@@ -308,7 +299,6 @@
308299
children = (
309300
7F0569E51DE28587007E1D0D /* AlertAction.swift */,
310301
7F0569E61DE28587007E1D0D /* UIBarButtonItem+Action.swift */,
311-
7F0569E71DE28587007E1D0D /* UIButton+Rx.swift */,
312302
);
313303
path = UIKitExtensions;
314304
sourceTree = "<group>";
@@ -691,10 +681,10 @@
691681
1FCDDA641EAC31EF006EB95B /* Action.swift in Sources */,
692682
1FCDDA651EAC31EF006EB95B /* Action+Internal.swift in Sources */,
693683
1FCDDA661EAC31EF006EB95B /* AlertAction.swift in Sources */,
684+
3D730DEA1F674043008534D3 /* Button+Action.swift in Sources */,
694685
1FCDDA671EAC31EF006EB95B /* UIBarButtonItem+Action.swift in Sources */,
695686
CA2861CB1ED6B08300BB327A /* InputSubject.swift in Sources */,
696687
3D730DE41F673C64008534D3 /* Control+Action.swift in Sources */,
697-
1FCDDA681EAC31EF006EB95B /* UIButton+Rx.swift in Sources */,
698688
);
699689
runOnlyForDeploymentPostprocessing = 0;
700690
};
@@ -705,7 +695,7 @@
705695
CA2861CC1ED6B08400BB327A /* InputSubject.swift in Sources */,
706696
3D730DE51F673C65008534D3 /* Control+Action.swift in Sources */,
707697
1FCDDA8A1EAC329E006EB95B /* Action.swift in Sources */,
708-
3D3D42771F5DBACF008CF951 /* NSButton+Action.swift in Sources */,
698+
3D730DEB1F674044008534D3 /* Button+Action.swift in Sources */,
709699
1FCDDA8B1EAC329E006EB95B /* Action+Internal.swift in Sources */,
710700
);
711701
runOnlyForDeploymentPostprocessing = 0;
@@ -760,10 +750,10 @@
760750
7F0569EC1DE28587007E1D0D /* AlertAction.swift in Sources */,
761751
7F0569E81DE28587007E1D0D /* Action+Internal.swift in Sources */,
762752
7F0569ED1DE28587007E1D0D /* UIBarButtonItem+Action.swift in Sources */,
753+
3D730DE91F673FD9008534D3 /* Button+Action.swift in Sources */,
763754
CA2861C81ED6979400BB327A /* InputSubject.swift in Sources */,
764755
3D730DE31F673BE4008534D3 /* Control+Action.swift in Sources */,
765756
7F0569EA1DE28587007E1D0D /* Action.swift in Sources */,
766-
7F0569EE1DE28587007E1D0D /* UIButton+Rx.swift in Sources */,
767757
);
768758
runOnlyForDeploymentPostprocessing = 0;
769759
};

Sources/Action/AppKitExtensions/NSButton+Action.swift renamed to Sources/Action/CommonUI/Button+Action.swift

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import Cocoa
1+
#if os(iOS) || os(tvOS) || os(macOS)
2+
import Foundation
3+
#if os(iOS) || os(tvOS)
4+
import UIKit
5+
public typealias Button = UIKit.UIButton
6+
#elseif os(macOS)
7+
import Cocoa
8+
public typealias Button = Cocoa.NSButton
9+
#endif
210
import RxSwift
311
import RxCocoa
4-
import ObjectiveC
512

6-
public extension Reactive where Base: NSButton {
7-
/// Binds enabled state of action to button, and subscribes to rx.tap to execute action.
13+
public extension Reactive where Base: Button {
14+
/// Binds enabled state of action to button, and subscribes to rx_tap to execute action.
815
/// These subscriptions are managed in a private, inaccessible dispose bag. To cancel
916
/// them, set the rx.action to nil or another action.
1017
public var action: CocoaAction? {
@@ -28,28 +35,55 @@ public extension Reactive where Base: NSButton {
2835
.bind(to: self.isEnabled)
2936
.disposed(by: self.base.actionDisposeBag)
3037

31-
self.tap
32-
.subscribe(onNext: {
33-
action.execute()
34-
})
38+
// Technically, this file is only included on tv/iOS platforms,
39+
// so this optional will never be nil. But let's be safe 😉
40+
let lookupControlEvent: ControlEvent<Void>?
41+
42+
#if os(tvOS)
43+
lookupControlEvent = self.primaryAction
44+
#elseif os(iOS) || os(macOS)
45+
lookupControlEvent = self.tap
46+
#endif
47+
48+
guard let controlEvent = lookupControlEvent else {
49+
return
50+
}
51+
52+
controlEvent
53+
.bind(to: action.inputs)
3554
.disposed(by: self.base.actionDisposeBag)
3655
}
3756
}
3857
}
3958

40-
/// Binds enabled state of action to button, and subscribes to rx.tap to execute action with given input transform.
59+
/// Binds enabled state of action to button, and subscribes to rx_tap to execute action with given input transform.
4160
/// These subscriptions are managed in a private, inaccessible dispose bag. To cancel
4261
/// them, call bindToAction with another action or call unbindAction().
4362
public func bind<Input, Output>(to action: Action<Input, Output>, inputTransform: @escaping (Base) -> (Input)) {
4463
// This effectively disposes of any existing subscriptions.
4564
unbindAction()
46-
self.bind(to: action, controlEvent: self.tap, inputTransform: inputTransform)
65+
66+
// Technically, this file is only included on tv/iOS platforms,
67+
// so this optional will never be nil. But let's be safe 😉
68+
let lookupControlEvent: ControlEvent<Void>?
69+
70+
#if os(tvOS)
71+
lookupControlEvent = self.primaryAction
72+
#elseif os(iOS) || os(macOS)
73+
lookupControlEvent = self.tap
74+
#endif
75+
76+
guard let controlEvent = lookupControlEvent else {
77+
return
78+
}
79+
self.bind(to: action, controlEvent: controlEvent, inputTransform: inputTransform)
4780
}
4881

49-
/// Binds enabled state of action to button, and subscribes to rx.tap to execute action with given input value.
82+
/// Binds enabled state of action to button, and subscribes to rx_tap to execute action with given input value.
5083
/// These subscriptions are managed in a private, inaccessible dispose bag. To cancel
5184
/// them, call bindToAction with another action or call unbindAction().
5285
public func bind<Input, Output>(to action: Action<Input, Output>, input: Input) {
5386
self.bind(to: action) { _ in input }
5487
}
5588
}
89+
#endif

Sources/Action/UIKitExtensions/UIButton+Rx.swift

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)