2023-12-30 04:04:59 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-07-11 17:41:38 +02:00
|
|
|
import {
|
|
|
|
|
ChapterBookmarkInfo,
|
|
|
|
|
ChapterDownloadInfo,
|
|
|
|
|
ChapterDownloadStatus,
|
|
|
|
|
ChapterReadInfo,
|
|
|
|
|
Chapters,
|
2024-10-05 19:15:25 +02:00
|
|
|
} from '@/modules/chapter/services/Chapters.ts';
|
2024-09-12 16:02:20 +02:00
|
|
|
import { DownloadState } from '@/lib/graphql/generated/graphql.ts';
|
2023-12-30 04:04:59 +01:00
|
|
|
|
2024-07-11 17:41:38 +02:00
|
|
|
export type ChapterWithMetaType<
|
|
|
|
|
Chapter extends ChapterDownloadInfo & ChapterReadInfo & ChapterBookmarkInfo = ChapterDownloadInfo &
|
|
|
|
|
ChapterReadInfo &
|
|
|
|
|
ChapterBookmarkInfo,
|
|
|
|
|
> = {
|
|
|
|
|
chapter: Chapter;
|
|
|
|
|
downloadChapter: ChapterDownloadStatus | undefined;
|
2023-12-30 04:04:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export class ChaptersWithMeta {
|
2024-07-02 16:20:28 +02:00
|
|
|
static getChapters<ChaptersWithMeta extends ChapterWithMetaType>(
|
|
|
|
|
chapters: ChaptersWithMeta[],
|
|
|
|
|
): ChaptersWithMeta['chapter'][] {
|
2023-12-30 04:04:59 +01:00
|
|
|
return chapters.map(({ chapter }) => chapter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getIds(chapters: ChapterWithMetaType[]): number[] {
|
|
|
|
|
return Chapters.getIds(ChaptersWithMeta.getChapters(chapters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDownloaded<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => Chapters.isDownloaded(chapter));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 23:39:04 +01:00
|
|
|
static getDeletable<Chapter extends ChapterWithMetaType>(
|
|
|
|
|
chapters: Chapter[],
|
|
|
|
|
canDeleteBookmarked?: boolean,
|
|
|
|
|
): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => Chapters.isDeletable(chapter, canDeleteBookmarked));
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 04:04:59 +01:00
|
|
|
static getNonDownloaded<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => !Chapters.isDownloaded(chapter));
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 12:50:04 +02:00
|
|
|
static isDownloadable<Chapter extends ChapterWithMetaType>({ chapter, downloadChapter }: Chapter): boolean {
|
|
|
|
|
return !Chapters.isDownloaded(chapter) && (!downloadChapter || downloadChapter?.state === DownloadState.Error);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 04:04:59 +01:00
|
|
|
static getDownloadable<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
2024-09-18 12:50:04 +02:00
|
|
|
return chapters.filter(this.isDownloadable);
|
2023-12-30 04:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getBookmarked<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => Chapters.isBookmarked(chapter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getNonBookmarked<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => !Chapters.isBookmarked(chapter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getRead<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => Chapters.isRead(chapter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getNonRead<Chapter extends ChapterWithMetaType>(chapters: Chapter[]): Chapter[] {
|
|
|
|
|
return chapters.filter(({ chapter }) => !Chapters.isRead(chapter));
|
|
|
|
|
}
|
|
|
|
|
}
|