fix: Delete duplicate chapters

This commit is contained in:
Ahmad Ansori Palembani 2024-12-14 14:29:10 +07:00
parent 8c5b54df5f
commit ee2acf8b98
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -107,12 +107,17 @@ suspend fun syncChaptersWithSource(
it.chapter_number = ChapterRecognition.parseChapterNumber(it.name, manga.title, it.chapter_number) it.chapter_number = ChapterRecognition.parseChapterNumber(it.name, manga.title, it.chapter_number)
} }
// Chapters from the db not in the source. val duplicates = dbChapters.groupBy { it.url }
val toDelete = dbChapters.filterNot { dbChapter -> .filter { it.value.size > 1 }
.flatMap { (_, chapters) ->
chapters.drop(1)
}
val notInSource = dbChapters.filterNot { dbChapter ->
sourceChapters.any { sourceChapter -> sourceChapters.any { sourceChapter ->
dbChapter.url == sourceChapter.url dbChapter.url == sourceChapter.url
} }
} }
val toDelete = duplicates + notInSource
// Return if there's nothing to add, delete or change, avoid unnecessary db transactions. // Return if there's nothing to add, delete or change, avoid unnecessary db transactions.
if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) { if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) {