From 45fc923f3421d7f26dbc06d2811ee173bb569e30 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 6 May 2020 12:01:51 -0400 Subject: [PATCH] Updating logic of hopper Also empty categories now show catergory is empty, even if filter is on --- .../tachiyomi/ui/library/LibraryController.kt | 77 ++++++++++++------- .../tachiyomi/ui/library/LibraryPresenter.kt | 53 ++++++++++--- 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index fcba9ff437..40b4710546 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -175,16 +175,8 @@ class LibraryController( category_hopper_frame.translationY += dy category_hopper_frame.translationY = category_hopper_frame.translationY.coerceIn(0f, 60f.dpToPx) - up_category.alpha = if (!recycler.canScrollVertically(-1)) { - 0.25f - } else { - 1f - } - down_category.alpha = if (!recycler.canScrollVertically(1)) { - 0.25f - } else { - 1f - } + up_category.alpha = if (isAtTop()) 0.25f else 1f + down_category.alpha = if (isAtBottom()) 0.25f else 1f } if (!filter_bottom_sheet.sheetBehavior.isHidden()) { scrollDistance += abs(dy) @@ -249,6 +241,22 @@ class LibraryController( } } + fun isAtTop(): Boolean { + return if (presenter.showAllCategories) { + getVisibleHeader() == adapter.headerItems.firstOrNull() + } else { + getVisibleHeader()?.category?.id == presenter.categories.firstOrNull()?.id + } + } + + fun isAtBottom(): Boolean { + return if (presenter.showAllCategories) { + getVisibleHeader() == adapter.headerItems.lastOrNull() + } else { + getVisibleHeader()?.category?.id == presenter.categories.lastOrNull()?.id + } + } + private fun showFilterTip() { if (preferences.shownFilterTutorial().get()) return val activity = activity ?: return @@ -381,24 +389,10 @@ class LibraryController( category_hopper_frame.gone() down_category.setOnClickListener { - val position = getVisibleHeader() ?: return@setOnClickListener - val newOffset = adapter.headerItems.indexOf(position) + 1 - if (newOffset < presenter.categories.size) { - val newOrder = (adapter.headerItems[newOffset] as LibraryHeaderItem).category.order - scrollToHeader(newOrder) - } else { - recycler.scrollToPosition(adapter.itemCount - 1) - } + jumpToNextCategory(true) } up_category.setOnClickListener { - val position = getVisibleHeader() ?: return@setOnClickListener - val newOffset = adapter.headerItems.indexOf(position) - 1 - if (newOffset > -1) { - val newOrder = (adapter.headerItems[newOffset] as LibraryHeaderItem).category.order - scrollToHeader(newOrder) - } else { - recycler.scrollToPosition(0) - } + jumpToNextCategory(false) } down_category.setOnLongClickListener { recycler.scrollToPosition(adapter.itemCount - 1) @@ -506,6 +500,37 @@ class LibraryController( } } + private fun jumpToNextCategory(next: Boolean) { + val category = getVisibleHeader() ?: return + if (presenter.showAllCategories) { + val newOffset = adapter.headerItems.indexOf(category) + (if (next) 1 else -1) + if (if (!next) { + newOffset > -1 + } else { + newOffset < adapter.headerItems.size + }) { + val newOrder = + (adapter.headerItems[newOffset] as LibraryHeaderItem).category.order + scrollToHeader(newOrder) + } else { + recycler.scrollToPosition(if (next) adapter.itemCount - 1 else 0) + } + } else { + val newOffset = + presenter.categories.indexOfFirst { presenter.currentCategory == it.id } + + (if (next) 1 else -1) + if (if (!next) { + newOffset > -1 + } else { + newOffset < presenter.categories.size + } + ) { + val newOrder = presenter.categories[newOffset].order + scrollToHeader(newOrder) + } + } + } + private fun getHeader(): LibraryHeaderItem? { val position = (recycler.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index fe611201d4..d048a032ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -16,11 +16,11 @@ import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet -import eu.kanade.tachiyomi.util.lang.removeArticles -import eu.kanade.tachiyomi.util.system.executeOnIO import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet.Companion.STATE_EXCLUDE import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet.Companion.STATE_IGNORE import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet.Companion.STATE_INCLUDE +import eu.kanade.tachiyomi.util.lang.removeArticles +import eu.kanade.tachiyomi.util.system.executeOnIO import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -110,7 +110,21 @@ class LibraryPresenter( preferences.lastUsedCategory().set(order) val category = categories.find { it.order == order }?.id ?: return currentCategory = category - view.onNextLibraryUpdate(sectionedLibraryItems[currentCategory] ?: emptyList()) + view.onNextLibraryUpdate( + sectionedLibraryItems[currentCategory] ?: blankItem() + ) + } + + private fun blankItem(id: Int = currentCategory): List { + return listOf( + LibraryItem( + LibraryManga.createBlank(id), LibraryHeaderItem( + { getCategory(id) }, + id, + preferences.alwaysShowSeeker() + ) + ) + ) } fun restoreLibrary() { @@ -124,7 +138,7 @@ class LibraryPresenter( } view.onNextLibraryUpdate( if (!show) sectionedLibraryItems[currentCategory] - ?: sectionedLibraryItems[categories.first().id] ?: emptyList() + ?: sectionedLibraryItems[categories.first().id] ?: blankItem() else libraryItems, true ) } @@ -141,7 +155,7 @@ class LibraryPresenter( withContext(Dispatchers.Main) { view.onNextLibraryUpdate( if (!show) sectionedLibraryItems[currentCategory] - ?: sectionedLibraryItems[categories.first().id] ?: emptyList() + ?: sectionedLibraryItems[categories.first().id] ?: blankItem() else libraryItems, freshStart ) } @@ -168,15 +182,32 @@ class LibraryPresenter( return items.filter f@{ item -> if (item.manga.status == -1) { return@f sectionedLibraryItems[item.manga.category]?.any { - matchesFilters(it, filterDownloaded, filterUnread, filterCompleted, - filterTracked, filterMangaType, filterTrackers) + matchesFilters( + it, + filterDownloaded, + filterUnread, + filterCompleted, + filterTracked, + filterMangaType, + filterTrackers + ) } ?: false } else if (item.manga.isBlank()) { - return@f filterDownloaded == 0 && filterUnread == 0 && filterCompleted == 0 && - filterTracked == 0 && filterMangaType == 0 + return@f if (showAllCategories) { + filterDownloaded == 0 && filterUnread == 0 && filterCompleted == 0 && filterTracked == 0 && filterMangaType == 0 + } else { + true + } } - matchesFilters(item, filterDownloaded, filterUnread, filterCompleted, filterTracked, - filterMangaType, filterTrackers) + matchesFilters( + item, + filterDownloaded, + filterUnread, + filterCompleted, + filterTracked, + filterMangaType, + filterTrackers + ) } }