From e171dfebc1672e13645916ac331c878ae48e5110 Mon Sep 17 00:00:00 2001 From: nyagami Date: Sun, 24 Mar 2024 13:55:23 +0700 Subject: [PATCH] Update lastest chapter logic (#1011) update lastest chapter logic --- src/hooks/persisted/useNovel.ts | 29 ++++++-------------- src/plugins/types/index.ts | 2 +- src/services/updates/LibraryUpdateQueries.ts | 25 +++++++++++------ 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/hooks/persisted/useNovel.ts b/src/hooks/persisted/useNovel.ts index 6766cf69..18832640 100644 --- a/src/hooks/persisted/useNovel.ts +++ b/src/hooks/persisted/useNovel.ts @@ -28,7 +28,6 @@ import { useCallback, useEffect, useState } from 'react'; import { NovelDownloadFolder } from '@utils/constants/download'; import * as RNFS from 'react-native-fs'; import { getString } from '@strings/translations'; -import { ChapterItem } from '@plugins/types'; import dayjs from 'dayjs'; import { parseChapterNumber } from '@utils/parseChapterNumber'; @@ -326,6 +325,12 @@ export const useNovel = (novelPath: string, pluginId: string) => { if (!novel) { return; } + if (sourceNovel.latestChapter) { + setMMKVObject( + `${NOVEL_LATEST_CHAPTER_PREFIX}_${novel.id}`, + sourceNovel.latestChapter, + ); + } } let pages: string[]; if (novel.totalPages > 0) { @@ -377,26 +382,8 @@ export const useNovel = (novelPath: string, pluginId: string) => { }; }); await insertChapters(novel.id, sourceChapters); - const latestChapkey = `${NOVEL_LATEST_CHAPTER_PREFIX}_${novel.id}`; - const latestChapter = getMMKVObject(latestChapkey); - if ( - sourcePage.latestChapter && - sourcePage.latestChapter.path !== latestChapter?.path - ) { - setMMKVObject(latestChapkey, sourcePage.latestChapter); - setMMKVObject( - hasUpdatesKey, - hasUpdates?.map((val, idx) => { - if (idx !== pageIndex) { - return false; - } - return true; - }), - ); - } else { - hasUpdates[pageIndex] = false; - setMMKVObject(hasUpdatesKey, hasUpdates); - } + hasUpdates[pageIndex] = false; + setMMKVObject(hasUpdatesKey, hasUpdates); chapters = await _getPageChapters( novel.id, novelSettings.sort, diff --git a/src/plugins/types/index.ts b/src/plugins/types/index.ts index 4cf2ba68..3de17377 100644 --- a/src/plugins/types/index.ts +++ b/src/plugins/types/index.ts @@ -33,11 +33,11 @@ export interface SourceNovel extends NovelItem { status?: NovelStatus; chapters: ChapterItem[]; totalPages?: number; + latestChapter?: ChapterItem; } export interface SourcePage { chapters: ChapterItem[]; - latestChapter?: ChapterItem; } export interface PopularNovelsOptions { diff --git a/src/services/updates/LibraryUpdateQueries.ts b/src/services/updates/LibraryUpdateQueries.ts index 329b8c9d..34e27505 100644 --- a/src/services/updates/LibraryUpdateQueries.ts +++ b/src/services/updates/LibraryUpdateQueries.ts @@ -2,12 +2,15 @@ import { fetchImage, fetchNovel } from '../plugin/fetch'; import { downloadChapter } from '../../database/queries/ChapterQueries'; import * as SQLite from 'expo-sqlite'; -import { SourceNovel } from '@plugins/types'; +import { ChapterItem, SourceNovel } from '@plugins/types'; import { LOCAL_PLUGIN_ID } from '@plugins/pluginManager'; import { NovelDownloadFolder } from '@utils/constants/download'; import * as RNFS from 'react-native-fs'; import { getMMKVObject, setMMKVObject } from '@utils/mmkv/mmkv'; -import { NOVEL_PAGE_UPDATES_PREFIX } from '@hooks/persisted/useNovel'; +import { + NOVEL_LATEST_CHAPTER_PREFIX, + NOVEL_PAGE_UPDATES_PREFIX, +} from '@hooks/persisted/useNovel'; const db = SQLite.openDatabase('lnreader.db'); const updateNovelMetadata = ( @@ -156,13 +159,17 @@ const updateNovel = async ( await updateNovelMetadata(pluginId, novelId, novel); } await updateNovelChapters(pluginId, novelId, novel, downloadNewChapters); - const key = `${NOVEL_PAGE_UPDATES_PREFIX}_${novelId}`; - const hasUpdates = getMMKVObject(key); - if (hasUpdates) { - setMMKVObject( - key, - hasUpdates.map(() => true), - ); + const latestChapterKey = `${NOVEL_LATEST_CHAPTER_PREFIX}_${novelId}`; + const latestChapter = getMMKVObject(latestChapterKey); + if (novel.latestChapter && novel.latestChapter.path !== latestChapter?.path) { + const hasUpdatesKey = `${NOVEL_PAGE_UPDATES_PREFIX}_${novelId}`; + const hasUpdates = getMMKVObject(hasUpdatesKey); + if (hasUpdates) { + setMMKVObject( + hasUpdatesKey, + hasUpdates.map(() => true), + ); + } } };