Skip to content

Commit

Permalink
Allow to prevent dialog item selection through listener (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosenpin authored Aug 27, 2023
1 parent c6ab337 commit be1a0e6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
6 changes: 6 additions & 0 deletions library/api/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <init> (Ljava/lang/String;II)V
public synthetic fun <init> (Ljava/lang/String;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}
Expand Down

0 comments on commit be1a0e6

Please sign in to comment.