Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to prevent dialog item selection through listener #139

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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