metatext/Data Sources/CompositionAttachmentsDataSource.swift

35 lines
1.3 KiB
Swift
Raw Normal View History

2020-12-17 06:48:06 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Mastodon
import UIKit
import ViewModels
final class CompositionAttachmentsDataSource: UICollectionViewDiffableDataSource<Int, Attachment> {
2020-12-19 06:30:19 +00:00
private let updateQueue =
DispatchQueue(label: "com.metabolist.metatext.composition-attachments-data-source.update-queue")
2021-01-01 00:49:59 +00:00
init(collectionView: UICollectionView,
2021-01-03 01:22:17 +00:00
viewModelProvider: @escaping (IndexPath) -> (CompositionAttachmentViewModel, CompositionViewModel)) {
2020-12-19 06:30:19 +00:00
let registration = UICollectionView.CellRegistration
2021-01-03 01:22:17 +00:00
<CompositionAttachmentCollectionViewCell, (CompositionAttachmentViewModel, CompositionViewModel)> {
$0.viewModel = $2.0
$0.parentViewModel = $2.1
2020-12-19 06:30:19 +00:00
}
super.init(collectionView: collectionView) { collectionView, indexPath, _ in
collectionView.dequeueConfiguredReusableCell(
using: registration,
for: indexPath,
item: viewModelProvider(indexPath))
}
}
override func apply(_ snapshot: NSDiffableDataSourceSnapshot<Int, Attachment>,
animatingDifferences: Bool = true,
completion: (() -> Void)? = nil) {
updateQueue.async {
super.apply(snapshot, animatingDifferences: animatingDifferences, completion: completion)
}
}
2020-12-17 06:48:06 +00:00
}