Fix crash when a server has an emoji category named after its domain (#1045)

This commit is contained in:
Jed Fox 2023-05-15 03:35:08 -04:00 committed by GitHub
parent 774b7830d5
commit 88d54b6151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 6 deletions

View File

@ -8,5 +8,6 @@
import Foundation import Foundation
public enum CustomEmojiPickerSection: Equatable, Hashable { public enum CustomEmojiPickerSection: Equatable, Hashable {
case uncategorized
case emoji(name: String) case emoji(name: String)
} }

View File

@ -11,6 +11,7 @@ import MastodonCore
extension CustomEmojiPickerSection { extension CustomEmojiPickerSection {
static func collectionViewDiffableDataSource( static func collectionViewDiffableDataSource(
collectionView: UICollectionView, collectionView: UICollectionView,
authContext: AuthContext,
context: AppContext context: AppContext
) -> UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem> { ) -> UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem> {
let dataSource = UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem>(collectionView: collectionView) { [weak context] collectionView, indexPath, item -> UICollectionViewCell? in let dataSource = UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem>(collectionView: collectionView) { [weak context] collectionView, indexPath, item -> UICollectionViewCell? in
@ -44,6 +45,8 @@ extension CustomEmojiPickerSection {
case String(describing: CustomEmojiPickerHeaderCollectionReusableView.self): case String(describing: CustomEmojiPickerHeaderCollectionReusableView.self):
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: String(describing: CustomEmojiPickerHeaderCollectionReusableView.self), for: indexPath) as! CustomEmojiPickerHeaderCollectionReusableView let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: String(describing: CustomEmojiPickerHeaderCollectionReusableView.self), for: indexPath) as! CustomEmojiPickerHeaderCollectionReusableView
switch section { switch section {
case .uncategorized:
header.titleLabel.text = authContext.mastodonAuthenticationBox.domain.uppercased()
case .emoji(let name): case .emoji(let name):
header.titleLabel.text = name header.titleLabel.text = name
} }

View File

@ -102,11 +102,11 @@ extension ComposeContentViewModel {
) { ) {
let diffableDataSource = CustomEmojiPickerSection.collectionViewDiffableDataSource( let diffableDataSource = CustomEmojiPickerSection.collectionViewDiffableDataSource(
collectionView: collectionView, collectionView: collectionView,
authContext: authContext,
context: context context: context
) )
self.customEmojiPickerDiffableDataSource = diffableDataSource self.customEmojiPickerDiffableDataSource = diffableDataSource
let domain = authContext.mastodonAuthenticationBox.domain.uppercased()
customEmojiViewModel?.emojis customEmojiViewModel?.emojis
// Don't block the main queue // Don't block the main queue
.receive(on: DispatchQueue.global(qos: .userInteractive)) .receive(on: DispatchQueue.global(qos: .userInteractive))
@ -144,11 +144,13 @@ extension ComposeContentViewModel {
.map({ (emojiMap) -> NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem> in .map({ (emojiMap) -> NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem> in
var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>() var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
let customEmojiSection = CustomEmojiPickerSection.emoji(name: domain) if !emojiMap.noCategory.isEmpty {
snapshot.appendSections([customEmojiSection]) let customEmojiSection = CustomEmojiPickerSection.uncategorized
snapshot.appendItems(emojiMap.noCategory.map({ emoji in snapshot.appendSections([customEmojiSection])
CustomEmojiPickerItem.emoji(attribute: CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji)) snapshot.appendItems(emojiMap.noCategory.map({ emoji in
}), toSection: customEmojiSection) CustomEmojiPickerItem.emoji(attribute: CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji))
}), toSection: customEmojiSection)
}
emojiMap.categorised.keys.sorted().forEach { category in emojiMap.categorised.keys.sorted().forEach { category in
let section = CustomEmojiPickerSection.emoji(name: category) let section = CustomEmojiPickerSection.emoji(name: category)
snapshot.appendSections([section]) snapshot.appendSections([section])