2021-05-30 04:01:49 +04:30
|
|
|
/*
|
|
|
|
|
* 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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
import DeleteIcon from '@mui/icons-material/Delete';
|
|
|
|
|
import DragHandle from '@mui/icons-material/DragHandle';
|
2021-09-09 17:51:22 +04:30
|
|
|
import PauseIcon from '@mui/icons-material/Pause';
|
2022-11-26 13:51:56 +01:00
|
|
|
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
|
2023-02-06 10:06:33 +01:00
|
|
|
import { Card, CardActionArea, Stack } from '@mui/material';
|
2021-09-09 17:51:22 +04:30
|
|
|
import IconButton from '@mui/material/IconButton';
|
2022-11-26 13:51:56 +01:00
|
|
|
import NavbarContext from 'components/context/NavbarContext';
|
|
|
|
|
import EmptyView from 'components/util/EmptyView';
|
|
|
|
|
import React, { useContext, useEffect } from 'react';
|
2023-05-11 18:00:37 +02:00
|
|
|
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2022-03-03 21:05:14 +00:00
|
|
|
import Typography from '@mui/material/Typography';
|
2022-11-26 13:51:56 +01:00
|
|
|
import { Box } from '@mui/system';
|
2022-11-02 21:48:22 +01:00
|
|
|
import useSubscription from 'components/library/useSubscription';
|
2022-11-26 13:51:56 +01:00
|
|
|
import DownloadStateIndicator from 'components/molecules/DownloadStateIndicator';
|
|
|
|
|
import { NavbarToolbar } from 'components/navbar/DefaultNavBar';
|
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
import { BACK } from 'util/useBackTo';
|
2023-02-14 20:08:03 +03:30
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { IChapter, IQueue } from 'typings';
|
2023-05-15 18:15:28 +02:00
|
|
|
import makeToast from 'components/util/Toast';
|
2023-05-18 13:25:19 +02:00
|
|
|
import requestManager from 'lib/RequestManager';
|
2021-05-30 04:01:49 +04:30
|
|
|
|
|
|
|
|
const initialQueue = {
|
|
|
|
|
status: 'Stopped',
|
|
|
|
|
queue: [],
|
|
|
|
|
} as IQueue;
|
|
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
const DownloadQueue: React.FC = () => {
|
2023-02-14 20:08:03 +03:30
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-05-18 13:25:19 +02:00
|
|
|
const { data: queueState } = useSubscription<IQueue>('downloads');
|
2022-11-02 21:48:22 +01:00
|
|
|
const { queue, status } = queueState ?? initialQueue;
|
2021-05-30 04:01:49 +04:30
|
|
|
|
|
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
|
|
|
|
|
|
|
|
|
const toggleQueueStatus = () => {
|
|
|
|
|
if (status === 'Stopped') {
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.startDownloads();
|
2021-05-30 04:01:49 +04:30
|
|
|
} else {
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.stopDownloads();
|
2021-05-30 04:01:49 +04:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('download.queue.title'));
|
2022-11-26 13:51:56 +01:00
|
|
|
setAction(null);
|
2023-03-28 23:14:03 +02:00
|
|
|
}, [t]);
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2023-05-11 18:00:37 +02:00
|
|
|
const onDragEnd = () => {};
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2021-10-29 17:51:10 +05:30
|
|
|
if (queue.length === 0) {
|
2023-03-23 13:59:32 +01:00
|
|
|
return <EmptyView message={t('download.queue.label.no_downloads')} />;
|
2021-10-29 17:51:10 +05:30
|
|
|
}
|
|
|
|
|
|
2023-05-15 18:15:28 +02:00
|
|
|
const handleDelete = async (chapter: IChapter) => {
|
|
|
|
|
const isRunning = status === 'Started';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (isRunning) {
|
|
|
|
|
// required to stop before deleting otherwise the download kept going. Server issue?
|
2023-05-18 13:25:19 +02:00
|
|
|
await requestManager.stopDownloads();
|
2023-05-15 18:15:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
2022-11-26 13:51:56 +01:00
|
|
|
// remove from download queue
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.removeChapterFromDownloadQueue(chapter.mangaId, chapter.index),
|
2022-11-26 13:51:56 +01:00
|
|
|
// delete partial download, should be handle server side?
|
|
|
|
|
// bug: The folder and the last image downloaded are not deleted
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.deleteDownloadedChapter(chapter.mangaId, chapter.index),
|
2023-05-15 18:15:28 +02:00
|
|
|
]);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
makeToast(t('download.queue.error.label.failed_to_remove'), 'error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isRunning) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.startDownloads().catch(() => {});
|
2022-06-16 09:29:43 -04:00
|
|
|
};
|
|
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
return (
|
|
|
|
|
<>
|
2022-11-26 13:51:56 +01:00
|
|
|
<NavbarToolbar>
|
|
|
|
|
<IconButton onClick={toggleQueueStatus} size="large">
|
|
|
|
|
{status === 'Stopped' ? <PlayArrowIcon /> : <PauseIcon />}
|
|
|
|
|
</IconButton>
|
|
|
|
|
</NavbarToolbar>
|
2021-05-30 04:01:49 +04:30
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
|
|
|
|
<Droppable droppableId="droppable">
|
2023-05-11 18:00:37 +02:00
|
|
|
{(droppableProvided) => (
|
|
|
|
|
<Box ref={droppableProvided.innerRef} sx={{ pt: 1 }}>
|
2021-05-30 04:01:49 +04:30
|
|
|
{queue.map((item, index) => (
|
|
|
|
|
<Draggable
|
|
|
|
|
key={`${item.mangaId}-${item.chapterIndex}`}
|
|
|
|
|
draggableId={`${item.mangaId}-${item.chapterIndex}`}
|
|
|
|
|
index={index}
|
|
|
|
|
>
|
2023-05-11 18:00:37 +02:00
|
|
|
{(draggableProvided, snapshot) => (
|
2022-11-26 13:51:56 +01:00
|
|
|
<Box
|
2023-05-11 18:00:37 +02:00
|
|
|
{...draggableProvided.draggableProps}
|
|
|
|
|
{...draggableProvided.dragHandleProps}
|
|
|
|
|
ref={draggableProvided.innerRef}
|
2022-11-26 13:51:56 +01:00
|
|
|
sx={{ p: 1, pb: 2 }}
|
2021-05-30 04:01:49 +04:30
|
|
|
>
|
2022-11-26 13:51:56 +01:00
|
|
|
<Card
|
|
|
|
|
sx={{
|
2023-02-08 18:19:26 +01:00
|
|
|
backgroundColor: snapshot.isDragging ? 'custom.light' : undefined,
|
2021-05-30 04:01:49 +04:30
|
|
|
}}
|
|
|
|
|
>
|
2022-11-26 13:51:56 +01:00
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
2023-02-06 10:06:33 +01:00
|
|
|
to={{
|
|
|
|
|
pathname: `/manga/${item.chapter.mangaId}`,
|
|
|
|
|
state: { backLink: BACK },
|
|
|
|
|
}}
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
p: 1,
|
|
|
|
|
}}
|
2022-11-26 13:51:56 +01:00
|
|
|
>
|
|
|
|
|
<IconButton sx={{ pointerEvents: 'none' }}>
|
|
|
|
|
<DragHandle />
|
|
|
|
|
</IconButton>
|
2023-02-08 18:19:26 +01:00
|
|
|
<Stack sx={{ flex: 1, ml: 1 }} direction="column">
|
|
|
|
|
<Typography variant="h6">{item.manga.title}</Typography>
|
|
|
|
|
<Typography variant="caption" display="block" gutterBottom>
|
2022-11-26 13:51:56 +01:00
|
|
|
{item.chapter.name}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
<DownloadStateIndicator download={item} />
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
handleDelete(item.chapter);
|
|
|
|
|
}}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
<DeleteIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
|
|
|
|
</Box>
|
2021-05-30 04:01:49 +04:30
|
|
|
)}
|
|
|
|
|
</Draggable>
|
|
|
|
|
))}
|
2023-05-11 18:00:37 +02:00
|
|
|
{droppableProvided.placeholder}
|
2022-11-26 13:51:56 +01:00
|
|
|
</Box>
|
2021-05-30 04:01:49 +04:30
|
|
|
)}
|
|
|
|
|
</Droppable>
|
|
|
|
|
</DragDropContext>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DownloadQueue;
|