Using TriStateBox for manage category dialog

so you can now exclude categories in that dialog as well
This commit is contained in:
Jays2Kings 2021-08-07 17:13:29 -04:00
parent e24686f0cf
commit ff9f79f2fc
2 changed files with 49 additions and 16 deletions

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.category
import android.app.Activity import android.app.Activity
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.widget.CompoundButton
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener import androidx.core.widget.addTextChangedListener
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
@ -21,6 +20,7 @@ import eu.kanade.tachiyomi.databinding.MangaCategoryDialogBinding
import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.library.LibrarySort import eu.kanade.tachiyomi.ui.library.LibrarySort
import eu.kanade.tachiyomi.util.view.withFadeTransaction import eu.kanade.tachiyomi.util.view.withFadeTransaction
import eu.kanade.tachiyomi.widget.TriStateCheckBox
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
class ManageCategoryDialog(bundle: Bundle? = null) : class ManageCategoryDialog(bundle: Bundle? = null) :
@ -98,12 +98,22 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
return false return false
} }
} }
when (updatePref(preferences.downloadNewCategories(), binding.downloadNew)) { when (
updatePref(
preferences.downloadNewCategories(),
preferences.downloadNewCategoriesExclude(),
binding.downloadNew
)
) {
true -> preferences.downloadNew().set(true) true -> preferences.downloadNew().set(true)
false -> preferences.downloadNew().set(false) false -> preferences.downloadNew().set(false)
} }
if (preferences.libraryUpdateInterval().getOrDefault() > 0 && if (preferences.libraryUpdateInterval().getOrDefault() > 0 &&
updatePref(preferences.libraryUpdateCategories(), binding.includeGlobal) == false updatePref(
preferences.libraryUpdateCategories(),
preferences.libraryUpdateCategoriesExclude(),
binding.includeGlobal
) == false
) { ) {
preferences.libraryUpdateInterval().set(0) preferences.libraryUpdateInterval().set(0)
LibraryUpdateJob.setupTask(preferences.context, 0) LibraryUpdateJob.setupTask(preferences.context, 0)
@ -140,6 +150,7 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
setCheckbox( setCheckbox(
binding.downloadNew, binding.downloadNew,
preferences.downloadNewCategories(), preferences.downloadNewCategories(),
preferences.downloadNewCategoriesExclude(),
true true
) )
if (downloadNew && preferences.downloadNewCategories().get().isEmpty()) { if (downloadNew && preferences.downloadNewCategories().get().isEmpty()) {
@ -147,37 +158,59 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
} else if (!downloadNew) { } else if (!downloadNew) {
binding.downloadNew.isVisible = true binding.downloadNew.isVisible = true
} }
binding.downloadNew.isChecked = if (!downloadNew) {
preferences.downloadNew().get() && binding.downloadNew.isChecked binding.downloadNew.isChecked = false
}
setCheckbox( setCheckbox(
binding.includeGlobal, binding.includeGlobal,
preferences.libraryUpdateCategories(), preferences.libraryUpdateCategories(),
preferences.libraryUpdateCategoriesExclude(),
preferences.libraryUpdateInterval().getOrDefault() > 0 preferences.libraryUpdateInterval().getOrDefault() > 0
) )
} }
/** Update a pref based on checkbox, and return if the pref is not empty */ /** Update a pref based on checkbox, and return if the pref is not empty */
private fun updatePref(categories: Preference<Set<String>>, box: CompoundButton): Boolean? { private fun updatePref(
categories: Preference<Set<String>>,
excludeCategories: Preference<Set<String>>,
box: TriStateCheckBox
): Boolean? {
val categoryId = category?.id ?: return null val categoryId = category?.id ?: return null
if (!box.isVisible) return null if (!box.isVisible) return null
val updateCategories = categories.get().toMutableSet() val updateCategories = categories.get().toMutableSet()
if (box.isChecked) { val excludeUpdateCategories = excludeCategories.get().toMutableSet()
when (box.state) {
TriStateCheckBox.State.CHECKED -> {
updateCategories.add(categoryId.toString()) updateCategories.add(categoryId.toString())
} else { excludeUpdateCategories.remove(categoryId.toString())
}
TriStateCheckBox.State.INVERSED -> {
updateCategories.remove(categoryId.toString()) updateCategories.remove(categoryId.toString())
excludeUpdateCategories.add(categoryId.toString())
}
TriStateCheckBox.State.UNCHECKED -> {
updateCategories.remove(categoryId.toString())
excludeUpdateCategories.remove(categoryId.toString())
}
} }
categories.set(updateCategories) categories.set(updateCategories)
excludeCategories.set(excludeUpdateCategories)
return updateCategories.isNotEmpty() return updateCategories.isNotEmpty()
} }
private fun setCheckbox( private fun setCheckbox(
box: CompoundButton, box: TriStateCheckBox,
categories: Preference<Set<String>>, categories: Preference<Set<String>>,
excludeCategories: Preference<Set<String>>,
shouldShow: Boolean shouldShow: Boolean
) { ) {
val updateCategories = categories.get() val updateCategories = categories.get()
box.isVisible = updateCategories.isNotEmpty() && shouldShow val excludeUpdateCategories = excludeCategories.get()
if (updateCategories.isNotEmpty() && shouldShow) box.isChecked = box.isVisible = (updateCategories.isNotEmpty() || excludeUpdateCategories.isNotEmpty()) && shouldShow
updateCategories.any { category?.id == it.toIntOrNull() } if (shouldShow) box.state = when {
updateCategories.any { category?.id == it.toIntOrNull() } -> TriStateCheckBox.State.CHECKED
excludeUpdateCategories.any { category?.id == it.toIntOrNull() } -> TriStateCheckBox.State.INVERSED
else -> TriStateCheckBox.State.UNCHECKED
}
} }
} }

View File

@ -27,7 +27,7 @@
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.checkbox.MaterialCheckBox <eu.kanade.tachiyomi.widget.TriStateCheckBox
android:id="@+id/download_new" android:id="@+id/download_new"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -35,7 +35,7 @@
android:layout_marginEnd="12dp" android:layout_marginEnd="12dp"
android:text="@string/download_new_chapters" /> android:text="@string/download_new_chapters" />
<com.google.android.material.checkbox.MaterialCheckBox <eu.kanade.tachiyomi.widget.TriStateCheckBox
android:id="@+id/include_global" android:id="@+id/include_global"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"