Site icon JoinAppStudio

3-step guide to adding emojis — Jetpack Emoji Picker | by Nav Singh | Sep, 2023

3-step guide to adding emojis — Jetpack Emoji Picker | by Nav Singh | Sep, 2023

class CustomRecentEmojiProvider(context: Context) : RecentEmojiAsyncProvider {

private val sharedPreferences by lazy {
context.getSharedPreferences(RECENT_EMOJI_LIST_PREF_NAME, Context.MODE_PRIVATE)
}

private val emoji2Frequency: MutableMap<String, Int> by lazy {
sharedPreferences.getString(CUSTOM_EMOJI_FREQ_PREF_KEY, null)?.split(SPLIT_CHAR)
?.associate { entry ->
entry.split(VALUE_DELIMITER, limit = 2)
.let { value -> value[0] to value[1].toInt() }
}?.toMutableMap() ?: mutableMapOf()
}

override fun getRecentEmojiListAsync(): ListenableFuture<List<String>> =
Futures.immediateFuture(emoji2Frequency.toList().sortedByDescending { it.second }
.map { it.first })

override fun recordSelection(emoji: String) {
emoji2Frequency[emoji] = (emoji2Frequency[emoji] ?: 0) + 1
saveToPreferences()
}

private fun saveToPreferences() {
sharedPreferences.edit(commit = true) {
putString(CUSTOM_EMOJI_FREQ_PREF_KEY, emoji2Frequency.entries.joinToString(SPLIT_CHAR))
}
}

companion object {
private const val CUSTOM_EMOJI_FREQ_PREF_KEY = “pref_key_custom_emoji_freq”
private const val RECENT_EMOJI_LIST_PREF_NAME = “emoji2_preferences”
private const val SPLIT_CHAR = “,”
private const val VALUE_DELIMITER = “=”
}

}

Source link

Exit mobile version