-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MERGE] LoginViewController MVVM-C로 변경 (#162)
- Loading branch information
Showing
22 changed files
with
533 additions
and
309 deletions.
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
129 changes: 129 additions & 0 deletions
129
LionHeart-iOS/LionHeart-iOS/Global/Extensions/Combine+.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,129 @@ | ||
// | ||
// Combine+.swift | ||
// LionHeart-iOS | ||
// | ||
// Created by 김민재 on 11/6/23. | ||
// | ||
|
||
import Combine | ||
import UIKit | ||
|
||
|
||
extension UIControl { | ||
func controlPublisher(for event: UIControl.Event) -> UIControl.EventPublisher { | ||
return UIControl.EventPublisher(control: self, event: event) | ||
} | ||
|
||
// Publisher | ||
struct EventPublisher: Publisher { | ||
typealias Output = UIControl | ||
typealias Failure = Never | ||
|
||
let control: UIControl | ||
let event: UIControl.Event | ||
|
||
func receive<S>(subscriber: S) | ||
where S: Subscriber, Never == S.Failure, UIControl == S.Input { | ||
let subscription = EventSubscription( | ||
control: control, | ||
subscriber: subscriber, | ||
event: event | ||
) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
} | ||
|
||
// Subscription | ||
fileprivate class EventSubscription<EventSubscriber: Subscriber>: Subscription | ||
where EventSubscriber.Input == UIControl, EventSubscriber.Failure == Never { | ||
|
||
let control: UIControl | ||
let event: UIControl.Event | ||
var subscriber: EventSubscriber? | ||
|
||
init(control: UIControl, subscriber: EventSubscriber, event: UIControl.Event) { | ||
self.control = control | ||
self.subscriber = subscriber | ||
self.event = event | ||
|
||
control.addTarget(self, action: #selector(eventDidOccur), for: event) | ||
} | ||
|
||
func request(_ demand: Subscribers.Demand) {} | ||
|
||
func cancel() { | ||
subscriber = nil | ||
control.removeTarget(self, action: #selector(eventDidOccur), for: event) | ||
} | ||
|
||
@objc func eventDidOccur() { | ||
_ = subscriber?.receive(control) | ||
} | ||
} | ||
} | ||
|
||
extension UITextField { | ||
var textPublisher: AnyPublisher<String, Never> { | ||
controlPublisher(for: .editingChanged) | ||
.map { $0 as! UITextField } | ||
.map { $0.text! } | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
extension UIButton { | ||
var tapPublisher: AnyPublisher<Void, Never> { | ||
controlPublisher(for: .touchUpInside) | ||
.map { _ in } | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
|
||
extension Publisher { | ||
|
||
func tryAwaitMap<T>(_ transform: @escaping (Self.Output) async throws -> T) -> Publishers.FlatMap<Future<T, NetworkError>, Self> { | ||
flatMap { value in | ||
Future { promise in | ||
Task { | ||
do { | ||
let result = try await transform(value) | ||
promise(.success(result)) | ||
} | ||
catch { | ||
promise(.failure(error as! NetworkError)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func task<T>(maxPublishers: Subscribers.Demand = .unlimited, _ transform: @escaping (Output) async -> T) | ||
-> Publishers.FlatMap<Deferred<Future<T, Never>>, Self> { | ||
flatMap(maxPublishers: maxPublishers) { value in | ||
Deferred { | ||
Future { promise in | ||
Task { | ||
let output = await transform(value) | ||
promise(.success(output)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func errorTask<T>(maxPublishers: Subscribers.Demand = .unlimited, _ transform: @escaping (Output) async throws -> T) | ||
-> Publishers.FlatMap<Deferred<Future<T, Never>>, Self> { | ||
flatMap(maxPublishers: maxPublishers) { value in | ||
Deferred { | ||
Future { promise in | ||
Task { | ||
let output = try await transform(value) | ||
promise(.success(output)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
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
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
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
Oops, something went wrong.