2022-04-14 15:15:21 +02:00
|
|
|
//
|
|
|
|
// DiscoveryForYouViewModel+Diffable.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-14.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2022-04-15 11:17:39 +02:00
|
|
|
import MastodonUI
|
2022-04-14 15:15:21 +02:00
|
|
|
|
|
|
|
extension DiscoveryForYouViewModel {
|
|
|
|
|
|
|
|
func setupDiffableDataSource(
|
2022-04-15 11:17:39 +02:00
|
|
|
tableView: UITableView,
|
|
|
|
profileCardTableViewCellDelegate: ProfileCardTableViewCellDelegate
|
2022-04-14 15:15:21 +02:00
|
|
|
) {
|
|
|
|
diffableDataSource = DiscoverySection.diffableDataSource(
|
|
|
|
tableView: tableView,
|
|
|
|
context: context,
|
2022-04-15 11:17:39 +02:00
|
|
|
configuration: DiscoverySection.Configuration(
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: authContext,
|
2022-05-16 13:42:03 +02:00
|
|
|
profileCardTableViewCellDelegate: profileCardTableViewCellDelegate,
|
|
|
|
familiarFollowers: $familiarFollowers
|
2022-04-15 11:17:39 +02:00
|
|
|
)
|
2022-04-14 15:15:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Task {
|
|
|
|
try await fetch()
|
|
|
|
}
|
|
|
|
|
|
|
|
userFetchedResultsController.$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<DiscoverySection, DiscoveryItem>()
|
|
|
|
snapshot.appendSections([.forYou])
|
|
|
|
|
|
|
|
let items = records.map { DiscoveryItem.user($0) }
|
|
|
|
snapshot.appendItems(items, toSection: .forYou)
|
|
|
|
|
2022-12-17 21:57:17 +01:00
|
|
|
diffableDataSource.apply(snapshot, animatingDifferences: false)
|
2022-04-14 15:15:21 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|