mastodon-ios/Mastodon/Scene/Profile/Favorite/FavoriteViewModel+Diffable....

66 lines
2.3 KiB
Swift
Raw Normal View History

2021-04-07 08:24:28 +02:00
//
// FavoriteViewModel+Diffable.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-4-7.
//
import UIKit
extension FavoriteViewModel {
func setupDiffableDataSource(
tableView: UITableView,
2021-04-07 08:24:28 +02:00
statusTableViewCellDelegate: StatusTableViewCellDelegate
) {
diffableDataSource = StatusSection.diffableDataSource(
tableView: tableView,
context: context,
configuration: StatusSection.Configuration(
statusTableViewCellDelegate: statusTableViewCellDelegate,
2022-02-15 12:44:45 +01:00
timelineMiddleLoaderTableViewCellDelegate: nil,
filterContext: .none,
activeFilters: nil
)
2021-04-07 08:24:28 +02:00
)
// set empty section to make update animation top-to-bottom style
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
2021-04-07 08:24:28 +02:00
snapshot.appendSections([.main])
diffableDataSource?.apply(snapshot)
stateMachine.enter(State.Reloading.self)
statusFetchedResultsController.$records
.receive(on: DispatchQueue.main)
.sink { [weak self] records in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
snapshot.appendSections([.main])
let items = records.map { StatusItem.status(record: $0) }
snapshot.appendItems(items, toSection: .main)
if let currentState = self.stateMachine.currentState {
switch currentState {
case is State.Reloading,
is State.Loading,
is State.Idle,
is State.Fail:
snapshot.appendItems([.bottomLoader], toSection: .main)
case is State.NoMore:
break
default:
assertionFailure()
break
}
}
diffableDataSource.applySnapshot(snapshot, animated: false)
}
.store(in: &disposeBag)
2021-04-07 08:24:28 +02:00
}
}