Skip to content

Commit

Permalink
Fix some bugs with task queue (#1384)
Browse files Browse the repository at this point in the history
* Fix novel chapter downloading spinner not showing in chapter list

* Fix novel chapter download notification staying on 'Prepairing'
  • Loading branch information
Soopyboo32 authored Jan 18, 2025
1 parent 430a067 commit 6599883
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/hooks/persisted/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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';

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({
Expand Down
2 changes: 1 addition & 1 deletion src/screens/novel/NovelScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
21 changes: 16 additions & 5 deletions src/services/ServiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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));
Expand Down

0 comments on commit 6599883

Please sign in to comment.