Skip to content

Commit e9cd098

Browse files
committed
Make CellEventCoordinator optional
1 parent f665f92 commit e9cd098

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

Sources/CellViewModel.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public protocol CellViewModel: DiffableViewModel, ViewRegistrationProvider {
2525

2626
func configure(cell: CellType)
2727

28-
func didSelect(with coordinator: CellEventCoordinator)
28+
func didSelect(with coordinator: CellEventCoordinator?)
2929
}
3030

3131
extension CellViewModel {
3232
public var shouldHighlight: Bool { true }
3333

3434
public var contextMenuConfiguration: UIContextMenuConfiguration? { nil }
3535

36-
public func didSelect(with coordinator: CellEventCoordinator) {
37-
coordinator.didSelectCell(viewModel: self)
36+
public func didSelect(with coordinator: CellEventCoordinator?) {
37+
coordinator?.didSelectCell(viewModel: self)
3838
}
3939
}
4040

@@ -89,7 +89,7 @@ public struct AnyCellViewModel: CellViewModel {
8989
self._configure(cell)
9090
}
9191

92-
public func didSelect(with coordinator: CellEventCoordinator) {
92+
public func didSelect(with coordinator: CellEventCoordinator?) {
9393
self._didSelect(coordinator)
9494
}
9595

@@ -101,7 +101,7 @@ public struct AnyCellViewModel: CellViewModel {
101101
private let _shouldHighlight: Bool
102102
private let _contextMenuConfiguration: UIContextMenuConfiguration?
103103
private let _configure: (CellType) -> Void
104-
private let _didSelect: (CellEventCoordinator) -> Void
104+
private let _didSelect: (CellEventCoordinator?) -> Void
105105

106106
// MARK: Init
107107

Sources/CollectionViewDriver.swift

+10-7
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public final class CollectionViewDriver: NSObject {
3737
}
3838
}
3939

40-
// avoiding a strong reference to prevent a retain cycle.
41-
// this is typically the view controller that owns `self` (the driver).
42-
// the caller is responsible for retaining this object for the lifetime of the driver.
43-
private unowned var _cellEventCoordinator: CellEventCoordinator
40+
// Avoiding a strong reference to prevent a possible retain cycle.
41+
// This is typically the view controller that owns `self` (the driver).
42+
// The caller is responsible for retaining this object for the lifetime of the driver.
43+
private weak var _cellEventCoordinator: CellEventCoordinator?
4444

4545
private(set) var _dataSource: DiffableDataSource
4646

@@ -56,8 +56,11 @@ public final class CollectionViewDriver: NSObject {
5656
/// - view: The collection view.
5757
/// - layout: The collection view layout.
5858
/// - viewModel: The collection view model.
59-
/// - cellEventCoordinator: The cell event coordinator. **This object is not retained by the driver.**
60-
/// - animateUpdates: Specifies whether or not to animate updates. Pass `true` to animate, `false` otherwise.
59+
/// - cellEventCoordinator: The cell event coordinator,
60+
/// if you wish to handle cell events outside of your cell view models.
61+
/// **Note: This object is not retained by the driver.**
62+
/// - animateUpdates: Specifies whether or not to animate updates.
63+
/// Pass `true` to animate, `false` otherwise.
6164
/// - didUpdate: A closure to call when the driver finishes diffing and updating the collection view.
6265
///
6366
/// - Warning: The driver **does not** retain the `cellEventCoordinator`,
@@ -67,7 +70,7 @@ public final class CollectionViewDriver: NSObject {
6770
public init(view: UICollectionView,
6871
layout: UICollectionViewCompositionalLayout,
6972
viewModel: CollectionViewModel = CollectionViewModel(),
70-
cellEventCoordinator: CellEventCoordinator,
73+
cellEventCoordinator: CellEventCoordinator?,
7174
animateUpdates: Bool = true,
7275
didUpdate: DidUpdate? = nil) {
7376
self.view = view

0 commit comments

Comments
 (0)