Chapters download menu download ahead only if required

Instead of checking if download ahead is required because not enough unread downloaded chapters existed, it just downloaded the next n unread undownloaded chapters
This commit is contained in:
schroda
2024-04-20 02:40:10 +02:00
parent 52e077ff19
commit b1f97fdf69
2 changed files with 23 additions and 14 deletions

View File

@@ -34,8 +34,9 @@ export const ChaptersDownloadActionMenuItems = ({
settings: { downloadAheadLimit },
} = useMetadataServerSettings();
const handleSelect = (size?: number, onlyUnread: boolean = true) => {
const handleSelect = (size?: number, onlyUnread: boolean = true, downloadAhead: boolean = false) => {
Mangas.performAction('download', mangaIds, {
downloadAhead,
onlyUnread,
size,
}).catch(defaultPromiseErrorHandler('ChapterDownloadButton::handleSelect'));
@@ -56,7 +57,7 @@ export const ChaptersDownloadActionMenuItems = ({
<MenuItem onClick={() => handleSelect(DownloadRange.NEXT_25)}>
{t('chapter.action.download.add.label.next_twentyfive')}
</MenuItem>
<MenuItem onClick={() => handleSelect(downloadAheadLimit)}>
<MenuItem onClick={() => handleSelect(downloadAheadLimit, undefined, true)}>
{t('chapter.action.download.add.label.ahead', { count: downloadAheadLimit })}
</MenuItem>
<MenuItem onClick={() => handleSelect(DownloadRange.UNREAD)}>

View File

@@ -118,6 +118,7 @@ export type MigrateMode = 'copy' | 'migrate';
type DownloadChaptersOptions = {
size?: number;
onlyUnread?: boolean;
downloadAhead?: boolean;
};
type MarkAsReadOptions = { wasManuallyMarkedAsRead: boolean };
type ChangeCategoriesOptions = { changeCategoriesPatch: UpdateMangaCategoriesPatchInput };
@@ -245,22 +246,28 @@ export class Mangas {
static async downloadChapters(
mangaIds: number[],
{ size, onlyUnread }: DownloadChaptersOptions = {},
{ size, onlyUnread, downloadAhead = false }: DownloadChaptersOptions = {},
): Promise<void> {
const chapters = await Mangas.getChapterIdsWithState(mangaIds, {
isRead: onlyUnread ? false : undefined,
isDownloaded: false,
});
const [unReadUnDownloadedChapters, unReadDownloadedChapters] = await Promise.all([
Mangas.getChapterIdsWithState(mangaIds, {
isRead: onlyUnread ? false : undefined,
isDownloaded: false,
}),
downloadAhead ? Mangas.getChapterIdsWithState(mangaIds, { isRead: false, isDownloaded: true }) : [],
]);
if (!chapters.length) {
const downloadAheadSize = Math.abs(unReadDownloadedChapters.length - (size ?? unReadDownloadedChapters.length));
const actualSize = downloadAhead ? downloadAheadSize : size;
const mangaIdToChapters = Object.groupBy(unReadUnDownloadedChapters, ({ mangaId }) => mangaId);
const chapterIdsToDownload = Object.values(mangaIdToChapters)
.map((mangaChapters) => mangaChapters!.slice(0, actualSize)) // the result of groupBy can't result in undefined values
.flat();
if (!chapterIdsToDownload.length) {
return Promise.resolve();
}
const mangaIdToChapters = Object.groupBy(chapters, ({ mangaId }) => mangaId);
const chapterIdsToDownload = Object.values(mangaIdToChapters)
.map((mangaChapters) => mangaChapters!.slice(0, size)) // the result of groupBy can't result in undefined values
.flat();
return Chapters.download(Chapters.getIds(chapterIdsToDownload));
}
@@ -412,6 +419,7 @@ export class Mangas {
wasManuallyMarkedAsRead,
changeCategoriesPatch,
mangaIdToMigrateTo,
downloadAhead,
onlyUnread,
size,
...migrateOptions
@@ -419,7 +427,7 @@ export class Mangas {
): Promise<void> {
switch (action) {
case 'download':
return Mangas.downloadChapters(mangaIds, { onlyUnread, size });
return Mangas.downloadChapters(mangaIds, { downloadAhead, onlyUnread, size });
case 'delete':
return Mangas.deleteChapters(mangaIds);
case 'mark_as_read':