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

Save and restore selected skin color to preferences

parent 260f76fe
...@@ -14,10 +14,12 @@ import androidx.annotation.ColorInt ...@@ -14,10 +14,12 @@ import androidx.annotation.ColorInt
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.graphics.drawable.DrawableCompat import androidx.core.graphics.drawable.DrawableCompat
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
import chat.rocket.android.emoji.internal.EmojiCategory import chat.rocket.android.emoji.internal.EmojiCategory
import chat.rocket.android.emoji.internal.EmojiPagerAdapter import chat.rocket.android.emoji.internal.EmojiPagerAdapter
import chat.rocket.android.emoji.internal.PREF_EMOJI_SKIN_TONE
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
...@@ -62,6 +64,11 @@ class EmojiKeyboardPopup(context: Context, view: View) : OverKeyboardPopupWindow ...@@ -62,6 +64,11 @@ class EmojiKeyboardPopup(context: Context, view: View) : OverKeyboardPopupWindow
changeColorView.setOnClickListener { changeColorView.setOnClickListener {
showSkinToneChooser() showSkinToneChooser()
} }
val sharedPreferences = context.getSharedPreferences("emoji", Context.MODE_PRIVATE)
sharedPreferences.getString(PREF_EMOJI_SKIN_TONE, "")?.let {
changeSkinTone(Fitzpatrick.valueOf(it))
}
} }
private fun showSkinToneChooser() { private fun showSkinToneChooser() {
...@@ -110,10 +117,15 @@ class EmojiKeyboardPopup(context: Context, view: View) : OverKeyboardPopupWindow ...@@ -110,10 +117,15 @@ class EmojiKeyboardPopup(context: Context, view: View) : OverKeyboardPopupWindow
DrawableCompat.setTint(wrappedDrawable, getFitzpatrickColor(tone)) DrawableCompat.setTint(wrappedDrawable, getFitzpatrickColor(tone))
(changeColorView as ImageView).setImageDrawable(wrappedDrawable) (changeColorView as ImageView).setImageDrawable(wrappedDrawable)
adapter.setFitzpatrick(tone) adapter.setFitzpatrick(tone)
} }
@ColorInt @ColorInt
private fun getFitzpatrickColor(tone: Fitzpatrick): Int { private fun getFitzpatrickColor(tone: Fitzpatrick): Int {
val sharedPreferences = context.getSharedPreferences("emoji", Context.MODE_PRIVATE)
sharedPreferences.edit {
putString(PREF_EMOJI_SKIN_TONE, tone.type)
}
return when (tone) { return when (tone) {
Fitzpatrick.Default -> ContextCompat.getColor(context, R.color.tone_default) Fitzpatrick.Default -> ContextCompat.getColor(context, R.color.tone_default)
Fitzpatrick.LightTone -> ContextCompat.getColor(context, R.color.tone_light) Fitzpatrick.LightTone -> ContextCompat.getColor(context, R.color.tone_light)
...@@ -204,8 +216,4 @@ class EmojiKeyboardPopup(context: Context, view: View) : OverKeyboardPopupWindow ...@@ -204,8 +216,4 @@ class EmojiKeyboardPopup(context: Context, view: View) : OverKeyboardPopupWindow
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
} }
} }
companion object {
const val PREF_EMOJI_RECENTS = "PREF_EMOJI_RECENTS"
}
} }
\ No newline at end of file
...@@ -4,6 +4,7 @@ import android.content.Context ...@@ -4,6 +4,7 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.graphics.Typeface import android.graphics.Typeface
import chat.rocket.android.emoji.internal.EmojiCategory import chat.rocket.android.emoji.internal.EmojiCategory
import chat.rocket.android.emoji.internal.PREF_EMOJI_RECENTS
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import java.io.BufferedReader import java.io.BufferedReader
...@@ -45,7 +46,7 @@ object EmojiRepository { ...@@ -45,7 +46,7 @@ object EmojiRepository {
if (hasFitzpatrick(emoji.shortname)) { if (hasFitzpatrick(emoji.shortname)) {
val matchResult = FITZPATRICK_REGEX.find(emoji.shortname) val matchResult = FITZPATRICK_REGEX.find(emoji.shortname)
val prefix = matchResult!!.groupValues[1] + ":" val prefix = matchResult!!.groupValues[1] + ":"
val fitzpatrick = getFitzpatrick(matchResult.groupValues[2]) val fitzpatrick = Fitzpatrick.valueOf(matchResult.groupValues[2])
val defaultEmoji = ALL_EMOJIS.firstOrNull { it.shortname == prefix } val defaultEmoji = ALL_EMOJIS.firstOrNull { it.shortname == prefix }
val emojiWithFitzpatrick = emojiWithUnicode.copy(fitzpatrick = fitzpatrick) val emojiWithFitzpatrick = emojiWithUnicode.copy(fitzpatrick = fitzpatrick)
if (defaultEmoji != null) { if (defaultEmoji != null) {
...@@ -69,17 +70,6 @@ object EmojiRepository { ...@@ -69,17 +70,6 @@ object EmojiRepository {
return FITZPATRICK_REGEX matches shortname return FITZPATRICK_REGEX matches shortname
} }
private fun getFitzpatrick(type: String): Fitzpatrick {
return when (type) {
Fitzpatrick.LightTone.type -> Fitzpatrick.LightTone
Fitzpatrick.MediumTone.type -> Fitzpatrick.MediumTone
Fitzpatrick.MediumLightTone.type -> Fitzpatrick.MediumLightTone
Fitzpatrick.MediumDarkTone.type -> Fitzpatrick.MediumDarkTone
Fitzpatrick.DarkTone.type -> Fitzpatrick.DarkTone
else -> Fitzpatrick.Default
}
}
/** /**
* Get all loaded emojis as list of Emoji objects. * Get all loaded emojis as list of Emoji objects.
* *
...@@ -112,14 +102,14 @@ object EmojiRepository { ...@@ -112,14 +102,14 @@ object EmojiRepository {
*/ */
internal fun addToRecents(emoji: Emoji) { internal fun addToRecents(emoji: Emoji) {
val emojiShortname = emoji.shortname val emojiShortname = emoji.shortname
val recentsJson = JSONObject(preferences.getString(EmojiKeyboardPopup.PREF_EMOJI_RECENTS, "{}")) val recentsJson = JSONObject(preferences.getString(PREF_EMOJI_RECENTS, "{}"))
if (recentsJson.has(emojiShortname)) { if (recentsJson.has(emojiShortname)) {
val useCount = recentsJson.getInt(emojiShortname) val useCount = recentsJson.getInt(emojiShortname)
recentsJson.put(emojiShortname, useCount + 1) recentsJson.put(emojiShortname, useCount + 1)
} else { } else {
recentsJson.put(emojiShortname, 1) recentsJson.put(emojiShortname, 1)
} }
preferences.edit().putString(EmojiKeyboardPopup.PREF_EMOJI_RECENTS, recentsJson.toString()).apply() preferences.edit().putString(PREF_EMOJI_RECENTS, recentsJson.toString()).apply()
} }
/** /**
...@@ -129,7 +119,7 @@ object EmojiRepository { ...@@ -129,7 +119,7 @@ object EmojiRepository {
*/ */
internal fun getRecents(): List<Emoji> { internal fun getRecents(): List<Emoji> {
val list = mutableListOf<Emoji>() val list = mutableListOf<Emoji>()
val recentsJson = JSONObject(preferences.getString(EmojiKeyboardPopup.PREF_EMOJI_RECENTS, "{}")) val recentsJson = JSONObject(preferences.getString(PREF_EMOJI_RECENTS, "{}"))
for (shortname in recentsJson.keys()) { for (shortname in recentsJson.keys()) {
val emoji = getEmojiByShortname(shortname) val emoji = getEmojiByShortname(shortname)
emoji?.let { emoji?.let {
......
...@@ -13,4 +13,18 @@ sealed class Fitzpatrick(val type: String) { ...@@ -13,4 +13,18 @@ sealed class Fitzpatrick(val type: String) {
object MediumTone: Fitzpatrick("tone3") object MediumTone: Fitzpatrick("tone3")
object MediumDarkTone: Fitzpatrick("tone4") object MediumDarkTone: Fitzpatrick("tone4")
object DarkTone: Fitzpatrick("tone5") object DarkTone: Fitzpatrick("tone5")
companion object {
fun valueOf(type: String): Fitzpatrick {
return when(type) {
"" -> Default
"tone1" -> LightTone
"tone2" -> MediumLightTone
"tone3" -> MediumTone
"tone4" -> MediumDarkTone
"tone5" -> DarkTone
else -> throw IllegalArgumentException("Fitzpatrick type '$type' is invalid")
}
}
}
} }
\ No newline at end of file
package chat.rocket.android.emoji.internal
internal const val PREF_EMOJI_RECENTS = "PREF_EMOJI_RECENTS"
internal const val PREF_EMOJI_SKIN_TONE = "PREF_EMOJI_SKIN_TONE"
\ No newline at end of file
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