This commit is contained in:
Siyu Yue 2023-05-06 21:14:14 -07:00
parent 3bf3b18a37
commit 5357c7c9ef
4 changed files with 9 additions and 9 deletions

View File

@ -12,7 +12,7 @@ final class ViewController: UIViewController {
private lazy var dataSource: ListDiffDataSource = { private lazy var dataSource: ListDiffDataSource = {
let dataSource = ListDiffDataSource( let dataSource = ListDiffDataSource(
collectionView: collectionView, appleUpdatesAsync: true, contextObjects: logger) collectionView: collectionView, applyUpdatesAsync: true, contextObjects: logger)
return dataSource return dataSource
}() }()

View File

@ -212,7 +212,7 @@ open class AnyListCellController {
fatalError("Must be provided by subclass") fatalError("Must be provided by subclass")
} }
/// Used for accessing context objects passed in from ``ListDiffDataSource/init(collectionView:appleUpdatesAsync:contextObjects:)``. /// Used for accessing context objects passed in from ``ListDiffDataSource/init(collectionView:applyUpdatesAsync:contextObjects:)``.
public let context: ListDiffContext public let context: ListDiffContext
weak var cell: ListCell? { weak var cell: ListCell? {

View File

@ -1,6 +1,6 @@
import Foundation import Foundation
/// Used for passing in dependencies from ``ListDiffDataSource/init(collectionView:appleUpdatesAsync:contextObjects:)`` /// Used for passing in dependencies from ``ListDiffDataSource/init(collectionView:applyUpdatesAsync:contextObjects:)``
/// and available on ``AnyListCellController/context`` property. /// and available on ``AnyListCellController/context`` property.
/// ///
/// Objects are stored in a dictonary with `ObjectIdentifier(type(of: object))` as its key/ /// Objects are stored in a dictonary with `ObjectIdentifier(type(of: object))` as its key/

View File

@ -21,7 +21,7 @@ public final class ListDiffDataSource: NSObject {
private let collectionView: UICollectionView private let collectionView: UICollectionView
private let context: ListDiffContext private let context: ListDiffContext
private let queue: DispatchQueue private let queue: DispatchQueue
private let appleUpdatesAsync: Bool private let applyUpdatesAsync: Bool
private var appearedCellIdentifiers = Set<String>() private var appearedCellIdentifiers = Set<String>()
private var fullyAppearedCellIdentifiers = Set<String>() private var fullyAppearedCellIdentifiers = Set<String>()
@ -36,16 +36,16 @@ public final class ListDiffDataSource: NSObject {
/// - Parameters: /// - Parameters:
/// - collectionView: UICollectionView's layout must be an instance of UICollectionFlowLayout (or its subclass), as it implements /// - collectionView: UICollectionView's layout must be an instance of UICollectionFlowLayout (or its subclass), as it implements
/// `collectionView(_:layout:sizeForItemAt:)` of UICollectionViewDelegateFlowLayout. /// `collectionView(_:layout:sizeForItemAt:)` of UICollectionViewDelegateFlowLayout.
/// - appleUpdatesAsync: If true, diffing will be performed asynchronously on a background thread. /// - applyUpdatesAsync: If true, diffing will be performed asynchronously on a background thread.
/// - contextObjects: A list of objects passed in here will be available to use inside ``ListCellController``. /// - contextObjects: A list of objects passed in here will be available to use inside ``ListCellController``.
/// This is a way to pass in dependencies (e.g. logger) so that you don't have to piggyback them on ViewModels. /// This is a way to pass in dependencies (e.g. logger) so that you don't have to piggyback them on ViewModels.
/// ///
public init(collectionView: UICollectionView, appleUpdatesAsync: Bool = false, contextObjects: AnyObject...) { public init(collectionView: UICollectionView, applyUpdatesAsync: Bool = false, contextObjects: AnyObject...) {
precondition(collectionView.collectionViewLayout is UICollectionViewFlowLayout) precondition(collectionView.collectionViewLayout is UICollectionViewFlowLayout)
self.collectionView = collectionView self.collectionView = collectionView
self.appleUpdatesAsync = appleUpdatesAsync self.applyUpdatesAsync = applyUpdatesAsync
self.context = ListDiffContext(objects: contextObjects) self.context = ListDiffContext(objects: contextObjects)
queue = appleUpdatesAsync ? DispatchQueue(label: "ListDiffui.ListDiffdatasource", qos: .default) : .main queue = applyUpdatesAsync ? DispatchQueue(label: "ListDiffui.ListDiffdatasource", qos: .default) : .main
super.init() super.init()
collectionView.dataSource = self collectionView.dataSource = self
collectionView.delegate = self collectionView.delegate = self
@ -61,7 +61,7 @@ public final class ListDiffDataSource: NSObject {
/// - completion: Completion block that gets called after diff update. /// - completion: Completion block that gets called after diff update.
/// ///
public func setRootSection(_ section: Section?, animate: Bool = false, completion: (() -> Void)? = nil) { public func setRootSection(_ section: Section?, animate: Bool = false, completion: (() -> Void)? = nil) {
if appleUpdatesAsync { if applyUpdatesAsync {
queue.async { queue.async {
let diff = self.computeDiff(section: section) let diff = self.computeDiff(section: section)
DispatchQueue.main.async { DispatchQueue.main.async {