Apply filters to Trending Posts

This commit is contained in:
Shadowfacts 2022-12-14 20:52:44 -05:00
parent 1f6074e539
commit a9edeaf5b9
1 changed files with 25 additions and 13 deletions

View File

@ -12,6 +12,7 @@ import Pachyderm
class TrendingStatusesViewController: UIViewController { class TrendingStatusesViewController: UIViewController {
weak var mastodonController: MastodonController! weak var mastodonController: MastodonController!
let filterer: Filterer
private var collectionView: UICollectionView { private var collectionView: UICollectionView {
view as! UICollectionView view as! UICollectionView
@ -22,6 +23,10 @@ class TrendingStatusesViewController: UIViewController {
init(mastodonController: MastodonController) { init(mastodonController: MastodonController) {
self.mastodonController = mastodonController self.mastodonController = mastodonController
self.filterer = Filterer(mastodonController: mastodonController, context: .public)
self.filterer.htmlConverter.font = TimelineStatusCollectionViewCell.contentFont
self.filterer.htmlConverter.monospaceFont = TimelineStatusCollectionViewCell.monospaceFont
self.filterer.htmlConverter.paragraphStyle = TimelineStatusCollectionViewCell.contentParagraphStyle
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
@ -49,7 +54,7 @@ class TrendingStatusesViewController: UIViewController {
config.topSeparatorVisibility = .hidden config.topSeparatorVisibility = .hidden
config.bottomSeparatorVisibility = .hidden config.bottomSeparatorVisibility = .hidden
} }
if case .status(_, _) = item { if case .status(_, _, _) = item {
config.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets config.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
config.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets config.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
} }
@ -64,18 +69,25 @@ class TrendingStatusesViewController: UIViewController {
} }
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> { private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
let statusCell = UICollectionView.CellRegistration<TimelineStatusCollectionViewCell, (String, CollapseState)> { [unowned self] cell, indexPath, item in let statusCell = UICollectionView.CellRegistration<TimelineStatusCollectionViewCell, (String, CollapseState, Filterer.Result, NSAttributedString?)> { [unowned self] cell, indexPath, item in
cell.delegate = self cell.delegate = self
// TODO: filter these cell.updateUI(statusID: item.0, state: item.1, filterResult: item.2, precomputedContent: item.3)
cell.updateUI(statusID: item.0, state: item.1, filterResult: .allow, precomputedContent: nil) }
let zeroHeightCell = UICollectionView.CellRegistration<ZeroHeightCollectionViewCell, Void> { _, _, _ in
} }
let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, _, _ in let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, _, _ in
cell.indicator.startAnimating() cell.indicator.startAnimating()
} }
return UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in return UICollectionViewDiffableDataSource(collectionView: collectionView) { [unowned self] collectionView, indexPath, itemIdentifier in
switch itemIdentifier { switch itemIdentifier {
case .status(id: let id, state: let state): case .status(id: let id, let collapseState, let filterState):
return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state)) let (result, attributedString) = self.filterer.resolve(state: filterState, status: { mastodonController.persistentContainer.status(for: id)! })
switch result {
case .allow, .warn(_):
return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, collapseState, result, attributedString))
case .hide:
return collectionView.dequeueConfiguredReusableCell(using: zeroHeightCell, for: indexPath, item: ())
}
case .loadingIndicator: case .loadingIndicator:
return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ()) return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ())
} }
@ -115,7 +127,7 @@ class TrendingStatusesViewController: UIViewController {
await mastodonController.persistentContainer.addAll(statuses: statuses) await mastodonController.persistentContainer.addAll(statuses: statuses)
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.statuses]) snapshot.appendSections([.statuses])
snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown) }) snapshot.appendItems(statuses.map { .status(id: $0.id, collapseState: .unknown, filterState: .unknown) })
await dataSource.apply(snapshot) await dataSource.apply(snapshot)
} }
} }
@ -125,12 +137,12 @@ extension TrendingStatusesViewController {
case statuses case statuses
} }
enum Item: Hashable { enum Item: Hashable {
case status(id: String, state: CollapseState) case status(id: String, collapseState: CollapseState, filterState: FilterState)
case loadingIndicator case loadingIndicator
static func ==(lhs: Item, rhs: Item) -> Bool { static func ==(lhs: Item, rhs: Item) -> Bool {
switch (lhs, rhs) { switch (lhs, rhs) {
case (.status(id: let a, state: _), .status(id: let b, state: _)): case (.status(id: let a, _, _), .status(id: let b, _, _)):
return a == b return a == b
case (.loadingIndicator, .loadingIndicator): case (.loadingIndicator, .loadingIndicator):
return true return true
@ -141,7 +153,7 @@ extension TrendingStatusesViewController {
func hash(into hasher: inout Hasher) { func hash(into hasher: inout Hasher) {
switch self { switch self {
case .status(id: let id, state: _): case .status(id: let id, _, _):
hasher.combine(0) hasher.combine(0)
hasher.combine(id) hasher.combine(id)
case .loadingIndicator: case .loadingIndicator:
@ -158,7 +170,7 @@ extension TrendingStatusesViewController {
} }
var isSelectable: Bool { var isSelectable: Bool {
if case .status(id: _, state: _) = self { if case .status(id: _, _, _) = self {
return true return true
} else { } else {
return false return false
@ -173,7 +185,7 @@ extension TrendingStatusesViewController: UICollectionViewDelegate {
} }
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard case .status(id: let id, state: let state) = dataSource.itemIdentifier(for: indexPath) else { guard case .status(id: let id, collapseState: let state, _) = dataSource.itemIdentifier(for: indexPath) else {
return return
} }
selected(status: id, state: state.copy()) selected(status: id, state: state.copy())