Also removed custom summary in favor of using a summary provider
This commit is contained in:
Jays2Kings 2021-04-13 14:56:47 -04:00
parent 3553013191
commit f9d42601e0
6 changed files with 43 additions and 37 deletions

View File

@ -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,

View File

@ -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 }
}

View File

@ -26,26 +26,15 @@ class IntListMatPreference @JvmOverloads constructor(
var entries: List<String> = 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<MatPreference>? = SummaryProvider<MatPreference> {
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 {

View File

@ -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<MatPreference>? = SummaryProvider<MatPreference> {
val index = entryValues.indexOf(prefs.getStringPref(key, defValue).getOrDefault())
return if (entries.isEmpty() || index == -1) ""
if (entries.isEmpty() || index == -1) ""
else entries[index]
}

View File

@ -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<MatPreference>? = 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 {

View File

@ -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<String>()).getOrDefault().mapNotNull {
override var customSummaryProvider: SummaryProvider<MatPreference>? = SummaryProvider<MatPreference> {
prefs.getStringSet(key, emptySet<String>()).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<Int>()
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)