Delete duplicate history on merge
This commit is contained in:
parent
2026f34956
commit
bc1274008d
@ -103,5 +103,15 @@ interface HistoryQueries : DbProvider {
|
|||||||
.objects(history)
|
.objects(history)
|
||||||
.withPutResolver(HistoryChapterIdPutResolver())
|
.withPutResolver(HistoryChapterIdPutResolver())
|
||||||
.prepare()
|
.prepare()
|
||||||
|
|
||||||
|
fun deleteHistoryIds(ids: List<Long>) = db.delete()
|
||||||
|
.byQuery(
|
||||||
|
DeleteQuery.builder()
|
||||||
|
.table(HistoryTable.TABLE)
|
||||||
|
.where("${HistoryTable.COL_ID} IN (?)")
|
||||||
|
.whereArgs(ids.joinToString())
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
.prepare()
|
||||||
// SY <--
|
// SY <--
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ class EHentaiUpdateHelper(context: Context) {
|
|||||||
if (toDiscard.isNotEmpty()) {
|
if (toDiscard.isNotEmpty()) {
|
||||||
// Copy chain chapters to curChapters
|
// Copy chain chapters to curChapters
|
||||||
val (newChapters, new) = getChapterList(accepted, toDiscard, chainsAsChapters)
|
val (newChapters, new) = getChapterList(accepted, toDiscard, chainsAsChapters)
|
||||||
val (history, urlHistory) = getHistory(newChapters, chainsAsChapters, chainsAsHistory)
|
val (history, urlHistory, deleteHistory) = getHistory(newChapters, chainsAsChapters, chainsAsHistory)
|
||||||
|
|
||||||
toDiscard.forEach {
|
toDiscard.forEach {
|
||||||
it.manga.favorite = false
|
it.manga.favorite = false
|
||||||
@ -98,6 +98,10 @@ class EHentaiUpdateHelper(context: Context) {
|
|||||||
// Insert new chapters for accepted manga
|
// Insert new chapters for accepted manga
|
||||||
val chapterPutResults = db.insertChapters(newAccepted.chapters).executeAsBlocking().results()
|
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
|
// Get a updated history list
|
||||||
val newHistory = urlHistory.mapNotNull { (url, history) ->
|
val newHistory = urlHistory.mapNotNull { (url, history) ->
|
||||||
val result = chapterPutResults.firstNotNullOfOrNull { (chapter, result) ->
|
val result = chapterPutResults.firstNotNullOfOrNull { (chapter, result) ->
|
||||||
@ -136,28 +140,53 @@ class EHentaiUpdateHelper(context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class HistoryUpdates(
|
||||||
|
val history: List<History>,
|
||||||
|
val urlHistory: List<Pair<String, History>>,
|
||||||
|
val historyToDelete: List<Long>
|
||||||
|
)
|
||||||
|
|
||||||
private fun getHistory(
|
private fun getHistory(
|
||||||
newChapters: List<Chapter>,
|
newChapters: List<Chapter>,
|
||||||
chainsAsChapters: List<Chapter>,
|
chainsAsChapters: List<Chapter>,
|
||||||
chainsAsHistory: List<History>
|
chainsAsHistory: List<History>
|
||||||
): Pair<List<History>, List<Pair<String, History>>> {
|
): HistoryUpdates {
|
||||||
return chainsAsHistory.filter { history ->
|
val historyMap = chainsAsHistory
|
||||||
val oldChapter = chainsAsChapters.find { it.id == history.chapter_id }
|
.groupBy { history ->
|
||||||
val newChapter = newChapters.find { it.url == oldChapter?.url }
|
chainsAsChapters.find { it.id == history.chapter_id }?.url.orEmpty()
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
.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(
|
private fun getChapterList(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user