Update chapter
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
import { MutableRefObject, useCallback, useMemo } from 'react';
|
||||
import { Direction } from '@mui/material/styles';
|
||||
import { userReaderStatePagesContext } from '@/modules/reader/contexts/state/ReaderStatePagesContext.tsx';
|
||||
import { getNextPageIndex, getPage } from '@/modules/reader/utils/ReaderProgressBar.utils.tsx';
|
||||
import { getNextIndexFromPage, getNextPageIndex, getPage } from '@/modules/reader/utils/ReaderProgressBar.utils.tsx';
|
||||
import { getOptionForDirection } from '@/theme.tsx';
|
||||
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
|
||||
import { useReaderStateChaptersContext } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
|
||||
@@ -22,12 +22,16 @@ import {
|
||||
} from '@/modules/reader/types/Reader.types.ts';
|
||||
import { ScrollDirection, ScrollOffset } from '@/modules/core/Core.types.ts';
|
||||
import { ReaderScrollAmount } from '@/modules/reader/constants/ReaderSettings.constants.tsx';
|
||||
import { isPageInViewport } from '@/modules/reader/utils/ReaderPager.utils.tsx';
|
||||
import { isEndOfPageInViewport, isPageInViewport } from '@/modules/reader/utils/ReaderPager.utils.tsx';
|
||||
import { useReaderOverlayContext } from '@/modules/reader/contexts/ReaderOverlayContext.tsx';
|
||||
import { useReaderTapZoneContext } from '@/modules/reader/contexts/ReaderTapZoneContext.tsx';
|
||||
import { TapZoneRegionType } from '@/modules/reader/types/TapZoneLayout.types.ts';
|
||||
import { ReaderTapZoneService } from '@/modules/reader/services/ReaderTapZoneService.ts';
|
||||
import { isContinuousReadingMode } from '@/modules/reader/utils/ReaderSettings.utils.tsx';
|
||||
import { getReaderChapterFromCache } from '@/modules/reader/utils/Reader.utils.ts';
|
||||
import { Chapters } from '@/modules/chapter/services/Chapters.ts';
|
||||
import { DirectionOffset } from '@/Base.types.ts';
|
||||
import { useMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
|
||||
|
||||
const READING_DIRECTION_TO_DIRECTION: Record<ReadingDirection, Direction> = {
|
||||
[ReadingDirection.LTR]: 'ltr',
|
||||
@@ -46,6 +50,8 @@ const SCROLL_DIRECTION_BY_SCROLL_OFFSET_BY_READING_DIRECTION: Record<ReadingDire
|
||||
};
|
||||
|
||||
export class ReaderControls {
|
||||
private static updateCurrentPageTimeout: NodeJS.Timeout;
|
||||
|
||||
static scroll(
|
||||
offset: ScrollOffset,
|
||||
direction: ScrollDirection,
|
||||
@@ -152,10 +158,11 @@ export class ReaderControls {
|
||||
() => getNextPageIndex('next', currentPage.pagesIndex, pages),
|
||||
[currentPage, pages],
|
||||
);
|
||||
const indexOfLastPage = getNextIndexFromPage(pages[pages.length - 1]);
|
||||
const direction = READING_DIRECTION_TO_DIRECTION[readingDirection.value];
|
||||
|
||||
const isFirstPage = currentPageIndex === 0;
|
||||
const isLastPage = currentPageIndex === pages[pages.length - 1].primary.index;
|
||||
const isLastPage = currentPageIndex === indexOfLastPage;
|
||||
const isATransitionPageVisible = transitionPageMode !== ReaderTransitionPageMode.NONE;
|
||||
const isContinuousReadingModeActive = isContinuousReadingMode(readingMode.value);
|
||||
|
||||
@@ -225,6 +232,7 @@ export class ReaderControls {
|
||||
direction,
|
||||
previousPageIndex,
|
||||
nextPageIndex,
|
||||
indexOfLastPage,
|
||||
isATransitionPageVisible,
|
||||
isContinuousReadingModeActive,
|
||||
isFirstPage,
|
||||
@@ -236,16 +244,96 @@ export class ReaderControls {
|
||||
);
|
||||
}
|
||||
|
||||
static useUpdateCurrentPageIndex(): (
|
||||
pageIndex: number,
|
||||
debounceChapterUpdate?: boolean,
|
||||
endReached?: boolean,
|
||||
) => void {
|
||||
const { currentPageIndex, setCurrentPageIndex } = userReaderStatePagesContext();
|
||||
const { initialChapter, currentChapter, nextChapter, mangaChapters } = useReaderStateChaptersContext();
|
||||
const updateChapter = ReaderService.useUpdateChapter();
|
||||
const { shouldSkipDupChapters } = ReaderService.useSettings();
|
||||
const {
|
||||
settings: { downloadAheadLimit },
|
||||
} = useMetadataServerSettings();
|
||||
|
||||
const nextChapters = useMemo(() => {
|
||||
if (!initialChapter || !currentChapter) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Chapters.getNextChapters(currentChapter, mangaChapters, {
|
||||
offset: DirectionOffset.NEXT,
|
||||
skipDupe: shouldSkipDupChapters,
|
||||
skipDupeChapter: initialChapter,
|
||||
});
|
||||
}, [initialChapter?.id, currentChapter?.id, mangaChapters, shouldSkipDupChapters]);
|
||||
|
||||
return useCallback(
|
||||
(pageIndex, debounceChapterUpdate = true, endReached = false) => {
|
||||
setCurrentPageIndex(pageIndex);
|
||||
|
||||
if (!currentChapter) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasPageIndexChanged = pageIndex !== currentPageIndex;
|
||||
if (!hasPageIndexChanged) {
|
||||
return;
|
||||
}
|
||||
|
||||
ReaderService.downloadAhead(currentChapter, nextChapter, nextChapters, pageIndex, downloadAheadLimit);
|
||||
|
||||
const handleCurrentPageIndexChange = () => {
|
||||
const currentChapterUpToDate = getReaderChapterFromCache(currentChapter.id);
|
||||
if (!currentChapterUpToDate) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasLastPageReadChanged = pageIndex !== currentChapterUpToDate.lastPageRead;
|
||||
const isLasPage = endReached || pageIndex === currentChapterUpToDate.pageCount - 1;
|
||||
const shouldUpdateChapter = hasLastPageReadChanged || isLasPage;
|
||||
if (!shouldUpdateChapter) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateChapter({
|
||||
lastPageRead: hasLastPageReadChanged ? pageIndex : undefined,
|
||||
isRead: isLasPage ? true : undefined,
|
||||
});
|
||||
};
|
||||
|
||||
clearTimeout(this.updateCurrentPageTimeout);
|
||||
if (debounceChapterUpdate) {
|
||||
this.updateCurrentPageTimeout = setTimeout(handleCurrentPageIndexChange, 1000);
|
||||
return;
|
||||
}
|
||||
|
||||
handleCurrentPageIndexChange();
|
||||
},
|
||||
[currentChapter?.id, nextChapter?.id, nextChapters, currentPageIndex, downloadAheadLimit],
|
||||
);
|
||||
}
|
||||
|
||||
static updateCurrentPageOnScroll(
|
||||
imageRefs: MutableRefObject<(HTMLElement | null)[]>,
|
||||
currentPageIndex: number,
|
||||
setCurrentPageIndex: (index: number) => void,
|
||||
lastPageIndex: number,
|
||||
updateCurrentPageIndex: ReturnType<typeof ReaderControls.useUpdateCurrentPageIndex>,
|
||||
type: PageInViewportType,
|
||||
readingDirection: ReadingDirection,
|
||||
) {
|
||||
const firstVisibleImageIndex = imageRefs.current.findIndex((image) => image && isPageInViewport(image, type));
|
||||
const lastPage = imageRefs.current?.[imageRefs.current.length - 1];
|
||||
const isEndReached = lastPage && isEndOfPageInViewport(lastPage, type, readingDirection);
|
||||
|
||||
if (firstVisibleImageIndex !== -1 && firstVisibleImageIndex !== currentPageIndex) {
|
||||
setCurrentPageIndex(firstVisibleImageIndex);
|
||||
// handle cases where the last page is too small to ever be the "firstVisibleImageIndex"
|
||||
if (isEndReached) {
|
||||
updateCurrentPageIndex(lastPageIndex, false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (firstVisibleImageIndex !== -1) {
|
||||
updateCurrentPageIndex(firstVisibleImageIndex, firstVisibleImageIndex !== lastPageIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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