Skip to content

Commit

Permalink
refactor: Simplify code and move stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Dec 14, 2024
1 parent ee2acf8 commit 62e26d1
Showing 1 changed file with 36 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,60 +65,51 @@ suspend fun syncChaptersWithSource(
// Chapters whose metadata have changed.
val toChange = mutableListOf<ChapterUpdate>()

val duplicates = dbChapters.groupBy { it.url }
.filter { it.value.size > 1 }
.flatMap { (_, chapters) ->
chapters.drop(1)
}
val notInSource = dbChapters.filterNot { dbChapter ->
sourceChapters.any { sourceChapter ->
dbChapter.url == sourceChapter.url
}
}
val toDelete = duplicates + notInSource

for (sourceChapter in sourceChapters) {
val dbChapter = dbChapters.find { it.url == sourceChapter.url }
val chapter = sourceChapter

if (source is HttpSource) {
source.prepareNewChapter(chapter, manga)
}
chapter.chapter_number = ChapterRecognition.parseChapterNumber(chapter.name, manga.title, chapter.chapter_number)

val dbChapter = dbChapters.find { it.url == chapter.url }

// Add the chapter if not in db already, or update if the metadata changed.
if (dbChapter == null) {
toAdd.add(sourceChapter)
toAdd.add(chapter)
} else {
// this forces metadata update for the main viewable things in the chapter list
if (source is HttpSource) {
source.prepareNewChapter(sourceChapter, manga)
}

sourceChapter.chapter_number =
ChapterRecognition.parseChapterNumber(sourceChapter.name, manga.title, sourceChapter.chapter_number)

if (shouldUpdateDbChapter(dbChapter, sourceChapter)) {
if ((dbChapter.name != sourceChapter.name || dbChapter.scanlator != sourceChapter.scanlator) &&
if (shouldUpdateDbChapter(dbChapter, chapter)) {
if ((dbChapter.name != chapter.name || dbChapter.scanlator != chapter.scanlator) &&
downloadManager.isChapterDownloaded(dbChapter, manga)
) {
downloadManager.renameChapter(source, manga, dbChapter, sourceChapter)
downloadManager.renameChapter(source, manga, dbChapter, chapter)
}
val update = ChapterUpdate(
dbChapter.id!!,
scanlator = sourceChapter.scanlator,
name = sourceChapter.name,
dateUpload = sourceChapter.date_upload,
chapterNumber = sourceChapter.chapter_number.toDouble(),
sourceOrder = sourceChapter.source_order.toLong(),
scanlator = chapter.scanlator,
name = chapter.name,
dateUpload = chapter.date_upload,
chapterNumber = chapter.chapter_number.toDouble(),
sourceOrder = chapter.source_order.toLong(),
)
toChange.add(update)
}
}
}

// Recognize number for new chapters.
toAdd.forEach {
if (source is HttpSource) {
source.prepareNewChapter(it, manga)
}
it.chapter_number = ChapterRecognition.parseChapterNumber(it.name, manga.title, it.chapter_number)
}

val duplicates = dbChapters.groupBy { it.url }
.filter { it.value.size > 1 }
.flatMap { (_, chapters) ->
chapters.drop(1)
}
val notInSource = dbChapters.filterNot { dbChapter ->
sourceChapters.any { sourceChapter ->
dbChapter.url == sourceChapter.url
}
}
val toDelete = duplicates + notInSource

// Return if there's nothing to add, delete or change, avoid unnecessary db transactions.
if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) {
val newestDate = dbChapters.maxOfOrNull { it.date_upload } ?: 0L
Expand Down Expand Up @@ -198,21 +189,17 @@ suspend fun syncChaptersWithSource(
manga.last_update = Date().time
updateManga.await(MangaUpdate(manga.id!!, lastUpdate = manga.last_update))

val reAddedSet = reAdded.toSet()
val reAddedUrls = reAdded.map { it.url }.toHashSet()
val filteredScanlators = ChapterUtil.getScanlators(manga.filtered_scanlators).toHashSet()

return Pair(
updatedToAdd.subtract(reAddedSet).toList().filterChaptersByScanlators(manga),
toDelete - reAddedSet,
updatedToAdd.filterNot {
it.url in reAddedUrls || it.scanlator in filteredScanlators
},
toDelete.filterNot { it.url in reAddedUrls },
)
}

private fun List<Chapter>.filterChaptersByScanlators(manga: Manga): List<Chapter> {
if (manga.filtered_scanlators.isNullOrBlank()) return this

return this.filter { chapter ->
!ChapterUtil.getScanlators(manga.filtered_scanlators).contains(chapter.scanlator)
}
}

// checks if the chapter in db needs updated
private fun shouldUpdateDbChapter(dbChapter: Chapter, sourceChapter: Chapter): Boolean {
return dbChapter.scanlator != sourceChapter.scanlator ||
Expand Down

0 comments on commit 62e26d1

Please sign in to comment.