Don't allow to create categories with the same name

This commit is contained in:
len 2016-12-01 20:34:30 +01:00
parent 907472403d
commit a0064a1699
2 changed files with 28 additions and 33 deletions

View File

@ -1,9 +1,11 @@
package eu.kanade.tachiyomi.ui.category package eu.kanade.tachiyomi.ui.category
import android.os.Bundle import android.os.Bundle
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.toast
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -15,57 +17,41 @@ import uy.kohesive.injekt.injectLazy
class CategoryPresenter : BasePresenter<CategoryActivity>() { class CategoryPresenter : BasePresenter<CategoryActivity>() {
/** /**
* Used to connect to database * Used to connect to database.
*/ */
val db: DatabaseHelper by injectLazy() private val db: DatabaseHelper by injectLazy()
/** /**
* List containing categories * List containing categories.
*/ */
private var categories: List<Category>? = null private var categories: List<Category> = emptyList()
companion object {
/**
* The id of the restartable.
*/
final private val GET_CATEGORIES = 1
}
override fun onCreate(savedState: Bundle?) { override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState) super.onCreate(savedState)
// Get categories as list
restartableLatestCache(GET_CATEGORIES,
{
db.getCategories().asRxObservable() db.getCategories().asRxObservable()
.doOnNext { categories -> this.categories = categories } .doOnNext { categories = it }
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
}, CategoryActivity::setCategories) .subscribeLatestCache(CategoryActivity::setCategories)
// Start get categories as list task
start(GET_CATEGORIES)
} }
/** /**
* Create category and add it to database * Create category and add it to database
* *
* @param name name of category * @param name name of category
*/ */
fun createCategory(name: String) { fun createCategory(name: String) {
// Do not allow duplicate categories.
if (categories.any { it.name.equals(name, true) }) {
context.toast(R.string.error_category_exists)
return
}
// Create category. // Create category.
val cat = Category.create(name) val cat = Category.create(name)
// Set the new item in the last position. // Set the new item in the last position.
var max = 0 cat.order = categories.map { it.order + 1 }.max() ?: 0
if (categories != null) {
for (cat2 in categories!!) {
if (cat2.order > max) {
max = cat2.order + 1
}
}
}
cat.order = max
// Insert into database. // Insert into database.
db.insertCategory(cat).asRxObservable().subscribe() db.insertCategory(cat).asRxObservable().subscribe()
@ -86,8 +72,8 @@ class CategoryPresenter : BasePresenter<CategoryActivity>() {
* @param categories list of categories * @param categories list of categories
*/ */
fun reorderCategories(categories: List<Category>) { fun reorderCategories(categories: List<Category>) {
for (i in categories.indices) { categories.forEachIndexed { i, category ->
categories[i].order = i category.order = i
} }
db.insertCategories(categories).asRxObservable().subscribe() db.insertCategories(categories).asRxObservable().subscribe()
@ -100,6 +86,12 @@ class CategoryPresenter : BasePresenter<CategoryActivity>() {
* @param name new name of category * @param name new name of category
*/ */
fun renameCategory(category: Category, name: String) { fun renameCategory(category: Category, name: String) {
// Do not allow duplicate categories.
if (categories.any { it.name.equals(name, true) }) {
context.toast(R.string.error_category_exists)
return
}
category.name = name category.name = name
db.insertCategory(category).asRxObservable().subscribe() db.insertCategory(category).asRxObservable().subscribe()
} }

View File

@ -268,6 +268,9 @@
<string name="status">Status</string> <string name="status">Status</string>
<string name="chapters">Chapters</string> <string name="chapters">Chapters</string>
<!-- Category activity -->
<string name="error_category_exists">A category with this name already exists!</string>
<!-- Dialog remove recently view --> <!-- Dialog remove recently view -->
<string name="dialog_remove_recently_description">This will remove the read date of this chapter. Are you sure?</string> <string name="dialog_remove_recently_description">This will remove the read date of this chapter. Are you sure?</string>
<string name="dialog_remove_recently_reset">Reset all chapters for this manga</string> <string name="dialog_remove_recently_reset">Reset all chapters for this manga</string>