Skip to content

Commit

Permalink
Allow to disable summary generation for choice preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Apr 25, 2021
1 parent c0c59a6 commit 7780c3d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ abstract class AbstractChoiceDialogPreference(

internal var selectionAdapter: SelectionAdapter? = null

/**
* Whether the summary should be auto-generated from the current selection.
* If true, [summary] and [summaryRes] are ignored.
*
* Default true, set to false to turn off this feature.
*/
var autoGeneratedSummary = true

init {
require(items.isNotEmpty()) { "Supplied list of items may not be empty!" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MultiChoiceDialogPreference(key: String, items: List<SelectionItem>) : Abs
}

override fun resolveSummary(context: Context): CharSequence? = when {
selections.isNotEmpty() -> selections.joinToString(limit = 3, truncated = "") { selection ->
autoGeneratedSummary && selections.isNotEmpty() -> selections.joinToString(limit = 3, truncated = "") { selection ->
when {
selection.titleRes != DEFAULT_RES_ID -> context.resources.getText(selection.titleRes)
else -> selection.title
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.Maxr1998.modernpreferences.preferences.choice

import android.content.Context
import de.Maxr1998.modernpreferences.helpers.DEFAULT_RES_ID

class SingleChoiceDialogPreference(key: String, items: List<SelectionItem>) : AbstractChoiceDialogPreference(key, items, false) {

/**
* The initial selection if no choice has been made yet and no value
* was persisted to [SharedPreferences][android.content.SharedPreferences]
* The initial selection if no choice has been made and no value persisted to [SharedPreferences][android.content.SharedPreferences] yet.
*
* Must match a [SelectionItem.key] in [items].
*/
var initialSelection: String? = null

Expand Down Expand Up @@ -44,9 +46,11 @@ class SingleChoiceDialogPreference(key: String, items: List<SelectionItem>) : Ab
override fun resolveSummary(context: Context): CharSequence? {
val selection = currentSelection
return when {
selection == null -> super.resolveSummary(context)
selection.titleRes != -1 -> context.resources.getText(selection.titleRes)
else -> selection.title
autoGeneratedSummary && selection != null -> when {
selection.titleRes != DEFAULT_RES_ID -> context.resources.getText(selection.titleRes)
else -> selection.title
}
else -> super.resolveSummary(context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ object Common {
)
singleChoice("single-choice-dialog", selectableItems) {
title = "Single choice selection dialog"
summary = "Only one item is selectable, de-selection is impossible"
}
multiChoice("multi-choice-dialog", selectableItems) {
title = "Multi choice selection dialog"
summary = "None, one or multiple items are selectable"
}
editText("edit-text") {
title = "Text input"
Expand Down

0 comments on commit 7780c3d

Please sign in to comment.