Commit 56dc7727 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Change EmojiRepository#getRecents() logic

parent e361544b
......@@ -20,7 +20,7 @@ interface EmojiDao {
fun loadAllCustomEmojis(): List<Emoji>
@Query("SELECT * FROM emoji WHERE shortname=:shortname")
fun loadEmojiByShortname(shortname: String): Emoji?
fun loadEmojiByShortname(shortname: String): List<Emoji>
@Query("SELECT * FROM emoji WHERE UPPER(category)=UPPER(:category)")
fun loadEmojisByCategory(category: String): List<Emoji>
......
......@@ -149,7 +149,7 @@ object EmojiRepository {
* @return Emoji given by shortname or null
*/
private suspend fun getEmojiByShortname(shortname: String): Emoji? = withContext(CommonPool) {
return@withContext db.emojiDao().loadEmojiByShortname(shortname)
return@withContext db.emojiDao().loadAllCustomEmojis().firstOrNull()
}
/**
......@@ -158,12 +158,14 @@ object EmojiRepository {
internal fun addToRecents(emoji: Emoji) {
val emojiShortname = emoji.shortname
val recentsJson = JSONObject(preferences.getString(PREF_EMOJI_RECENTS, "{}"))
if (recentsJson.has(emojiShortname)) {
val useCount = recentsJson.getInt(emojiShortname)
recentsJson.put(emojiShortname, useCount + 1)
} else {
recentsJson.put(emojiShortname, 1)
}
preferences.edit().putString(PREF_EMOJI_RECENTS, recentsJson.toString()).apply()
}
......@@ -182,23 +184,26 @@ object EmojiRepository {
*
* @return All recent emojis ordered by usage.
*/
internal suspend fun getRecents(): List<Emoji> {
internal suspend fun getRecents(): List<Emoji> = withContext(CommonPool) {
val list = mutableListOf<Emoji>()
val recentsJson = JSONObject(preferences.getString(PREF_EMOJI_RECENTS, "{}"))
// for (shortname in recentsJson.keys()) {
// val emoji = getEmojiByShortname(shortname)
// emoji?.let {
// val useCount = recentsJson.getInt(it.shortname)
// list.add(it.copy(count = useCount))
// }
// }
//
// list.sortWith(Comparator { o1, o2 ->
// o2.count - o1.count
// })
val allEmojis = db.emojiDao().loadAllEmojis()
val len = recentsJson.length()
val recentShortnames = recentsJson.keys()
for (i in 0 until len) {
val shortname = recentShortnames.next()
allEmojis.firstOrNull { it.shortname == shortname }?.let {
val useCount = recentsJson.getInt(it.shortname)
list.add(it.copy(count = useCount))
}
}
list.sortWith(Comparator { o1, o2 ->
o2.count - o1.count
})
return list
return@withContext list
}
/**
......
......@@ -4,4 +4,4 @@ distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
distributionSha256Sum=9bc16f929e5920a9a27fef22c65a8ab8140d705e418de0159ca6eb7f08a74200
distributionSha256Sum=39e2d5803bbd5eaf6c8efe07067b0e5a00235e8c71318642b2ed262920b27721
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