diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index 5356f40e6a..2e5815efbb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import android.util.AttributeSet +import android.view.MotionEvent import android.view.View import android.widget.FrameLayout import eu.davidea.flexibleadapter.FlexibleAdapter @@ -59,6 +60,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att */ private var subscriptions = CompositeSubscription() + private var lastTouchUpY = 0f + fun onCreate(controller: LibraryController) { this.controller = controller @@ -210,11 +213,18 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att toggleSelection(position) return true } else { - openManga(item.manga) + openManga(item.manga, lastTouchUpY) return false } } + override fun dispatchTouchEvent(ev: MotionEvent?): Boolean { + when (ev?.action) { + MotionEvent.ACTION_UP -> lastTouchUpY = ev?.y + } + return super.dispatchTouchEvent(ev) + } + /** * Called when a manga is long clicked. * @@ -230,8 +240,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att * * @param manga the manga to open. */ - private fun openManga(manga: Manga) { - controller.openManga(manga) + private fun openManga(manga: Manga, startY:Float?) { + controller.openManga(manga, startY) } /** 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 ed21533c38..b07cd99fb3 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 @@ -428,11 +428,11 @@ class LibraryController( actionMode = null } - fun openManga(manga: Manga) { + fun openManga(manga: Manga, startY: Float?) { // Notify the presenter a manga is being opened. presenter.onOpenManga() - router.pushController(MangaController(manga).withFadeTransaction()) + router.pushController(MangaController(manga, startY).withFadeTransaction()) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 71e742e4e9..695bec88ca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -48,6 +48,17 @@ class MangaController : RxController, TabbedController { } } + constructor(manga: Manga?, startY:Float?) : super(Bundle().apply { + putLong(MANGA_EXTRA, manga?.id ?: 0) + putBoolean(FROM_CATALOGUE_EXTRA, false) + }) { + this.manga = manga + startingChapterYPos = startY + if (manga != null) { + source = Injekt.get().getOrStub(manga.source) + } + } + constructor(mangaId: Long) : this( Injekt.get().getManga(mangaId).executeAsBlocking()) @@ -60,6 +71,8 @@ class MangaController : RxController, TabbedController { var source: Source? = null private set + var startingChapterYPos:Float? = null + private var adapter: MangaDetailAdapter? = null val fromCatalogue = args.getBoolean(FROM_CATALOGUE_EXTRA, false) @@ -164,7 +177,7 @@ class MangaController : RxController, TabbedController { if (!router.hasRootController()) { val controller = when (position) { INFO_CONTROLLER -> MangaInfoController() - CHAPTERS_CONTROLLER -> ChaptersController() + CHAPTERS_CONTROLLER -> ChaptersController(startingChapterYPos) TRACK_CONTROLLER -> TrackController() else -> error("Wrong position $position") } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt index 2fb9f1d0c8..db801fa42b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt @@ -36,7 +36,7 @@ import androidx.core.view.ViewCompat import androidx.recyclerview.widget.RecyclerView import kotlin.math.* -class ChaptersController : NucleusController(), +class ChaptersController() : NucleusController(), ActionMode.Callback, FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemLongClickListener, @@ -47,6 +47,10 @@ class ChaptersController : NucleusController(), DownloadCustomChaptersDialog.Listener, DeleteChaptersDialog.Listener { + constructor(startY: Float?) : this() { + this.startingChapterYPos = startY + } + /** * Adapter containing a list of chapters. */ @@ -69,6 +73,7 @@ class ChaptersController : NucleusController(), setHasOptionsMenu(true) setOptionsMenuHidden(true) } + var startingChapterYPos:Float? = null override fun createPresenter(): ChaptersPresenter { val ctrl = parentController as MangaController @@ -233,11 +238,15 @@ class ChaptersController : NucleusController(), } private fun scrollToUnread() { + if (adapter?.items.isNullOrEmpty()) return if (scrollToUnread) { val index = presenter.getFirstUnreadIndex() - val centerOfScreen = recycler.height / 2 - 96 - (recycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(index, - centerOfScreen) + val centerOfScreen = + if (startingChapterYPos != null) startingChapterYPos!!.toInt() - recycler.top - 96 + else recycler.height / 2 - 96 + (recycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset( + index, centerOfScreen + ) } scrollToUnread = false }