-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Rename TNCocoaCombine to
CombineCocoa
- Loading branch information
Showing
4 changed files
with
75 additions
and
1 deletion.
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,9 @@ | ||
import ProjectDescription | ||
import ProjectDescriptionHelpers | ||
|
||
let project = Project.makeModule( | ||
name: "CombineCocoa", | ||
product: .framework, | ||
resources: nil, | ||
isTestable: false | ||
) |
36 changes: 36 additions & 0 deletions
36
iOS/Projects/Shared/CombineCocoa/Sources/EventSubscription.swift
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,36 @@ | ||
// | ||
// EventSubscription.swift | ||
// TNCocoaCombine | ||
// | ||
// Created by MaraMincho on 11/15/23. | ||
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import UIKit | ||
|
||
class EventSubscription<EventSubscriber: Subscriber>: | ||
Subscription where EventSubscriber.Input == UIControl, EventSubscriber.Failure == Never { | ||
func request(_: Subscribers.Demand) {} | ||
|
||
func cancel() { | ||
subscriber = nil | ||
control.removeTarget(self, action: #selector(eventDidOccur), for: event) | ||
} | ||
|
||
@objc func eventDidOccur() { | ||
_ = subscriber?.receive(control) | ||
} | ||
|
||
let control: UIControl | ||
let event: UIControl.Event | ||
var subscriber: EventSubscriber? | ||
|
||
init(control: UIControl, event: UIControl.Event, subscriber: EventSubscriber) { | ||
self.control = control | ||
self.event = event | ||
self.subscriber = subscriber | ||
|
||
control.addTarget(self, action: #selector(eventDidOccur), for: event) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
iOS/Projects/Shared/CombineCocoa/Sources/UIControl+Publisher.swift
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 @@ | ||
// | ||
// UIControl+Publisher.swift | ||
// TNCocoaCombine | ||
// | ||
// Created by MaraMincho on 11/15/23. | ||
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import UIKit | ||
|
||
public extension UIControl { | ||
func publisher(_ event: UIControl.Event) -> EventPublisher { | ||
return EventPublisher(control: self, event: event) | ||
} | ||
|
||
struct EventPublisher: Publisher { | ||
public typealias Output = UIControl | ||
public typealias Failure = Never | ||
|
||
let control: UIControl | ||
let event: UIControl.Event | ||
|
||
public func receive<S>(subscriber: S) where S: Subscriber, Never == S.Failure, UIControl == S.Input { | ||
let subscription = EventSubscription(control: control, event: event, subscriber: subscriber) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
} | ||
} |