EmojiManager.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Foundation
  2. struct Emoji: Decodable {
  3. let emoji: String
  4. let aliases: [String]
  5. let tags: [String]
  6. func getUnicode() -> String {
  7. return emoji
  8. }
  9. }
  10. class EmojiManager {
  11. private static let tag = "EmojiManager"
  12. private static var emojis: Dictionary<String, Emoji> = [:]
  13. static let shared = EmojiManager()
  14. init() {
  15. // emojis.json pulled from https://github.com/github/gemoji/blob/master/db/emoji.json
  16. if let url = Bundle.main.url(forResource: "emojis", withExtension: "json") {
  17. do {
  18. let jsonData = try Data(contentsOf: url)
  19. if let jsonEmojis = try? JSONDecoder().decode([Emoji].self, from: jsonData) {
  20. for emoji in jsonEmojis {
  21. if !emoji.aliases.isEmpty {
  22. EmojiManager.emojis[emoji.aliases.first!] = emoji
  23. }
  24. }
  25. }
  26. } catch {
  27. Log.e(EmojiManager.tag, "Unable to load emojis: \(error.localizedDescription)", error)
  28. }
  29. }
  30. }
  31. func getEmojiByAlias(alias: String) -> Emoji? {
  32. if alias.isEmpty {
  33. return nil
  34. }
  35. return EmojiManager.emojis[alias]
  36. }
  37. }