From be1a0e6e9cbd9346c616f2eaf384187aa925c8b1 Mon Sep 17 00:00:00 2001 From: Tomer Rosenfeld Date: Sun, 27 Aug 2023 10:54:31 -0700 Subject: [PATCH] Allow to prevent dialog item selection through listener (#139) --- library/api/library.api | 6 ++++++ .../choice/AbstractChoiceDialogPreference.kt | 12 +++++++++++- .../preferences/choice/OnItemClickListener.kt | 14 ++++++++++++++ .../preferences/choice/SelectionAdapter.kt | 8 +++++--- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener.kt diff --git a/library/api/library.api b/library/api/library.api index 46cdfdaf..a9d9efa1 100644 --- a/library/api/library.api +++ b/library/api/library.api @@ -391,11 +391,13 @@ public abstract class de/Maxr1998/modernpreferences/preferences/choice/AbstractC public fun createDialog (Landroid/content/Context;)Landroid/app/Dialog; public final fun getAutoGeneratedSummary ()Z protected final fun getItems ()Ljava/util/List; + public final fun getOnItemClickListener ()Lde/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener; public abstract fun isSelected (Lde/Maxr1998/modernpreferences/preferences/choice/SelectionItem;)Z public fun onStateChanged (Landroidx/lifecycle/LifecycleOwner;Landroidx/lifecycle/Lifecycle$Event;)V protected abstract fun persistSelection ()V protected abstract fun resetSelection ()V public final fun setAutoGeneratedSummary (Z)V + public final fun setOnItemClickListener (Lde/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener;)V } public final class de/Maxr1998/modernpreferences/preferences/choice/MultiChoiceDialogPreference : de/Maxr1998/modernpreferences/preferences/choice/AbstractChoiceDialogPreference { @@ -412,6 +414,10 @@ public abstract interface class de/Maxr1998/modernpreferences/preferences/choice public abstract fun onSelectionChange (Lde/Maxr1998/modernpreferences/preferences/choice/MultiChoiceDialogPreference;Ljava/util/Set;)Z } +public abstract interface class de/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener { + public abstract fun onItemSelected (Lde/Maxr1998/modernpreferences/preferences/choice/SelectionItem;)Z +} + public final class de/Maxr1998/modernpreferences/preferences/choice/SelectionItem { public fun (Ljava/lang/String;II)V public synthetic fun (Ljava/lang/String;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V diff --git a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/AbstractChoiceDialogPreference.kt b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/AbstractChoiceDialogPreference.kt index 5b0137d5..f4ab7cdb 100644 --- a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/AbstractChoiceDialogPreference.kt +++ b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/AbstractChoiceDialogPreference.kt @@ -17,6 +17,8 @@ abstract class AbstractChoiceDialogPreference( internal var selectionAdapter: SelectionAdapter? = null + var onItemClickListener: OnItemClickListener? = null + /** * Whether the summary should be auto-generated from the current selection. * If true, [summary] and [summaryRes] are ignored. @@ -32,7 +34,11 @@ abstract class AbstractChoiceDialogPreference( override fun createDialog(context: Context): Dialog = Config.dialogBuilderFactory(context).apply { if (titleRes != DEFAULT_RES_ID) setTitle(titleRes) else setTitle(title) val dialogContent = RecyclerView(context).apply { - selectionAdapter = SelectionAdapter(this@AbstractChoiceDialogPreference, items, allowMultiSelect) + selectionAdapter = SelectionAdapter( + this@AbstractChoiceDialogPreference, + items, + allowMultiSelect, + ) adapter = selectionAdapter layoutManager = LinearLayoutManager(context) } @@ -47,6 +53,10 @@ abstract class AbstractChoiceDialogPreference( } }.create() + internal fun shouldSelect(item: SelectionItem): Boolean { + return onItemClickListener?.onItemSelected(item) ?: true + } + internal abstract fun select(item: SelectionItem) protected abstract fun persistSelection() diff --git a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener.kt b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener.kt new file mode 100644 index 00000000..9f6e38cf --- /dev/null +++ b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/OnItemClickListener.kt @@ -0,0 +1,14 @@ +package de.Maxr1998.modernpreferences.preferences.choice + +fun interface OnItemClickListener { + /** + * Notified when the user clicks a [SelectionItem]. + * This is called before the change gets persisted and can be prevented by returning false. + * + * @param item the clicked item + * @param index the index of the clicked item + * + * @return true to to allow the selection of the item + */ + fun onItemSelected(item: SelectionItem): Boolean +} \ No newline at end of file diff --git a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/SelectionAdapter.kt b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/SelectionAdapter.kt index 433dfea6..a6550fcc 100644 --- a/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/SelectionAdapter.kt +++ b/library/src/main/java/de/Maxr1998/modernpreferences/preferences/choice/SelectionAdapter.kt @@ -36,9 +36,11 @@ internal class SelectionAdapter( isVisible = item.summaryRes != -1 || item.summary != null } itemView.setOnClickListener { - preference.select(item) - if (allowMultiSelect) notifyItemChanged(position) - else notifySelectionChanged() + if (preference.shouldSelect(item)) { + preference.select(item) + if (allowMultiSelect) notifyItemChanged(position) + else notifySelectionChanged() + } } } }