From a9edeaf5b978d38ef9c79e601ba2a1a462d32e84 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 14 Dec 2022 20:52:44 -0500 Subject: [PATCH] Apply filters to Trending Posts --- .../TrendingStatusesViewController.swift | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/Tusker/Screens/Explore/TrendingStatusesViewController.swift b/Tusker/Screens/Explore/TrendingStatusesViewController.swift index 34bb2a1d..6a8bd5fe 100644 --- a/Tusker/Screens/Explore/TrendingStatusesViewController.swift +++ b/Tusker/Screens/Explore/TrendingStatusesViewController.swift @@ -12,6 +12,7 @@ import Pachyderm class TrendingStatusesViewController: UIViewController { weak var mastodonController: MastodonController! + let filterer: Filterer private var collectionView: UICollectionView { view as! UICollectionView @@ -22,6 +23,10 @@ class TrendingStatusesViewController: UIViewController { init(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) @@ -49,7 +54,7 @@ class TrendingStatusesViewController: UIViewController { config.topSeparatorVisibility = .hidden config.bottomSeparatorVisibility = .hidden } - if case .status(_, _) = item { + if case .status(_, _, _) = item { config.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets config.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets } @@ -64,18 +69,25 @@ class TrendingStatusesViewController: UIViewController { } private func createDataSource() -> UICollectionViewDiffableDataSource { - let statusCell = UICollectionView.CellRegistration { [unowned self] cell, indexPath, item in + let statusCell = UICollectionView.CellRegistration { [unowned self] cell, indexPath, item in cell.delegate = self - // TODO: filter these - cell.updateUI(statusID: item.0, state: item.1, filterResult: .allow, precomputedContent: nil) + cell.updateUI(statusID: item.0, state: item.1, filterResult: item.2, precomputedContent: item.3) + } + let zeroHeightCell = UICollectionView.CellRegistration { _, _, _ in } let loadingCell = UICollectionView.CellRegistration { cell, _, _ in cell.indicator.startAnimating() } - return UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in + return UICollectionViewDiffableDataSource(collectionView: collectionView) { [unowned self] collectionView, indexPath, itemIdentifier in switch itemIdentifier { - case .status(id: let id, state: let state): - return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state)) + case .status(id: let id, let collapseState, let filterState): + 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: return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ()) } @@ -115,7 +127,7 @@ class TrendingStatusesViewController: UIViewController { await mastodonController.persistentContainer.addAll(statuses: statuses) var snapshot = NSDiffableDataSourceSnapshot() 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) } } @@ -125,12 +137,12 @@ extension TrendingStatusesViewController { case statuses } enum Item: Hashable { - case status(id: String, state: CollapseState) + case status(id: String, collapseState: CollapseState, filterState: FilterState) case loadingIndicator static func ==(lhs: Item, rhs: Item) -> Bool { 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 case (.loadingIndicator, .loadingIndicator): return true @@ -141,7 +153,7 @@ extension TrendingStatusesViewController { func hash(into hasher: inout Hasher) { switch self { - case .status(id: let id, state: _): + case .status(id: let id, _, _): hasher.combine(0) hasher.combine(id) case .loadingIndicator: @@ -158,7 +170,7 @@ extension TrendingStatusesViewController { } var isSelectable: Bool { - if case .status(id: _, state: _) = self { + if case .status(id: _, _, _) = self { return true } else { return false @@ -173,7 +185,7 @@ extension TrendingStatusesViewController: UICollectionViewDelegate { } 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 } selected(status: id, state: state.copy())