From bc1274008da19f5840755c58f6e2760789747cd9 Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Fri, 4 Mar 2022 12:27:18 -0500 Subject: [PATCH] Delete duplicate history on merge --- .../data/database/queries/HistoryQueries.kt | 10 +++ .../main/java/exh/eh/EHentaiUpdateHelper.kt | 63 ++++++++++++++----- 2 files changed, 56 insertions(+), 17 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 97c9ef75d..d0d7e63e0 100755 --- 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 @@ -103,5 +103,15 @@ interface HistoryQueries : DbProvider { .objects(history) .withPutResolver(HistoryChapterIdPutResolver()) .prepare() + + fun deleteHistoryIds(ids: List) = db.delete() + .byQuery( + DeleteQuery.builder() + .table(HistoryTable.TABLE) + .where("${HistoryTable.COL_ID} IN (?)") + .whereArgs(ids.joinToString()) + .build() + ) + .prepare() // SY <-- } diff --git a/app/src/main/java/exh/eh/EHentaiUpdateHelper.kt b/app/src/main/java/exh/eh/EHentaiUpdateHelper.kt index d6b27d666..833e61e7a 100644 --- a/app/src/main/java/exh/eh/EHentaiUpdateHelper.kt +++ b/app/src/main/java/exh/eh/EHentaiUpdateHelper.kt @@ -78,7 +78,7 @@ class EHentaiUpdateHelper(context: Context) { if (toDiscard.isNotEmpty()) { // Copy chain chapters to curChapters val (newChapters, new) = getChapterList(accepted, toDiscard, chainsAsChapters) - val (history, urlHistory) = getHistory(newChapters, chainsAsChapters, chainsAsHistory) + val (history, urlHistory, deleteHistory) = getHistory(newChapters, chainsAsChapters, chainsAsHistory) toDiscard.forEach { it.manga.favorite = false @@ -98,6 +98,10 @@ class EHentaiUpdateHelper(context: Context) { // Insert new chapters for accepted manga val chapterPutResults = db.insertChapters(newAccepted.chapters).executeAsBlocking().results() + // Delete the duplicate history first + if (deleteHistory.isNotEmpty()) { + db.deleteHistoryIds(deleteHistory).executeAsBlocking() + } // Get a updated history list val newHistory = urlHistory.mapNotNull { (url, history) -> val result = chapterPutResults.firstNotNullOfOrNull { (chapter, result) -> @@ -136,28 +140,53 @@ class EHentaiUpdateHelper(context: Context) { } } + data class HistoryUpdates( + val history: List, + val urlHistory: List>, + val historyToDelete: List + ) + private fun getHistory( newChapters: List, chainsAsChapters: List, chainsAsHistory: List - ): Pair, List>> { - return chainsAsHistory.filter { history -> - val oldChapter = chainsAsChapters.find { it.id == history.chapter_id } - val newChapter = newChapters.find { it.url == oldChapter?.url } - if (oldChapter != newChapter && newChapter?.id != null) { - history.chapter_id = newChapter.id!! - true - } else false - } to chainsAsHistory.mapNotNull { history -> - val oldChapter = chainsAsChapters.find { it.id == history.chapter_id } - val newChapter = newChapters.find { it.url == oldChapter?.url } - if (oldChapter != newChapter && newChapter?.id == null) { - val url = newChapter?.url ?: return@mapNotNull null - url to history - } else { - null + ): HistoryUpdates { + val historyMap = chainsAsHistory + .groupBy { history -> + chainsAsChapters.find { it.id == history.chapter_id }?.url.orEmpty() } + .filterKeys { it.isNotBlank() } + val latestHistory = historyMap.mapValues { entry -> + entry.value.maxByOrNull { + it.time_read + }!! } + val oldHistory = historyMap.flatMap { entry -> + val topEntry = entry.value.maxByOrNull { + it.time_read + }!! + entry.value - topEntry + }.mapNotNull { it.id } + return HistoryUpdates( + latestHistory.filter { (_, history) -> + val oldChapter = chainsAsChapters.find { it.id == history.chapter_id } + val newChapter = newChapters.find { it.url == oldChapter?.url } + if (oldChapter != newChapter && newChapter?.id != null) { + history.chapter_id = newChapter.id!! + true + } else false + }.mapNotNull { it.value }, + latestHistory.mapNotNull { (url, history) -> + val oldChapter = chainsAsChapters.find { it.id == history.chapter_id } + val newChapter = newChapters.find { it.url == oldChapter?.url } + if (oldChapter != newChapter && newChapter?.id == null) { + url to history + } else { + null + } + }, + oldHistory + ) } private fun getChapterList(