From 33e90d64497f920be825b803e1342a2e6c937111 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 30 Oct 2022 22:56:07 -0400 Subject: [PATCH] Clean up library download chapters logic We can probably clean up the same logic in the manga controller at some point too, but that stuff's messy. Also fixes the spacing issue that the new icon introduced. --- .../java/eu/kanade/domain/DomainModule.kt | 2 +- .../interactor/GetNextUnreadChapters.kt | 18 ++++---- .../components/MangaBottomActionMenu.kt | 20 ++++----- .../manga/components/MangaToolbar.kt | 1 - .../tachiyomi/ui/library/LibraryPresenter.kt | 41 +++++++------------ 5 files changed, 34 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/DomainModule.kt b/app/src/main/java/eu/kanade/domain/DomainModule.kt index c8c7d4dfd..0240a4027 100644 --- a/app/src/main/java/eu/kanade/domain/DomainModule.kt +++ b/app/src/main/java/eu/kanade/domain/DomainModule.kt @@ -94,7 +94,7 @@ class DomainModule : InjektModule { addFactory { GetLibraryManga(get()) } addFactory { GetMangaWithChapters(get(), get()) } addFactory { GetManga(get()) } - addFactory { GetNextUnreadChapters(get(), get(), get(), get()) } + addFactory { GetNextUnreadChapters(get(), get(), get()) } addFactory { ResetViewerFlags(get()) } addFactory { SetMangaChapterFlags(get()) } addFactory { SetMangaDefaultChapterFlags(get(), get(), get()) } diff --git a/app/src/main/java/eu/kanade/domain/history/interactor/GetNextUnreadChapters.kt b/app/src/main/java/eu/kanade/domain/history/interactor/GetNextUnreadChapters.kt index e03114fe2..8c61eaca7 100644 --- a/app/src/main/java/eu/kanade/domain/history/interactor/GetNextUnreadChapters.kt +++ b/app/src/main/java/eu/kanade/domain/history/interactor/GetNextUnreadChapters.kt @@ -1,14 +1,13 @@ package eu.kanade.domain.history.interactor -import eu.kanade.domain.chapter.interactor.GetChapter import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.history.repository.HistoryRepository import eu.kanade.domain.manga.interactor.GetManga import eu.kanade.tachiyomi.util.chapter.getChapterSort +import kotlin.math.max class GetNextUnreadChapters( - private val getChapter: GetChapter, private val getChapterByMangaId: GetChapterByMangaId, private val getManga: GetManga, private val historyRepository: HistoryRepository, @@ -19,15 +18,16 @@ class GetNextUnreadChapters( return await(history.mangaId, history.chapterId).firstOrNull() } - suspend fun await(mangaId: Long, chapterId: Long): List { - val chapter = getChapter.await(chapterId) ?: return emptyList() + suspend fun await(mangaId: Long): List { val manga = getManga.await(mangaId) ?: return emptyList() - - val chapters = getChapterByMangaId.await(mangaId) + return getChapterByMangaId.await(mangaId) .sortedWith(getChapterSort(manga, sortDescending = false)) - val currChapterIndex = chapters.indexOfFirst { chapter.id == it.id } - return chapters - .subList(currChapterIndex, chapters.size) .filterNot { it.read } } + + suspend fun await(mangaId: Long, fromChapterId: Long): List { + val unreadChapters = await(mangaId) + val currChapterIndex = unreadChapters.indexOfFirst { it.id == fromChapterId } + return unreadChapters.subList(max(0, currChapterIndex), unreadChapters.size) + } } diff --git a/app/src/main/java/eu/kanade/presentation/components/MangaBottomActionMenu.kt b/app/src/main/java/eu/kanade/presentation/components/MangaBottomActionMenu.kt index 22def9446..ac9f34fb1 100644 --- a/app/src/main/java/eu/kanade/presentation/components/MangaBottomActionMenu.kt +++ b/app/src/main/java/eu/kanade/presentation/components/MangaBottomActionMenu.kt @@ -9,7 +9,6 @@ import androidx.compose.animation.shrinkVertically import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.RowScope @@ -174,6 +173,7 @@ private fun RowScope.Button( toConfirm: Boolean, onLongClick: () -> Unit, onClick: () -> Unit, + content: (@Composable () -> Unit)? = null, ) { val animatedWeight by animateFloatAsState(if (toConfirm) 2f else 1f) Column( @@ -205,6 +205,7 @@ private fun RowScope.Button( style = MaterialTheme.typography.labelSmall, ) } + content?.invoke() } } @@ -274,15 +275,14 @@ fun LibraryBottomActionMenu( ) } if (onDownloadClicked != null) { - Box { - var downloadExpanded by remember { mutableStateOf(false) } - this@Row.Button( - title = stringResource(R.string.action_download), - icon = Icons.Outlined.Download, - toConfirm = confirm[3], - onLongClick = { onLongClickItem(3) }, - onClick = { downloadExpanded = !downloadExpanded }, - ) + var downloadExpanded by remember { mutableStateOf(false) } + Button( + title = stringResource(R.string.action_download), + icon = Icons.Outlined.Download, + toConfirm = confirm[3], + onLongClick = { onLongClickItem(3) }, + onClick = { downloadExpanded = !downloadExpanded }, + ) { val onDismissRequest = { downloadExpanded = false } DownloadDropdownMenu( expanded = downloadExpanded, diff --git a/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt b/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt index 138136eb7..52be85e7e 100644 --- a/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt +++ b/app/src/main/java/eu/kanade/presentation/manga/components/MangaToolbar.kt @@ -28,7 +28,6 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import eu.kanade.presentation.components.AppStateBanners import eu.kanade.presentation.components.DownloadDropdownMenu -import eu.kanade.presentation.components.DropdownMenu import eu.kanade.presentation.components.OverflowMenu import eu.kanade.presentation.manga.DownloadAction import eu.kanade.presentation.theme.active diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index d712736d0..c48b4d838 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -17,10 +17,9 @@ import eu.kanade.domain.base.BasePreferences import eu.kanade.domain.category.interactor.GetCategories import eu.kanade.domain.category.interactor.SetMangaCategories import eu.kanade.domain.category.model.Category -import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.interactor.SetReadStatus -import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.chapter.model.toDbChapter +import eu.kanade.domain.history.interactor.GetNextUnreadChapters import eu.kanade.domain.library.model.LibraryManga import eu.kanade.domain.library.model.LibrarySort import eu.kanade.domain.library.model.sort @@ -40,13 +39,11 @@ import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.models.toDomainManga import eu.kanade.tachiyomi.data.download.DownloadCache import eu.kanade.tachiyomi.data.download.DownloadManager -import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter -import eu.kanade.tachiyomi.util.chapter.getChapterSort import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchNonCancellable import eu.kanade.tachiyomi.util.lang.withIOContext @@ -81,7 +78,7 @@ class LibraryPresenter( private val getLibraryManga: GetLibraryManga = Injekt.get(), private val getTracksPerManga: GetTracksPerManga = Injekt.get(), private val getCategories: GetCategories = Injekt.get(), - private val getChapterByMangaId: GetChapterByMangaId = Injekt.get(), + private val getNextUnreadChapters: GetNextUnreadChapters = Injekt.get(), private val setReadStatus: SetReadStatus = Injekt.get(), private val updateManga: UpdateManga = Injekt.get(), private val setMangaCategories: SetMangaCategories = Injekt.get(), @@ -404,25 +401,6 @@ class LibraryPresenter( return mangaCategories.flatten().distinct().subtract(common) } - fun shouldDownloadChapter(manga: Manga, chapter: Chapter): Boolean { - val activeDownload = downloadManager.queue.find { chapter.id == it.chapter.id } - val downloaded = downloadManager.isChapterDownloaded(chapter.name, chapter.scanlator, manga.title, manga.source) - val state = when { - activeDownload != null -> activeDownload.status - downloaded -> Download.State.DOWNLOADED - else -> Download.State.NOT_DOWNLOADED - } - return state == Download.State.NOT_DOWNLOADED - } - - suspend fun getNotDownloadedUnreadChapters(manga: Manga): List { - return getChapterByMangaId.await(manga.id) - .filter { chapter -> - !chapter.read && shouldDownloadChapter(manga, chapter) - } - .sortedWith(getChapterSort(manga, sortDescending = false)) - } - /** * Queues the amount specified of unread chapters from the list of mangas given. * @@ -432,10 +410,19 @@ class LibraryPresenter( fun downloadUnreadChapters(mangas: List, amount: Int?) { presenterScope.launchNonCancellable { mangas.forEach { manga -> - val chapters = getNotDownloadedUnreadChapters(manga) + val chapters = getNextUnreadChapters.await(manga.id) + .filterNot { chapter -> + downloadManager.queue.any { chapter.id == it.chapter.id } || + downloadManager.isChapterDownloaded( + chapter.name, + chapter.scanlator, + manga.title, + manga.source, + ) + } .let { if (amount != null) it.take(amount) else it } - .map { it.toDbChapter() } - downloadManager.downloadChapters(manga, chapters) + + downloadManager.downloadChapters(manga, chapters.map { it.toDbChapter() }) } } }