From 8086b0974b774612d1a3b9549b5678dd0697a949 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sat, 30 Jan 2021 22:30:12 -0800 Subject: [PATCH] Cleanup --- Data Sources/ExploreDataSource.swift | 67 +++++++++++---------- Views/UIKit/CompositionView.swift | 9 ++- Views/UIKit/SecondaryNavigationButton.swift | 7 ++- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/Data Sources/ExploreDataSource.swift b/Data Sources/ExploreDataSource.swift index efaaeb9..2c36a4d 100644 --- a/Data Sources/ExploreDataSource.swift +++ b/Data Sources/ExploreDataSource.swift @@ -13,44 +13,21 @@ final class ExploreDataSource: UICollectionViewDiffableDataSource { - $0.viewModel = $2 - } - - let instanceRegistration = UICollectionView.CellRegistration { - $0.viewModel = $2 - } - - let itemRegistration = UICollectionView.CellRegistration - { - var configuration = $0.defaultContentConfiguration() - - switch $2 { - case .profileDirectory: - configuration.text = NSLocalizedString("explore.profile-directory", comment: "") - configuration.image = UIImage(systemName: "person.crop.square.fill.and.at.rectangle") - default: - break - } - - $0.contentConfiguration = configuration - $0.accessories = [.disclosureIndicator()] - } super.init(collectionView: collectionView) { switch $2 { case let .tag(tag): return $0.dequeueConfiguredReusableCell( - using: tagRegistration, + using: Self.tagRegistration, for: $1, item: viewModel.viewModel(tag: tag)) case .instance: return $0.dequeueConfiguredReusableCell( - using: instanceRegistration, + using: Self.instanceRegistration, for: $1, item: viewModel.instanceViewModel) default: - return $0.dequeueConfiguredReusableCell(using: itemRegistration, for: $1, item: $2) + return $0.dequeueConfiguredReusableCell(using: Self.itemRegistration, for: $1, item: $2) } } @@ -78,27 +55,51 @@ final class ExploreDataSource: UICollectionViewDiffableDataSource { + $0.viewModel = $2 + } + + static let instanceRegistration = UICollectionView.CellRegistration { + $0.viewModel = $2 + } + + static let itemRegistration = UICollectionView.CellRegistration + { + var configuration = $0.defaultContentConfiguration() + + switch $2 { + case .profileDirectory: + configuration.text = NSLocalizedString("explore.profile-directory", comment: "") + configuration.image = UIImage(systemName: "person.crop.square.fill.and.at.rectangle") + default: + break + } + + $0.contentConfiguration = configuration + $0.accessories = [.disclosureIndicator()] + } + func update(tags: [Tag], instanceViewModel: InstanceViewModel?) { - var snapshot = NSDiffableDataSourceSnapshot() + var newsnapshot = NSDiffableDataSourceSnapshot() if !tags.isEmpty { - snapshot.appendSections([.trending]) - snapshot.appendItems(tags.map(ExploreViewModel.Item.tag), toSection: .trending) + newsnapshot.appendSections([.trending]) + newsnapshot.appendItems(tags.map(ExploreViewModel.Item.tag), toSection: .trending) } if let instanceViewModel = instanceViewModel { - snapshot.appendSections([.instance]) - snapshot.appendItems([.instance], toSection: .instance) + newsnapshot.appendSections([.instance]) + newsnapshot.appendItems([.instance], toSection: .instance) if instanceViewModel.instance.canShowProfileDirectory { - snapshot.appendItems([.profileDirectory], toSection: .instance) + newsnapshot.appendItems([.profileDirectory], toSection: .instance) } } let wasEmpty = self.snapshot().itemIdentifiers.isEmpty let contentOffset = collectionView?.contentOffset - apply(snapshot, animatingDifferences: false) { + apply(newsnapshot, animatingDifferences: false) { if let contentOffset = contentOffset, !wasEmpty { self.collectionView?.contentOffset = contentOffset } diff --git a/Views/UIKit/CompositionView.swift b/Views/UIKit/CompositionView.swift index 84b8249..0fada60 100644 --- a/Views/UIKit/CompositionView.swift +++ b/Views/UIKit/CompositionView.swift @@ -246,14 +246,19 @@ private extension CompositionView { } func changeIdentityMenu(identities: [Identity]) -> UIMenu { - UIMenu(children: identities.map { identity in + let processor = RoundCornerImageProcessor(radius: .widthFraction(0.5)) + var imageOptions = KingfisherManager.shared.defaultOptions + + imageOptions.append(.processor(processor)) + + return UIMenu(children: identities.map { identity in UIDeferredMenuElement { completion in let action = UIAction(title: identity.handle) { [weak self] _ in self?.parentViewModel.changeIdentity(identity) } if let image = identity.image { - KingfisherManager.shared.retrieveImage(with: image) { + KingfisherManager.shared.retrieveImage(with: image, options: imageOptions) { if case let .success(value) = $0 { action.image = value.image } diff --git a/Views/UIKit/SecondaryNavigationButton.swift b/Views/UIKit/SecondaryNavigationButton.swift index 15e3bd6..de2690c 100644 --- a/Views/UIKit/SecondaryNavigationButton.swift +++ b/Views/UIKit/SecondaryNavigationButton.swift @@ -34,6 +34,11 @@ final class SecondaryNavigationButton: UIBarButtonItem { } .store(in: &cancellables) + let processor = RoundCornerImageProcessor(radius: .widthFraction(0.5)) + var imageOptions = KingfisherManager.shared.defaultOptions + + imageOptions.append(.processor(processor)) + viewModel.$recentIdentities.sink { identities in button.menu = UIMenu(children: identities.map { identity in UIDeferredMenuElement { completion in @@ -42,7 +47,7 @@ final class SecondaryNavigationButton: UIBarButtonItem { } if let image = identity.image { - KingfisherManager.shared.retrieveImage(with: image) { + KingfisherManager.shared.retrieveImage(with: image, options: imageOptions) { if case let .success(value) = $0 { action.image = value.image }