Fix some bugs with task queue (#1384)
* Fix novel chapter downloading spinner not showing in chapter list * Fix novel chapter download notification staying on 'Prepairing'
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { ChapterInfo, NovelInfo } from '@database/types';
|
||||
import ServiceManager, { BackgroundTask } from '@services/ServiceManager';
|
||||
import ServiceManager, {
|
||||
BackgroundTaskMetadata,
|
||||
DownloadChapterTask,
|
||||
QueuedBackgroundTask,
|
||||
} from '@services/ServiceManager';
|
||||
import { useMemo } from 'react';
|
||||
import { useMMKVObject } from 'react-native-mmkv';
|
||||
|
||||
@@ -7,13 +11,13 @@ export const DOWNLOAD_QUEUE = 'DOWNLOAD';
|
||||
export const CHAPTER_DOWNLOADING = 'CHAPTER_DOWNLOADING';
|
||||
|
||||
export default function useDownload() {
|
||||
const [queue] = useMMKVObject<BackgroundTask[]>(
|
||||
const [queue] = useMMKVObject<QueuedBackgroundTask[]>(
|
||||
ServiceManager.manager.STORE_KEY,
|
||||
);
|
||||
const downloadQueue = useMemo(
|
||||
() => queue?.filter(t => t.name === 'DOWNLOAD_CHAPTER') || [],
|
||||
() => queue?.filter(t => t.task.name === 'DOWNLOAD_CHAPTER') || [],
|
||||
[queue],
|
||||
);
|
||||
) as { task: DownloadChapterTask; meta: BackgroundTaskMetadata }[];
|
||||
|
||||
const downloadChapter = (novel: NovelInfo, chapter: ChapterInfo) =>
|
||||
ServiceManager.manager.addTask({
|
||||
|
||||
@@ -438,7 +438,7 @@ const Novel = ({ route, navigation }: NovelScreenProps) => {
|
||||
renderItem={({ item }) => (
|
||||
<ChapterItem
|
||||
isDownloading={downloadQueue.some(
|
||||
c => c.data.chapterId === item.id,
|
||||
c => c.task.data.chapterId === item.id,
|
||||
)}
|
||||
isLocal={novel.isLocal}
|
||||
theme={theme}
|
||||
|
||||
@@ -35,10 +35,11 @@ export type BackgroundTask =
|
||||
| { name: 'SELF_HOST_BACKUP'; data: SelfHostData }
|
||||
| { name: 'SELF_HOST_RESTORE'; data: SelfHostData }
|
||||
| { name: 'MIGRATE_NOVEL'; data: MigrateNovelData }
|
||||
| {
|
||||
name: 'DOWNLOAD_CHAPTER';
|
||||
data: { chapterId: number; novelName: string; chapterName: string };
|
||||
};
|
||||
| DownloadChapterTask;
|
||||
export type DownloadChapterTask = {
|
||||
name: 'DOWNLOAD_CHAPTER';
|
||||
data: { chapterId: number; novelName: string; chapterName: string };
|
||||
};
|
||||
|
||||
export type BackgroundTaskMetadata = {
|
||||
name: string;
|
||||
@@ -105,7 +106,7 @@ export default class ServiceManager {
|
||||
if (taskList[0].meta.isRunning) {
|
||||
BackgroundService.updateNotification({
|
||||
taskTitle: taskList[0].meta.name,
|
||||
taskDesc: taskList[0].meta.progressText,
|
||||
taskDesc: taskList[0].meta.progressText ?? '',
|
||||
progressBar: {
|
||||
indeterminate: taskList[0].meta.progress === undefined,
|
||||
value: (taskList[0].meta.progress || 0) * 100,
|
||||
@@ -117,6 +118,16 @@ export default class ServiceManager {
|
||||
setMMKVObject(this.STORE_KEY, taskList);
|
||||
}
|
||||
async executeTask(task: QueuedBackgroundTask) {
|
||||
await BackgroundService.updateNotification({
|
||||
taskTitle: task.meta.name,
|
||||
taskDesc: task.meta.progressText ?? '',
|
||||
progressBar: {
|
||||
indeterminate: true,
|
||||
max: 100,
|
||||
value: 0,
|
||||
},
|
||||
});
|
||||
|
||||
switch (task.task.name) {
|
||||
case 'IMPORT_EPUB':
|
||||
return importEpub(task.task.data, this.setMeta.bind(this));
|
||||
|
||||
Reference in New Issue
Block a user