mastodon-ios/Mastodon/Scene/HashtagTimeline/HashtagTimelineViewModel+Di...

71 lines
2.4 KiB
Swift
Raw Normal View History

2021-04-01 04:12:57 +02:00
//
// HashtagTimelineViewModel+Diffable.swift
// Mastodon
//
// Created by BradGao on 2021/3/30.
//
import UIKit
import Combine
2021-04-01 04:12:57 +02:00
import CoreData
import CoreDataStack
extension HashtagTimelineViewModel {
func setupDiffableDataSource(
tableView: UITableView,
statusTableViewCellDelegate: StatusTableViewCellDelegate
2021-04-01 04:12:57 +02:00
) {
diffableDataSource = StatusSection.diffableDataSource(
tableView: tableView,
context: context,
configuration: StatusSection.Configuration(
context: context,
authContext: authContext,
statusTableViewCellDelegate: statusTableViewCellDelegate,
2022-02-15 12:44:45 +01:00
timelineMiddleLoaderTableViewCellDelegate: nil,
filterContext: .none,
activeFilters: nil
)
2021-04-01 04:12:57 +02:00
)
stateMachine.enter(State.Reloading.self)
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
snapshot.appendSections([.main])
diffableDataSource?.apply(snapshot)
fetchedResultsController.$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.Initial,
is State.Reloading,
is State.Loading,
is State.Idle,
is State.Fail:
if !items.isEmpty {
snapshot.appendItems([.bottomLoader], toSection: .main)
}
case is State.NoMore:
break
default:
assertionFailure()
break
}
}
diffableDataSource.apply(snapshot)
2021-04-01 04:12:57 +02:00
}
.store(in: &disposeBag)
2021-04-01 04:12:57 +02:00
}
}