|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +package be.scri.helpers |
| 3 | + |
| 4 | +import android.content.Context |
| 5 | +import be.scri.R |
| 6 | + |
| 7 | +private var cachedEmojiData: MutableList<EmojiData>? = null |
| 8 | + |
| 9 | +const val EMOJI_SPEC_FILE_PATH = "emoji_spec.txt" |
| 10 | + |
| 11 | +/** |
| 12 | + * Reads the emoji spec file and returns a parsed list of EmojiData. |
| 13 | + * Directly based on Fossify's parseRawEmojiSpecsFile() implementation. |
| 14 | + * |
| 15 | + * @param context The application context used to access assets. |
| 16 | + * @param path The path to the emoji spec file within assets. |
| 17 | + * @return A mutable list of [EmojiData] objects parsed from the file. |
| 18 | + */ |
| 19 | +fun parseRawEmojiSpecsFile( |
| 20 | + context: Context, |
| 21 | + path: String, |
| 22 | +): MutableList<EmojiData> { |
| 23 | + // if (cachedEmojiData != null) { |
| 24 | + // return cachedEmojiData!! |
| 25 | + // } |
| 26 | + |
| 27 | + val emojis = mutableListOf<EmojiData>() |
| 28 | + var emojiEditorList: MutableList<String>? = null |
| 29 | + var category: String? = null |
| 30 | + |
| 31 | + fun commitEmojiEditorList() { |
| 32 | + emojiEditorList?.let { |
| 33 | + val base = it.first() |
| 34 | + val variants = it.drop(1) |
| 35 | + emojis.add(EmojiData(category ?: "none", base, variants)) |
| 36 | + } |
| 37 | + emojiEditorList = null |
| 38 | + } |
| 39 | + |
| 40 | + context.assets.open(path).bufferedReader().useLines { lines -> |
| 41 | + for (line in lines) { |
| 42 | + when { |
| 43 | + line.startsWith("#") -> { } |
| 44 | + line.startsWith("[") -> { |
| 45 | + commitEmojiEditorList() |
| 46 | + category = line.replace("[", "").replace("]", "") |
| 47 | + } |
| 48 | + line.trim().isEmpty() -> continue |
| 49 | + else -> { |
| 50 | + if (!line.startsWith("\t")) { |
| 51 | + commitEmojiEditorList() |
| 52 | + } |
| 53 | + val data = line.split(";") |
| 54 | + if (data.size == 3) { |
| 55 | + val emoji = data[0].trim() |
| 56 | + if (emojiEditorList != null) { |
| 57 | + emojiEditorList!!.add(emoji) |
| 58 | + } else { |
| 59 | + emojiEditorList = mutableListOf(emoji) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + commitEmojiEditorList() |
| 66 | + } |
| 67 | + |
| 68 | + cachedEmojiData = emojis |
| 69 | + return emojis |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Data class representing a single emoji with its category and skin tone variants. |
| 74 | + * Directly based on Fossify's EmojiData model. |
| 75 | + * |
| 76 | + * @param category The category this emoji belongs to. |
| 77 | + * @param emoji The base emoji character string. |
| 78 | + * @param variants The list of skin tone or other variants. |
| 79 | + */ |
| 80 | +data class EmojiData( |
| 81 | + val category: String, |
| 82 | + val emoji: String, |
| 83 | + val variants: List<String>, |
| 84 | +) |
| 85 | + |
| 86 | +/** |
| 87 | + * Returns the drawable resource ID for a given emoji category icon. |
| 88 | + * Based on Fossify's getCategoryIconRes() function. |
| 89 | + * |
| 90 | + * @param category The category name from the emoji spec file. |
| 91 | + * @return The drawable resource ID for the category icon. |
| 92 | + */ |
| 93 | +fun getCategoryIconRes(category: String): Int = |
| 94 | + when (category) { |
| 95 | + "smileys_emotion" -> R.drawable.ic_emoji_smileys |
| 96 | + "people_body" -> R.drawable.ic_emoji_people |
| 97 | + "animals_nature" -> R.drawable.ic_emoji_animals |
| 98 | + "food_drink" -> R.drawable.ic_emoji_food |
| 99 | + "travel_places" -> R.drawable.ic_emoji_travel |
| 100 | + "activities" -> R.drawable.ic_emoji_activities |
| 101 | + "objects" -> R.drawable.ic_emoji_objects |
| 102 | + "symbols" -> R.drawable.ic_emoji_symbols |
| 103 | + "flags" -> R.drawable.ic_emoji_flags |
| 104 | + else -> R.drawable.ic_emoji_vector |
| 105 | + } |
| 106 | + |
| 107 | +/** |
| 108 | + * Returns the string resource ID for a given emoji category title. |
| 109 | + * Based on Fossify's getCategoryTitleRes() function. |
| 110 | + * |
| 111 | + * @param category The category name from the emoji spec file. |
| 112 | + * @return The string resource ID for the category title. |
| 113 | + */ |
| 114 | +fun getCategoryTitleRes(category: String): Int = |
| 115 | + when (category) { |
| 116 | + "smileys_emotion" -> R.string.smileys_and_emotions |
| 117 | + "people_body" -> R.string.people_and_body |
| 118 | + "animals_nature" -> R.string.animals_and_nature |
| 119 | + "food_drink" -> R.string.food_and_drink |
| 120 | + "travel_places" -> R.string.travel_and_places |
| 121 | + "activities" -> R.string.activities |
| 122 | + "objects" -> R.string.objects |
| 123 | + "symbols" -> R.string.symbols |
| 124 | + "flags" -> R.string.flags |
| 125 | + else -> R.string.recently_used |
| 126 | + } |
0 commit comments