Integrate emojibase (#1449)

* Integrate emojibase

* pin emojibase to version rather than revision

* Log error in case of failed loading of emojis from emojibase.

* Update Package.resolved

* Fix tests and bindings version
This commit is contained in:
David Langley
2023-08-10 19:14:18 +01:00
committed by GitHub
parent 975db4deb3
commit 8512d4c857
14 changed files with 86 additions and 30105 deletions
@@ -17,19 +17,13 @@
import Foundation
struct EmojiItem: Equatable, Identifiable {
var id: String
let name: String
var id: String {
label
}
let label: String
let unicode: String
let keywords: [String]
let shortcodes: [String]
let skins: [String]
}
extension EmojiItem {
init?(from emojiMart: EmojiMartEmoji) {
id = emojiMart.id
name = emojiMart.name
keywords = emojiMart.keywords
skins = emojiMart.skins.map { emojiMartEmojiSkin in
emojiMartEmojiSkin.native
}
}
}
@@ -1,22 +0,0 @@
//
// Copyright 2022 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
struct EmojiMartCategory: Decodable {
let id: String
let emojis: [String]
}
@@ -1,29 +0,0 @@
//
// Copyright 2022 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
struct EmojiMartEmoji: Decodable {
let id: String
let name: String
let keywords: [String]
let skins: [EmojiMartEmojiSkin]
}
struct EmojiMartEmojiSkin: Decodable {
let unified: String
let native: String
}
@@ -1,60 +0,0 @@
//
// Copyright 2022 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
private enum EmojiMartJSONLoaderError: Error {
case fileNotFound
}
class EmojiMartJSONLoader: EmojiLoaderProtocol {
/// Emoji data coming from https://github.com/missive/emoji-mart/blob/main/packages/emoji-mart-data/sets/14/apple.json
private let jsonFilename = "apple_emojis_data"
func load() async -> [EmojiCategory] {
do {
let data = try await loadJSONData()
let store = try await decodeJSONData(data: data)
return emojiCategories(from: store)
} catch {
MXLog.error("Couldn't parse emoji json")
return []
}
}
private func loadJSONData() async throws -> Data {
guard let jsonDataURL = Bundle.main.url(forResource: jsonFilename, withExtension: "json") else {
throw EmojiMartJSONLoaderError.fileNotFound
}
return try Data(contentsOf: jsonDataURL)
}
private func decodeJSONData(data: Data) async throws -> EmojiMartStore {
try JSONDecoder().decode(EmojiMartStore.self, from: data)
}
private func emojiCategories(from emojiMartStore: EmojiMartStore) -> [EmojiCategory] {
emojiMartStore.categories.map { emojiMartCategory -> EmojiCategory in
let emojiItems = emojiMartCategory.emojis.compactMap { emoji -> EmojiItem? in
guard let emojiMartEmoji = emojiMartStore.emojis.first(where: { $0.id == emoji }) else {
return nil
}
return EmojiItem(from: emojiMartEmoji)
}
return EmojiCategory(id: emojiMartCategory.id, emojis: emojiItems)
}
}
}
@@ -1,35 +0,0 @@
//
// Copyright 2022 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
struct EmojiMartStore: Decodable {
let categories: [EmojiMartCategory]
let emojis: [EmojiMartEmoji]
enum CodingKeys: CodingKey {
case categories
case emojis
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
categories = try container.decode([EmojiMartCategory].self, forKey: .categories)
let emojisDictionary = try container.decode([String: EmojiMartEmoji].self, forKey: .emojis)
emojis = emojisDictionary.map(\.value)
}
}
File diff suppressed because one or more lines are too long
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Emojibase
import Foundation
@MainActor
@@ -31,7 +32,7 @@ class EmojiProvider: EmojiProviderProtocol {
private let loader: EmojiLoaderProtocol
private var state: EmojiProviderState = .notLoaded
init(loader: EmojiLoaderProtocol = EmojiMartJSONLoader()) {
init(loader: EmojiLoaderProtocol = EmojibaseDatasource()) {
self.loader = loader
Task {
await loadIfNeeded()
@@ -50,7 +51,7 @@ class EmojiProvider: EmojiProviderProtocol {
private func search(searchString: String, emojiCategories: [EmojiCategory]) -> [EmojiCategory] {
emojiCategories.compactMap { category in
let emojis = category.emojis.filter { emoji in
let searchArray = [emoji.id, emoji.name] + emoji.keywords
let searchArray = [emoji.label + emoji.unicode] + emoji.shortcodes + emoji.keywords
return searchArray.description.range(of: searchString, options: .caseInsensitive) != nil
}
return emojis.isEmpty ? nil : EmojiCategory(id: category.id, emojis: emojis)
@@ -74,3 +75,28 @@ class EmojiProvider: EmojiProviderProtocol {
}
}
}
extension EmojibaseDatasource: EmojiLoaderProtocol {
func load() async -> [EmojiCategory] {
do {
let store: EmojibaseStore = try await load()
return EmojibaseCategory.allCases.map { category in
let emojis = store.categories[category.rawValue] ?? []
return EmojiCategory(id: category.rawValue, emojis: emojis.compactMap(EmojiItem.init(from:)))
}
} catch {
MXLog.error("Failed retrieving emojis from the emojibase datasource: \(error)")
}
return []
}
}
extension EmojiItem {
init?(from emojibase: Emoji) {
unicode = emojibase.unicode
label = emojibase.label
shortcodes = emojibase.shortcodes
keywords = emojibase.tags ?? []
skins = emojibase.skins?.map(\.unicode) ?? []
}
}