metatext/Data Sources/TableViewDataSource.swift

43 lines
1.7 KiB
Swift
Raw Normal View History

2020-10-07 21:06:26 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import ViewModels
2020-10-15 07:44:01 +00:00
final class TableViewDataSource: UITableViewDiffableDataSource<Int, CollectionItem> {
2020-10-07 21:06:26 +00:00
private let updateQueue =
DispatchQueue(label: "com.metabolist.metatext.collection-data-source.update-queue")
init(tableView: UITableView, viewModelProvider: @escaping (IndexPath) -> CollectionItemViewModel) {
2020-10-15 07:44:01 +00:00
for cellClass in CollectionItem.cellClasses {
tableView.register(cellClass, forCellReuseIdentifier: String(describing: cellClass))
2020-10-07 21:06:26 +00:00
}
2020-10-15 07:44:01 +00:00
super.init(tableView: tableView) { tableView, indexPath, item in
2020-10-07 21:06:26 +00:00
let cell = tableView.dequeueReusableCell(
2020-10-15 07:44:01 +00:00
withIdentifier: String(describing: item.cellClass),
2020-10-07 21:06:26 +00:00
for: indexPath)
switch (cell, viewModelProvider(indexPath)) {
case let (statusListCell as StatusListCell, statusViewModel as StatusViewModel):
statusListCell.viewModel = statusViewModel
case let (accountListCell as AccountListCell, accountViewModel as AccountViewModel):
accountListCell.viewModel = accountViewModel
case let (loadMoreCell as LoadMoreCell, loadMoreViewModel as LoadMoreViewModel):
loadMoreCell.viewModel = loadMoreViewModel
default:
break
}
return cell
}
}
2020-10-15 07:44:01 +00:00
override func apply(_ snapshot: NSDiffableDataSourceSnapshot<Int, CollectionItem>,
animatingDifferences: Bool = true,
completion: (() -> Void)? = nil) {
updateQueue.async {
super.apply(snapshot, animatingDifferences: animatingDifferences, completion: completion)
}
}
2020-10-07 21:06:26 +00:00
}