diff --git a/src/database/queries/ChapterQueries.ts b/src/database/queries/ChapterQueries.ts index a96a07f1a..9ef20f305 100644 --- a/src/database/queries/ChapterQueries.ts +++ b/src/database/queries/ChapterQueries.ts @@ -384,16 +384,16 @@ export const bookmarkChapter = async (chapterId: number) => { }); }; -const markPreviuschaptersReadQuery = +const markPreviousChaptersReadQuery = 'UPDATE Chapter SET `unread` = 0 WHERE id <= ? AND novelId = ?'; -export const markPreviuschaptersRead = async ( +export const markPreviousChaptersRead = async ( chapterId: number, novelId: number, ) => { db.transaction(tx => { tx.executeSql( - markPreviuschaptersReadQuery, + markPreviousChaptersReadQuery, [chapterId, novelId], (_txObj, _res) => {}, (_txObj, _error) => { @@ -405,7 +405,7 @@ export const markPreviuschaptersRead = async ( }; const markPreviousChaptersUnreadQuery = - 'UPDATE Chapter SET `unread` = 1 WHERE id <= ? AND novelId = ?'; + 'UPDATE Chapter SET `unread` = 1 WHERE id >= ? AND novelId = ?'; export const markPreviousChaptersUnread = async ( chapterId: number, diff --git a/src/hooks/persisted/useNovel.ts b/src/hooks/persisted/useNovel.ts index 9bb8a0d03..6c3ee8360 100644 --- a/src/hooks/persisted/useNovel.ts +++ b/src/hooks/persisted/useNovel.ts @@ -14,7 +14,7 @@ import { bookmarkChapter as _bookmarkChapter, markChapterRead as _markChapterRead, markChaptersRead as _markChaptersRead, - markPreviuschaptersRead as _markPreviuschaptersRead, + markPreviousChaptersRead as _markPreviousChaptersRead, markPreviousChaptersUnread as _markPreviousChaptersUnread, markChaptersUnread as _markChaptersUnread, deleteChapter as _deleteChapter, @@ -32,6 +32,7 @@ import { parseChapterNumber } from '@utils/parseChapterNumber'; import { NOVEL_STORAGE } from '@utils/Storages'; import FileManager from '@native/FileManager'; import { useAppSettings } from './useSettings'; +import { getPlugin } from '@plugins/pluginManager'; // store key: '__', // store key: '_', @@ -195,6 +196,7 @@ export const useNovel = (novelPath: string, pluginId: string) => { _chapters.map(_chapter => { _bookmarkChapter(_chapter.id); }); + setChapters( chapters.map(chapter => { if (_chapters.some(_c => _c.id === chapter.id)) { @@ -208,19 +210,51 @@ export const useNovel = (novelPath: string, pluginId: string) => { ); }; - const markPreviouschaptersRead = (chapterId: number) => { + const markPreviousChaptersRead = (chapterId: number) => { if (novel) { - _markPreviuschaptersRead(chapterId, novel.id); + // Track progress if the plugin supports it + const plugin = getPlugin(novel.pluginId); + if (plugin?.trackProgress) { + const chapterPosition = chapters.find( + chapter => chapter.id === chapterId, + )?.position; + const trackedChapter = chapters.find( + chapter => chapter.position === chapterPosition, + ); + + if (trackedChapter) { + plugin.trackProgress(novel.path, trackedChapter.path); + } + } + _markPreviousChaptersRead(chapterId, novel.id); + setChapters( - chapters.map(chapter => - chapter.id <= chapterId ? { ...chapter, unread: false } : chapter, - ), + chapters.map(chapter => { + if (chapter.id <= chapterId) { + return { ...chapter, unread: false }; + } + return chapter; + }), ); } }; const markChapterRead = (chapterId: number) => { + if (novel) { + // Track progress if the plugin supports it + const plugin = getPlugin(novel.pluginId); + if (plugin?.trackProgress) { + const selectedChapter = chapters.find( + chapter => chapter.id === chapterId, + ); + + if (selectedChapter) { + plugin.trackProgress(novel.path, selectedChapter.path); + } + } + } _markChapterRead(chapterId); + setChapters( chapters.map(c => { if (c.id !== chapterId) { @@ -235,6 +269,19 @@ export const useNovel = (novelPath: string, pluginId: string) => { }; const markChaptersRead = (_chapters: ChapterInfo[]) => { + if (novel) { + // Track progress if the plugin supports it + const plugin = getPlugin(novel.pluginId); + if (plugin?.trackProgress) { + // Sort the selected chapters based on the position + const sortedChapters = [..._chapters].sort((a, b) => { + return b.position! - a.position!; + }); + const trackedChapter = sortedChapters[0]; + + plugin.trackProgress?.(novel.path, trackedChapter.path); + } + } const chapterIds = _chapters.map(chapter => chapter.id); _markChaptersRead(chapterIds); @@ -253,16 +300,56 @@ export const useNovel = (novelPath: string, pluginId: string) => { const markPreviousChaptersUnread = (chapterId: number) => { if (novel) { + // Track progress if the plugin supports it + const plugin = getPlugin(novel.pluginId); + if (plugin?.trackProgress) { + const chapterPosition = chapters.find( + chapter => chapter.id === chapterId, + )?.position; + const trackedChapter = + chapters.find(chapter => chapter.position === chapterPosition! - 1) || + chapters.find(chapter => chapter.position === chapterPosition); + + if (trackedChapter) { + plugin.trackProgress(novel.path, trackedChapter.path); + } + } _markPreviousChaptersUnread(chapterId, novel.id); + setChapters( - chapters.map(chapter => - chapter.id <= chapterId ? { ...chapter, unread: true } : chapter, - ), + chapters.map(chapter => { + if (chapter.id >= chapterId) { + return { ...chapter, unread: true }; + } + return chapter; + }), ); } }; const markChaptersUnread = (_chapters: ChapterInfo[]) => { + if (novel) { + // Track progress if the plugin supports it + const plugin = getPlugin(novel.pluginId); + if (plugin?.trackProgress) { + // Sort the selected chapters based on the position + const sortedChapters = [..._chapters].sort((a, b) => { + return a.position! - b.position!; + }); + const firstChapterPosition = chapters.find( + chapter => chapter.id === sortedChapters[0].id, + )?.position; + const trackedChapter = + chapters.find( + chapter => chapter.position === firstChapterPosition! - 1, + ) || + chapters.find(chapter => chapter.position === firstChapterPosition); + + if (trackedChapter) { + plugin.trackProgress(novel.path, trackedChapter.path); + } + } + } const chapterIds = _chapters.map(chapter => chapter.id); _markChaptersUnread(chapterIds); @@ -406,7 +493,7 @@ export const useNovel = (novelPath: string, pluginId: string) => { sortAndFilterChapters, followNovel, bookmarkChapters, - markPreviouschaptersRead, + markPreviousChaptersRead, markChaptersRead, markPreviousChaptersUnread, markChaptersUnread, diff --git a/src/plugins/types/index.ts b/src/plugins/types/index.ts index 1f70cf613..b8a7695fa 100644 --- a/src/plugins/types/index.ts +++ b/src/plugins/types/index.ts @@ -77,6 +77,7 @@ export interface Plugin extends PluginItem { parseNovel: (novelPath: string) => Promise; parsePage?: (novelPath: string, page: string) => Promise; parseChapter: (chapterPath: string) => Promise; + trackProgress?: (novelPath: string, chapterPath: string) => Promise; searchNovels: (searchTerm: string, pageNo: number) => Promise; resolveUrl?: (path: string, isNovel?: boolean) => string; webStorageUtilized?: boolean; diff --git a/src/screens/novel/NovelScreen.tsx b/src/screens/novel/NovelScreen.tsx index 12e0f2d62..0c0d31efc 100644 --- a/src/screens/novel/NovelScreen.tsx +++ b/src/screens/novel/NovelScreen.tsx @@ -82,7 +82,7 @@ const Novel = ({ route, navigation }: NovelScreenProps) => { bookmarkChapters, markChaptersRead, markChaptersUnread, - markPreviouschaptersRead, + markPreviousChaptersRead, markPreviousChaptersUnread, followNovel, deleteChapter, @@ -261,7 +261,7 @@ const Novel = ({ route, navigation }: NovelScreenProps) => { list.push({ icon: 'playlist-check', onPress: () => { - markPreviouschaptersRead(selected[0].id); + markPreviousChaptersRead(selected[0].id); setSelected([]); }, }); diff --git a/src/screens/reader/hooks/useChapter.ts b/src/screens/reader/hooks/useChapter.ts index 3574102cd..feee3c196 100644 --- a/src/screens/reader/hooks/useChapter.ts +++ b/src/screens/reader/hooks/useChapter.ts @@ -29,6 +29,7 @@ import { defaultTo } from 'lodash-es'; import { useChapterContext } from '../ChapterContext'; import { showToast } from '@utils/showToast'; import { getString } from '@strings/translations'; +import { getPlugin } from '@plugins/pluginManager'; const emmiter = new NativeEventEmitter(VolumeButtonListener); @@ -146,6 +147,13 @@ export default function useChapter(webViewRef: RefObject) { if (!incognitoMode && percentage >= 97) { // a relative number + + // Track progress if the plugin supports it + const plugin = getPlugin(novel.pluginId); + if (plugin?.trackProgress && chapter.unread) { + plugin.trackProgress(novel.path, chapter.path); + } + markChapterRead(chapter.id); updateTracker(); }