rename reorderPopupKeysDialog to reorderDialog

add icon to layout, should later display toolbar key
This commit is contained in:
Helium314 2024-04-06 11:17:56 +02:00
parent 0ab9aeb8e6
commit 52f567ff9e
5 changed files with 96 additions and 86 deletions

View file

@ -375,12 +375,12 @@ class LanguageSettingsDialog(
private fun setupPopupSettings() { private fun setupPopupSettings() {
binding.popupOrder.setOnClickListener { binding.popupOrder.setOnClickListener {
val popupKeyTypesDefault = prefs.getString(Settings.PREF_POPUP_KEYS_ORDER, POPUP_KEYS_ORDER_DEFAULT)!! val popupKeyTypesDefault = prefs.getString(Settings.PREF_POPUP_KEYS_ORDER, POPUP_KEYS_ORDER_DEFAULT)!!
reorderPopupKeysDialog(context, Settings.PREF_POPUP_KEYS_ORDER + "_" + mainLocale.toLanguageTag(), popupKeyTypesDefault, R.string.popup_order) reorderDialog(context, Settings.PREF_POPUP_KEYS_ORDER + "_" + mainLocale.toLanguageTag(), popupKeyTypesDefault, R.string.popup_order)
KeyboardLayoutSet.onKeyboardThemeChanged() KeyboardLayoutSet.onKeyboardThemeChanged()
} }
binding.popupLabelPriority.setOnClickListener { binding.popupLabelPriority.setOnClickListener {
val popupKeyTypesDefault = prefs.getString(Settings.PREF_POPUP_KEYS_LABELS_ORDER, POPUP_KEYS_LABEL_DEFAULT)!! val popupKeyTypesDefault = prefs.getString(Settings.PREF_POPUP_KEYS_LABELS_ORDER, POPUP_KEYS_LABEL_DEFAULT)!!
reorderPopupKeysDialog(context, Settings.PREF_POPUP_KEYS_LABELS_ORDER + "_" + mainLocale.toLanguageTag(), popupKeyTypesDefault, R.string.hint_source) reorderDialog(context, Settings.PREF_POPUP_KEYS_LABELS_ORDER + "_" + mainLocale.toLanguageTag(), popupKeyTypesDefault, R.string.hint_source)
KeyboardLayoutSet.onKeyboardThemeChanged() KeyboardLayoutSet.onKeyboardThemeChanged()
} }
} }

View file

@ -20,6 +20,7 @@ import helium314.keyboard.keyboard.KeyboardSwitcher;
import helium314.keyboard.latin.AudioAndHapticFeedbackManager; import helium314.keyboard.latin.AudioAndHapticFeedbackManager;
import helium314.keyboard.latin.R; import helium314.keyboard.latin.R;
import helium314.keyboard.latin.RichInputMethodManager; import helium314.keyboard.latin.RichInputMethodManager;
import helium314.keyboard.latin.utils.DialogUtilsKt;
import helium314.keyboard.latin.utils.PopupKeysUtilsKt; import helium314.keyboard.latin.utils.PopupKeysUtilsKt;
import helium314.keyboard.latin.utils.SubtypeSettingsKt; import helium314.keyboard.latin.utils.SubtypeSettingsKt;
import helium314.keyboard.latin.utils.SubtypeUtilsKt; import helium314.keyboard.latin.utils.SubtypeUtilsKt;
@ -59,15 +60,15 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
setLocalizedNumberRowVisibility(); setLocalizedNumberRowVisibility();
findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false)); findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false));
findPreference(Settings.PREF_POPUP_KEYS_ORDER).setOnPreferenceClickListener((pref) -> { findPreference(Settings.PREF_POPUP_KEYS_ORDER).setOnPreferenceClickListener((pref) -> {
PopupKeysUtilsKt.reorderPopupKeysDialog(requireContext(), Settings.PREF_POPUP_KEYS_ORDER, PopupKeysUtilsKt.POPUP_KEYS_ORDER_DEFAULT, R.string.popup_order); DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_ORDER, PopupKeysUtilsKt.POPUP_KEYS_ORDER_DEFAULT, R.string.popup_order);
return true; return true;
}); });
findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setOnPreferenceClickListener((pref) -> { findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setOnPreferenceClickListener((pref) -> {
PopupKeysUtilsKt.reorderPopupKeysDialog(requireContext(), Settings.PREF_POPUP_KEYS_LABELS_ORDER, PopupKeysUtilsKt.POPUP_KEYS_LABEL_DEFAULT, R.string.hint_source); DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_LABELS_ORDER, PopupKeysUtilsKt.POPUP_KEYS_LABEL_DEFAULT, R.string.hint_source);
return true; return true;
}); });
findPreference(Settings.PREF_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> { findPreference(Settings.PREF_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
PopupKeysUtilsKt.reorderPopupKeysDialog(requireContext(), Settings.PREF_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultToolbarPref(), R.string.toolbar_keys); DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultToolbarPref(), R.string.toolbar_keys);
return true; return true;
}); });
} }

View file

@ -1,9 +1,21 @@
package helium314.keyboard.latin.utils package helium314.keyboard.latin.utils
import android.content.Context import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Switch
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import helium314.keyboard.latin.R import helium314.keyboard.latin.R
import java.util.Collections
fun getPlatformDialogThemeContext(context: Context): Context { fun getPlatformDialogThemeContext(context: Context): Context {
// Because {@link AlertDialog.Builder.create()} doesn't honor the specified theme with // Because {@link AlertDialog.Builder.create()} doesn't honor the specified theme with
@ -32,3 +44,74 @@ fun infoDialog(context: Context, message: String) {
.setNegativeButton(android.R.string.ok, null) .setNegativeButton(android.R.string.ok, null)
.show() .show()
} }
/**
* Show a dialog that allows re-ordering and dis/enabling items (currently toolbar keys and popup keys).
* The items are stored in a string pref in [key]. Each item contains a name and true/false, comma-separated.
* Items are semicolon-separated, see e.g. [POPUP_KEYS_LABEL_DEFAULT] for an example.
*/
// this should probably be a class
fun reorderDialog(context: Context, key: String, defaultSetting: String, @StringRes dialogTitleId: Int) {
val prefs = DeviceProtectedUtils.getSharedPreferences(context)
val orderedItems = prefs.getString(key, defaultSetting)!!.split(";").mapTo(ArrayList()) {
val both = it.split(",")
both.first() to both.last().toBoolean()
}
val rv = RecyclerView(context)
val bgColor = ContextCompat.getColor(context, R.color.sliding_items_background)
val padding = ResourceUtils.toPx(8, context.resources)
rv.setPadding(3 * padding, padding, padding, padding)
rv.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val callback = object : DiffUtil.ItemCallback<Pair<String, Boolean>>() {
override fun areItemsTheSame(p0: Pair<String, Boolean>, p1: Pair<String, Boolean>) = p0 == p1
override fun areContentsTheSame(p0: Pair<String, Boolean>, p1: Pair<String, Boolean>) = p0 == p1
}
val adapter = object : ListAdapter<Pair<String, Boolean>, RecyclerView.ViewHolder>(callback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val b = LayoutInflater.from(context).inflate(R.layout.reorder_dialog_item, rv, false)
b.setBackgroundColor(bgColor)
return object : RecyclerView.ViewHolder(b) { }
}
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
val (text, wasChecked) = orderedItems[position]
val displayText = text.lowercase().getStringResourceOrName("", context)
viewHolder.itemView.findViewById<TextView>(R.id.reorder_item_name)?.text = displayText
val switch = viewHolder.itemView.findViewById<Switch>(R.id.reorder_item_switch)
switch?.setOnCheckedChangeListener(null)
switch?.isChecked = wasChecked
switch?.setOnCheckedChangeListener { _, isChecked ->
val pos = orderedItems.indexOfFirst { it.first == text }
orderedItems[pos] = text to isChecked
}
}
}
rv.adapter = adapter
ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) {
override fun onMove(rv: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
val pos1 = viewHolder.absoluteAdapterPosition
val pos2 = target.absoluteAdapterPosition
Collections.swap(orderedItems, pos1, pos2)
adapter.notifyItemMoved(pos1, pos2)
return true
}
override fun onSwiped(rv: RecyclerView.ViewHolder, direction: Int) { }
}).attachToRecyclerView(rv)
adapter.submitList(orderedItems)
AlertDialog.Builder(context)
.setTitle(dialogTitleId)
.setPositiveButton(android.R.string.ok) { _, _ ->
val value = orderedItems.joinToString(";") { it.first + "," + it.second }
prefs.edit().putString(key, value).apply()
}
.setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(R.string.button_default) { _, _ ->
prefs.edit().remove(key).apply()
}
.setView(rv)
.show()
}

View file

@ -1,27 +1,12 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin.utils package helium314.keyboard.latin.utils
import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import helium314.keyboard.keyboard.Key import helium314.keyboard.keyboard.Key
import helium314.keyboard.keyboard.internal.KeySpecParser import helium314.keyboard.keyboard.internal.KeySpecParser
import helium314.keyboard.keyboard.internal.KeyboardParams import helium314.keyboard.keyboard.internal.KeyboardParams
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.PopupSet import helium314.keyboard.keyboard.internal.keyboard_parser.floris.PopupSet
import helium314.keyboard.keyboard.internal.keyboard_parser.rtlLabel import helium314.keyboard.keyboard.internal.keyboard_parser.rtlLabel
import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings
import java.util.Collections
const val POPUP_KEYS_NUMBER = "popup_keys_number" const val POPUP_KEYS_NUMBER = "popup_keys_number"
private const val POPUP_KEYS_LANGUAGE_PRIORITY = "popup_keys_language_priority" private const val POPUP_KEYS_LANGUAGE_PRIORITY = "popup_keys_language_priority"
@ -112,67 +97,3 @@ fun getEnabledPopupKeys(prefs: SharedPreferences, key: String, defaultSetting: S
if (split.last() == "true") split.first() else null if (split.last() == "true") split.first() else null
} ?: emptyList() } ?: emptyList()
} }
/**
* show a dialog that allows re-ordering and dis/enabling the popup keys list for the pref [key]
* see e.g. [POPUP_KEYS_LABEL_DEFAULT] for the internally used format
*/
fun reorderPopupKeysDialog(context: Context, key: String, defaultSetting: String, title: Int) {
val prefs = DeviceProtectedUtils.getSharedPreferences(context)
val orderedItems = prefs.getString(key, defaultSetting)!!.split(";").mapTo(ArrayList()) {
val both = it.split(",")
both.first() to both.last().toBoolean()
}
val rv = RecyclerView(context)
val padding = ResourceUtils.toPx(8, context.resources)
rv.setPadding(3 * padding, padding, padding, padding)
rv.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val callback = object : DiffUtil.ItemCallback<Pair<String, Boolean>>() {
override fun areItemsTheSame(p0: Pair<String, Boolean>, p1: Pair<String, Boolean>) = p0 == p1
override fun areContentsTheSame(p0: Pair<String, Boolean>, p1: Pair<String, Boolean>) = p0 == p1
}
val bgColor = ContextCompat.getColor(context, R.color.sliding_items_background)
val adapter = object : ListAdapter<Pair<String, Boolean>, RecyclerView.ViewHolder>(callback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val b = LayoutInflater.from(context).inflate(R.layout.popup_keys_list_item, rv, false)
b.setBackgroundColor(bgColor)
return object : RecyclerView.ViewHolder(b) { }
}
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
val (text, wasChecked) = orderedItems[position]
val displayText = text.lowercase().getStringResourceOrName("", context)
viewHolder.itemView.findViewById<TextView>(R.id.popup_keys_type)?.text = displayText
val switch = viewHolder.itemView.findViewById<Switch>(R.id.popup_keys_switch)
switch?.setOnCheckedChangeListener(null)
switch?.isChecked = wasChecked
switch?.setOnCheckedChangeListener { _, isChecked ->
val pos = orderedItems.indexOfFirst { it.first == text }
orderedItems[pos] = text to isChecked
}
}
}
rv.adapter = adapter
ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) {
override fun onMove(rv: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
val pos1 = viewHolder.absoluteAdapterPosition
val pos2 = target.absoluteAdapterPosition
Collections.swap(orderedItems, pos1, pos2)
adapter.notifyItemMoved(pos1, pos2)
return true
}
override fun onSwiped(rv: RecyclerView.ViewHolder, direction: Int) { }
}).attachToRecyclerView(rv)
adapter.submitList(orderedItems)
AlertDialog.Builder(context)
.setTitle(title)
.setPositiveButton(android.R.string.ok) { _, _ ->
val value = orderedItems.joinToString(";") { it.first + "," + it.second }
prefs.edit().putString(key, value).apply()
}
.setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(R.string.button_default) { _, _ ->
prefs.edit().remove(key).apply()
}
.setView(rv)
.show()
}

View file

@ -11,15 +11,20 @@
android:minHeight="48dp" android:minHeight="48dp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" > android:layout_height="wrap_content" >
<ImageView
android:id="@+id/reorder_item_icon"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView <TextView
android:id="@+id/popup_keys_type" android:id="@+id/reorder_item_name"
style="@style/PreferenceTitleText" style="@style/PreferenceTitleText"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<Switch <Switch
android:id="@+id/popup_keys_switch" android:id="@+id/reorder_item_switch"
android:padding="6dp" android:padding="6dp"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_width="wrap_content" android:layout_width="wrap_content"