metatext/Data Sources/TableViewDataSource.swift

82 lines
3.6 KiB
Swift
Raw Normal View History

2020-10-07 21:06:26 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import ViewModels
2021-01-23 06:15:52 +00:00
final class TableViewDataSource: UITableViewDiffableDataSource<CollectionSection.Identifier, CollectionItem> {
2020-10-07 21:06:26 +00:00
private let updateQueue =
DispatchQueue(label: "com.metabolist.metatext.collection-data-source.update-queue")
2021-01-31 01:43:48 +00:00
private let viewModel: CollectionViewModel
init(tableView: UITableView, viewModel: CollectionViewModel) {
self.viewModel = viewModel
2020-10-07 21:06:26 +00:00
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)
2021-01-31 01:43:48 +00:00
switch (cell, viewModel.viewModel(indexPath: indexPath)) {
2021-01-29 06:22:13 +00:00
case let (statusCell as StatusTableViewCell, statusViewModel as StatusViewModel):
statusCell.viewModel = statusViewModel
case let (accountCell as AccountTableViewCell, accountViewModel as AccountViewModel):
accountCell.viewModel = accountViewModel
case let (loadMoreCell as LoadMoreTableViewCell, loadMoreViewModel as LoadMoreViewModel):
2020-10-07 21:06:26 +00:00
loadMoreCell.viewModel = loadMoreViewModel
2021-01-29 06:22:13 +00:00
case let (notificationCell as NotificationTableViewCell, notificationViewModel as NotificationViewModel):
notificationCell.viewModel = notificationViewModel
case let (conversationCell as ConversationTableViewCell, conversationViewModel as ConversationViewModel):
conversationCell.viewModel = conversationViewModel
case let (tagCell as TagTableViewCell, tagViewModel as TagViewModel):
tagCell.viewModel = tagViewModel
2021-04-25 19:38:36 +00:00
case let (announcementCell as AnnouncementTableViewCell, announcementViewModel as AnnouncementViewModel):
announcementCell.viewModel = announcementViewModel
2021-01-25 07:42:39 +00:00
case let (_, moreResultsViewModel as MoreResultsViewModel):
var configuration = cell.defaultContentConfiguration()
2021-01-31 01:43:48 +00:00
let statusWord = viewModel.identityContext.appPreferences.statusWord
2021-01-25 07:42:39 +00:00
2021-01-31 01:43:48 +00:00
configuration.text = moreResultsViewModel.scope.moreDescription(statusWord: statusWord)
2021-01-25 07:42:39 +00:00
cell.contentConfiguration = configuration
cell.accessoryType = .disclosureIndicator
2020-10-07 21:06:26 +00:00
default:
break
}
return cell
}
}
2020-10-15 07:44:01 +00:00
2021-01-23 06:15:52 +00:00
override func apply(_ snapshot: NSDiffableDataSourceSnapshot<CollectionSection.Identifier, CollectionItem>,
2020-10-15 07:44:01 +00:00
animatingDifferences: Bool = true,
completion: (() -> Void)? = nil) {
updateQueue.async {
super.apply(snapshot, animatingDifferences: animatingDifferences, completion: completion)
}
}
2021-01-23 06:15:52 +00:00
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let currentSnapshot = snapshot()
let section = currentSnapshot.sectionIdentifiers[section]
if currentSnapshot.numberOfItems(inSection: section) > 0,
2021-01-31 01:43:48 +00:00
let searchScope = section.searchScope {
return searchScope.title(statusWord: viewModel.identityContext.appPreferences.statusWord)
2021-01-23 06:15:52 +00:00
}
return nil
}
2020-10-07 21:06:26 +00:00
}
2020-10-31 00:53:57 +00: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)
}
}