Skip to content

Commit

Permalink
Apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
shabnix committed Aug 21, 2024
1 parent 839ad09 commit 4076b96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,46 @@ class FilterChaptersForDownload(
*
* @param manga The manga for which chapters may be downloaded.
* @param newChapters The list of new chapters available for the manga.
* @return A list of chapters that should be downloaded. If new chapters should not be downloaded,
* returns an empty list.
* @return A list of chapters that should be downloaded
*/
suspend fun await(manga: Manga, newChapters: List<Chapter>): List<Chapter> {
if (!shouldDownloadNewChapters(manga)) {
if (!manga.shouldDownloadNewChapters()) {
return emptyList()
}

val downloadNewUnreadChaptersOnly = downloadPreferences.downloadNewUnreadChaptersOnly().get()
return if (downloadNewUnreadChaptersOnly) {
getUnreadChapters(manga, newChapters)
} else {
newChapters
}
}
if (!downloadPreferences.downloadNewUnreadChaptersOnly().get()) return newChapters

/**
* Filters out chapters that have already been read.
*
* @param manga The manga whose chapters are being checked.
* @param newChapters The list of new chapters to filter.
* @return A list of unread chapters that are present in `newChapters`.
*/
private suspend fun getUnreadChapters(manga: Manga, newChapters: List<Chapter>): List<Chapter> {
val dbChapters = getChaptersByMangaId.await(manga.id)
val unreadChapters = dbChapters
.groupBy { it.chapterNumber }
.filterValues { chapters -> chapters.none { it.read } }
val readChapterNumbers = getChaptersByMangaId.await(manga.id)
.filter { it.read && it.isRecognizedNumber }
.map { it.chapterNumber }

return newChapters.filter { it.chapterNumber in unreadChapters }
return newChapters.filterNot { it.chapterNumber in readChapterNumbers }
}

/**
* Determines whether new chapters should be downloaded for the manga based on user preferences and the
* categories to which the manga belongs.
*
* @param manga The manga to check for download eligibility.
* @return `true` if new chapters should be downloaded; otherwise `false`.
* @return `true` if chapters of the manga should be downloaded
*/
private suspend fun shouldDownloadNewChapters(manga: Manga): Boolean {
if (!manga.favorite) return false
private suspend fun Manga.shouldDownloadNewChapters(): Boolean {
if (!this.favorite) return false

// Boolean to determine if user wants to automatically download new chapters.
val downloadNewChapters = downloadPreferences.downloadNewChapters().get()
if (!downloadNewChapters) return false
if (!downloadPreferences.downloadNewChapters().get()) return false

val categories = getCategories.await(manga.id).map { it.id }.ifEmpty { listOf(DEFAULT_CATEGORY_ID) }
val categories = getCategories.await(this.id).map { it.id }.ifEmpty { listOf(DEFAULT_CATEGORY_ID) }
val includedCategories = downloadPreferences.downloadNewChapterCategories().get().map { it.toLong() }
val excludedCategories = downloadPreferences.downloadNewChapterCategoriesExclude().get().map { it.toLong() }

return when {
includedCategories.isEmpty() && excludedCategories.isEmpty() -> true // Default Download from all categories
categories.any { it in excludedCategories } -> false // In excluded category
includedCategories.isEmpty() -> true // Included category not selected
else -> categories.any { it in includedCategories } // In included category
// Default Download from all categories
includedCategories.isEmpty() && excludedCategories.isEmpty() -> true
// In excluded category
categories.any { it in excludedCategories } -> false
// Included category not selected
includedCategories.isEmpty() -> true
// In included category
else -> categories.any { it in includedCategories }
}
}

Expand Down
2 changes: 1 addition & 1 deletion i18n/src/commonMain/moko-resources/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@
<string name="fifth_to_last">Fifth to last read chapter</string>
<string name="pref_category_auto_download">Auto-download</string>
<string name="pref_download_new">Download new chapters</string>
<string name="pref_download_new_unread_chapters_only">Skip downloading read chapters</string>
<string name="pref_download_new_unread_chapters_only">Skip downloading duplicate read chapters</string>
<string name="pref_download_new_categories_details">Entries in excluded categories will not be downloaded even if they are also in included categories.</string>
<string name="download_ahead">Download ahead</string>
<string name="auto_download_while_reading">Auto download while reading</string>
Expand Down

0 comments on commit 4076b96

Please sign in to comment.