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

[GWL-41] TNCombineCococa 구현 #41

Merged
merged 2 commits into from
Nov 15, 2023
Merged
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
1 change: 1 addition & 0 deletions iOS/Projects/Features/Record/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let project = Project.makeModule(
dependencies: [
ProjectTargetDependency.Trinet,
ProjectTargetDependency.DesignSystem,
ProjectTargetDependency.TNCocoaCombine,
],
isTestable: true
)
8 changes: 8 additions & 0 deletions iOS/Projects/Shared/TNCocoaCombine/Project.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ProjectDescription
import ProjectDescriptionHelpers

let project = Project.makeModule(
name: "TNCocoaCombine",
product: .framework,
isTestable: false
)
36 changes: 36 additions & 0 deletions iOS/Projects/Shared/TNCocoaCombine/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)
}
}
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)
}
}
}
1 change: 1 addition & 0 deletions iOS/Tuist/ProjectDescriptionHelpers/TargetDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public enum ProjectTargetDependency {
public static let DesignSystem: TargetDependency = .project(target: "DesignSystem", path: .relativeToRoot("Projects/Shared/DesignSystem"))
public static let Trinet: TargetDependency = .project(target: "Trinet", path: .relativeToRoot("Projects/Core/Network"))
public static let Record: TargetDependency = .project(target: "RecordFeature", path: .relativeToRoot("Projects/Features/Record"))
public static let TNCocoaCombine: TargetDependency = .project(target: "TNCocoaCombine", path: .relativeToRoot("Projects/Shared/TNCocoaCombine"))
}