Allow excluding categories from library update

Closes #3467, #4661, #1839

Supersedes #4474
This commit is contained in:
arkon
2021-04-04 16:48:39 -04:00
parent b2fee7035f
commit 4f1275ac01
10 changed files with 334 additions and 27 deletions

View File

@@ -4,10 +4,10 @@ import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.core.text.buildSpannedString
import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.list.listItemsMultiChoice
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
@@ -29,6 +29,8 @@ import eu.kanade.tachiyomi.util.preference.summaryRes
import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.widget.MinMaxNumberPicker
import eu.kanade.tachiyomi.widget.materialdialogs.QuadStateCheckBox
import eu.kanade.tachiyomi.widget.materialdialogs.listItemsQuadStateMultiChoice
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@@ -174,18 +176,37 @@ class SettingsLibraryController : SettingsController() {
LibraryGlobalUpdateCategoriesDialog().showDialog(router)
}
preferences.libraryUpdateCategories().asFlow()
.onEach { mutableSet ->
val selectedCategories = mutableSet
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.sortedBy { it.order }
summary = if (selectedCategories.isEmpty()) {
context.getString(R.string.all)
} else {
selectedCategories.joinToString { it.name }
}
fun updateSummary() {
val selectedCategories = preferences.libraryUpdateCategories().get()
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.sortedBy { it.order }
val includedItemsText = if (selectedCategories.isEmpty()) {
context.getString(R.string.all)
} else {
selectedCategories.joinToString { it.name }
}
val excludedCategories = preferences.libraryUpdateCategoriesExclude().get()
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.sortedBy { it.order }
val excludedItemsText = if (excludedCategories.isEmpty()) {
context.getString(R.string.none)
} else {
excludedCategories.joinToString { it.name }
}
summary = buildSpannedString {
append(context.getString(R.string.include, includedItemsText))
appendLine()
append(context.getString(R.string.exclude, excludedItemsText))
}
}
preferences.libraryUpdateCategories().asFlow()
.onEach { updateSummary() }
.launchIn(viewScope)
preferences.libraryUpdateCategoriesExclude().asFlow()
.onEach { updateSummary() }
.launchIn(viewScope)
}
intListPreference {
@@ -281,19 +302,34 @@ class SettingsLibraryController : SettingsController() {
val items = categories.map { it.name }
val preselected = categories
.filter { it.id.toString() in preferences.libraryUpdateCategories().get() }
.map { categories.indexOf(it) }
.map {
when (it.id.toString()) {
in preferences.libraryUpdateCategories().get() -> QuadStateCheckBox.State.CHECKED.ordinal
in preferences.libraryUpdateCategoriesExclude().get() -> QuadStateCheckBox.State.INVERSED.ordinal
else -> QuadStateCheckBox.State.UNCHECKED.ordinal
}
}
.toIntArray()
return MaterialDialog(activity!!)
.title(R.string.pref_library_update_categories)
.listItemsMultiChoice(
.listItemsQuadStateMultiChoice(
items = items,
initialSelection = preselected,
allowEmptySelection = true
) { _, selections, _ ->
val newCategories = selections.map { categories[it] }
preferences.libraryUpdateCategories().set(newCategories.map { it.id.toString() }.toSet())
initialSelected = preselected
) { selections ->
val included = selections
.mapIndexed { index, value -> if (value == QuadStateCheckBox.State.CHECKED.ordinal) index else null }
.filterNotNull()
.map { categories[it].id.toString() }
.toSet()
val excluded = selections
.mapIndexed { index, value -> if (value == QuadStateCheckBox.State.INVERSED.ordinal) index else null }
.filterNotNull()
.map { categories[it].id.toString() }
.toSet()
preferences.libraryUpdateCategories().set(included)
preferences.libraryUpdateCategoriesExclude().set(excluded)
}
.positiveButton(android.R.string.ok)
.negativeButton(android.R.string.cancel)