Skip to content

Commit

Permalink
chore: Rename TNCocoaCombine to CombineCocoa
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteHyun committed Nov 19, 2023
1 parent a649e53 commit 8c87f5a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public extension TargetDependency {
static let designSystem: TargetDependency = .project(target: "DesignSystem", path: .relativeToRoot("Projects/Shared/DesignSystem"))
static let trinet: TargetDependency = .project(target: "Trinet", path: .relativeToRoot("Projects/Core/Network"))
static let coordinator: TargetDependency = .project(target: "Coordinator", path: .relativeToRoot("Projects/Core/Coordinator"))
static let combineCocoa: TargetDependency = .project(target: "TNCocoaCombine", path: .relativeToRoot("Projects/Shared/TNCocoaCombine"))
static let combineCocoa: TargetDependency = .project(target: "CombineCocoa", path: .relativeToRoot("Projects/Shared/CombineCocoa"))

static func feature(_ feature: Feature) -> TargetDependency {
return .project(
Expand Down
9 changes: 9 additions & 0 deletions iOS/Projects/Shared/CombineCocoa/Project.swift
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 iOS/Projects/Shared/CombineCocoa/Sources/EventSubscription.swift
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 iOS/Projects/Shared/CombineCocoa/Sources/UIControl+Publisher.swift
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)
}
}
}

0 comments on commit 8c87f5a

Please sign in to comment.