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
public enum CustomEmojiPickerSection: Equatable, Hashable {
case uncategorized
case emoji(name: String)
}

View File

@ -11,6 +11,7 @@ import MastodonCore
extension CustomEmojiPickerSection {
static func collectionViewDiffableDataSource(
collectionView: UICollectionView,
authContext: AuthContext,
context: AppContext
) -> UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem> {
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):
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: String(describing: CustomEmojiPickerHeaderCollectionReusableView.self), for: indexPath) as! CustomEmojiPickerHeaderCollectionReusableView
switch section {
case .uncategorized:
header.titleLabel.text = authContext.mastodonAuthenticationBox.domain.uppercased()
case .emoji(let name):
header.titleLabel.text = name
}

View File

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