Skip to content

Commit

Permalink
use language setting for unread chapters count
Browse files Browse the repository at this point in the history
  • Loading branch information
xgi committed Aug 14, 2022
1 parent b1eb7f3 commit 5d74231
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/components/general/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '../../state/libraryStates';
import library from '../../services/library';
import { statusTextState } from '../../state/statusBarStates';
import { refreshOnStartState } from '../../state/settingStates';
import { chapterLanguagesState, refreshOnStartState } from '../../state/settingStates';

const { Content, Sider } = Layout;

Expand All @@ -42,6 +42,7 @@ const DashboardPage: React.FC<Props> = (props: Props) => {
const [completedStartReload, setCompletedStartReload] = useRecoilState(completedStartReloadState);
const setStatusText = useSetRecoilState(statusTextState);
const refreshOnStart = useRecoilValue(refreshOnStartState);
const chapterLanguages = useRecoilValue(chapterLanguagesState);

useEffect(() => {
if (refreshOnStart && !completedStartReload && seriesList.length > 0) {
Expand All @@ -50,7 +51,8 @@ const DashboardPage: React.FC<Props> = (props: Props) => {
library.fetchSeriesList(),
setSeriesList,
setReloadingSeriesList,
setStatusText
setStatusText,
chapterLanguages
)
// eslint-disable-next-line promise/always-return
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/library/ChapterTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const ChapterTable: React.FC<Props> = (props: Props) => {
<Checkbox
checked={record.read}
onChange={() => {
toggleChapterRead(record, props.series, setChapterList, setSeries);
toggleChapterRead(record, props.series, setChapterList, setSeries, chapterLanguages);
if (!record.read && trackerAutoUpdate) {
sendProgressToTrackers(record, props.series);
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/library/ChapterTableContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { toggleChapterRead } from '../../features/library/utils';
import routes from '../../constants/routes.json';
import ipcChannels from '../../constants/ipcChannels.json';
import { chapterListState, seriesState } from '../../state/libraryStates';
import { customDownloadsDirState } from '../../state/settingStates';
import { chapterLanguagesState, customDownloadsDirState } from '../../state/settingStates';

const defaultDownloadsDir = await ipcRenderer.invoke(ipcChannels.GET_PATH.DEFAULT_DOWNLOADS_DIR);

Expand All @@ -33,6 +33,7 @@ const ChapterTableContextMenu: React.FC<Props> = (props: Props) => {
const setChapterList = useSetRecoilState(chapterListState);
const setSeries = useSetRecoilState(seriesState);
const customDownloadsDir = useRecoilValue(customDownloadsDirState);
const chapterLanguages = useRecoilValue(chapterLanguagesState);

if (!props.visible) return <></>;

Expand Down Expand Up @@ -69,15 +70,15 @@ const ChapterTableContextMenu: React.FC<Props> = (props: Props) => {
const handleToggleRead = () => {
props.close();
if (props.chapter !== undefined) {
toggleChapterRead(props.chapter, props.series, setChapterList, setSeries);
toggleChapterRead(props.chapter, props.series, setChapterList, setSeries, chapterLanguages);
}
};

const handleMarkPrevious = (read: boolean) => {
props.close();
getPreviousChapters().forEach((chapter: Chapter) => {
if ((read && !chapter.read) || (!read && chapter.read)) {
toggleChapterRead(chapter, props.series, setChapterList, setSeries);
toggleChapterRead(chapter, props.series, setChapterList, setSeries, chapterLanguages);
}
});
};
Expand Down
12 changes: 10 additions & 2 deletions src/components/library/LibraryControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, Input, Dropdown, Menu } from 'antd';
import { DownOutlined, SyncOutlined } from '@ant-design/icons';
import { Header } from 'antd/lib/layout/layout';
import { SeriesStatus } from 'houdoku-extension-lib';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import styles from './LibraryControlBar.css';
import { reloadSeriesList } from '../../features/library/utils';
import { LibrarySort, LibraryView, ProgressFilter } from '../../models/types';
Expand All @@ -16,6 +16,7 @@ import {
libraryColumnsState,
libraryViewsState,
librarySortState,
chapterLanguagesState,
} from '../../state/settingStates';

// eslint-disable-next-line @typescript-eslint/ban-types
Expand All @@ -40,6 +41,7 @@ const LibraryControlBar: React.FC<Props> = (props: Props) => {
const [libraryColumns, setLibraryColumns] = useRecoilState(libraryColumnsState);
const [libraryViews, setLibraryViews] = useRecoilState(libraryViewsState);
const [librarySort, setLibrarySort] = useRecoilState(librarySortState);
const chapterLanguages = useRecoilValue(chapterLanguagesState);
const [viewSubmenu, setViewSubmenu] = useState('');
const [filterSubmenu, setFilterSubmenu] = useState('');

Expand All @@ -50,7 +52,13 @@ const LibraryControlBar: React.FC<Props> = (props: Props) => {
type="primary"
onClick={() => {
if (!reloadingSeriesList) {
reloadSeriesList(seriesList, setSeriesList, setReloadingSeriesList, setStatusText);
reloadSeriesList(
seriesList,
setSeriesList,
setReloadingSeriesList,
setStatusText,
chapterLanguages
);
}
}}
>
Expand Down
8 changes: 7 additions & 1 deletion src/components/library/SeriesDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,13 @@ const SeriesDetails: React.FC<Props> = (props: Props) => {
className={styles.refreshButton}
onClick={() => {
if (series !== undefined && !reloadingSeriesList)
reloadSeriesList([series], setSeriesList, setReloadingSeriesList, setStatusText)
reloadSeriesList(
[series],
setSeriesList,
setReloadingSeriesList,
setStatusText,
chapterLanguages
)
.then(loadContent)
.catch((e) => log.error(e));
}}
Expand Down
9 changes: 8 additions & 1 deletion src/components/reader/ReaderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const ReaderPage: React.FC<Props> = (props: Props) => {
const trackerAutoUpdate = useRecoilValue(settingStates.trackerAutoUpdateState);
const discordPresenceEnabled = useRecoilValue(settingStates.discordPresenceEnabledState);
const offsetDoubleSpreadsState = useRecoilValue(settingStates.offsetDoubleSpreadsState);
const chapterLanguages = useRecoilValue(settingStates.chapterLanguagesState);
const keyPreviousPage = useRecoilValue(settingStates.keyPreviousPageState);
const keyFirstPage = useRecoilValue(settingStates.keyFirstPageState);
const keyNextPage = useRecoilValue(settingStates.keyNextPageState);
Expand Down Expand Up @@ -485,7 +486,13 @@ const ReaderPage: React.FC<Props> = (props: Props) => {
lastPageNumber > 0
) {
if (pageNumber >= Math.floor(0.8 * lastPageNumber)) {
toggleChapterRead(readerChapter, readerSeries, setChapterList, setLibrarySeries);
toggleChapterRead(
readerChapter,
readerSeries,
setChapterList,
setLibrarySeries,
chapterLanguages
);
setReaderChapter({ ...readerChapter, read: true });
if (trackerAutoUpdate) sendProgressToTrackers(readerChapter, readerSeries);
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/search/AddSeriesModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Paragraph from 'antd/lib/typography/Paragraph';
import { ipcRenderer } from 'electron';
import log from 'electron-log';
import { Series } from 'houdoku-extension-lib';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import styles from './AddSeriesModal.css';
import ipcChannels from '../../constants/ipcChannels.json';
import SeriesEditControls from '../general/SeriesEditControls';
Expand All @@ -13,6 +13,7 @@ import library from '../../services/library';
import { downloadCover } from '../../util/download';
import { seriesListState } from '../../state/libraryStates';
import { statusTextState } from '../../state/statusBarStates';
import { chapterLanguagesState } from '../../state/settingStates';

type Props = {
series: Series | undefined;
Expand All @@ -24,6 +25,7 @@ type Props = {
const AddSeriesModal: React.FC<Props> = (props: Props) => {
const [, setSeriesList] = useRecoilState(seriesListState);
const setStatusText = useSetRecoilState(statusTextState);
const chapterLanguages = useRecoilValue(chapterLanguagesState);
const [customSeries, setCustomSeries] = useState<Series>();
const [loading, setLoading] = useState(true);

Expand Down Expand Up @@ -59,7 +61,7 @@ const AddSeriesModal: React.FC<Props> = (props: Props) => {

const handleAdd = () => {
if (customSeries !== undefined) {
importSeries(customSeries, setStatusText)
importSeries(customSeries, setStatusText, chapterLanguages)
// eslint-disable-next-line promise/always-return
.then((addedSeries) => {
setSeriesList(library.fetchSeriesList());
Expand Down
30 changes: 19 additions & 11 deletions src/features/library/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import { ipcRenderer } from 'electron';
import log from 'electron-log';
import { Chapter, Series } from 'houdoku-extension-lib';
import { Chapter, LanguageKey, Series } from 'houdoku-extension-lib';
import {
deleteAllDownloadedChapters,
deleteThumbnail,
Expand All @@ -13,12 +13,14 @@ import ipcChannels from '../../constants/ipcChannels.json';
import library from '../../services/library';
import { getNumberUnreadChapters } from '../../util/comparison';

const updateSeriesNumberUnread = (series: Series) => {
const updateSeriesNumberUnread = (series: Series, chapterLanguages: LanguageKey[]) => {
if (series.id !== undefined) {
const chapters: Chapter[] = library.fetchChapters(series.id);
library.upsertSeries({
...series,
numberUnread: getNumberUnreadChapters(chapters),
numberUnread: getNumberUnreadChapters(
chapters.filter((chapter) => chapterLanguages.includes(chapter.languageKey))
),
});
}
};
Expand Down Expand Up @@ -61,7 +63,8 @@ export function removeSeries(

export async function importSeries(
series: Series,
setStatusText: (statusText: string) => void
setStatusText: (statusText: string) => void,
chapterLanguages: LanguageKey[]
): Promise<Series> {
log.debug(`Importing series ${series.sourceId} from extension ${series.extensionId}`);
setStatusText(`Adding "${series.title}" to your library...`);
Expand All @@ -75,7 +78,7 @@ export async function importSeries(

const addedSeries = library.upsertSeries(series);
library.upsertChapters(chapters, addedSeries);
updateSeriesNumberUnread(addedSeries);
updateSeriesNumberUnread(addedSeries, chapterLanguages);

log.debug(`Imported series ${series.sourceId} with database ID ${series.id}`);
setStatusText(`Added "${addedSeries.title}" to your library.`);
Expand All @@ -86,7 +89,8 @@ export function toggleChapterRead(
chapter: Chapter,
series: Series,
setChapterList: (chapterList: Chapter[]) => void,
setSeries: (series: Series) => void
setSeries: (series: Series) => void,
chapterLanguages: LanguageKey[]
) {
log.debug(
`Toggling chapter read status for series ${series.title} chapterNum ${chapter.chapterNumber}`
Expand All @@ -96,7 +100,7 @@ export function toggleChapterRead(

if (series.id !== undefined) {
library.upsertChapters([newChapter], series);
updateSeriesNumberUnread(series);
updateSeriesNumberUnread(series, chapterLanguages);
if (series.id !== undefined) {
loadChapterList(series.id, setChapterList);
loadSeries(series.id, setSeries);
Expand All @@ -105,7 +109,10 @@ export function toggleChapterRead(
}

// eslint-disable-next-line consistent-return
async function reloadSeries(series: Series): Promise<Error | void> {
async function reloadSeries(
series: Series,
chapterLanguages: LanguageKey[]
): Promise<Error | void> {
log.info(`Reloading series ${series.id} - ${series.title}`);
if (series.id === undefined) {
return new Promise((resolve) => resolve(Error('Series does not have database ID')));
Expand Down Expand Up @@ -162,7 +169,7 @@ async function reloadSeries(series: Series): Promise<Error | void> {
library.removeChapters(orphanedChapterIds, newSeries.id);
}

updateSeriesNumberUnread(newSeries);
updateSeriesNumberUnread(newSeries, chapterLanguages);

// download the cover as a thumbnail if the remote URL has changed or
// there is no existing thumbnail
Expand All @@ -180,7 +187,8 @@ export async function reloadSeriesList(
seriesList: Series[],
setSeriesList: (seriesList: Series[]) => void,
setReloadingSeriesList: (reloadingSeriesList: boolean) => void,
setStatusText: (statusText: string) => void
setStatusText: (statusText: string) => void,
chapterLanguages: LanguageKey[]
) {
log.debug(`Reloading series list...`);
setReloadingSeriesList(true);
Expand All @@ -195,7 +203,7 @@ export async function reloadSeriesList(
for (const series of sortedSeriesList) {
setStatusText(`Reloading library (${cur}/${seriesList.length}) - ${series.title}`);
// eslint-disable-next-line no-await-in-loop
const ret = await reloadSeries(series);
const ret = await reloadSeries(series, chapterLanguages);
if (ret instanceof Error) {
log.error(ret);
errs += 1;
Expand Down

0 comments on commit 5d74231

Please sign in to comment.