jump to last chapter now moves to where to you last tapped

This commit is contained in:
Jay 2019-11-03 14:47:07 -08:00
parent 2c7f0743db
commit 1265a3a790
4 changed files with 42 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.FlexibleAdapter
@ -59,6 +60,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
*/ */
private var subscriptions = CompositeSubscription() private var subscriptions = CompositeSubscription()
private var lastTouchUpY = 0f
fun onCreate(controller: LibraryController) { fun onCreate(controller: LibraryController) {
this.controller = controller this.controller = controller
@ -210,11 +213,18 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
toggleSelection(position) toggleSelection(position)
return true return true
} else { } else {
openManga(item.manga) openManga(item.manga, lastTouchUpY)
return false 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. * 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. * @param manga the manga to open.
*/ */
private fun openManga(manga: Manga) { private fun openManga(manga: Manga, startY:Float?) {
controller.openManga(manga) controller.openManga(manga, startY)
} }
/** /**

View File

@ -428,11 +428,11 @@ class LibraryController(
actionMode = null actionMode = null
} }
fun openManga(manga: Manga) { fun openManga(manga: Manga, startY: Float?) {
// Notify the presenter a manga is being opened. // Notify the presenter a manga is being opened.
presenter.onOpenManga() presenter.onOpenManga()
router.pushController(MangaController(manga).withFadeTransaction()) router.pushController(MangaController(manga, startY).withFadeTransaction())
} }
/** /**

View File

@ -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<SourceManager>().getOrStub(manga.source)
}
}
constructor(mangaId: Long) : this( constructor(mangaId: Long) : this(
Injekt.get<DatabaseHelper>().getManga(mangaId).executeAsBlocking()) Injekt.get<DatabaseHelper>().getManga(mangaId).executeAsBlocking())
@ -60,6 +71,8 @@ class MangaController : RxController, TabbedController {
var source: Source? = null var source: Source? = null
private set private set
var startingChapterYPos:Float? = null
private var adapter: MangaDetailAdapter? = null private var adapter: MangaDetailAdapter? = null
val fromCatalogue = args.getBoolean(FROM_CATALOGUE_EXTRA, false) val fromCatalogue = args.getBoolean(FROM_CATALOGUE_EXTRA, false)
@ -164,7 +177,7 @@ class MangaController : RxController, TabbedController {
if (!router.hasRootController()) { if (!router.hasRootController()) {
val controller = when (position) { val controller = when (position) {
INFO_CONTROLLER -> MangaInfoController() INFO_CONTROLLER -> MangaInfoController()
CHAPTERS_CONTROLLER -> ChaptersController() CHAPTERS_CONTROLLER -> ChaptersController(startingChapterYPos)
TRACK_CONTROLLER -> TrackController() TRACK_CONTROLLER -> TrackController()
else -> error("Wrong position $position") else -> error("Wrong position $position")
} }

View File

@ -36,7 +36,7 @@ import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlin.math.* import kotlin.math.*
class ChaptersController : NucleusController<ChaptersPresenter>(), class ChaptersController() : NucleusController<ChaptersPresenter>(),
ActionMode.Callback, ActionMode.Callback,
FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemClickListener,
FlexibleAdapter.OnItemLongClickListener, FlexibleAdapter.OnItemLongClickListener,
@ -47,6 +47,10 @@ class ChaptersController : NucleusController<ChaptersPresenter>(),
DownloadCustomChaptersDialog.Listener, DownloadCustomChaptersDialog.Listener,
DeleteChaptersDialog.Listener { DeleteChaptersDialog.Listener {
constructor(startY: Float?) : this() {
this.startingChapterYPos = startY
}
/** /**
* Adapter containing a list of chapters. * Adapter containing a list of chapters.
*/ */
@ -69,6 +73,7 @@ class ChaptersController : NucleusController<ChaptersPresenter>(),
setHasOptionsMenu(true) setHasOptionsMenu(true)
setOptionsMenuHidden(true) setOptionsMenuHidden(true)
} }
var startingChapterYPos:Float? = null
override fun createPresenter(): ChaptersPresenter { override fun createPresenter(): ChaptersPresenter {
val ctrl = parentController as MangaController val ctrl = parentController as MangaController
@ -233,11 +238,15 @@ class ChaptersController : NucleusController<ChaptersPresenter>(),
} }
private fun scrollToUnread() { private fun scrollToUnread() {
if (adapter?.items.isNullOrEmpty()) return
if (scrollToUnread) { if (scrollToUnread) {
val index = presenter.getFirstUnreadIndex() val index = presenter.getFirstUnreadIndex()
val centerOfScreen = recycler.height / 2 - 96 val centerOfScreen =
(recycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(index, if (startingChapterYPos != null) startingChapterYPos!!.toInt() - recycler.top - 96
centerOfScreen) else recycler.height / 2 - 96
(recycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
index, centerOfScreen
)
} }
scrollToUnread = false scrollToUnread = false
} }