Small fixes (#740)

This commit is contained in:
Bram van de Kerkhof 2017-04-07 21:17:21 +02:00 committed by GitHub
parent f6a79bde6f
commit 6069659e0f
3 changed files with 72 additions and 46 deletions

View File

@ -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<CataloguePresenter>(),
* @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()
if (manga.favorite){
MaterialDialog.Builder(activity)
.items(getString(R.string.remove_from_library ))
.itemsCallback { _, _, which, _ ->
when (which) {
0 -> {
presenter.changeMangaFavorite(manga)
adapter.notifyItemChanged(position)
}
}
}.show()
}else{
val defaultCategory = categories.find { it.id == preferences.defaultCategory()}
if(defaultCategory != null) {
if(!manga.favorite) {
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 (defaultSelectedWithOtherCategory(position)) {
if (position.contains(0) && position.count() > 1) {
// Deselect default category
dialog.setSelectedIndices(position.filter {it > 0}.toTypedArray())
Toast.makeText(dialog.context, R.string.invalid_combination, Toast.LENGTH_SHORT).show()
dialog.context.toast(R.string.invalid_combination)
}
true
}
.alwaysCallMultiChoiceCallback()
.positiveText(android.R.string.ok)
.negativeText(android.R.string.cancel)
.onPositive { dialog, _ ->
updateMangaCategories(manga, dialog, categories, position)
val selectedCategories = dialog.selectedIndices?.map { categories[it] } ?: emptyList()
updateMangaCategories(manga, selectedCategories, position)
}
.build()
.show()
}
}
private fun defaultSelectedWithOtherCategory(position: Array<Int>): Boolean {
return position.contains(0) && position.count() > 1
}
private fun updateMangaCategories(manga: Manga, dialog: MaterialDialog, categories: List<Category>, position: Int) {
val selectedCategories = dialog.selectedIndices?.map { categories[it] } ?: emptyList()
if(!selectedCategories.isEmpty()) {
if(!manga.favorite) {
presenter.changeMangaFavorite(manga)
}
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<Category>, position: Int) {
presenter.updateMangaCategories(manga,selectedCategories)
adapter.notifyItemChanged(position)
}

View File

@ -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<CatalogueFragment>() {
*/
fun getMangaCategoryIds(manga: Manga): Array<Int?> {
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<CatalogueFragment>() {
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<Category>) {
if (!selectedCategories.isEmpty()) {
if (!manga.favorite)
changeMangaFavorite(manga)
moveMangaToCategories(selectedCategories.filter { it.id != 0 }, manga)
} else {
changeMangaFavorite(manga)
}
}
}

View File

@ -272,6 +272,7 @@
<string name="no_more_results">No more results</string>
<string name="local_source">Local manga</string>
<string name="invalid_combination">Default can\'t be selected with other categories</string>
<string name="added_to_library">The manga has been added to your library</string>
<!-- Manga activity -->
<string name="manga_not_in_db">This manga was removed from the database!</string>