metatext-app-ios-iphone-ipad/Data Sources/TableViewDataSource.swift

76 lines
3.3 KiB
Swift
Raw Normal View History

2020-10-07 23:06:26 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import ViewModels
2021-01-23 07:15:52 +01:00
final class TableViewDataSource: UITableViewDiffableDataSource<CollectionSection.Identifier, CollectionItem> {
2020-10-07 23:06:26 +02:00
private let updateQueue =
DispatchQueue(label: "com.metabolist.metatext.collection-data-source.update-queue")
init(tableView: UITableView, viewModelProvider: @escaping (IndexPath) -> CollectionItemViewModel) {
2020-10-15 09:44:01 +02:00
for cellClass in CollectionItem.cellClasses {
tableView.register(cellClass, forCellReuseIdentifier: String(describing: cellClass))
2020-10-07 23:06:26 +02:00
}
2020-10-15 09:44:01 +02:00
super.init(tableView: tableView) { tableView, indexPath, item in
2020-10-07 23:06:26 +02:00
let cell = tableView.dequeueReusableCell(
2020-10-15 09:44:01 +02:00
withIdentifier: String(describing: item.cellClass),
2020-10-07 23:06:26 +02: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
2020-10-30 08:11:24 +01:00
case let (notificationListCell as NotificationListCell, notificationViewModel as NotificationViewModel):
notificationListCell.viewModel = notificationViewModel
2020-10-29 07:03:45 +01:00
case let (conversationListCell as ConversationListCell, conversationViewModel as ConversationViewModel):
conversationListCell.viewModel = conversationViewModel
2021-01-24 04:12:30 +01:00
case let (tagTableViewCell as TagTableViewCell, tagViewModel as TagViewModel):
tagTableViewCell.viewModel = tagViewModel
2021-01-25 08:42:39 +01:00
case let (_, moreResultsViewModel as MoreResultsViewModel):
var configuration = cell.defaultContentConfiguration()
configuration.text = moreResultsViewModel.scope.moreDescription
cell.contentConfiguration = configuration
cell.accessoryType = .disclosureIndicator
2020-10-07 23:06:26 +02:00
default:
break
}
return cell
}
}
2020-10-15 09:44:01 +02:00
2021-01-23 07:15:52 +01:00
override func apply(_ snapshot: NSDiffableDataSourceSnapshot<CollectionSection.Identifier, CollectionItem>,
2020-10-15 09:44:01 +02:00
animatingDifferences: Bool = true,
completion: (() -> Void)? = nil) {
updateQueue.async {
super.apply(snapshot, animatingDifferences: animatingDifferences, completion: completion)
}
}
2021-01-23 07:15:52 +01:00
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let currentSnapshot = snapshot()
let section = currentSnapshot.sectionIdentifiers[section]
if currentSnapshot.numberOfItems(inSection: section) > 0,
let localizedStringKey = section.titleLocalizedStringKey {
return NSLocalizedString(localizedStringKey, comment: "")
}
return nil
}
2020-10-07 23:06:26 +02:00
}
2020-10-31 01:53:57 +01:00
extension TableViewDataSource {
func indexPath(itemId: CollectionItem.Id) -> IndexPath? {
guard let item = snapshot().itemIdentifiers.first(where: { $0.itemId == itemId }) else { return nil }
return indexPath(for: item)
}
}