Commit 37b86ee9 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Load custom emojis from host

parent 60570bc8
...@@ -19,7 +19,6 @@ import chat.rocket.android.server.domain.GetCurrentServerInteractor ...@@ -19,7 +19,6 @@ import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.GetSettingsInteractor import chat.rocket.android.server.domain.GetSettingsInteractor
import chat.rocket.android.server.domain.SITE_URL import chat.rocket.android.server.domain.SITE_URL
import chat.rocket.android.server.domain.TokenRepository import chat.rocket.android.server.domain.TokenRepository
import chat.rocket.android.emoji.EmojiRepository
import com.crashlytics.android.Crashlytics import com.crashlytics.android.Crashlytics
import com.crashlytics.android.core.CrashlyticsCore import com.crashlytics.android.core.CrashlyticsCore
import com.facebook.drawee.backends.pipeline.DraweeConfig import com.facebook.drawee.backends.pipeline.DraweeConfig
...@@ -87,7 +86,6 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -87,7 +86,6 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
context = WeakReference(applicationContext) context = WeakReference(applicationContext)
AndroidThreeTen.init(this) AndroidThreeTen.init(this)
EmojiRepository.load(this)
setupCrashlytics() setupCrashlytics()
setupFresco() setupFresco()
......
...@@ -80,7 +80,9 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -80,7 +80,9 @@ class ChatRoomsPresenter @Inject constructor(
} else { } else {
val id = if (isDirectMessage && !open) { val id = if (isDirectMessage && !open) {
retryIO("createDirectMessage($name)") { retryIO("createDirectMessage($name)") {
client.createDirectMessage(name) client.createDirectMessage(name, callback = { b, s ->
})
} }
val fromTo = mutableListOf(myself.id, id).apply { val fromTo = mutableListOf(myself.id, id).apply {
sort() sort()
......
package chat.rocket.android.main.presentation package chat.rocket.android.main.presentation
import android.content.Context
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManagerFactory import chat.rocket.android.db.DatabaseManagerFactory
import chat.rocket.android.emoji.Emoji
import chat.rocket.android.emoji.EmojiRepository
import chat.rocket.android.emoji.Fitzpatrick
import chat.rocket.android.emoji.internal.EmojiCategory
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.main.uimodel.NavHeaderUiModel import chat.rocket.android.main.uimodel.NavHeaderUiModel
import chat.rocket.android.main.uimodel.NavHeaderUiModelMapper import chat.rocket.android.main.uimodel.NavHeaderUiModelMapper
...@@ -27,6 +32,7 @@ import chat.rocket.common.model.UserStatus ...@@ -27,6 +32,7 @@ import chat.rocket.common.model.UserStatus
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.realtime.setDefaultStatus import chat.rocket.core.internal.realtime.setDefaultStatus
import chat.rocket.core.internal.rest.getCustomEmojis
import chat.rocket.core.internal.rest.logout import chat.rocket.core.internal.rest.logout
import chat.rocket.core.internal.rest.me import chat.rocket.core.internal.rest.me
import chat.rocket.core.internal.rest.unregisterPushToken import chat.rocket.core.internal.rest.unregisterPushToken
...@@ -116,6 +122,33 @@ class MainPresenter @Inject constructor( ...@@ -116,6 +122,33 @@ class MainPresenter @Inject constructor(
} }
} }
fun loadEmojis() {
launchUI(strategy) {
val customEmojiList = mutableListOf<Emoji>()
try {
for (customEmoji in client.getCustomEmojis()) {
customEmojiList.add(Emoji(
shortname = ":${customEmoji.name}:",
category = EmojiCategory.CUSTOM.name,
url = "$currentServer/emoji-custom/${customEmoji.name}.${customEmoji.extension}",
count = 0,
fitzpatrick = Fitzpatrick.Default,
keywords = customEmoji.aliases,
shortnameAlternates = customEmoji.aliases,
siblings = mutableListOf(),
unicode = ""
))
}
EmojiRepository.load(view as Context, customEmojis = customEmojiList)
} catch (ex: RocketChatException) {
Timber.e(ex)
EmojiRepository.load(view as Context)
}
}
}
/** /**
* Logout from current server. * Logout from current server.
*/ */
......
...@@ -77,6 +77,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector, ...@@ -77,6 +77,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
presenter.connect() presenter.connect()
presenter.loadServerAccounts() presenter.loadServerAccounts()
presenter.loadCurrentInfo() presenter.loadCurrentInfo()
presenter.loadEmojis()
setupToolbar() setupToolbar()
setupNavigationView() setupNavigationView()
} }
......
...@@ -8,5 +8,6 @@ data class Emoji( ...@@ -8,5 +8,6 @@ data class Emoji(
val category: String, val category: String,
val count: Int = 0, val count: Int = 0,
val siblings: MutableCollection<Emoji> = mutableListOf(), val siblings: MutableCollection<Emoji> = mutableListOf(),
val fitzpatrick: Fitzpatrick = Fitzpatrick.Default val fitzpatrick: Fitzpatrick = Fitzpatrick.Default,
val url: String? = null // Filled for custom emojis
) )
...@@ -3,12 +3,8 @@ package chat.rocket.android.emoji ...@@ -3,12 +3,8 @@ package chat.rocket.android.emoji
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.graphics.Typeface import android.graphics.Typeface
import android.os.SystemClock
import chat.rocket.android.emoji.internal.EmojiCategory import chat.rocket.android.emoji.internal.EmojiCategory
import chat.rocket.android.emoji.internal.PREF_EMOJI_RECENTS import chat.rocket.android.emoji.internal.PREF_EMOJI_RECENTS
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
import kotlinx.coroutines.experimental.yield
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import java.io.BufferedReader import java.io.BufferedReader
...@@ -16,6 +12,7 @@ import java.io.InputStream ...@@ -16,6 +12,7 @@ import java.io.InputStream
import java.io.InputStreamReader import java.io.InputStreamReader
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.collections.ArrayList
import kotlin.coroutines.experimental.buildSequence import kotlin.coroutines.experimental.buildSequence
object EmojiRepository { object EmojiRepository {
...@@ -27,14 +24,24 @@ object EmojiRepository { ...@@ -27,14 +24,24 @@ object EmojiRepository {
private lateinit var preferences: SharedPreferences private lateinit var preferences: SharedPreferences
internal lateinit var cachedTypeface: Typeface internal lateinit var cachedTypeface: Typeface
fun load(context: Context, path: String = "emoji.json") { fun load(context: Context, customEmojis: List<Emoji> = emptyList(), path: String = "emoji.json") {
preferences = context.getSharedPreferences("emoji", Context.MODE_PRIVATE) preferences = context.getSharedPreferences("emoji", Context.MODE_PRIVATE)
ALL_EMOJIS.clear() ALL_EMOJIS.clear()
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).also {
emojis.forEach { emoji -> it.addAll(customEmojis)
}.toList()
for (emoji in emojis) {
val unicodeIntList = mutableListOf<Int>() val unicodeIntList = mutableListOf<Int>()
// If empty it's a custom emoji.
if (emoji.unicode.isEmpty()) {
ALL_EMOJIS.add(emoji)
continue
}
emoji.unicode.split("-").forEach { emoji.unicode.split("-").forEach {
val value = it.toInt(16) val value = it.toInt(16)
if (value >= 0x10000) { if (value >= 0x10000) {
...@@ -95,7 +102,7 @@ object EmojiRepository { ...@@ -95,7 +102,7 @@ object EmojiRepository {
internal fun getEmojiSequenceByCategory(category: EmojiCategory): Sequence<Emoji> { internal fun getEmojiSequenceByCategory(category: EmojiCategory): Sequence<Emoji> {
val list = ALL_EMOJIS.filter { it.category.toLowerCase() == category.name.toLowerCase() } val list = ALL_EMOJIS.filter { it.category.toLowerCase() == category.name.toLowerCase() }
return buildSequence{ return buildSequence {
list.forEach { list.forEach {
yield(it) yield(it)
} }
...@@ -163,7 +170,7 @@ object EmojiRepository { ...@@ -163,7 +170,7 @@ object EmojiRepository {
return result return result
} }
private fun loadEmojis(stream: InputStream): List<Emoji> { private fun loadEmojis(stream: InputStream): MutableList<Emoji> {
val emojisJSON = JSONArray(inputStreamToString(stream)) val emojisJSON = JSONArray(inputStreamToString(stream))
val emojis = ArrayList<Emoji>(emojisJSON.length()); val emojis = ArrayList<Emoji>(emojisJSON.length());
for (i in 0 until emojisJSON.length()) { for (i in 0 until emojisJSON.length()) {
......
...@@ -7,7 +7,7 @@ import chat.rocket.android.emoji.EmojiRepository ...@@ -7,7 +7,7 @@ import chat.rocket.android.emoji.EmojiRepository
import chat.rocket.android.emoji.EmojiTypefaceSpan import chat.rocket.android.emoji.EmojiTypefaceSpan
import chat.rocket.android.emoji.R import chat.rocket.android.emoji.R
internal enum class EmojiCategory { enum class EmojiCategory {
RECENTS { RECENTS {
override fun resourceIcon() = R.drawable.ic_emoji_recents override fun resourceIcon() = R.drawable.ic_emoji_recents
......
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