From 52f567ff9e89cbbcd17300a2f4ca05cbe144d160 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sat, 6 Apr 2024 11:17:56 +0200 Subject: [PATCH] rename reorderPopupKeysDialog to reorderDialog add icon to layout, should later display toolbar key --- .../latin/settings/LanguageSettingsDialog.kt | 4 +- .../settings/PreferencesSettingsFragment.java | 7 +- .../keyboard/latin/utils/DialogUtils.kt | 83 +++++++++++++++++++ .../keyboard/latin/utils/PopupKeysUtils.kt | 79 ------------------ ..._list_item.xml => reorder_dialog_item.xml} | 9 +- 5 files changed, 96 insertions(+), 86 deletions(-) rename app/src/main/res/layout/{popup_keys_list_item.xml => reorder_dialog_item.xml} (81%) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt b/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt index 4805f44a9..8fbab5d99 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt @@ -375,12 +375,12 @@ class LanguageSettingsDialog( private fun setupPopupSettings() { binding.popupOrder.setOnClickListener { 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() } binding.popupLabelPriority.setOnClickListener { 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() } } diff --git a/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java b/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java index 813457652..8fdc97b4c 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java @@ -20,6 +20,7 @@ import helium314.keyboard.keyboard.KeyboardSwitcher; import helium314.keyboard.latin.AudioAndHapticFeedbackManager; import helium314.keyboard.latin.R; import helium314.keyboard.latin.RichInputMethodManager; +import helium314.keyboard.latin.utils.DialogUtilsKt; import helium314.keyboard.latin.utils.PopupKeysUtilsKt; import helium314.keyboard.latin.utils.SubtypeSettingsKt; import helium314.keyboard.latin.utils.SubtypeUtilsKt; @@ -59,15 +60,15 @@ public final class PreferencesSettingsFragment extends SubScreenFragment { setLocalizedNumberRowVisibility(); findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false)); 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; }); 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; }); 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; }); } diff --git a/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt index 536843035..1f356bd9c 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt @@ -1,9 +1,21 @@ package helium314.keyboard.latin.utils 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.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 java.util.Collections fun getPlatformDialogThemeContext(context: Context): Context { // 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) .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>() { + override fun areItemsTheSame(p0: Pair, p1: Pair) = p0 == p1 + override fun areContentsTheSame(p0: Pair, p1: Pair) = p0 == p1 + } + + val adapter = object : ListAdapter, 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(R.id.reorder_item_name)?.text = displayText + val switch = viewHolder.itemView.findViewById(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() +} diff --git a/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt index 21abb38f6..e08d16995 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt @@ -1,27 +1,12 @@ // SPDX-License-Identifier: GPL-3.0-only package helium314.keyboard.latin.utils -import android.content.Context 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.internal.KeySpecParser import helium314.keyboard.keyboard.internal.KeyboardParams import helium314.keyboard.keyboard.internal.keyboard_parser.floris.PopupSet 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" 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 } ?: 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>() { - override fun areItemsTheSame(p0: Pair, p1: Pair) = p0 == p1 - override fun areContentsTheSame(p0: Pair, p1: Pair) = p0 == p1 - } - val bgColor = ContextCompat.getColor(context, R.color.sliding_items_background) - val adapter = object : ListAdapter, 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(R.id.popup_keys_type)?.text = displayText - val switch = viewHolder.itemView.findViewById(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() -} diff --git a/app/src/main/res/layout/popup_keys_list_item.xml b/app/src/main/res/layout/reorder_dialog_item.xml similarity index 81% rename from app/src/main/res/layout/popup_keys_list_item.xml rename to app/src/main/res/layout/reorder_dialog_item.xml index 311fe9ed7..7ba6d7f9e 100644 --- a/app/src/main/res/layout/popup_keys_list_item.xml +++ b/app/src/main/res/layout/reorder_dialog_item.xml @@ -11,15 +11,20 @@ android:minHeight="48dp" android:layout_width="match_parent" android:layout_height="wrap_content" > +