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:
@@ -34,8 +34,9 @@ export const ChaptersDownloadActionMenuItems = ({
|
|||||||
settings: { downloadAheadLimit },
|
settings: { downloadAheadLimit },
|
||||||
} = useMetadataServerSettings();
|
} = useMetadataServerSettings();
|
||||||
|
|
||||||
const handleSelect = (size?: number, onlyUnread: boolean = true) => {
|
const handleSelect = (size?: number, onlyUnread: boolean = true, downloadAhead: boolean = false) => {
|
||||||
Mangas.performAction('download', mangaIds, {
|
Mangas.performAction('download', mangaIds, {
|
||||||
|
downloadAhead,
|
||||||
onlyUnread,
|
onlyUnread,
|
||||||
size,
|
size,
|
||||||
}).catch(defaultPromiseErrorHandler('ChapterDownloadButton::handleSelect'));
|
}).catch(defaultPromiseErrorHandler('ChapterDownloadButton::handleSelect'));
|
||||||
@@ -56,7 +57,7 @@ export const ChaptersDownloadActionMenuItems = ({
|
|||||||
<MenuItem onClick={() => handleSelect(DownloadRange.NEXT_25)}>
|
<MenuItem onClick={() => handleSelect(DownloadRange.NEXT_25)}>
|
||||||
{t('chapter.action.download.add.label.next_twentyfive')}
|
{t('chapter.action.download.add.label.next_twentyfive')}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onClick={() => handleSelect(downloadAheadLimit)}>
|
<MenuItem onClick={() => handleSelect(downloadAheadLimit, undefined, true)}>
|
||||||
{t('chapter.action.download.add.label.ahead', { count: downloadAheadLimit })}
|
{t('chapter.action.download.add.label.ahead', { count: downloadAheadLimit })}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onClick={() => handleSelect(DownloadRange.UNREAD)}>
|
<MenuItem onClick={() => handleSelect(DownloadRange.UNREAD)}>
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ export type MigrateMode = 'copy' | 'migrate';
|
|||||||
type DownloadChaptersOptions = {
|
type DownloadChaptersOptions = {
|
||||||
size?: number;
|
size?: number;
|
||||||
onlyUnread?: boolean;
|
onlyUnread?: boolean;
|
||||||
|
downloadAhead?: boolean;
|
||||||
};
|
};
|
||||||
type MarkAsReadOptions = { wasManuallyMarkedAsRead: boolean };
|
type MarkAsReadOptions = { wasManuallyMarkedAsRead: boolean };
|
||||||
type ChangeCategoriesOptions = { changeCategoriesPatch: UpdateMangaCategoriesPatchInput };
|
type ChangeCategoriesOptions = { changeCategoriesPatch: UpdateMangaCategoriesPatchInput };
|
||||||
@@ -245,22 +246,28 @@ export class Mangas {
|
|||||||
|
|
||||||
static async downloadChapters(
|
static async downloadChapters(
|
||||||
mangaIds: number[],
|
mangaIds: number[],
|
||||||
{ size, onlyUnread }: DownloadChaptersOptions = {},
|
{ size, onlyUnread, downloadAhead = false }: DownloadChaptersOptions = {},
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const chapters = await Mangas.getChapterIdsWithState(mangaIds, {
|
const [unReadUnDownloadedChapters, unReadDownloadedChapters] = await Promise.all([
|
||||||
|
Mangas.getChapterIdsWithState(mangaIds, {
|
||||||
isRead: onlyUnread ? false : undefined,
|
isRead: onlyUnread ? false : undefined,
|
||||||
isDownloaded: false,
|
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();
|
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));
|
return Chapters.download(Chapters.getIds(chapterIdsToDownload));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,6 +419,7 @@ export class Mangas {
|
|||||||
wasManuallyMarkedAsRead,
|
wasManuallyMarkedAsRead,
|
||||||
changeCategoriesPatch,
|
changeCategoriesPatch,
|
||||||
mangaIdToMigrateTo,
|
mangaIdToMigrateTo,
|
||||||
|
downloadAhead,
|
||||||
onlyUnread,
|
onlyUnread,
|
||||||
size,
|
size,
|
||||||
...migrateOptions
|
...migrateOptions
|
||||||
@@ -419,7 +427,7 @@ export class Mangas {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'download':
|
case 'download':
|
||||||
return Mangas.downloadChapters(mangaIds, { onlyUnread, size });
|
return Mangas.downloadChapters(mangaIds, { downloadAhead, onlyUnread, size });
|
||||||
case 'delete':
|
case 'delete':
|
||||||
return Mangas.deleteChapters(mangaIds);
|
return Mangas.deleteChapters(mangaIds);
|
||||||
case 'mark_as_read':
|
case 'mark_as_read':
|
||||||
|
|||||||
Reference in New Issue
Block a user