Updates to manage category dialog

Fixes to default category basically + adding global update check to default
This commit is contained in:
Jays2Kings 2021-04-27 00:48:58 -04:00
parent d72189b8e4
commit 63e6fb715d

View File

@ -70,36 +70,40 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
} }
private fun onPositiveButtonClick(): Boolean { private fun onPositiveButtonClick(): Boolean {
if (category?.id ?: 0 <= 0 && category != null) return false
val text = binding.title.text.toString() val text = binding.title.text.toString()
val categoryExists = categoryExists(text) val categoryExists = categoryExists(text)
val category = this.category ?: Category.create(text) val category = this.category ?: Category.create(text)
if (text.isNotBlank() && !categoryExists && !text.equals(this.category?.name ?: "", true)) { if (category.id != 0) {
category.name = text if (text.isNotBlank() && !categoryExists &&
if (this.category == null) { !text.equals(this.category?.name ?: "", true)
val categories = db.getCategories().executeAsBlocking() ) {
category.order = categories.maxOf { it.order } + 1 category.name = text
category.mangaSort = LibrarySort.Title.categoryValue if (this.category == null) {
val dbCategory = db.insertCategory(category).executeAsBlocking() val categories = db.getCategories().executeAsBlocking()
category.id = dbCategory.insertedId()?.toInt() category.order = categories.maxOf { it.order } + 1
this.category = category category.mangaSort = LibrarySort.Title.categoryValue
} else { val dbCategory = db.insertCategory(category).executeAsBlocking()
db.insertCategory(category).executeAsBlocking() category.id = dbCategory.insertedId()?.toInt()
this.category = category
} else {
db.insertCategory(category).executeAsBlocking()
}
} else if (categoryExists) {
binding.categoryTextLayout.error =
binding.categoryTextLayout.context.getString(R.string.category_with_name_exists)
return false
} else if (text.isBlank()) {
binding.categoryTextLayout.error =
binding.categoryTextLayout.context.getString(R.string.category_cannot_be_blank)
return false
} }
} else if (categoryExists) {
binding.categoryTextLayout.error = binding.categoryTextLayout.context.getString(R.string.category_with_name_exists)
return false
} else if (text.isBlank()) {
binding.categoryTextLayout.error = binding.categoryTextLayout.context.getString(R.string.category_cannot_be_blank)
return false
} }
if (!updatePref(preferences.downloadNewCategories(), binding.downloadNew)) { when (updatePref(preferences.downloadNewCategories(), binding.downloadNew)) {
preferences.downloadNew().set(false) true -> preferences.downloadNew().set(true)
} else { false -> preferences.downloadNew().set(false)
preferences.downloadNew().set(true)
} }
if (preferences.libraryUpdateInterval().getOrDefault() > 0 && if (preferences.libraryUpdateInterval().getOrDefault() > 0 &&
!updatePref(preferences.libraryUpdateCategories(), binding.includeGlobal) updatePref(preferences.libraryUpdateCategories(), binding.includeGlobal) == false
) { ) {
preferences.libraryUpdateInterval().set(0) preferences.libraryUpdateInterval().set(0)
LibraryUpdateJob.setupTask(0) LibraryUpdateJob.setupTask(0)
@ -119,10 +123,8 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
fun onViewCreated() { fun onViewCreated() {
if (category?.id ?: 0 <= 0 && category != null) { if (category?.id ?: 0 <= 0 && category != null) {
binding.title.isVisible = false binding.categoryTextLayout.isVisible = false
binding.downloadNew.isVisible = false binding.downloadNew.isVisible = false
binding.includeGlobal.isVisible = false
return
} }
binding.editCategories.isVisible = category != null binding.editCategories.isVisible = category != null
binding.editCategories.setOnClickListener { binding.editCategories.setOnClickListener {
@ -134,19 +136,21 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
} }
binding.title.hint = category?.name ?: binding.editCategories.context.getString(R.string.category) binding.title.hint = category?.name ?: binding.editCategories.context.getString(R.string.category)
binding.title.append(category?.name ?: "") binding.title.append(category?.name ?: "")
val downloadNew = preferences.downloadNew().get() if (binding.downloadNew.isVisible) {
setCheckbox( val downloadNew = preferences.downloadNew().get()
binding.downloadNew, setCheckbox(
preferences.downloadNewCategories(), binding.downloadNew,
true preferences.downloadNewCategories(),
) true
if (downloadNew && preferences.downloadNewCategories().get().isEmpty()) { )
binding.downloadNew.isVisible = false if (downloadNew && preferences.downloadNewCategories().get().isEmpty()) {
} else if (!downloadNew) { binding.downloadNew.isVisible = false
binding.downloadNew.isVisible = true } else if (!downloadNew) {
binding.downloadNew.isVisible = true
}
binding.downloadNew.isChecked =
preferences.downloadNew().get() && binding.downloadNew.isChecked
} }
binding.downloadNew.isChecked =
preferences.downloadNew().get() && binding.downloadNew.isChecked
setCheckbox( setCheckbox(
binding.includeGlobal, binding.includeGlobal,
preferences.libraryUpdateCategories(), preferences.libraryUpdateCategories(),
@ -155,8 +159,9 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
} }
/** 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>>, box: CompoundButton): Boolean? {
val categoryId = category?.id ?: return true val categoryId = category?.id ?: return null
if (!box.isVisible) return null
val updateCategories = categories.get().toMutableSet() val updateCategories = categories.get().toMutableSet()
if (box.isChecked) { if (box.isChecked) {
updateCategories.add(categoryId.toString()) updateCategories.add(categoryId.toString())