From a0064a1699ccd9ac28f53fd7525d165a518e3cc9 Mon Sep 17 00:00:00 2001 From: len Date: Thu, 1 Dec 2016 20:34:30 +0100 Subject: [PATCH] Don't allow to create categories with the same name --- .../ui/category/CategoryPresenter.kt | 58 ++++++++----------- app/src/main/res/values/strings.xml | 3 + 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt index d2dbd05b9..9f129a150 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryPresenter.kt @@ -1,9 +1,11 @@ package eu.kanade.tachiyomi.ui.category import android.os.Bundle +import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter +import eu.kanade.tachiyomi.util.toast import rx.android.schedulers.AndroidSchedulers import uy.kohesive.injekt.injectLazy @@ -15,57 +17,41 @@ import uy.kohesive.injekt.injectLazy class CategoryPresenter : BasePresenter() { /** - * 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? = null - - companion object { - /** - * The id of the restartable. - */ - final private val GET_CATEGORIES = 1 - } + private var categories: List = emptyList() override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) - // Get categories as list - restartableLatestCache(GET_CATEGORIES, - { - db.getCategories().asRxObservable() - .doOnNext { categories -> this.categories = categories } - .observeOn(AndroidSchedulers.mainThread()) - }, CategoryActivity::setCategories) - - // Start get categories as list task - start(GET_CATEGORIES) + db.getCategories().asRxObservable() + .doOnNext { categories = it } + .observeOn(AndroidSchedulers.mainThread()) + .subscribeLatestCache(CategoryActivity::setCategories) } - /** * Create category and add it to database * * @param name name of category */ 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. val cat = Category.create(name) // Set the new item in the last position. - var max = 0 - if (categories != null) { - for (cat2 in categories!!) { - if (cat2.order > max) { - max = cat2.order + 1 - } - } - } - cat.order = max + cat.order = categories.map { it.order + 1 }.max() ?: 0 // Insert into database. db.insertCategory(cat).asRxObservable().subscribe() @@ -86,8 +72,8 @@ class CategoryPresenter : BasePresenter() { * @param categories list of categories */ fun reorderCategories(categories: List) { - for (i in categories.indices) { - categories[i].order = i + categories.forEachIndexed { i, category -> + category.order = i } db.insertCategories(categories).asRxObservable().subscribe() @@ -100,6 +86,12 @@ class CategoryPresenter : BasePresenter() { * @param name new name of category */ 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 db.insertCategory(category).asRxObservable().subscribe() } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6a877440e..ec71d04fc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -268,6 +268,9 @@ Status Chapters + + A category with this name already exists! + This will remove the read date of this chapter. Are you sure? Reset all chapters for this manga