Skip to content

Commit

Permalink
Resolve issue where read chapters were still being downloaded during …
Browse files Browse the repository at this point in the history
…manual manga fetch
  • Loading branch information
shabnix committed Aug 19, 2024
1 parent 271b336 commit 3a543c1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import logcat.LogPriority
import mihon.core.chapters.utils.filterChaptersToDownload
import mihon.domain.chapter.interactor.GetReadChapterCountByMangaIdAndChapterNumber
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.core.common.preference.getAndSet
Expand Down Expand Up @@ -330,14 +331,10 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
hasDownloads: AtomicBoolean
) {
val categoryIds = getCategories.await(manga.id).map { it.id }
val onlyDownloadUnreadChapters = downloadPreferences.downloadUnreadChaptersOnly().get()

if (manga.shouldDownloadNewChapters(categoryIds, downloadPreferences)) {
val chaptersToDownload = if (onlyDownloadUnreadChapters) {
newChapters.filter { getReadChapterCount.await(manga.id, it.chapterNumber) == 0L }
} else {
newChapters
}
val chaptersToDownload = newChapters
.filterChaptersToDownload(manga, getReadChapterCount, downloadPreferences)

if (chaptersToDownload.isNotEmpty()) {
downloadChapters(manga, chaptersToDownload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import logcat.LogPriority
import mihon.core.chapters.utils.filterChaptersToDownload
import mihon.domain.chapter.interactor.GetReadChapterCountByMangaIdAndChapterNumber
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.core.common.preference.CheckboxState
import tachiyomi.core.common.preference.TriState
Expand Down Expand Up @@ -109,6 +111,7 @@ class MangaScreenModel(
private val addTracks: AddTracks = Injekt.get(),
private val setMangaCategories: SetMangaCategories = Injekt.get(),
private val mangaRepository: MangaRepository = Injekt.get(),
private val getReadChapterCount: GetReadChapterCountByMangaIdAndChapterNumber = Injekt.get(),
val snackbarHostState: SnackbarHostState = SnackbarHostState(),
) : StateScreenModel<MangaScreenModel.State>(State.Loading) {

Expand Down Expand Up @@ -779,7 +782,8 @@ class MangaScreenModel(
return@launchNonCancellable
}

downloadChapters(chapters)
val chaptersToDownload = chapters.filterChaptersToDownload(manga, getReadChapterCount, downloadPreferences)
downloadChapters(chaptersToDownload)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mihon.core.chapters.utils

import mihon.domain.chapter.interactor.GetReadChapterCountByMangaIdAndChapterNumber
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga

/**
* Filters the list of chapters based on the user's download preferences.
*
* This function checks the user's preference for downloading only unread chapters. If the
* preference is enabled, it filters the chapters to include only those that haven't been read.
* Otherwise, it returns the original list of chapters.
*
* @param manga The manga to which the chapters belong.
* @param getReadChapterCount A function to get the count of times a chapter has been read.
* @param downloadPreferences User preferences related to downloading chapters.
* @return A list of chapters filtered according to the download preferences. If the user prefers to download only
* unread chapters, the list will contain only unread chapters. Otherwise, it will return the original list of chapters.
*/
suspend fun List<Chapter>.filterChaptersToDownload(
manga: Manga,
getReadChapterCount: GetReadChapterCountByMangaIdAndChapterNumber,
downloadPreferences: DownloadPreferences,
): List<Chapter> {
val onlyDownloadUnreadChapters = downloadPreferences.downloadUnreadChaptersOnly().get()

return if (onlyDownloadUnreadChapters) {
this.filter { getReadChapterCount.await(manga.id, it.chapterNumber) == 0L }
} else {
this
}
}

0 comments on commit 3a543c1

Please sign in to comment.