Files
bundesmessenger-ios/Riot/Modules/Room/EmojiPicker/Data/Store/EmojiStore.swift
T
Johannes Marbach 2050c0dd3f EmojiStore: Include short name when searching for emojis
This adds the "common" short name to the list of strings to match the search text
against. Previously, only the "other" short names were included in the comparison.
This causes an issue for certain emojis like, for instance, the "Hundred Points
Symbol" where the term "100" is *only* included in the common short name. As a
result, the emoji did not previously show up when searching for "100".

Note that as a side effect, searching for "2" will now also return things such as
the "dog2" emoji. This matches the behavior in the Element Android app and also in
the emoji-mart Node.js package.

Closes: #4063

Signed-off-by: Johannes Marbach <n0-0ne+github@mailbox.org>
2021-03-05 19:48:00 +01:00

74 lines
2.5 KiB
Swift

/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
final class EmojiStore {
static let shared = EmojiStore()
// MARK: - Properties
private var emojiCategories: [EmojiCategory] = []
// MARK: - Public
func getAll() -> [EmojiCategory] {
return self.emojiCategories
}
func set(_ emojiCategories: [EmojiCategory]) {
self.emojiCategories = emojiCategories
}
func findEmojiItemsSortedByCategory(with searchText: String) -> [EmojiCategory] {
let initial: [EmojiCategory] = []
let filteredEmojiCategories = emojiCategories.reduce(into: initial) { (filteredEmojiCategories, emojiCategory) in
let filteredEmojiItems = emojiCategory.emojis.filter({ (emojiItem) -> Bool in
// Do not use `String.localizedCaseInsensitiveContains` here as EmojiItem data is not localized for the moment
if emojiItem.shortName.vc_caseInsensitiveContains(searchText) {
return true
}
if emojiItem.name.vc_caseInsensitiveContains(searchText) {
return true
}
if emojiItem.keywords.contains(where: { $0.vc_caseInsensitiveContains(searchText) }) {
return true
}
let shortNamesMatch = emojiItem.shortNames.contains { text -> Bool in
return text.vc_caseInsensitiveContains(searchText)
}
return shortNamesMatch
})
if filteredEmojiItems.isEmpty == false {
let filteredEmojiCategory = EmojiCategory(identifier: emojiCategory.identifier, emojis: filteredEmojiItems)
filteredEmojiCategories.append(filteredEmojiCategory)
}
}
return filteredEmojiCategories
}
}