Commit 8ee1100c authored by Leonardo Aramaki's avatar Leonardo Aramaki

Query custom emojis by current url prefix in order to contemplate multiple servers

parent 37073556
......@@ -125,8 +125,13 @@ class MainPresenter @Inject constructor(
}
}
/**
* Load all emojis for the current server. Simple emojis are always the same for every server,
* but custom emojis vary according to the its url.
*/
fun loadEmojis() {
launchUI(strategy) {
EmojiRepository.setCurrentServerUrl(currentServer)
val customEmojiList = mutableListOf<Emoji>()
try {
for (customEmoji in retryIO("getCustomEmojis()") { client.getCustomEmojis() }) {
......
......@@ -25,6 +25,9 @@ interface EmojiDao {
@Query("SELECT * FROM emoji WHERE UPPER(category)=UPPER(:category)")
fun loadEmojisByCategory(category: String): List<Emoji>
@Query("SELECT * FROM emoji WHERE UPPER(category)=UPPER(:category) AND url LIKE :url")
fun loadEmojisByCategoryAndUrl(category: String, url: String): List<Emoji>
@Insert(onConflict = IGNORE)
fun insertEmoji(emoji: Emoji)
......
......@@ -31,6 +31,15 @@ object EmojiRepository {
private lateinit var preferences: SharedPreferences
internal lateinit var cachedTypeface: Typeface
private lateinit var db: EmojiDatabase
private lateinit var currentServerUrl: String
fun setCurrentServerUrl(url: String) {
currentServerUrl = url
}
fun getCurrentServerUrl(): String? {
return if (::currentServerUrl.isInitialized) currentServerUrl else null
}
fun load(context: Context, customEmojis: List<Emoji> = emptyList(), path: String = "emoji.json") {
launch(CommonPool) {
......@@ -141,6 +150,18 @@ object EmojiRepository {
}
}
internal suspend fun getEmojiSequenceByCategoryAndUrl(category: EmojiCategory, url: String): Sequence<Emoji> {
val list = withContext(CommonPool) {
db.emojiDao().loadEmojisByCategoryAndUrl(category.name, "$url%")
}
return buildSequence {
list.forEach {
yield(it)
}
}
}
/**
* Get the emoji given by a specified shortname. Returns null if can't find any.
*
......
......@@ -45,8 +45,15 @@ internal class EmojiPagerAdapter(private val listener: EmojiKeyboardListener) :
container.addView(view)
launch(UI) {
val currentServerUrl = EmojiRepository.getCurrentServerUrl()
val emojis = if (category != EmojiCategory.RECENTS) {
EmojiRepository.getEmojiSequenceByCategory(category)
if (category == EmojiCategory.CUSTOM) {
currentServerUrl?.let { url ->
EmojiRepository.getEmojiSequenceByCategoryAndUrl(category, url)
} ?: emptySequence()
} else {
EmojiRepository.getEmojiSequenceByCategory(category)
}
} else {
sequenceOf(*EmojiRepository.getRecents().toTypedArray())
}
......
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