2022-02-10 12:30:41 +01:00
|
|
|
//
|
|
|
|
// SuggestionAccountViewModel+Diffable.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-2-10.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension SuggestionAccountViewModel {
|
|
|
|
|
|
|
|
func setupDiffableDataSource(
|
|
|
|
tableView: UITableView,
|
|
|
|
suggestionAccountTableViewCellDelegate: SuggestionAccountTableViewCellDelegate
|
|
|
|
) {
|
|
|
|
tableViewDiffableDataSource = RecommendAccountSection.tableViewDiffableDataSource(
|
|
|
|
tableView: tableView,
|
|
|
|
context: context,
|
|
|
|
configuration: RecommendAccountSection.Configuration(
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: authContext,
|
2022-02-10 12:30:41 +01:00
|
|
|
suggestionAccountTableViewCellDelegate: suggestionAccountTableViewCellDelegate
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
userFetchedResultsController.$records
|
2022-02-16 10:25:55 +01:00
|
|
|
.removeDuplicates()
|
2022-02-10 12:30:41 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] records in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let tableViewDiffableDataSource = self.tableViewDiffableDataSource else { return }
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<RecommendAccountSection, RecommendAccountItem>()
|
|
|
|
snapshot.appendSections([.main])
|
|
|
|
let items: [RecommendAccountItem] = records.map { RecommendAccountItem.account($0) }
|
|
|
|
snapshot.appendItems(items, toSection: .main)
|
|
|
|
|
2022-12-17 20:26:20 +01:00
|
|
|
tableViewDiffableDataSource.applySnapshotUsingReloadData(snapshot, completion: nil)
|
2022-02-10 12:30:41 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupDiffableDataSource(
|
|
|
|
collectionView: UICollectionView
|
|
|
|
) {
|
|
|
|
collectionViewDiffableDataSource = SelectedAccountSection.collectionViewDiffableDataSource(
|
|
|
|
collectionView: collectionView,
|
|
|
|
context: context
|
|
|
|
)
|
|
|
|
|
2022-02-16 10:25:55 +01:00
|
|
|
selectedUserFetchedResultsController.$records
|
2022-02-10 12:30:41 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] records in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let collectionViewDiffableDataSource = self.collectionViewDiffableDataSource else { return }
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<SelectedAccountSection, SelectedAccountItem>()
|
|
|
|
snapshot.appendSections([.main])
|
2022-02-16 10:25:55 +01:00
|
|
|
var items: [SelectedAccountItem] = records.map { SelectedAccountItem.account($0) }
|
|
|
|
|
|
|
|
if items.count < 10 {
|
|
|
|
let count = 10 - items.count
|
|
|
|
let placeholderItems: [SelectedAccountItem] = (0..<count).map { _ in
|
|
|
|
SelectedAccountItem.placeHolder(uuid: UUID())
|
|
|
|
}
|
|
|
|
items.append(contentsOf: placeholderItems)
|
|
|
|
}
|
|
|
|
|
2022-02-10 12:30:41 +01:00
|
|
|
snapshot.appendItems(items, toSection: .main)
|
2022-12-17 20:26:20 +01:00
|
|
|
collectionViewDiffableDataSource.applySnapshotUsingReloadData(snapshot, completion: nil)
|
2022-02-10 12:30:41 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|