Commit 59961967 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Group emojis by its tone

parent 35ed646c
...@@ -6,5 +6,7 @@ data class Emoji( ...@@ -6,5 +6,7 @@ data class Emoji(
val unicode: String, val unicode: String,
val keywords: List<String>, val keywords: List<String>,
val category: String, val category: String,
val count: Int = 0 val count: Int = 0,
val siblings: MutableCollection<Emoji> = mutableListOf(),
val fitzpatrick: Fitzpatrick = Fitzpatrick.Default
) )
\ No newline at end of file
...@@ -12,6 +12,7 @@ import java.util.* ...@@ -12,6 +12,7 @@ import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
object EmojiRepository { object EmojiRepository {
private val FITZPATRICK_REGEX = "(.*)_(tone[0-9]):".toRegex(RegexOption.IGNORE_CASE)
private val shortNameToUnicode = HashMap<String, String>() private val shortNameToUnicode = HashMap<String, String>()
private val SHORTNAME_PATTERN = Pattern.compile(":([-+\\w]+):") private val SHORTNAME_PATTERN = Pattern.compile(":([-+\\w]+):")
private val ALL_EMOJIS = mutableListOf<Emoji>() private val ALL_EMOJIS = mutableListOf<Emoji>()
...@@ -24,9 +25,9 @@ object EmojiRepository { ...@@ -24,9 +25,9 @@ object EmojiRepository {
cachedTypeface = Typeface.createFromAsset(context.assets, "fonts/emojione-android.ttf") cachedTypeface = Typeface.createFromAsset(context.assets, "fonts/emojione-android.ttf")
val stream = context.assets.open(path) val stream = context.assets.open(path)
val emojis = loadEmojis(stream) val emojis = loadEmojis(stream)
emojis.forEach { emojis.forEach { emoji ->
val unicodeIntList = mutableListOf<Int>() val unicodeIntList = mutableListOf<Int>()
it.unicode.split("-").forEach { emoji.unicode.split("-").forEach {
val value = it.toInt(16) val value = it.toInt(16)
if (value >= 0x10000) { if (value >= 0x10000) {
val surrogatePair = calculateSurrogatePairs(value) val surrogatePair = calculateSurrogatePairs(value)
...@@ -38,14 +39,45 @@ object EmojiRepository { ...@@ -38,14 +39,45 @@ object EmojiRepository {
} }
val unicodeIntArray = unicodeIntList.toIntArray() val unicodeIntArray = unicodeIntList.toIntArray()
val unicode = String(unicodeIntArray, 0, unicodeIntArray.size) val unicode = String(unicodeIntArray, 0, unicodeIntArray.size)
ALL_EMOJIS.add(it.copy(unicode = unicode)) val emojiWithUnicode = emoji.copy(unicode = unicode)
if (hasFitzpatrick(emoji.shortname)) {
val matchResult = FITZPATRICK_REGEX.find(emoji.shortname)
val prefix = matchResult!!.groupValues[1] + ":"
val fitzpatrick = getFitzpatric(matchResult.groupValues[2])
val defaultEmoji = ALL_EMOJIS.firstOrNull { it.shortname == prefix }
val emojiWithFitzpatrick = emojiWithUnicode.copy(fitzpatrick = fitzpatrick)
if (defaultEmoji != null) {
defaultEmoji.siblings.add(emojiWithFitzpatrick)
} else {
// This emoji doesn't have a default tone, ie. :man_in_business_suit_levitating_tone1:
// In this case, the default emoji becomes the first toned one.
ALL_EMOJIS.add(emojiWithFitzpatrick)
}
} else {
ALL_EMOJIS.add(emojiWithUnicode)
}
shortNameToUnicode.apply { shortNameToUnicode.apply {
put(it.shortname, unicode) put(emoji.shortname, unicode)
it.shortnameAlternates.forEach { alternate -> put(alternate, unicode) } emoji.shortnameAlternates.forEach { alternate -> put(alternate, unicode) }
} }
} }
} }
private fun hasFitzpatrick(shortname: String): Boolean {
return FITZPATRICK_REGEX matches shortname
}
private fun getFitzpatric(type: String): Fitzpatrick {
return when(type) {
Fitzpatrick.LightTone.type -> Fitzpatrick.LightTone
Fitzpatrick.MediumTone.type -> Fitzpatrick.MediumTone
Fitzpatrick.MediumLightTone.type -> Fitzpatrick.MediumLightTone
Fitzpatrick.MediumDarkTone.type -> Fitzpatrick.MediumDarkTone
Fitzpatrick.DarkTone.type -> Fitzpatrick.DarkTone
else -> Fitzpatrick.Default
}
}
/** /**
* Get all loaded emojis as list of Emoji objects. * Get all loaded emojis as list of Emoji objects.
* *
......
package chat.rocket.android.emoji
sealed class Fitzpatrick(val type: String) {
object Default: Fitzpatrick("")
object LightTone: Fitzpatrick("tone1")
object MediumLightTone: Fitzpatrick("tone2")
object MediumTone: Fitzpatrick("tone3")
object MediumDarkTone: Fitzpatrick("tone4")
object DarkTone: Fitzpatrick("tone5")
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment