From 011510574db810c96a45e09d5fa9c1d6d402230d Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 24 Nov 2019 18:32:03 -0800 Subject: [PATCH] Recent Reads now expanded to a year also has lazy loading --- .../data/database/queries/HistoryQueries.kt | 4 +- .../data/database/queries/RawQueries.kt | 4 +- .../RecentChaptersController.kt | 4 +- .../ui/recently_read/RecentlyReadAdapter.kt | 3 +- .../recently_read/RecentlyReadController.kt | 41 ++++++++++++++++--- .../ui/recently_read/RecentlyReadPresenter.kt | 24 ++++++++--- 6 files changed, 62 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt index fc17e36ec2..e9650347aa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt @@ -22,10 +22,10 @@ interface HistoryQueries : DbProvider { * Returns history of recent manga containing last read chapter * @param date recent date range */ - fun getRecentManga(date: Date) = db.get() + fun getRecentManga(date: Date, offset: Int = 0) = db.get() .listOfObjects(MangaChapterHistory::class.java) .withQuery(RawQuery.builder() - .query(getRecentMangasQuery()) + .query(getRecentMangasQuery(offset)) .args(date.time) .observesTables(HistoryTable.TABLE) .build()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt index daa5c48fd0..83a9866b09 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/RawQueries.kt @@ -47,7 +47,7 @@ fun getRecentsQuery() = """ * and are read after the given time period * @return return limit is 25 */ -fun getRecentMangasQuery() = """ +fun getRecentMangasQuery(offset: Int = 0) = """ SELECT ${Manga.TABLE}.${Manga.COL_URL} as mangaUrl, ${Manga.TABLE}.*, ${Chapter.TABLE}.*, ${History.TABLE}.* FROM ${Manga.TABLE} JOIN ${Chapter.TABLE} @@ -62,7 +62,7 @@ fun getRecentMangasQuery() = """ ON ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} = max_last_read.${Chapter.COL_MANGA_ID} WHERE ${History.TABLE}.${History.COL_LAST_READ} > ? AND max_last_read.${History.COL_CHAPTER_ID} = ${History.TABLE}.${History.COL_CHAPTER_ID} ORDER BY max_last_read.${History.COL_LAST_READ} DESC - LIMIT 25 + LIMIT 25 OFFSET $offset """ fun getHistoryByMangaId() = """ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt index 0b07263555..64a1393baa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersController.kt @@ -72,9 +72,9 @@ class RecentChaptersController : NucleusController(), super.onViewCreated(view) view.context.notificationManager.cancel(Notifications.ID_NEW_CHAPTERS) // Init RecyclerView and adapter - val layoutManager = androidx.recyclerview.widget.LinearLayoutManager(view.context) + val layoutManager = LinearLayoutManager(view.context) recycler.layoutManager = layoutManager - recycler.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(view.context, androidx.recyclerview.widget.DividerItemDecoration.VERTICAL)) + recycler.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL)) recycler.setHasFixedSize(true) adapter = RecentChaptersAdapter(this@RecentChaptersController) recycler.adapter = adapter diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt index 2a7f082003..9d4f2970e8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.recently_read import eu.davidea.flexibleadapter.FlexibleAdapter +import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.tachiyomi.source.SourceManager import uy.kohesive.injekt.injectLazy import java.text.DateFormat @@ -16,7 +17,7 @@ import java.text.DecimalFormatSymbols * @constructor creates an instance of the adapter. */ class RecentlyReadAdapter(controller: RecentlyReadController) -: FlexibleAdapter(null, controller, true) { +: FlexibleAdapter>(null, controller, true) { val sourceManager by injectLazy() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadController.kt index 0e25601bc4..5c15a968de 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadController.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.History import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction +import eu.kanade.tachiyomi.ui.catalogue.browse.ProgressItem import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.util.RecyclerWindowInsetsListener @@ -23,6 +24,7 @@ import kotlinx.android.synthetic.main.recently_read_controller.* */ class RecentlyReadController : NucleusController(), FlexibleAdapter.OnUpdateListener, + FlexibleAdapter.EndlessScrollListener, RecentlyReadAdapter.OnRemoveClickListener, RecentlyReadAdapter.OnResumeClickListener, RecentlyReadAdapter.OnCoverClickListener, @@ -34,6 +36,11 @@ class RecentlyReadController : NucleusController(), var adapter: RecentlyReadAdapter? = null private set + /** + * Endless loading item. + */ + private var progressItem: ProgressItem? = null + override fun getTitle(): String? { return resources?.getString(R.string.label_recent_manga) } @@ -55,7 +62,7 @@ class RecentlyReadController : NucleusController(), super.onViewCreated(view) // Initialize adapter - recycler.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(view.context) + recycler.layoutManager = LinearLayoutManager(view.context) adapter = RecentlyReadAdapter(this@RecentlyReadController) recycler.setHasFixedSize(true) recycler.adapter = adapter @@ -73,7 +80,14 @@ class RecentlyReadController : NucleusController(), * @param mangaHistory list of manga history */ fun onNextManga(mangaHistory: List) { - adapter?.updateDataSet(mangaHistory) + if (adapter?.itemCount ?: 0 == 0) + resetProgressItem() + adapter?.onLoadMoreComplete(mangaHistory) + } + + fun onAddPageError(error: Throwable) { + adapter?.onLoadMoreComplete(null) + adapter?.endlessTargetCount = 1 } override fun onUpdateEmptyView(size: Int) { @@ -84,9 +98,26 @@ class RecentlyReadController : NucleusController(), } } + /** + * Sets a new progress item and reenables the scroll listener. + */ + private fun resetProgressItem() { + progressItem = ProgressItem() + adapter?.endlessTargetCount = 0 + adapter?.setEndlessScrollListener(this, progressItem!!) + } + + override fun onLoadMore(lastPosition: Int, currentPage: Int) { + val adapter = adapter ?: return + presenter.requestNext(adapter.itemCount) + } + + override fun noMoreLoad(newItemsSize: Int) { } + + override fun onResumeClick(position: Int) { val activity = activity ?: return - val (manga, chapter, _) = adapter?.getItem(position)?.mch ?: return + val (manga, chapter, _) = (adapter?.getItem(position) as? RecentlyReadItem)?.mch ?: return val nextChapter = presenter.getNextChapter(chapter, manga) if (nextChapter != null) { @@ -98,12 +129,12 @@ class RecentlyReadController : NucleusController(), } override fun onRemoveClick(position: Int) { - val (manga, _, history) = adapter?.getItem(position)?.mch ?: return + val (manga, _, history) = (adapter?.getItem(position) as? RecentlyReadItem)?.mch ?: return RemoveHistoryDialog(this, manga, history).showDialog(router) } override fun onCoverClick(position: Int, lastTouchY: Float) { - val manga = adapter?.getItem(position)?.mch?.manga ?: return + val manga = (adapter?.getItem(position) as? RecentlyReadItem)?.mch?.manga ?: return router.pushController(MangaController(manga, lastTouchY).withFadeTransaction()) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadPresenter.kt index 8a5e0ed8f6..7c0fb90c9f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadPresenter.kt @@ -7,7 +7,9 @@ import eu.kanade.tachiyomi.data.database.models.History import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import rx.Observable +import rx.Subscription import rx.android.schedulers.AndroidSchedulers +import rx.schedulers.Schedulers import uy.kohesive.injekt.injectLazy import java.util.Calendar import java.util.Comparator @@ -28,24 +30,34 @@ class RecentlyReadPresenter : BasePresenter() { override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) + //pageSubscription?.let { remove(it) } // Used to get a list of recently read manga getRecentMangaObservable() - .subscribeLatestCache(RecentlyReadController::onNextManga) + .subscribeLatestCache({ view, mangas -> + view.onNextManga(mangas) + }, RecentlyReadController::onAddPageError) + } + + fun requestNext(offset: Int) { + getRecentMangaObservable((offset)) + .subscribeLatestCache({ view, mangas -> + view.onNextManga(mangas) + }, RecentlyReadController::onAddPageError) } /** * Get recent manga observable * @return list of history */ - fun getRecentMangaObservable(): Observable> { + private fun getRecentMangaObservable(offset: Int = 0): Observable> { // Set date for recent manga val cal = Calendar.getInstance() cal.time = Date() - cal.add(Calendar.MONTH, -1) + cal.add(Calendar.YEAR, -1) - return db.getRecentManga(cal.time).asRxObservable() - .map { recents -> recents.map(::RecentlyReadItem) } - .observeOn(AndroidSchedulers.mainThread()) + return db.getRecentManga(cal.time, offset).asRxObservable() + .map { recents -> recents.map(::RecentlyReadItem) } + .observeOn(AndroidSchedulers.mainThread()) } /**