WIP prefs: implement emoji preference
This commit is contained in:
parent
9b1bffaf8e
commit
90a338aa69
|
@ -11,8 +11,6 @@ import android.view.View
|
|||
import android.widget.RadioButton
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.keylesspalace.tusky.MainActivity
|
||||
import com.keylesspalace.tusky.R
|
||||
import com.keylesspalace.tusky.databinding.DialogEmojicompatBinding
|
||||
|
@ -31,34 +29,35 @@ import okhttp3.OkHttpClient
|
|||
import kotlin.system.exitProcess
|
||||
|
||||
/**
|
||||
* This Preference lets the user select their preferred emoji font
|
||||
* This selector lets the user select their preferred emoji font.
|
||||
* It will also handle the downloading.
|
||||
*/
|
||||
class EmojiPreference(
|
||||
context: Context,
|
||||
private val okHttpClient: OkHttpClient
|
||||
) : Preference(context) {
|
||||
class EmojiSelector(
|
||||
private val context: Context,
|
||||
private val okHttpClient: OkHttpClient,
|
||||
initialSelectedFontId: Int,
|
||||
private val saveSelectedId: (Int) -> Unit
|
||||
) {
|
||||
|
||||
private lateinit var selected: EmojiCompatFont
|
||||
private lateinit var original: EmojiCompatFont
|
||||
private var selected: EmojiCompatFont
|
||||
private var original: EmojiCompatFont
|
||||
private val radioButtons = mutableListOf<RadioButton>()
|
||||
private var updated = false
|
||||
private var currentNeedsUpdate = false
|
||||
|
||||
private val downloadDisposables = MutableList<Disposable?>(FONTS.size) { null }
|
||||
|
||||
override fun onAttachedToHierarchy(preferenceManager: PreferenceManager) {
|
||||
super.onAttachedToHierarchy(preferenceManager)
|
||||
|
||||
init {
|
||||
// Find out which font is currently active
|
||||
selected = EmojiCompatFont.byId(
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getInt(key, 0)
|
||||
)
|
||||
selected = EmojiCompatFont.byId(initialSelectedFontId)
|
||||
// We'll use this later to determine if anything has changed
|
||||
original = selected
|
||||
summary = selected.getDisplay(context)
|
||||
}
|
||||
|
||||
override fun onClick() {
|
||||
val summary: String
|
||||
get() = selected.getDisplay(context)
|
||||
|
||||
fun showSelectionDialog() {
|
||||
val binding = DialogEmojicompatBinding.inflate(LayoutInflater.from(context))
|
||||
|
||||
setupItem(BLOBMOJI, binding.itemBlobmoji)
|
||||
|
@ -194,12 +193,7 @@ class EmojiPreference(
|
|||
private fun saveSelectedFont() {
|
||||
val index = selected.id
|
||||
Log.i(TAG, "saveSelectedFont: Font ID: $index")
|
||||
PreferenceManager
|
||||
.getDefaultSharedPreferences(context)
|
||||
.edit()
|
||||
.putInt(key, index)
|
||||
.apply()
|
||||
summary = selected.getDisplay(context)
|
||||
saveSelectedId(selected.id)
|
||||
}
|
||||
|
||||
/**
|
|
@ -29,6 +29,7 @@ import com.keylesspalace.tusky.settings.AppTheme
|
|||
import com.keylesspalace.tusky.settings.PrefData
|
||||
import com.keylesspalace.tusky.settings.PrefStore
|
||||
import com.keylesspalace.tusky.settings.PreferenceOption
|
||||
import com.keylesspalace.tusky.settings.customListPreference
|
||||
import com.keylesspalace.tusky.settings.getBlocking
|
||||
import com.keylesspalace.tusky.settings.listPreference
|
||||
import com.keylesspalace.tusky.settings.makePreferenceScreen
|
||||
|
@ -98,6 +99,20 @@ class PreferencesFragment : Fragment(), Injectable {
|
|||
) {
|
||||
updatePrefs { data -> data.copy(appTheme = it) }
|
||||
}
|
||||
|
||||
val emojiSelector = EmojiSelector(context, okhttpclient, prefs.emojiFont) {
|
||||
updatePrefs { data -> data.copy(emojiFont = it) }
|
||||
}
|
||||
customListPreference(
|
||||
getString(R.string.emoji_style),
|
||||
{
|
||||
emojiSelector.summary
|
||||
},
|
||||
{
|
||||
emojiSelector.showSelectionDialog()
|
||||
}
|
||||
)
|
||||
|
||||
val languageNames = resources.getStringArray(R.array.language_entries)
|
||||
val languageValues = resources.getStringArray(R.array.language_values)
|
||||
val languageOptions = languageNames
|
||||
|
|
|
@ -160,7 +160,6 @@ fun PreferenceParent.switchPreference(
|
|||
addPref(layout)
|
||||
}
|
||||
|
||||
|
||||
data class PreferenceOption<T>(val name: String, val value: T)
|
||||
@Suppress("FunctionName")
|
||||
fun <T> PreferenceParent.PreferenceOption(pair: Pair<T, Int>): PreferenceOption<T> {
|
||||
|
@ -174,31 +173,8 @@ fun <T> PreferenceParent.listPreference(
|
|||
selected: () -> T,
|
||||
onSelection: (T) -> Unit,
|
||||
) {
|
||||
val layout = itemLayout(context).apply {
|
||||
isClickable = true
|
||||
val outValue = TypedValue()
|
||||
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)
|
||||
setBackgroundResource(outValue.resourceId)
|
||||
setPadding(dpToPx(16), dpToPx(16), dpToPx(16), dpToPx(16))
|
||||
}
|
||||
val linearLayout = LinearLayout(context).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
}
|
||||
|
||||
|
||||
val titleView = TextView(context).apply {
|
||||
text = title
|
||||
setTextAppearanceRef(android.R.attr.textAppearanceListItem)
|
||||
setTextColorRef(android.R.attr.textColorPrimary)
|
||||
}
|
||||
linearLayout.addView(titleView)
|
||||
|
||||
val optionView = TextView(context)
|
||||
linearLayout.addView(optionView)
|
||||
|
||||
layout.addView(linearLayout)
|
||||
|
||||
addPref(layout)
|
||||
val (layout, summaryView, optionView) = makeListPreferenceLayout()
|
||||
summaryView.text = title
|
||||
|
||||
registerUpdate {
|
||||
val selectedOptionIndex = options.indexOfFirst { it.value == selected() }
|
||||
|
@ -220,6 +196,57 @@ fun <T> PreferenceParent.listPreference(
|
|||
}
|
||||
}
|
||||
|
||||
fun PreferenceParent.customListPreference(
|
||||
title: String,
|
||||
selected: () -> String,
|
||||
onClick: () -> Unit
|
||||
) {
|
||||
val (layout, summaryView, optionView) = makeListPreferenceLayout()
|
||||
summaryView.text = title
|
||||
|
||||
layout.setOnClickListener {
|
||||
onClick()
|
||||
}
|
||||
|
||||
registerUpdate {
|
||||
optionView.text = selected()
|
||||
}
|
||||
}
|
||||
|
||||
private data class ListPreferenceLayout(
|
||||
val layout: LinearLayout,
|
||||
val summaryView: TextView,
|
||||
val optionView: TextView,
|
||||
)
|
||||
|
||||
private fun PreferenceParent.makeListPreferenceLayout(): ListPreferenceLayout {
|
||||
val layout = itemLayout(context).apply {
|
||||
isClickable = true
|
||||
val outValue = TypedValue()
|
||||
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)
|
||||
setBackgroundResource(outValue.resourceId)
|
||||
setPadding(dpToPx(16), dpToPx(16), dpToPx(16), dpToPx(16))
|
||||
}
|
||||
val linearLayout = LinearLayout(context).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
}
|
||||
|
||||
|
||||
val summaryView = TextView(context).apply {
|
||||
setTextAppearanceRef(android.R.attr.textAppearanceListItem)
|
||||
setTextColorRef(android.R.attr.textColorPrimary)
|
||||
}
|
||||
linearLayout.addView(summaryView)
|
||||
|
||||
val optionView = TextView(context)
|
||||
linearLayout.addView(optionView)
|
||||
|
||||
layout.addView(linearLayout)
|
||||
|
||||
addPref(layout)
|
||||
return ListPreferenceLayout(layout, summaryView, optionView)
|
||||
}
|
||||
|
||||
|
||||
fun PreferenceParent.preferenceCategory(
|
||||
@StringRes title: Int,
|
||||
|
|
Loading…
Reference in New Issue