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

Convert to using UIAction based API #46

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 7 additions & 8 deletions Sources/Fisticuffs/UIKit/TargetActionBindableProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ class TargetActionBindableProperty<Control: UIControl, ValueType>: Bidirectional

init(control: Control, getter: @escaping Getter, setter: @escaping Setter, events: UIControl.Event) {
super.init(control: control, getter: getter, setter: setter, extraCleanup: disposables)
control.addTarget(self, action: #selector(TargetActionBindableProperty.controlEventFired), for: events)
disposables.add(DisposableBlock { [weak self, weak control] in
control?.removeTarget(self, action: #selector(TargetActionBindableProperty.controlEventFired), for: events)
let actionId = UIAction.Identifier(UUID().uuidString)
control.addAction(.init(identifier: actionId,handler: { [weak self] _ in
guard let self else { return }
self.pushChangeToCurrentValueSubscribable()
}), for: events)
disposables.add(DisposableBlock { [weak control] in
control?.removeAction(identifiedBy: actionId, for: events)
})
}

@objc func controlEventFired() {
self.pushChangeToCurrentValueSubscribable()
}

}
20 changes: 9 additions & 11 deletions Sources/Fisticuffs/UIKit/UIControl+Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ public extension UIControl {

var b_onTap: Fisticuffs.Event<Void> {
associatedObjectProperty(self, &b_onTap_key) { _ in
self.addTarget(self, action: #selector(self.b_receivedOnTap(_:)), for: .touchUpInside)
addAction(.init(handler: { [weak self] _ in
guard let self else { return }
self.b_onTap.fire(())
}), for: .touchUpInside)
return Fisticuffs.Event<Void>()
}
}

@objc fileprivate func b_receivedOnTap(_ sender: AnyObject) {
b_onTap.fire(())
}

}

public extension UIControl {
Expand All @@ -68,7 +66,11 @@ public extension UIControl {
return trampoline.event
} else {
let trampoline = ControlEventTrampoline()
addTarget(trampoline, action: #selector(ControlEventTrampoline.receivedEvent(_:uiEvent:)), for: controlEvents)
addAction(.init(handler: { [weak self] _ in
guard let self else { return }
// cannot get `UIEvent` from a UIAction
trampoline.event.fire(nil)
}), for: controlEvents)
trampolinesCollection.trampolines[controlEvents.rawValue] = trampoline
return trampoline.event
}
Expand All @@ -83,9 +85,5 @@ private class ControlEventTrampolineCollection: NSObject {

private class ControlEventTrampoline: NSObject {
let event = Event<UIEvent?>()

@objc func receivedEvent(_ sender: AnyObject?, uiEvent: UIEvent?) {
event.fire(uiEvent)
}
}

20 changes: 9 additions & 11 deletions Sources/Fisticuffs/UIKit/UISegmentedControl+Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ private class SegmentControlManager<Item: Equatable> : NSObject {
var itemValues: [Item] = []
let display: (Item) -> SegmentDisplay
let selection: CurrentValueSubscribable<Item>

let actionID: UIAction.Identifier
let disposableBag = DisposableBag()

init(control: UISegmentedControl, items: Subscribable<[Item]>, display: @escaping (Item) -> SegmentDisplay, selection: CurrentValueSubscribable<Item>) {
self.control = control
self.items = items
self.display = display
self.selection = selection
let actionID = UIAction.Identifier(UUID().uuidString)
self.actionID = actionID
super.init()

items.subscribe { [weak self] _, value in
Expand All @@ -64,11 +66,14 @@ private class SegmentControlManager<Item: Equatable> : NSObject {
}
.addTo(disposableBag)

control.addTarget(self, action: #selector(SegmentControlManager.userChangedSelection(_:)), for: .valueChanged)
control.addAction(.init(identifier: actionID, handler: { [weak self] _ in
guard let self else { return }
selection.value = itemValues[control.selectedSegmentIndex]
}), for: .valueChanged)
}

deinit {
control?.removeTarget(self, action: #selector(SegmentControlManager.userChangedSelection(_:)), for: .valueChanged)
control?.removeAction(identifiedBy: actionID, for: .valueChanged)
}

func itemsChanged(_ newValue: [Item]) {
Expand Down Expand Up @@ -108,11 +113,4 @@ private class SegmentControlManager<Item: Equatable> : NSObject {
control.selectedSegmentIndex = UISegmentedControl.noSegment
}
}

@IBAction func userChangedSelection(_ sender: AnyObject) {
guard let control = control else { return }

selection.value = itemValues[control.selectedSegmentIndex]
}

}
Loading