Browse Source

WIP prefs: implement emoji preference

prefs
charlag 4 years ago
parent
commit
90a338aa69
No known key found for this signature in database
GPG Key ID: 5B96E7C76F0CA558
  1. 42
      app/src/main/java/com/keylesspalace/tusky/components/preference/EmojiSelector.kt
  2. 15
      app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt
  3. 73
      app/src/main/java/com/keylesspalace/tusky/settings/SettingsDSL.kt

42
app/src/main/java/com/keylesspalace/tusky/components/preference/EmojiPreference.kt → app/src/main/java/com/keylesspalace/tusky/components/preference/EmojiSelector.kt

@ -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) {
private lateinit var selected: EmojiCompatFont
private lateinit var original: EmojiCompatFont
class EmojiSelector(
private val context: Context,
private val okHttpClient: OkHttpClient,
initialSelectedFontId: Int,
private val saveSelectedId: (Int) -> Unit
) {
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)
}
/**

15
app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt

@ -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

73
app/src/main/java/com/keylesspalace/tusky/settings/SettingsDSL.kt

@ -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,6 +173,53 @@ fun <T> PreferenceParent.listPreference(
selected: () -> T,
onSelection: (T) -> Unit,
) {
val (layout, summaryView, optionView) = makeListPreferenceLayout()
summaryView.text = title
registerUpdate {
val selectedOptionIndex = options.indexOfFirst { it.value == selected() }
optionView.setText(options[selectedOptionIndex].name)
layout.setOnClickListener {
AlertDialog.Builder(context)
.setSingleChoiceItems(
options.map { it.name }.toTypedArray(),
selectedOptionIndex,
) { dialog, wh ->
onSelection(options[wh].value)
dialog.dismiss()
}
.setCancelable(true)
.show()
}
}
}
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()
@ -186,12 +232,11 @@ fun <T> PreferenceParent.listPreference(
}
val titleView = TextView(context).apply {
text = title
val summaryView = TextView(context).apply {
setTextAppearanceRef(android.R.attr.textAppearanceListItem)
setTextColorRef(android.R.attr.textColorPrimary)
}
linearLayout.addView(titleView)
linearLayout.addView(summaryView)
val optionView = TextView(context)
linearLayout.addView(optionView)
@ -199,25 +244,7 @@ fun <T> PreferenceParent.listPreference(
layout.addView(linearLayout)
addPref(layout)
registerUpdate {
val selectedOptionIndex = options.indexOfFirst { it.value == selected() }
optionView.setText(options[selectedOptionIndex].name)
layout.setOnClickListener {
AlertDialog.Builder(context)
.setSingleChoiceItems(
options.map { it.name }.toTypedArray(),
selectedOptionIndex,
) { dialog, wh ->
onSelection(options[wh].value)
dialog.dismiss()
}
.setCancelable(true)
.show()
}
}
return ListPreferenceLayout(layout, summaryView, optionView)
}

Loading…
Cancel
Save