-
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.
- Loading branch information
1 parent
8c956a0
commit 2b1fb4e
Showing
5 changed files
with
132 additions
and
77 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
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
28 changes: 28 additions & 0 deletions
28
GEON-PPANG-iOS/Presentation/Utils/Combine/Publisher/CollectionViewPublisher.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,28 @@ | ||
// | ||
// CollectionViewPublisher.swift | ||
// GEON-PPANG-iOS | ||
// | ||
// Created by JEONGEUN KIM on 8/13/24. | ||
// | ||
|
||
import Combine | ||
|
||
import UIKit | ||
|
||
struct CollectionViewPublisher: Publisher { | ||
typealias Output = IndexPath | ||
typealias Failure = Never | ||
|
||
let collectionView: UICollectionView | ||
let event: Event | ||
|
||
func receive<S>(subscriber: S) where S: Subscriber, S.Input == IndexPath, S.Failure == Never { | ||
let subscription = CollectionViewSubscription( | ||
subscriber: subscriber, | ||
collectionView: collectionView, | ||
event: event | ||
) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
} | ||
|
69 changes: 69 additions & 0 deletions
69
GEON-PPANG-iOS/Presentation/Utils/Combine/Subscription/CollectionViewSubscription.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,69 @@ | ||
// | ||
// CollectionViewSubscription.swift | ||
// GEON-PPANG-iOS | ||
// | ||
// Created by JEONGEUN KIM on 8/13/24. | ||
// | ||
|
||
import Combine | ||
|
||
import UIKit | ||
|
||
|
||
enum Event { | ||
case didSelect | ||
case didDeselect | ||
} | ||
|
||
final class CollectionViewSubscription<SubscriberType: Subscriber>: Subscription where SubscriberType.Input == IndexPath { | ||
private var subscriber: SubscriberType? | ||
private weak var collectionView: UICollectionView? | ||
private var delegateProxy: CollectionViewDelegateProxy? | ||
|
||
init(subscriber: SubscriberType, collectionView: UICollectionView, event: Event) { | ||
self.subscriber = subscriber | ||
self.collectionView = collectionView | ||
|
||
let delegateProxy = CollectionViewDelegateProxy( | ||
event: event, | ||
handler: { [weak self] indexPath in | ||
_ = self?.subscriber?.receive(indexPath) | ||
} | ||
) | ||
|
||
self.delegateProxy = delegateProxy | ||
collectionView.delegate = delegateProxy | ||
} | ||
|
||
func request(_ demand: Subscribers.Demand) { | ||
// 요구 처리 로직 추가 가능 | ||
} | ||
|
||
func cancel() { | ||
subscriber = nil | ||
delegateProxy = nil | ||
} | ||
} | ||
|
||
|
||
private class CollectionViewDelegateProxy: NSObject, UICollectionViewDelegate { | ||
private let event: Event | ||
private let handler: (IndexPath) -> Void | ||
|
||
init(event: Event, handler: @escaping (IndexPath) -> Void) { | ||
self.event = event | ||
self.handler = handler | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | ||
if event == .didSelect { | ||
handler(indexPath) | ||
} | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { | ||
if event == .didDeselect { | ||
handler(indexPath) | ||
} | ||
} | ||
} |
File renamed without changes.