mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
Migrate custom emoji prefs to repo pattern (#2767)
This commit is contained in:
parent
a46065865f
commit
e29003c79d
3 changed files with 53 additions and 18 deletions
|
|
@ -17,19 +17,18 @@
|
|||
|
||||
package com.geeksville.mesh.util
|
||||
|
||||
import android.content.Context
|
||||
import androidx.emoji2.emojipicker.RecentEmojiAsyncProvider
|
||||
import com.google.common.util.concurrent.Futures
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
|
||||
/** Define a custom recent emoji provider which shows most frequently used emoji */
|
||||
class CustomRecentEmojiProvider(context: Context) : RecentEmojiAsyncProvider {
|
||||
|
||||
private val sharedPreferences = context.getSharedPreferences(RECENT_EMOJI_LIST_FILE_NAME, Context.MODE_PRIVATE)
|
||||
class CustomRecentEmojiProvider(
|
||||
private val customEmojiFrequency: String?,
|
||||
private val onUpdateCustomEmojiFrequency: (updatedValue: String) -> Unit,
|
||||
) : RecentEmojiAsyncProvider {
|
||||
|
||||
private val emoji2Frequency: MutableMap<String, Int> by lazy {
|
||||
sharedPreferences
|
||||
.getString(PREF_KEY_CUSTOM_EMOJI_FREQ, null)
|
||||
customEmojiFrequency
|
||||
?.split(SPLIT_CHAR)
|
||||
?.associate { entry ->
|
||||
entry.split(KEY_VALUE_DELIMITER, limit = 2).takeIf { it.size == 2 }?.let { it[0] to it[1].toInt() }
|
||||
|
|
@ -43,19 +42,10 @@ class CustomRecentEmojiProvider(context: Context) : RecentEmojiAsyncProvider {
|
|||
|
||||
override fun recordSelection(emoji: String) {
|
||||
emoji2Frequency[emoji] = (emoji2Frequency[emoji] ?: 0) + 1
|
||||
saveToPreferences()
|
||||
}
|
||||
|
||||
private fun saveToPreferences() {
|
||||
sharedPreferences
|
||||
.edit()
|
||||
.putString(PREF_KEY_CUSTOM_EMOJI_FREQ, emoji2Frequency.entries.joinToString(SPLIT_CHAR))
|
||||
.apply()
|
||||
onUpdateCustomEmojiFrequency(emoji2Frequency.entries.joinToString(SPLIT_CHAR))
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PREF_KEY_CUSTOM_EMOJI_FREQ = "pref_key_custom_emoji_freq"
|
||||
private const val RECENT_EMOJI_LIST_FILE_NAME = "org.geeksville.emoji.prefs"
|
||||
private const val SPLIT_CHAR = ","
|
||||
private const val KEY_VALUE_DELIMITER = "="
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue