2021-04-01 08:39:15 +02:00
|
|
|
//
|
|
|
|
// UserTimelineViewModel+Diffable.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-01-27 14:23:39 +01:00
|
|
|
import Combine
|
2021-04-01 08:39:15 +02:00
|
|
|
|
|
|
|
extension UserTimelineViewModel {
|
|
|
|
|
|
|
|
func setupDiffableDataSource(
|
2022-01-27 14:23:39 +01:00
|
|
|
tableView: UITableView,
|
2021-04-01 08:39:15 +02:00
|
|
|
statusTableViewCellDelegate: StatusTableViewCellDelegate
|
|
|
|
) {
|
2022-01-27 14:23:39 +01:00
|
|
|
diffableDataSource = StatusSection.diffableDataSource(
|
|
|
|
tableView: tableView,
|
|
|
|
context: context,
|
|
|
|
configuration: StatusSection.Configuration(
|
|
|
|
statusTableViewCellDelegate: statusTableViewCellDelegate,
|
|
|
|
timelineMiddleLoaderTableViewCellDelegate: nil
|
|
|
|
)
|
2021-04-01 08:39:15 +02:00
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
// set empty section to make update animation top-to-bottom style
|
2022-01-27 14:23:39 +01:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
|
2021-04-01 08:39:15 +02:00
|
|
|
snapshot.appendSections([.main])
|
2021-06-22 11:49:07 +02:00
|
|
|
diffableDataSource?.apply(snapshot)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// trigger user timeline loading
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
$domain.removeDuplicates(),
|
|
|
|
$userID.removeDuplicates()
|
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.stateMachine.enter(UserTimelineViewModel.State.Reloading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
let needsTimelineHidden = Publishers.CombineLatest3(
|
|
|
|
isBlocking,
|
|
|
|
isBlockedBy,
|
|
|
|
isSuspended
|
|
|
|
).map { $0 || $1 || $2 }
|
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
statusFetchedResultsController.$records,
|
|
|
|
needsTimelineHidden.removeDuplicates()
|
|
|
|
)
|
|
|
|
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
|
|
|
|
.sink { [weak self] records, needsTimelineHidden in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let diffableDataSource = self.diffableDataSource else { return }
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
|
|
|
|
snapshot.appendSections([.main])
|
|
|
|
|
|
|
|
guard !needsTimelineHidden else {
|
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
return
|
|
|
|
}
|
2021-06-22 11:49:07 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
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-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|