mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 00:01:00 +00:00
Cleanup
This commit is contained in:
parent
e62e87510d
commit
8086b0974b
3 changed files with 47 additions and 36 deletions
|
@ -13,44 +13,21 @@ final class ExploreDataSource: UICollectionViewDiffableDataSource<ExploreViewMod
|
||||||
|
|
||||||
init(collectionView: UICollectionView, viewModel: ExploreViewModel) {
|
init(collectionView: UICollectionView, viewModel: ExploreViewModel) {
|
||||||
self.collectionView = collectionView
|
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) {
|
super.init(collectionView: collectionView) {
|
||||||
switch $2 {
|
switch $2 {
|
||||||
case let .tag(tag):
|
case let .tag(tag):
|
||||||
return $0.dequeueConfiguredReusableCell(
|
return $0.dequeueConfiguredReusableCell(
|
||||||
using: tagRegistration,
|
using: Self.tagRegistration,
|
||||||
for: $1,
|
for: $1,
|
||||||
item: viewModel.viewModel(tag: tag))
|
item: viewModel.viewModel(tag: tag))
|
||||||
case .instance:
|
case .instance:
|
||||||
return $0.dequeueConfiguredReusableCell(
|
return $0.dequeueConfiguredReusableCell(
|
||||||
using: instanceRegistration,
|
using: Self.instanceRegistration,
|
||||||
for: $1,
|
for: $1,
|
||||||
item: viewModel.instanceViewModel)
|
item: viewModel.instanceViewModel)
|
||||||
default:
|
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 {
|
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?) {
|
func update(tags: [Tag], instanceViewModel: InstanceViewModel?) {
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<ExploreViewModel.Section, ExploreViewModel.Item>()
|
var newsnapshot = NSDiffableDataSourceSnapshot<ExploreViewModel.Section, ExploreViewModel.Item>()
|
||||||
|
|
||||||
if !tags.isEmpty {
|
if !tags.isEmpty {
|
||||||
snapshot.appendSections([.trending])
|
newsnapshot.appendSections([.trending])
|
||||||
snapshot.appendItems(tags.map(ExploreViewModel.Item.tag), toSection: .trending)
|
newsnapshot.appendItems(tags.map(ExploreViewModel.Item.tag), toSection: .trending)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let instanceViewModel = instanceViewModel {
|
if let instanceViewModel = instanceViewModel {
|
||||||
snapshot.appendSections([.instance])
|
newsnapshot.appendSections([.instance])
|
||||||
snapshot.appendItems([.instance], toSection: .instance)
|
newsnapshot.appendItems([.instance], toSection: .instance)
|
||||||
|
|
||||||
if instanceViewModel.instance.canShowProfileDirectory {
|
if instanceViewModel.instance.canShowProfileDirectory {
|
||||||
snapshot.appendItems([.profileDirectory], toSection: .instance)
|
newsnapshot.appendItems([.profileDirectory], toSection: .instance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let wasEmpty = self.snapshot().itemIdentifiers.isEmpty
|
let wasEmpty = self.snapshot().itemIdentifiers.isEmpty
|
||||||
let contentOffset = collectionView?.contentOffset
|
let contentOffset = collectionView?.contentOffset
|
||||||
|
|
||||||
apply(snapshot, animatingDifferences: false) {
|
apply(newsnapshot, animatingDifferences: false) {
|
||||||
if let contentOffset = contentOffset, !wasEmpty {
|
if let contentOffset = contentOffset, !wasEmpty {
|
||||||
self.collectionView?.contentOffset = contentOffset
|
self.collectionView?.contentOffset = contentOffset
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,14 +246,19 @@ private extension CompositionView {
|
||||||
}
|
}
|
||||||
|
|
||||||
func changeIdentityMenu(identities: [Identity]) -> UIMenu {
|
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
|
UIDeferredMenuElement { completion in
|
||||||
let action = UIAction(title: identity.handle) { [weak self] _ in
|
let action = UIAction(title: identity.handle) { [weak self] _ in
|
||||||
self?.parentViewModel.changeIdentity(identity)
|
self?.parentViewModel.changeIdentity(identity)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let image = identity.image {
|
if let image = identity.image {
|
||||||
KingfisherManager.shared.retrieveImage(with: image) {
|
KingfisherManager.shared.retrieveImage(with: image, options: imageOptions) {
|
||||||
if case let .success(value) = $0 {
|
if case let .success(value) = $0 {
|
||||||
action.image = value.image
|
action.image = value.image
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,11 @@ final class SecondaryNavigationButton: UIBarButtonItem {
|
||||||
}
|
}
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
|
|
||||||
|
let processor = RoundCornerImageProcessor(radius: .widthFraction(0.5))
|
||||||
|
var imageOptions = KingfisherManager.shared.defaultOptions
|
||||||
|
|
||||||
|
imageOptions.append(.processor(processor))
|
||||||
|
|
||||||
viewModel.$recentIdentities.sink { identities in
|
viewModel.$recentIdentities.sink { identities in
|
||||||
button.menu = UIMenu(children: identities.map { identity in
|
button.menu = UIMenu(children: identities.map { identity in
|
||||||
UIDeferredMenuElement { completion in
|
UIDeferredMenuElement { completion in
|
||||||
|
@ -42,7 +47,7 @@ final class SecondaryNavigationButton: UIBarButtonItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let image = identity.image {
|
if let image = identity.image {
|
||||||
KingfisherManager.shared.retrieveImage(with: image) {
|
KingfisherManager.shared.retrieveImage(with: image, options: imageOptions) {
|
||||||
if case let .success(value) = $0 {
|
if case let .success(value) = $0 {
|
||||||
action.image = value.image
|
action.image = value.image
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue