diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index f7dc58fea2..2dae209867 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt @@ -94,14 +94,14 @@ class SettingsDownloadController : SettingsController() { entryValues = dbCategories.map { it.id.toString() } allSelectionRes = R.string.all - preferences.downloadNew().asImmediateFlow { isVisible = it } + preferences.downloadNew().asImmediateFlow(viewScope) { isVisible = it } - preferences.downloadNewCategories().asImmediateFlow { list -> + preferences.downloadNewCategories().asImmediateFlow(viewScope) { list -> val selectedCategories = list .mapNotNull { id -> dbCategories.find { it.id == id.toInt() } } .sortedBy { it.order } - customSummary = if (selectedCategories.isEmpty()) { + summary = if (selectedCategories.isEmpty()) { resources?.getString(R.string.all) } else { selectedCategories.joinToString { it.name } @@ -112,7 +112,7 @@ class SettingsDownloadController : SettingsController() { intListPreference(activity) { key = Keys.deleteRemovedChapters titleRes = R.string.delete_removed_chapters - customSummary = activity?.getString(R.string.delete_downloaded_if_removed_online) + summary = activity?.getString(R.string.delete_downloaded_if_removed_online) entriesRes = arrayOf( R.string.ask_on_chapters_page, R.string.always_keep, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index c71341742b..fa9322c2ef 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -96,7 +96,7 @@ class SettingsLibraryController : SettingsController() { titleRes = R.string.library_update_restriction entriesRes = arrayOf(R.string.wifi, R.string.charging) entryValues = listOf("wifi", "ac") - customSummaryRes = R.string.library_update_restriction_summary + summaryRes = R.string.library_update_restriction_summary preferences.libraryUpdateInterval().asObservable() .subscribeUntilDestroy { isVisible = it > 0 } @@ -140,7 +140,7 @@ class SettingsLibraryController : SettingsController() { list.mapNotNull { id -> dbCategories.find { it.id == id.toInt() } } .sortedBy { it.order } - customSummary = + summary = if (selectedCategories.isEmpty()) context.getString(R.string.all) else selectedCategories.joinToString { it.name } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt index eae0e21900..31b26454b8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListMatPreference.kt @@ -26,26 +26,15 @@ class IntListMatPreference @JvmOverloads constructor( var entries: List = emptyList() var customSelectedValue: Int? = null - override fun onSetInitialValue(defaultValue: Any?) { - super.onSetInitialValue(defaultValue) - defValue = defaultValue as? Int ?: defValue - } - override fun getSummary(): CharSequence { - if (customSummary != null) return customSummary!! - if (key == null) return super.getSummary() + override var customSummaryProvider: SummaryProvider? = SummaryProvider { val index = entryValues.indexOf(prefs.getInt(key, defValue).getOrDefault()) - return if (entries.isEmpty() || index == -1) "" + if (entries.isEmpty() || index == -1) "" else entries[index] } - override fun setSummary(summaryResId: Int) { - super.setSummary(summaryResId) - customSummary = summary.toString() - } - - override fun setSummary(summary: CharSequence?) { - super.setSummary(summary) - customSummary = summary?.toString() + override fun onSetInitialValue(defaultValue: Any?) { + super.onSetInitialValue(defaultValue) + defValue = defaultValue as? Int ?: defValue } override fun dialog(): MaterialDialog { diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt index dbe7c70df9..106b358e86 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/ListMatPreference.kt @@ -30,10 +30,10 @@ open class ListMatPreference @JvmOverloads constructor( super.onSetInitialValue(defaultValue) defValue = defaultValue as? String ?: defValue } - override fun getSummary(): CharSequence { - if (customSummary != null) return customSummary!! + + override var customSummaryProvider: SummaryProvider? = SummaryProvider { val index = entryValues.indexOf(prefs.getStringPref(key, defValue).getOrDefault()) - return if (entries.isEmpty() || index == -1) "" + if (entries.isEmpty() || index == -1) "" else entries[index] } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt index f47d2a3c55..0357c39c47 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MatPreference.kt @@ -22,7 +22,6 @@ open class MatPreference @JvmOverloads constructor( protected val prefs: PreferencesHelper = Injekt.get() private var isShowing = false - var customSummary: String? = null @StringRes var dialogTitleRes: Int? = null @@ -35,8 +34,30 @@ open class MatPreference @JvmOverloads constructor( isShowing = true } - override fun getSummary(): CharSequence { - return customSummary ?: super.getSummary() + protected open var customSummaryProvider: SummaryProvider? = null + set(value) { + field = value + summaryProvider = customSummaryProvider + } + + override fun setSummary(summaryResId: Int) { + if (summaryResId == 0) { + summaryProvider = customSummaryProvider + return + } else { + summaryProvider = null + } + super.setSummary(summaryResId) + } + + override fun setSummary(summary: CharSequence?) { + if (summary == null) { + summaryProvider = customSummaryProvider + return + } else { + summaryProvider = null + } + super.setSummary(summary) } open fun dialog(): MaterialDialog { 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 cd569795a1..5ee3385b54 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 @@ -20,13 +20,9 @@ class MultiListMatPreference @JvmOverloads constructor( ListMatPreference(activity, context, attrs) { var allSelectionRes: Int? = null - var customSummaryRes: Int - get() = 0 - set(value) { customSummary = context.getString(value) } - override fun getSummary(): CharSequence { - if (customSummary != null) return customSummary!! - return prefs.getStringSet(key, emptySet()).getOrDefault().mapNotNull { + override var customSummaryProvider: SummaryProvider? = SummaryProvider { + prefs.getStringSet(key, emptySet()).getOrDefault().mapNotNull { if (entryValues.indexOf(it) == -1) null else entryValues.indexOf(it) + if (allSelectionRes != null) 1 else 0 }.toIntArray().joinToString(",") { @@ -50,9 +46,9 @@ class MultiListMatPreference @JvmOverloads constructor( 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() + var value = pos.mapNotNull { + entryValues.getOrNull(it - if (allSelectionRes != null) 1 else 0) + }.toSet() if (allSelectionRes != null && isItemChecked(0)) value = emptySet() prefs.getStringSet(key, emptySet()).set(value) callChangeListener(value)