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
import chat.rocket.android.server.domain.GetSettingsInteractor
import chat.rocket.android.server.domain.SITE_URL
import chat.rocket.android.server.domain.TokenRepository
import chat.rocket.android.emoji.EmojiRepository
import com.crashlytics.android.Crashlytics
import com.crashlytics.android.core.CrashlyticsCore
import com.facebook.drawee.backends.pipeline.DraweeConfig
......@@ -87,7 +86,6 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
context = WeakReference(applicationContext)
AndroidThreeTen.init(this)
EmojiRepository.load(this)
setupCrashlytics()
setupFresco()
......@@ -130,8 +128,8 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
Fabric.with(this, Crashlytics.Builder().core(core).build())
installCrashlyticsWrapper(this@RocketChatApplication,
getCurrentServerInteractor, settingsInteractor,
accountRepository, localRepository)
getCurrentServerInteractor, settingsInteractor,
accountRepository, localRepository)
}
private fun setupFresco() {
......
......@@ -80,7 +80,9 @@ class ChatRoomsPresenter @Inject constructor(
} else {
val id = if (isDirectMessage && !open) {
retryIO("createDirectMessage($name)") {
client.createDirectMessage(name)
client.createDirectMessage(name, callback = { b, s ->
})
}
val fromTo = mutableListOf(myself.id, id).apply {
sort()
......
package chat.rocket.android.main.presentation
import android.content.Context
import chat.rocket.android.core.lifecycle.CancelStrategy
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.main.uimodel.NavHeaderUiModel
import chat.rocket.android.main.uimodel.NavHeaderUiModelMapper
......@@ -27,6 +32,7 @@ import chat.rocket.common.model.UserStatus
import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient
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.me
import chat.rocket.core.internal.rest.unregisterPushToken
......@@ -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.
*/
......
......@@ -77,6 +77,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
presenter.connect()
presenter.loadServerAccounts()
presenter.loadCurrentInfo()
presenter.loadEmojis()
setupToolbar()
setupNavigationView()
}
......
......@@ -8,5 +8,6 @@ data class Emoji(
val category: String,
val count: Int = 0,
val siblings: MutableCollection<Emoji> = mutableListOf(),
val fitzpatrick: Fitzpatrick = Fitzpatrick.Default
)
\ No newline at end of file
val fitzpatrick: Fitzpatrick = Fitzpatrick.Default,
val url: String? = null // Filled for custom emojis
)
......@@ -3,12 +3,8 @@ package chat.rocket.android.emoji
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Typeface
import android.os.SystemClock
import chat.rocket.android.emoji.internal.EmojiCategory
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.JSONObject
import java.io.BufferedReader
......@@ -16,6 +12,7 @@ import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import java.util.regex.Pattern
import kotlin.collections.ArrayList
import kotlin.coroutines.experimental.buildSequence
object EmojiRepository {
......@@ -27,14 +24,24 @@ object EmojiRepository {
private lateinit var preferences: SharedPreferences
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)
ALL_EMOJIS.clear()
cachedTypeface = Typeface.createFromAsset(context.assets, "fonts/emojione-android.ttf")
val stream = context.assets.open(path)
val emojis = loadEmojis(stream)
emojis.forEach { emoji ->
val emojis = loadEmojis(stream).also {
it.addAll(customEmojis)
}.toList()
for (emoji in emojis) {
val unicodeIntList = mutableListOf<Int>()
// If empty it's a custom emoji.
if (emoji.unicode.isEmpty()) {
ALL_EMOJIS.add(emoji)
continue
}
emoji.unicode.split("-").forEach {
val value = it.toInt(16)
if (value >= 0x10000) {
......@@ -95,7 +102,7 @@ object EmojiRepository {
internal fun getEmojiSequenceByCategory(category: EmojiCategory): Sequence<Emoji> {
val list = ALL_EMOJIS.filter { it.category.toLowerCase() == category.name.toLowerCase() }
return buildSequence{
return buildSequence {
list.forEach {
yield(it)
}
......@@ -163,7 +170,7 @@ object EmojiRepository {
return result
}
private fun loadEmojis(stream: InputStream): List<Emoji> {
private fun loadEmojis(stream: InputStream): MutableList<Emoji> {
val emojisJSON = JSONArray(inputStreamToString(stream))
val emojis = ArrayList<Emoji>(emojisJSON.length());
for (i in 0 until emojisJSON.length()) {
......
......@@ -7,7 +7,7 @@ import chat.rocket.android.emoji.EmojiRepository
import chat.rocket.android.emoji.EmojiTypefaceSpan
import chat.rocket.android.emoji.R
internal enum class EmojiCategory {
enum class EmojiCategory {
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