From e41173ad1d2ee8f2707f08cf2cf86be90b08ddd3 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 21 Jan 2020 21:20:59 -0800 Subject: [PATCH] Fix crash for multichoice pref dialog with an "all" option --- .../preference/MultiListMatPreference.kt | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt index 18f5301806..14ea22b901 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MultiListMatPreference.kt @@ -21,7 +21,6 @@ class MultiListMatPreference @JvmOverloads constructor(activity: Activity?, cont get() = 0 set(value) { customSummary = context.getString(value) } var customSummary:String? = null - var positions:IntArray? = null override fun getSummary(): CharSequence { return customSummary ?: super.getSummary() @@ -29,32 +28,34 @@ class MultiListMatPreference @JvmOverloads constructor(activity: Activity?, cont @SuppressLint("CheckResult") override fun MaterialDialog.setItems() { + val set = prefs.getStringSet(key, emptySet()).getOrDefault() + var default = set.mapNotNull { + if (entryValues.indexOf(it) == -1) null + else entryValues.indexOf(it) + if (allSelectionRes != null) 1 else 0 } + .toIntArray() + if (allSelectionRes != null && default.isEmpty()) default = intArrayOf(0) + val items = if (allSelectionRes != null) + (listOf(context.getString(allSelectionRes!!)) + entries) else entries positiveButton(android.R.string.ok) { - var value = positions?.map { + val pos = mutableListOf() + for (i in items.indices) + if (!(allSelectionRes != null && i == 0) && isItemChecked(i)) pos.add(i) + var value = pos.map { entryValues[it - if (allSelectionRes != null) 1 else 0] }?.toSet() ?: emptySet() if (allSelectionRes != null && isItemChecked(0)) value = emptySet() prefs.getStringSet(key, emptySet()).set(value) callChangeListener(value) this@MultiListMatPreference.summary = this@MultiListMatPreference.summary } - val set = prefs.getStringSet(key, emptySet()).getOrDefault() - var default = set.map { - entryValues.indexOf(it) + if (allSelectionRes != null) 1 else 0 } - .toIntArray() - if (allSelectionRes != null && default.isEmpty()) default = intArrayOf(0) - val items = if (allSelectionRes != null) - (listOf(context.getString(allSelectionRes!!)) + entries) else entries listItemsMultiChoice(items = items, allowEmptySelection = true, disabledIndices = if (allSelectionRes != null) intArrayOf(0) else null, waitForPositiveButton = false, initialSelection = default) { _, pos, _ -> - positions = pos if (allSelectionRes != null) { if (pos.isEmpty()) checkItem(0) else uncheckItem(0) } - callChangeListener(positions) } } } \ No newline at end of file