Fix crash/hang when showing emoji autocomplete with very many emojis

Closes #424
This commit is contained in:
Shadowfacts 2023-07-13 23:41:02 -07:00
parent 7f12479514
commit 33999fe895
1 changed files with 11 additions and 16 deletions

View File

@ -18,19 +18,7 @@ class AutocompleteEmojisController: ViewController {
@Published var expanded = false @Published var expanded = false
@Published var emojis: [Emoji] = [] @Published var emojis: [Emoji] = []
@Published var emojisBySection: [String: [Emoji]] = [:]
var emojisBySection: [String: [Emoji]] {
var values: [String: [Emoji]] = [:]
for emoji in emojis {
let key = emoji.category ?? ""
if !values.keys.contains(key) {
values[key] = [emoji]
} else {
values[key]!.append(emoji)
}
}
return values
}
init(composeController: ComposeController) { init(composeController: ComposeController) {
self.composeController = composeController self.composeController = composeController
@ -77,11 +65,20 @@ class AutocompleteEmojisController: ViewController {
var shortcodes = Set<String>() var shortcodes = Set<String>()
var newEmojis = [Emoji]() var newEmojis = [Emoji]()
var newEmojisBySection = [String: [Emoji]]()
for emoji in emojis where !shortcodes.contains(emoji.shortcode) { for emoji in emojis where !shortcodes.contains(emoji.shortcode) {
newEmojis.append(emoji) newEmojis.append(emoji)
shortcodes.insert(emoji.shortcode) shortcodes.insert(emoji.shortcode)
let category = emoji.category ?? ""
if newEmojisBySection.keys.contains(category) {
newEmojisBySection[category]!.append(emoji)
} else {
newEmojisBySection[category] = [emoji]
}
} }
self.emojis = newEmojis self.emojis = newEmojis
self.emojisBySection = newEmojisBySection
} }
private func toggleExpanded() { private func toggleExpanded() {
@ -160,7 +157,7 @@ class AutocompleteEmojisController: ViewController {
private var horizontalScrollView: some View { private var horizontalScrollView: some View {
ScrollView(.horizontal) { ScrollView(.horizontal) {
HStack(spacing: 8) { LazyHStack(spacing: 8) {
ForEach(controller.emojis, id: \.shortcode) { emoji in ForEach(controller.emojis, id: \.shortcode) { emoji in
Button(action: { controller.autocomplete(with: emoji) }) { Button(action: { controller.autocomplete(with: emoji) }) {
HStack(spacing: 4) { HStack(spacing: 4) {
@ -174,8 +171,6 @@ class AutocompleteEmojisController: ViewController {
.frame(height: emojiSize) .frame(height: emojiSize)
} }
.animation(.linear(duration: 0.2), value: controller.emojis) .animation(.linear(duration: 0.2), value: controller.emojis)
Spacer(minLength: emojiSize)
} }
.padding(.horizontal, 8) .padding(.horizontal, 8)
.frame(height: emojiSize + 16) .frame(height: emojiSize + 16)