1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

Emoji cross-platform sync issues (#981)

This commit is contained in:
Evgenii Kozlov 2020-10-13 15:42:55 +03:00 committed by GitHub
parent 86abc25ed5
commit b688554423
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 27 deletions

View file

@ -43,15 +43,12 @@ dependencies {
def appDependencies = rootProject.ext.mainApplication
def unitTestDependencies = rootProject.ext.unitTesting
def lib = rootProject.ext.libraryPageIconPicker
implementation project(':domain')
implementation project(':core-utils')
implementation(lib.emojiJava) {
exclude group: 'org.json', module: 'json'
}
implementation appDependencies.gson
implementation appDependencies.coroutines
testImplementation unitTestDependencies.junit
testImplementation unitTestDependencies.kotlinTest
}

View file

@ -4,6 +4,9 @@ import com.anytypeio.anytype.emojifier.data.Emoji
object Emojifier {
private const val EMOJI_SEPARATOR_INT = 65039
private const val SEPARATOR = EMOJI_SEPARATOR_INT.toChar()
/**
* cache for [search] results.
*/
@ -15,7 +18,22 @@ object Emojifier {
*/
@Throws(IllegalStateException::class)
fun uri(unicode: String): String {
val (page, index) = search(unicode)
var result = search(unicode)
if (result == null) {
if (unicode.last() == SEPARATOR) {
val sb = StringBuilder()
unicode.forEachIndexed { index, char ->
if (index < unicode.length.dec()) sb.append(char)
}
result = search(sb.toString())
}
}
checkNotNull(result) { "Could not find emoji for: $unicode" }
val (page, index) = result
return uri(page, index)
}
@ -32,8 +50,7 @@ object Emojifier {
* @param unicode emoji unicode
* @return a pair constisting of emoji's page and emoji's index for this [unicode]
*/
@Throws(IllegalStateException::class)
private fun search(unicode: String): Pair<Int, Int> {
private fun search(unicode: String): Pair<Int, Int>? {
val cached = cache[unicode]
if (cached != null) return cached
@ -49,10 +66,11 @@ object Emojifier {
return@forEachIndexed
}
}
return result ?: throw IllegalStateException("Result not found for: $unicode")
return result
}
object Config {
const val EMOJI_FILE = "emoji.json"
}
}

View file

@ -0,0 +1,27 @@
package com.agileburo.anytype.emojifier
import com.anytypeio.anytype.emojifier.Emojifier
import org.junit.Test
class EmojiSearchTest {
@Test
fun `should find uri for all emojis`() {
val emojis = listOf(
"⛰️", "\uD83C\uDFD8",
"⛰️", "⚔️", "\uD83C\uDFDB",
"▶️", "\uD83D\uDDE3", "⛩️",
"\uD83D\uDC87\u200D♂️", "⚙️",
"☺️", "\uD83D\uDDC4", "\uD83D\uDDD2",
"\uD83D\uDD78", "\uD83D\uDE47\u200D♀️",
"\uD83C\uDF21", "\uD83D\uDDB2",
"✴️", "⌨️", "\uD83E\uDDD1\uD83C\uDFFD\u200D✈️",
"\uD83D\uDC69\uD83C\uDFFC\u200D⚕️", "☂️"
)
emojis.forEach { unicode ->
Emojifier.uri(unicode)
}
}
}