mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: migrate MapFragment to Composable (#647)
This commit is contained in:
parent
e15cdc42f1
commit
d4879ceea9
13 changed files with 1207 additions and 823 deletions
|
|
@ -0,0 +1,48 @@
|
|||
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)
|
||||
|
||||
private val emoji2Frequency: MutableMap<String, Int> by lazy {
|
||||
sharedPreferences.getString(PREF_KEY_CUSTOM_EMOJI_FREQ, null)?.split(SPLIT_CHAR)
|
||||
?.associate { entry ->
|
||||
entry.split(KEY_VALUE_DELIMITER, limit = 2).takeIf { it.size == 2 }
|
||||
?.let { it[0] to it[1].toInt() } ?: ("" to 0)
|
||||
}?.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()
|
||||
.putString(PREF_KEY_CUSTOM_EMOJI_FREQ, emoji2Frequency.entries.joinToString(SPLIT_CHAR))
|
||||
.apply()
|
||||
}
|
||||
|
||||
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