From f297b746828a94515ea15de86482359ccf728031 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 11 Jan 2020 00:56:24 -0800 Subject: [PATCH] Fixed recently read bug set it to 50 years back because I'm too tired to figured out the sql query Also reworded the restore complete text --- .../data/backup/BackupRestoreService.kt | 2 +- .../data/database/queries/HistoryQueries.kt | 18 +++++++- .../data/database/queries/RawQueries.kt | 24 ++++++++++ .../recently_read/RecentlyReadController.kt | 34 ++++++++++++-- .../ui/recently_read/RecentlyReadPresenter.kt | 46 +++++++++++++------ app/src/main/res/values/strings.xml | 2 +- 6 files changed, 106 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt index de4f14e47c..79512333c0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt @@ -434,7 +434,7 @@ class BackupRestoreService : Service() { * @param context the application context. * @return true if the service is running, false otherwise. */ - private fun isRunning(context: Context): Boolean = + fun isRunning(context: Context): Boolean = context.isServiceRunning(BackupRestoreService::class.java) /** 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 e9650347aa..853a2cd476 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 @@ -19,8 +19,9 @@ interface HistoryQueries : DbProvider { fun insertHistory(history: History) = db.put().`object`(history).prepare() /** - * Returns history of recent manga containing last read chapter + * Returns history of recent manga containing last read chapter in 25s * @param date recent date range + * @offset offset the db by */ fun getRecentManga(date: Date, offset: Int = 0) = db.get() .listOfObjects(MangaChapterHistory::class.java) @@ -32,6 +33,21 @@ interface HistoryQueries : DbProvider { .withGetResolver(MangaChapterHistoryGetResolver.INSTANCE) .prepare() + /** + * Returns history of recent manga containing last read chapter in 25s + * @param date recent date range + * @offset offset the db by + */ + fun getRecentMangaLimit(date: Date, limit: Int = 0) = db.get() + .listOfObjects(MangaChapterHistory::class.java) + .withQuery(RawQuery.builder() + .query(getRecentMangasLimitQuery(limit)) + .args(date.time) + .observesTables(HistoryTable.TABLE) + .build()) + .withGetResolver(MangaChapterHistoryGetResolver.INSTANCE) + .prepare() + fun getHistoryByMangaId(mangaId: Long) = db.get() .listOfObjects(History::class.java) .withQuery(RawQuery.builder() 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 83a9866b09..468e5026ba 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 @@ -65,6 +65,30 @@ fun getRecentMangasQuery(offset: Int = 0) = """ LIMIT 25 OFFSET $offset """ +/** + * Query to get the recently read chapters of manga from the library up to a date. + * The max_last_read table contains the most recent chapters grouped by manga + * The select statement returns all information of chapters that have the same id as the chapter in max_last_read + * and are read after the given time period + */ +fun getRecentMangasLimitQuery(limit: Int = 25) = """ + SELECT ${Manga.TABLE}.${Manga.COL_URL} as mangaUrl, ${Manga.TABLE}.*, ${Chapter.TABLE}.*, ${History.TABLE}.* + FROM ${Manga.TABLE} + JOIN ${Chapter.TABLE} + ON ${Manga.TABLE}.${Manga.COL_ID} = ${Chapter.TABLE}.${Chapter.COL_MANGA_ID} + JOIN ${History.TABLE} + ON ${Chapter.TABLE}.${Chapter.COL_ID} = ${History.TABLE}.${History.COL_CHAPTER_ID} + JOIN ( + SELECT ${Chapter.TABLE}.${Chapter.COL_MANGA_ID},${Chapter.TABLE}.${Chapter.COL_ID} as ${History.COL_CHAPTER_ID}, MAX(${History.TABLE}.${History.COL_LAST_READ}) as ${History.COL_LAST_READ} + FROM ${Chapter.TABLE} JOIN ${History.TABLE} + ON ${Chapter.TABLE}.${Chapter.COL_ID} = ${History.TABLE}.${History.COL_CHAPTER_ID} + GROUP BY ${Chapter.TABLE}.${Chapter.COL_MANGA_ID}) AS max_last_read + 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 $limit +""" + fun getHistoryByMangaId() = """ SELECT ${History.TABLE}.* FROM ${History.TABLE} 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 b72f9faf12..4205979990 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 @@ -6,6 +6,7 @@ import android.view.View import android.view.ViewGroup import eu.davidea.flexibleadapter.FlexibleAdapter import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.backup.BackupRestoreService import eu.kanade.tachiyomi.data.database.models.History import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.ui.base.controller.NucleusController @@ -79,10 +80,11 @@ class RecentlyReadController : NucleusController(), * * @param mangaHistory list of manga history */ - fun onNextManga(mangaHistory: List) { - if (adapter?.itemCount ?: 0 == 0) + fun onNextManga(mangaHistory: List, cleanBatch: Boolean = false) { + if (adapter?.itemCount ?: 0 == 0 || cleanBatch) resetProgressItem() - adapter?.onLoadMoreComplete(mangaHistory) + if (cleanBatch) adapter?.updateDataSet(mangaHistory) + else adapter?.onLoadMoreComplete(mangaHistory) } fun onAddPageError(error: Throwable) { @@ -108,13 +110,17 @@ class RecentlyReadController : NucleusController(), } override fun onLoadMore(lastPosition: Int, currentPage: Int) { + val view = view ?: return + if (BackupRestoreService.isRunning(view.context.applicationContext)) { + onAddPageError(Throwable()) + return + } 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) as? RecentlyReadItem)?.mch ?: return @@ -142,9 +148,29 @@ class RecentlyReadController : NucleusController(), if (all) { // Reset last read of chapter to 0L presenter.removeAllFromHistory(manga.id!!) + /*val safeAdapter = adapter ?: return + val items = (0 until safeAdapter.itemCount).filter { + val item = safeAdapter.getItem(it) + if (item is RecentlyReadItem) + item.mch.manga.id == manga.id + + else + false + } + adapter?.removeItems(items)*/ } else { // Remove all chapters belonging to manga from library presenter.removeFromHistory(history) + /*val safeAdapter = adapter ?: return + val item = (0 until safeAdapter.itemCount).find { + val item = safeAdapter.getItem(it) + if (item is RecentlyReadItem) + item.mch.history == history + + else + false + } ?: return + adapter?.removeItem(item)*/ } } 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 7c0fb90c9f..e18149268c 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 @@ -26,19 +26,18 @@ class RecentlyReadPresenter : BasePresenter() { * Used to connect to database */ val db: DatabaseHelper by injectLazy() + var lastCount = 25 override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) //pageSubscription?.let { remove(it) } // Used to get a list of recently read manga - getRecentMangaObservable() - .subscribeLatestCache({ view, mangas -> - view.onNextManga(mangas) - }, RecentlyReadController::onAddPageError) + updateList() } fun requestNext(offset: Int) { + lastCount = offset getRecentMangaObservable((offset)) .subscribeLatestCache({ view, mangas -> view.onNextManga(mangas) @@ -53,21 +52,44 @@ class RecentlyReadPresenter : BasePresenter() { // Set date for recent manga val cal = Calendar.getInstance() cal.time = Date() - cal.add(Calendar.YEAR, -1) + cal.add(Calendar.YEAR, -50) return db.getRecentManga(cal.time, offset).asRxObservable() .map { recents -> recents.map(::RecentlyReadItem) } .observeOn(AndroidSchedulers.mainThread()) } + /** + * Get recent manga observable + * @return list of history + */ + private fun getRecentMangaLimitObservable(offset: Int = 0): Observable> { + // Set date for recent manga + val cal = Calendar.getInstance() + cal.time = Date() + cal.add(Calendar.YEAR, -50) + + return db.getRecentMangaLimit(cal.time, lastCount).asRxObservable() + .map { recents -> recents.map(::RecentlyReadItem) } + .observeOn(AndroidSchedulers.mainThread()) + } + /** * Reset last read of chapter to 0L * @param history history belonging to chapter */ fun removeFromHistory(history: History) { history.last_read = 0L - db.updateHistoryLastRead(history).asRxObservable() - .subscribe() + db.updateHistoryLastRead(history).executeAsBlocking() + updateList() + } + + + fun updateList() { + getRecentMangaLimitObservable(lastCount).take(1) + .subscribeLatestCache({ view, mangas -> + view.onNextManga(mangas, true) + }, RecentlyReadController::onAddPageError) } /** @@ -75,12 +97,10 @@ class RecentlyReadPresenter : BasePresenter() { * @param mangaId id of manga */ fun removeAllFromHistory(mangaId: Long) { - db.getHistoryByMangaId(mangaId).asRxSingle() - .map { list -> - list.forEach { it.last_read = 0L } - db.updateHistoryLastRead(list).executeAsBlocking() - } - .subscribe() + val history = db.getHistoryByMangaId(mangaId).executeAsBlocking() + history.forEach { it.last_read = 0L } + db.updateHistoryLastRead(history).executeAsBlocking() + updateList() } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 325023aad1..0bea4694fe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -308,7 +308,7 @@ Restore completed Restore error Could not open log - Restored %1$s. %2$s errors found + %1$s Restored. %2$s errors found %1$d skipped Restore uses the network to fetch data, carrier costs may apply.\n\nMake sure you have installed all necessary extensions and are logged in to sources and tracking services before restoring. File saved at %1$s