mastodon-ios/Mastodon/Scene/Profile/Follower/FollowerListViewModel+Diffa...

64 lines
2.5 KiB
Swift
Raw Normal View History

2021-11-01 12:54:07 +01:00
//
// FollowerListViewModel+Diffable.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-11-1.
//
import UIKit
import MastodonAsset
import MastodonLocalization
2021-11-01 12:54:07 +01:00
extension FollowerListViewModel {
func setupDiffableDataSource(
tableView: UITableView,
userTableViewCellDelegate: UserTableViewCellDelegate?
2021-11-01 12:54:07 +01:00
) {
diffableDataSource = UserSection.diffableDataSource(
tableView: tableView,
context: context,
configuration: UserSection.Configuration(
userTableViewCellDelegate: userTableViewCellDelegate
)
2021-11-01 12:54:07 +01:00
)
2021-11-02 07:56:42 +01:00
// workaround to append loader wrong animation issue
2021-11-01 12:54:07 +01:00
// set empty section to make update animation top-to-bottom style
var snapshot = NSDiffableDataSourceSnapshot<UserSection, UserItem>()
snapshot.appendSections([.main])
snapshot.appendItems([.bottomLoader], toSection: .main)
diffableDataSource?.applySnapshotUsingReloadData(snapshot, completion: nil)
2021-11-01 12:54:07 +01:00
userFetchedResultsController.$records
2021-11-01 12:54:07 +01:00
.receive(on: DispatchQueue.main)
.sink { [weak self] records in
2021-11-01 12:54:07 +01:00
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
var snapshot = NSDiffableDataSourceSnapshot<UserSection, UserItem>()
snapshot.appendSections([.main])
let items = records.map { UserItem.user(record: $0) }
2021-11-01 12:54:07 +01:00
snapshot.appendItems(items, toSection: .main)
if let currentState = self.stateMachine.currentState {
switch currentState {
case is State.Idle, is State.Loading, is State.Fail:
snapshot.appendItems([.bottomLoader], toSection: .main)
case is State.NoMore:
guard let userID = self.userID,
userID != self.authContext.mastodonAuthenticationBox.userID
else { break }
// display hint footer exclude self
let text = L10n.Scene.Follower.footer
2021-11-02 07:56:42 +01:00
snapshot.appendItems([.bottomHeader(text: text)], toSection: .main)
2021-11-01 12:54:07 +01:00
default:
break
}
}
diffableDataSource.apply(snapshot, animatingDifferences: false)
2021-11-01 12:54:07 +01:00
}
.store(in: &disposeBag)
}
}