Sort emoji alphabetically, and into sections
This commit is contained in:
parent
c2bb14eaab
commit
5c508dfce0
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
import MastodonCore
|
import MastodonCore
|
||||||
|
import MastodonSDK
|
||||||
import CoreDataStack
|
import CoreDataStack
|
||||||
import UIHostingConfigurationBackport
|
import UIHostingConfigurationBackport
|
||||||
|
|
||||||
@ -114,24 +115,64 @@ extension ComposeContentViewModel {
|
|||||||
|
|
||||||
let domain = authContext.mastodonAuthenticationBox.domain.uppercased()
|
let domain = authContext.mastodonAuthenticationBox.domain.uppercased()
|
||||||
customEmojiViewModel?.emojis
|
customEmojiViewModel?.emojis
|
||||||
.receive(on: DispatchQueue.main)
|
// Don't block the main queue
|
||||||
.sink { [weak self, weak diffableDataSource] emojis in
|
.receive(on: DispatchQueue.global(qos: .userInteractive))
|
||||||
guard let _ = self else { return }
|
// Sort emojis
|
||||||
guard let diffableDataSource = diffableDataSource else { return }
|
.map({ (emojis) -> [Mastodon.Entity.Emoji] in
|
||||||
|
return emojis.sorted { a, b in
|
||||||
|
a.shortcode.lowercased() < b.shortcode.lowercased()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// Collate emojis into categories
|
||||||
|
.map({ (emojis) -> (noCategory: [Mastodon.Entity.Emoji], categorised: [String:[Mastodon.Entity.Emoji]]) in
|
||||||
|
let emojiMap: (noCategory: [Mastodon.Entity.Emoji], categorised: [String:[Mastodon.Entity.Emoji]]) = {
|
||||||
|
var noCategory = [Mastodon.Entity.Emoji]()
|
||||||
|
var categorised = [String:[Mastodon.Entity.Emoji]]()
|
||||||
|
|
||||||
|
for emoji in emojis where emoji.visibleInPicker {
|
||||||
|
if let category = emoji.category {
|
||||||
|
var categoryArray = categorised[category] ?? [Mastodon.Entity.Emoji]()
|
||||||
|
categoryArray.append(emoji)
|
||||||
|
categorised[category] = categoryArray
|
||||||
|
} else {
|
||||||
|
noCategory.append(emoji)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
noCategory,
|
||||||
|
categorised
|
||||||
|
)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return emojiMap
|
||||||
|
})
|
||||||
|
// Build snapshot from emoji map
|
||||||
|
.map({ (emojiMap) -> NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem> in
|
||||||
|
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
|
var snapshot = NSDiffableDataSourceSnapshot<CustomEmojiPickerSection, CustomEmojiPickerItem>()
|
||||||
let customEmojiSection = CustomEmojiPickerSection.emoji(name: domain)
|
let customEmojiSection = CustomEmojiPickerSection.emoji(name: domain)
|
||||||
snapshot.appendSections([customEmojiSection])
|
snapshot.appendSections([customEmojiSection])
|
||||||
let items: [CustomEmojiPickerItem] = {
|
snapshot.appendItems(emojiMap.noCategory.map({ emoji in
|
||||||
var items = [CustomEmojiPickerItem]()
|
CustomEmojiPickerItem.emoji(attribute: CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji))
|
||||||
for emoji in emojis where emoji.visibleInPicker {
|
}), toSection: customEmojiSection)
|
||||||
let attribute = CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji)
|
emojiMap.categorised.keys.sorted().forEach { category in
|
||||||
let item = CustomEmojiPickerItem.emoji(attribute: attribute)
|
let section = CustomEmojiPickerSection.emoji(name: category)
|
||||||
items.append(item)
|
snapshot.appendSections([section])
|
||||||
|
if let items = emojiMap.categorised[category] {
|
||||||
|
snapshot.appendItems(items.map({ emoji in
|
||||||
|
CustomEmojiPickerItem.emoji(attribute: CustomEmojiPickerItem.CustomEmojiAttribute(emoji: emoji))
|
||||||
|
}), toSection: section)
|
||||||
}
|
}
|
||||||
return items
|
}
|
||||||
}()
|
|
||||||
snapshot.appendItems(items, toSection: customEmojiSection)
|
return snapshot
|
||||||
|
})
|
||||||
|
// Apply snapshot
|
||||||
|
.receive(on: DispatchQueue.main)
|
||||||
|
.sink { [weak self, weak diffableDataSource] snapshot in
|
||||||
|
guard let _ = self else { return }
|
||||||
|
guard let diffableDataSource = diffableDataSource else { return }
|
||||||
|
|
||||||
diffableDataSource.apply(snapshot)
|
diffableDataSource.apply(snapshot)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user