This commit is contained in:
Justin Mazzocchi 2021-01-30 22:30:12 -08:00
parent e62e87510d
commit 8086b0974b
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
3 changed files with 47 additions and 36 deletions

View File

@ -13,44 +13,21 @@ final class ExploreDataSource: UICollectionViewDiffableDataSource<ExploreViewMod
init(collectionView: UICollectionView, viewModel: ExploreViewModel) {
self.collectionView = collectionView
let tagRegistration = UICollectionView.CellRegistration<TagCollectionViewCell, TagViewModel> {
$0.viewModel = $2
}
let instanceRegistration = UICollectionView.CellRegistration<InstanceCollectionViewCell, InstanceViewModel> {
$0.viewModel = $2
}
let itemRegistration = UICollectionView.CellRegistration
<SeparatorConfiguredCollectionViewListCell, ExploreViewModel.Item> {
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<ExploreViewMod
}
private extension ExploreDataSource {
static let tagRegistration = UICollectionView.CellRegistration<TagCollectionViewCell, TagViewModel> {
$0.viewModel = $2
}
static let instanceRegistration = UICollectionView.CellRegistration<InstanceCollectionViewCell, InstanceViewModel> {
$0.viewModel = $2
}
static let itemRegistration = UICollectionView.CellRegistration
<SeparatorConfiguredCollectionViewListCell, ExploreViewModel.Item> {
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<ExploreViewModel.Section, ExploreViewModel.Item>()
var newsnapshot = NSDiffableDataSourceSnapshot<ExploreViewModel.Section, ExploreViewModel.Item>()
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
}

View File

@ -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
}

View File

@ -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
}