mastodon-ios/Mastodon/Scene/Discovery/News/DiscoveryNewsViewModel+Diff...

61 lines
1.9 KiB
Swift
Raw Normal View History

//
// DiscoveryNewsViewModel+Diffable.swift
// Mastodon
//
// Created by MainasuK on 2022-4-13.
//
import UIKit
import Combine
extension DiscoveryNewsViewModel {
func setupDiffableDataSource(
tableView: UITableView
) {
diffableDataSource = DiscoverySection.diffableDataSource(
tableView: tableView,
context: context,
configuration: DiscoverySection.Configuration(authContext: authContext)
)
stateMachine.enter(State.Reloading.self)
$links
.receive(on: DispatchQueue.main)
.sink { [weak self] links in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
var snapshot = NSDiffableDataSourceSnapshot<DiscoverySection, DiscoveryItem>()
snapshot.appendSections([.news])
let items = links.map { DiscoveryItem.link($0) }
snapshot.appendItems(items, toSection: .news)
if let currentState = self.stateMachine.currentState {
switch currentState {
case is State.Initial,
is State.Loading,
is State.Idle,
is State.Fail:
if !items.isEmpty {
snapshot.appendItems([.bottomLoader], toSection: .news)
}
case is State.Reloading:
break
case is State.NoMore:
break
default:
assertionFailure()
break
}
}
2022-12-17 21:57:17 +01:00
diffableDataSource.apply(snapshot, animatingDifferences: false)
}
.store(in: &disposeBag)
}
}