Update chapter
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { useCallback } from 'react';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Direction, useTheme } from '@mui/material/styles';
|
||||
import { t as translate } from 'i18next';
|
||||
@@ -33,6 +33,16 @@ import { updateMetadataList } from '@/modules/metadata/services/MetadataApolloCa
|
||||
import { useBackButton } from '@/modules/core/hooks/useBackButton.ts';
|
||||
import { GLOBAL_READER_SETTING_KEYS } from '@/modules/reader/constants/ReaderSettings.constants.tsx';
|
||||
import { useReaderStateSettingsContext } from '@/modules/reader/contexts/state/ReaderStateSettingsContext.tsx';
|
||||
import { UpdateChapterPatchInput } from '@/lib/graphql/generated/graphql.ts';
|
||||
import { useReaderStateChaptersContext } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
|
||||
import { useMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
|
||||
import { DirectionOffset } from '@/Base.types.ts';
|
||||
import {
|
||||
getChapterIdsForDownloadAhead,
|
||||
getChapterIdsToDeleteForChapterUpdate,
|
||||
} from '@/modules/reader/utils/Reader.utils.ts';
|
||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||
import { Queue } from '@/lib/Queue.ts';
|
||||
|
||||
const DIRECTION_TO_INVERTED: Record<Direction, Direction> = {
|
||||
ltr: 'rtl',
|
||||
@@ -45,6 +55,8 @@ const DIRECTION_TO_READING_DIRECTION: Record<Direction, ReadingDirection> = {
|
||||
};
|
||||
|
||||
export class ReaderService {
|
||||
private static downloadAheadQueue: Queue = new Queue(1);
|
||||
|
||||
static useNavigateToChapter(chapter?: TChapterReader, resumeMode?: ReaderResumeMode): () => void {
|
||||
const navigate = useNavigate();
|
||||
return useCallback(
|
||||
@@ -60,6 +72,103 @@ export class ReaderService {
|
||||
);
|
||||
}
|
||||
|
||||
static downloadAhead(
|
||||
currentChapter: TChapterReader,
|
||||
nextChapter: TChapterReader | undefined,
|
||||
nextChapters: TChapterReader[],
|
||||
pageIndex: number,
|
||||
downloadAheadLimit: number,
|
||||
): void {
|
||||
const key = `${currentChapter.id}_${nextChapter?.id}_${pageIndex}_${downloadAheadLimit}`;
|
||||
|
||||
this.downloadAheadQueue.enqueue(key, async () => {
|
||||
const chapterIdsForDownloadAhead = getChapterIdsForDownloadAhead(
|
||||
currentChapter,
|
||||
nextChapter,
|
||||
nextChapters,
|
||||
pageIndex,
|
||||
downloadAheadLimit,
|
||||
);
|
||||
|
||||
if (!chapterIdsForDownloadAhead.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await Chapters.download(chapterIdsForDownloadAhead);
|
||||
} catch (e) {
|
||||
defaultPromiseErrorHandler('ReaderService::useUpdateCurrentPageIndex: download ahead');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static useUpdateChapter(): (patch: UpdateChapterPatchInput) => void {
|
||||
const { manga } = useReaderStateMangaContext();
|
||||
const { initialChapter, currentChapter, mangaChapters } = useReaderStateChaptersContext();
|
||||
const { shouldSkipDupChapters } = this.useSettings();
|
||||
const {
|
||||
settings: { deleteChaptersWhileReading, deleteChaptersWithBookmark, updateProgressAfterReading },
|
||||
} = useMetadataServerSettings();
|
||||
|
||||
const previousChapters = useMemo(() => {
|
||||
if (!initialChapter || !currentChapter) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Chapters.getNextChapters(currentChapter, mangaChapters, {
|
||||
offset: DirectionOffset.PREVIOUS,
|
||||
skipDupe: shouldSkipDupChapters,
|
||||
skipDupeChapter: initialChapter,
|
||||
});
|
||||
}, [initialChapter?.id, currentChapter?.id, mangaChapters, shouldSkipDupChapters]);
|
||||
|
||||
return useCallback(
|
||||
(patch) => {
|
||||
if (!manga || !currentChapter) {
|
||||
return;
|
||||
}
|
||||
|
||||
const chapterIdsToUpdate = Chapters.getIds(
|
||||
shouldSkipDupChapters ? Chapters.addDuplicates([currentChapter], mangaChapters) : [currentChapter],
|
||||
);
|
||||
const chapterIdsToDelete = getChapterIdsToDeleteForChapterUpdate(
|
||||
currentChapter,
|
||||
mangaChapters,
|
||||
previousChapters,
|
||||
patch,
|
||||
deleteChaptersWhileReading,
|
||||
deleteChaptersWithBookmark,
|
||||
shouldSkipDupChapters,
|
||||
);
|
||||
|
||||
requestManager
|
||||
.updateChapters(
|
||||
chapterIdsToUpdate,
|
||||
{
|
||||
...patch,
|
||||
chapterIdsToDelete,
|
||||
trackProgressMangaId:
|
||||
updateProgressAfterReading && patch.isRead && manga.trackRecords.totalCount
|
||||
? manga.id
|
||||
: undefined,
|
||||
},
|
||||
{ errorPolicy: 'all' },
|
||||
)
|
||||
.response.catch(defaultPromiseErrorHandler('ReaderService::useUpdateChapter'));
|
||||
},
|
||||
[
|
||||
manga?.id,
|
||||
currentChapter?.id,
|
||||
mangaChapters,
|
||||
previousChapters,
|
||||
shouldSkipDupChapters,
|
||||
deleteChaptersWhileReading,
|
||||
deleteChaptersWithBookmark,
|
||||
updateProgressAfterReading,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
static useSettings(): IReaderSettingsWithDefaultFlag {
|
||||
return useReaderStateSettingsContext().settings;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user