Skip to content

Commit

Permalink
Apply code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shabnix committed Aug 20, 2024
1 parent 3a543c1 commit 7c5caa1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/eu/kanade/domain/DomainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class DomainModule : InjektModule {
addFactory { UpdateChapter(get()) }
addFactory { SetReadStatus(get(), get(), get(), get()) }
addFactory { ShouldUpdateDbChapter() }
addFactory { SyncChaptersWithSource(get(), get(), get(), get(), get(), get(), get(), get()) }
addFactory { SyncChaptersWithSource(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
addFactory { GetAvailableScanlators(get()) }
addFactory { GetReadChapterCountByMangaIdAndChapterNumber(get()) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.download.DownloadProvider
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.online.HttpSource
import mihon.domain.chapter.interactor.GetReadChapterCountByMangaIdAndChapterNumber
import tachiyomi.data.chapter.ChapterSanitizer
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
import tachiyomi.domain.chapter.interactor.ShouldUpdateDbChapter
Expand All @@ -19,6 +20,7 @@ import tachiyomi.domain.chapter.model.NoChaptersException
import tachiyomi.domain.chapter.model.toChapterUpdate
import tachiyomi.domain.chapter.repository.ChapterRepository
import tachiyomi.domain.chapter.service.ChapterRecognition
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga
import tachiyomi.source.local.isLocal
import java.lang.Long.max
Expand All @@ -34,6 +36,8 @@ class SyncChaptersWithSource(
private val updateChapter: UpdateChapter,
private val getChaptersByMangaId: GetChaptersByMangaId,
private val getExcludedScanlators: GetExcludedScanlators,
private val downloadPreferences: DownloadPreferences,
private val getReadChapterCount: GetReadChapterCountByMangaIdAndChapterNumber,
) {

/**
Expand All @@ -42,15 +46,20 @@ class SyncChaptersWithSource(
* @param rawSourceChapters the chapters from the source.
* @param manga the manga the chapters belong to.
* @param source the source the manga belongs to.
* @return Newly added chapters
* @param manualFetch A flag indicating if this fetch was manually triggered. Defaults to `false`.
* @param fetchWindow A time window to determine if a fetch should be performed. Defaults to `(0, 0)`.
* @return A pair of lists:
* - First: List of chapters that were added or updated after filtering out re-added or excluded scanlator chapters.
* - Second: List of chapters that are eligible for download, based on the unread chapters-only preference.
* @throws NoChaptersException If the source is not local and there are no chapters in `rawSourceChapters`.
*/
suspend fun await(
rawSourceChapters: List<SChapter>,
manga: Manga,
source: Source,
manualFetch: Boolean = false,
fetchWindow: Pair<Long, Long> = Pair(0, 0),
): List<Chapter> {
): Pair<List<Chapter>, List<Chapter>> {
if (rawSourceChapters.isEmpty() && !source.isLocal()) {
throw NoChaptersException()
}
Expand Down Expand Up @@ -142,7 +151,7 @@ class SyncChaptersWithSource(
fetchWindow,
)
}
return emptyList()
return Pair(emptyList(), emptyList())
}

val reAdded = mutableListOf<Chapter>()
Expand Down Expand Up @@ -206,8 +215,16 @@ class SyncChaptersWithSource(

val excludedScanlators = getExcludedScanlators.await(manga.id).toHashSet()

return updatedToAdd.filterNot {
val nonExcludedChapters = updatedToAdd.filterNot {
it.url in reAddedUrls || it.scanlator in excludedScanlators
}

val chaptersToDownload = if (downloadPreferences.downloadUnreadChaptersOnly().get()) {
nonExcludedChapters.filter { getReadChapterCount.await(manga.id, it.chapterNumber) == 0L }
} else {
nonExcludedChapters
}

return Pair(nonExcludedChapters, chaptersToDownload)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ 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
import tachiyomi.core.common.util.lang.withIOContext
Expand Down Expand Up @@ -90,7 +88,6 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
private val getCategories: GetCategories = Injekt.get()
private val syncChaptersWithSource: SyncChaptersWithSource = Injekt.get()
private val fetchInterval: FetchInterval = Injekt.get()
private val getReadChapterCount: GetReadChapterCountByMangaIdAndChapterNumber = Injekt.get()

private val notifier = LibraryUpdateNotifier(context)

Expand Down Expand Up @@ -269,11 +266,16 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
manga,
) {
try {
val newChapters = updateManga(manga, fetchWindow)
.sortedByDescending { it.sourceOrder }
val (newChapters, chaptersToDownload) = updateManga(manga, fetchWindow)

if (newChapters.isNotEmpty()) {
handleNewChapters(manga, newChapters, newUpdates, hasDownloads)
handleNewChapters(
manga,
newChapters,
chaptersToDownload,
newUpdates,
hasDownloads
)
}
} catch (e: Throwable) {
val errorMessage = when (e) {
Expand Down Expand Up @@ -315,35 +317,33 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
}

/**
* This function handles the new chapters for a given manga, checks the download preferences,
* and initiates the download based on the user preferences. It also adds the new chapters to the new updates list
* and updates the hasDownloads flag.
* Processes new chapters for a given manga and handles related updates, such as downloading
* new chapters and updating the notification states.
*
* @param manga The manga for which new chapters are being handled.
* @param newChapters A list of new chapters for the manga.
* @param chaptersToDownload A list of chapters to be downloaded based on user preferences.
* @param newUpdates A thread-safe list that stores pairs of manga and their respective new chapters.
* @param hasDownloads A flag indicating whether there are any chapters to download.
*/
private suspend fun handleNewChapters(
manga: Manga,
newChapters: List<Chapter>,
chaptersToDownload: List<Chapter>,
newUpdates: CopyOnWriteArrayList<Pair<Manga, Array<Chapter>>>,
hasDownloads: AtomicBoolean
) {
val categoryIds = getCategories.await(manga.id).map { it.id }

if (manga.shouldDownloadNewChapters(categoryIds, downloadPreferences)) {
val chaptersToDownload = newChapters
.filterChaptersToDownload(manga, getReadChapterCount, downloadPreferences)

if (chaptersToDownload.isNotEmpty()) {
downloadChapters(manga, chaptersToDownload)
hasDownloads.set(true)
}
if (manga.shouldDownloadNewChapters(categoryIds, downloadPreferences) && chaptersToDownload.isNotEmpty()) {
downloadChapters(manga, chaptersToDownload)
hasDownloads.set(true)
}

libraryPreferences.newUpdatesCount().getAndSet { it + newChapters.size }
newUpdates.add(manga to newChapters.toTypedArray())

// Add sorted chapters to the updates list
val sortedChapters = newChapters.sortedByDescending { it.sourceOrder }
newUpdates.add(manga to sortedChapters.toTypedArray())
}

private fun downloadChapters(manga: Manga, chapters: List<Chapter>) {
Expand All @@ -355,10 +355,13 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
/**
* Updates the chapters for the given manga and adds them to the database.
*
* @param manga the manga to update.
* @return a pair of the inserted and removed chapters.
* @param manga The manga to be updated.
* @param fetchWindow A time window defining the interval for fetching chapters, represented as a pair of timestamps.
* @return A pair of lists:
* - First: List of chapters that were added or updated after filtering out re-added or excluded scanlator chapters.
* - Second: List of chapters that are eligible for download, based on the unread chapters-only preference.
*/
private suspend fun updateManga(manga: Manga, fetchWindow: Pair<Long, Long>): List<Chapter> {
private suspend fun updateManga(manga: Manga, fetchWindow: Pair<Long, Long>): Pair<List<Chapter>, List<Chapter>> {
val source = sourceManager.getOrStub(manga.source)

// Update manga metadata if needed
Expand All @@ -371,7 +374,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet

// Get manga from database to account for if it was removed during the update and
// to get latest data so it doesn't get overwritten later on
val dbManga = getManga.await(manga.id)?.takeIf { it.favorite } ?: return emptyList()
val dbManga = getManga.await(manga.id)?.takeIf { it.favorite } ?: return Pair(emptyList(), emptyList())

return syncChaptersWithSource.await(chapters, dbManga, source, false, fetchWindow)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DeepLinkScreenModel(

return if (localChapter == null) {
val sourceChapters = source.getChapterList(manga.toSManga())
val newChapters = syncChaptersWithSource.await(sourceChapters, manga, source, false)
val (newChapters, _) = syncChaptersWithSource.await(sourceChapters, manga, source, false)
newChapters.find { it.url == sChapter.url }
} else {
localChapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ 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 @@ -111,7 +109,6 @@ 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 @@ -538,15 +535,15 @@ class MangaScreenModel(
withIOContext {
val chapters = state.source.getChapterList(state.manga.toSManga())

val newChapters = syncChaptersWithSource.await(
val (_, chaptersToDownload) = syncChaptersWithSource.await(
chapters,
state.manga,
state.source,
manualFetch,
)

if (manualFetch) {
downloadNewChapters(newChapters)
downloadNewChapters(chaptersToDownload)
}
}
} catch (e: Throwable) {
Expand Down Expand Up @@ -782,8 +779,7 @@ class MangaScreenModel(
return@launchNonCancellable
}

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

Expand Down

This file was deleted.

0 comments on commit 7c5caa1

Please sign in to comment.