diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt index 3e50386028..a14dc0c426 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt @@ -15,7 +15,9 @@ import com.f2prateek.rx.preferences.Preference import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment import eu.kanade.tachiyomi.ui.main.MainActivity @@ -33,11 +35,8 @@ import nucleus.factory.RequiresPresenter import rx.Subscription import rx.android.schedulers.AndroidSchedulers import rx.subjects.PublishSubject -import java.util.concurrent.TimeUnit.MILLISECONDS -import android.widget.Toast -import eu.kanade.tachiyomi.data.database.models.Category -import eu.kanade.tachiyomi.data.preference.PreferencesHelper import uy.kohesive.injekt.injectLazy +import java.util.concurrent.TimeUnit.MILLISECONDS /** * Fragment that shows the manga from the catalogue. @@ -546,54 +545,64 @@ open class CatalogueFragment : BaseRxFragment(), * @param position the position of the element clicked. */ override fun onItemLongClick(position: Int) { + // Get manga val manga = (adapter.getItem(position) as? CatalogueItem?)?.manga ?: return + // Fetch categories val categories = presenter.getCategories() - val defaultCategory = categories.find { it.id == preferences.defaultCategory()} - if(defaultCategory != null) { - if(!manga.favorite) { - presenter.changeMangaFavorite(manga) - } - presenter.moveMangaToCategory(defaultCategory, manga) - } else { + if (manga.favorite){ MaterialDialog.Builder(activity) - .title(R.string.action_move_category) - .items(categories.map { it.name }) - .itemsCallbackMultiChoice(presenter.getMangaCategoryIds(manga)) { dialog, position, _ -> - if (defaultSelectedWithOtherCategory(position)) { - // Deselect default category - dialog.setSelectedIndices(position.filter {it > 0}.toTypedArray()) - Toast.makeText(dialog.context, R.string.invalid_combination, Toast.LENGTH_SHORT).show() + .items(getString(R.string.remove_from_library )) + .itemsCallback { _, _, which, _ -> + when (which) { + 0 -> { + presenter.changeMangaFavorite(manga) + adapter.notifyItemChanged(position) + } } - - true - } - .alwaysCallMultiChoiceCallback() - .positiveText(android.R.string.ok) - .negativeText(android.R.string.cancel) - .onPositive { dialog, _ -> - updateMangaCategories(manga, dialog, categories, position) - } - .build() - .show() - } - } - - private fun defaultSelectedWithOtherCategory(position: Array): Boolean { - return position.contains(0) && position.count() > 1 - } - - private fun updateMangaCategories(manga: Manga, dialog: MaterialDialog, categories: List, position: Int) { - val selectedCategories = dialog.selectedIndices?.map { categories[it] } ?: emptyList() - - if(!selectedCategories.isEmpty()) { - if(!manga.favorite) { + }.show() + }else{ + val defaultCategory = categories.find { it.id == preferences.defaultCategory()} + if(defaultCategory != null) { presenter.changeMangaFavorite(manga) + presenter.moveMangaToCategory(defaultCategory, manga) + // Show manga has been added + context.toast(R.string.added_to_library) + adapter.notifyItemChanged(position) + } else { + MaterialDialog.Builder(activity) + .title(R.string.action_move_category) + .items(categories.map { it.name }) + .itemsCallbackMultiChoice(presenter.getMangaCategoryIds(manga)) { dialog, position, _ -> + if (position.contains(0) && position.count() > 1) { + // Deselect default category + dialog.setSelectedIndices(position.filter {it > 0}.toTypedArray()) + dialog.context.toast(R.string.invalid_combination) + } + true + } + .alwaysCallMultiChoiceCallback() + .positiveText(android.R.string.ok) + .negativeText(android.R.string.cancel) + .onPositive { dialog, _ -> + val selectedCategories = dialog.selectedIndices?.map { categories[it] } ?: emptyList() + updateMangaCategories(manga, selectedCategories, position) + } + .build() + .show() } - presenter.moveMangaToCategories(selectedCategories.filter { it.id != 0}, manga) - } else { - presenter.changeMangaFavorite(manga) } + } + + /** + * Update manga to use selected categories. + * + * @param manga needed to change + * @param selectedCategories selected categories + * @param position position of adapter + */ + private fun updateMangaCategories(manga: Manga, selectedCategories: List, position: Int) { + presenter.updateMangaCategories(manga,selectedCategories) adapter.notifyItemChanged(position) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt index cd9b889324..daa6d58862 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt @@ -26,7 +26,6 @@ import rx.schedulers.Schedulers import rx.subjects.PublishSubject import timber.log.Timber import uy.kohesive.injekt.injectLazy -import java.util.ArrayList /** * Presenter of [CatalogueFragment]. @@ -416,7 +415,7 @@ open class CataloguePresenter : BasePresenter() { */ fun getMangaCategoryIds(manga: Manga): Array { val categories = db.getCategoriesForManga(manga).executeAsBlocking() - if(categories.isEmpty()) { + if (categories.isEmpty()) { return arrayListOf(Category.createDefault().id).toTypedArray() } return categories.map { it.id }.toTypedArray() @@ -444,4 +443,21 @@ open class CataloguePresenter : BasePresenter() { moveMangaToCategories(arrayListOf(category), manga) } + /** + * Update manga to use selected categories. + * + * @param manga needed to change + * @param selectedCategories selected categories + */ + fun updateMangaCategories(manga: Manga, selectedCategories: List) { + if (!selectedCategories.isEmpty()) { + if (!manga.favorite) + changeMangaFavorite(manga) + + moveMangaToCategories(selectedCategories.filter { it.id != 0 }, manga) + } else { + changeMangaFavorite(manga) + } + } + } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4d5f1bcceb..9fed481594 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -272,6 +272,7 @@ No more results Local manga Default can\'t be selected with other categories + The manga has been added to your library This manga was removed from the database!