Loading display for current download if it hasn't started

This commit is contained in:
AnidlSupport 2023-03-02 22:20:04 +01:00
parent bba20bb255
commit 8f9a21c773
6 changed files with 52 additions and 11 deletions

View file

@ -3,7 +3,8 @@ import { ExtendedProgress, QueueItem } from './messageHandler';
export type RandomEvents = { export type RandomEvents = {
progress: ExtendedProgress, progress: ExtendedProgress,
finish: undefined, finish: undefined,
queueChange: QueueItem[] queueChange: QueueItem[],
current: QueueItem|undefined
} }
export interface RandomEvent<T extends keyof RandomEvents> { export interface RandomEvent<T extends keyof RandomEvents> {

View file

@ -58,7 +58,7 @@ const SearchBox: React.FC = () => {
}}> }}>
<Box sx={{ display: 'flex' }}> <Box sx={{ display: 'flex' }}>
<Box sx={{ width: '20%', height: '100%', pr: 2 }}> <Box sx={{ width: '20%', height: '100%', pr: 2 }}>
<img ref={imageRef} src={a.image} style={{ width: '100%', height: '100%' }} alt="thumbnail"/> <img ref={imageRef} src={a.image} style={{ width: '100%', height: 'auto' }} alt="thumbnail"/>
</Box> </Box>
<Box sx={{ display: 'flex', flexDirection: 'column', maxWidth: '70%' }}> <Box sx={{ display: 'flex', flexDirection: 'column', maxWidth: '70%' }}>
<Typography variant='h6' component='h6' color='text.primary' sx={{ }}> <Typography variant='h6' component='h6' color='text.primary' sx={{ }}>

View file

@ -1,5 +1,5 @@
import React from "react"; import React from "react";
import { ExtendedProgress } from "../../../../../../@types/messageHandler"; import { ExtendedProgress, QueueItem } from "../../../../../../@types/messageHandler";
import { RandomEvent } from "../../../../../../@types/randomEvents"; import { RandomEvent } from "../../../../../../@types/randomEvents";
import { messageChannelContext } from "../../../provider/MessageChannel"; import { messageChannelContext } from "../../../provider/MessageChannel";
@ -7,26 +7,34 @@ const useDownloadManager = () => {
const messageHandler = React.useContext(messageChannelContext); const messageHandler = React.useContext(messageChannelContext);
const [progressData, setProgressData] = React.useState<ExtendedProgress|undefined>(); const [progressData, setProgressData] = React.useState<ExtendedProgress|undefined>();
const [current, setCurrent] = React.useState<undefined|QueueItem>();
React.useEffect(() => { React.useEffect(() => {
const handler = (ev: RandomEvent<'progress'>) => { const handler = (ev: RandomEvent<'progress'>) => {
console.log(ev.data); console.log(ev.data);
setProgressData(ev.data); setProgressData(ev.data);
} }
messageHandler?.randomEvents.on('progress', handler);
const currentHandler = (ev: RandomEvent<'current'>) => {
setCurrent(ev.data);
}
const finishHandler = () => { const finishHandler = () => {
setProgressData(undefined); setProgressData(undefined);
} }
messageHandler?.randomEvents.on('progress', handler);
messageHandler?.randomEvents.on('current', currentHandler);
messageHandler?.randomEvents.on('finish', finishHandler); messageHandler?.randomEvents.on('finish', finishHandler);
return () => { return () => {
messageHandler?.randomEvents.removeListener('progress', handler); messageHandler?.randomEvents.removeListener('progress', handler);
messageHandler?.randomEvents.removeListener('finish', finishHandler) messageHandler?.randomEvents.removeListener('finish', finishHandler);
messageHandler?.randomEvents.removeListener('current', currentHandler);
}; };
}, [messageHandler]); }, [messageHandler]);
return progressData; return { data: progressData, current};
} }
export default useDownloadManager; export default useDownloadManager;

View file

@ -1,4 +1,4 @@
import { Box, Button, Divider, LinearProgress, Skeleton, Typography } from "@mui/material"; import { Box, Button, CircularProgress, Divider, LinearProgress, Skeleton, Typography } from "@mui/material";
import React from "react"; import React from "react";
import { messageChannelContext } from "../../../provider/MessageChannel"; import { messageChannelContext } from "../../../provider/MessageChannel";
import { queueContext } from "../../../provider/QueueProvider"; import { queueContext } from "../../../provider/QueueProvider";
@ -6,17 +6,18 @@ import { queueContext } from "../../../provider/QueueProvider";
import useDownloadManager from "../DownloadManager/DownloadManager"; import useDownloadManager from "../DownloadManager/DownloadManager";
const Queue: React.FC = () => { const Queue: React.FC = () => {
const data = useDownloadManager(); const { data, current } = useDownloadManager();
const queue = React.useContext(queueContext); const queue = React.useContext(queueContext);
const msg = React.useContext(messageChannelContext); const msg = React.useContext(messageChannelContext);
if (!msg) if (!msg)
return <>Never</> return <>Never</>
return data || queue.length > 0 ? <> return data || queue.length > 0 ? <>
{data && <> {data && <>
<Box sx={{ height: 200, display: 'grid', gridTemplateColumns: '20% 1fr', gap: 1, mb: 1, mt: 1 }}> <Box sx={{ height: 200, display: 'grid', gridTemplateColumns: '20% 1fr', gap: 1, mb: 1, mt: 1 }}>
<img src={data.downloadInfo.image} height='200px' width='100%' alt="Thumbnail" /> <img src={data.downloadInfo.image} height='auto' width='100%' alt="Thumbnail" />
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
<Box sx={{ display: 'flex', flexDirection: 'column' }}> <Box sx={{ display: 'flex', flexDirection: 'column' }}>
<Box sx={{ display: 'grid', gridTemplateColumns: '1fr max-content' }}> <Box sx={{ display: 'grid', gridTemplateColumns: '1fr max-content' }}>
@ -41,11 +42,39 @@ const Queue: React.FC = () => {
</Box> </Box>
</> </>
} }
{
current && !data && <>
<Box sx={{ height: 200, display: 'grid', gridTemplateColumns: '20% 1fr', gap: 1, mb: 1, mt: 1 }}>
<img src={current.image} height='auto' width='100%' alt="Thumbnail" />
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
<Box sx={{ display: 'grid', gridTemplateColumns: '1fr max-content' }}>
<Typography variant='h5' color='text.primary'>
{current.title}
</Typography>
<Typography variant='h5' color='text.primary'>
Language: <CircularProgress variant="indeterminate" />
</Typography>
</Box>
<Typography variant='h6' color='text.primary'>
{current.parent.title}
</Typography>
</Box>
<LinearProgress variant='indeterminate' sx={{ height: '10px' }} />
<Box>
<Typography variant="body1" color='text.primary'>
0 / ? parts (0% | X:XX | 0 MB/s | 0MB)
</Typography>
</Box>
</Box>
</Box>
</>
}
{queue.length && data && <Divider variant="fullWidth" />} {queue.length && data && <Divider variant="fullWidth" />}
{queue.map((queueItem, index, { length }) => { {queue.map((queueItem, index, { length }) => {
return <Box key={`queue_item_${index}`}> return <Box key={`queue_item_${index}`}>
<Box sx={{ height: 200, display: 'grid', gridTemplateColumns: '20% 1fr', gap: 1, mb: 1, mt: 1 }}> <Box sx={{ height: 200, display: 'grid', gridTemplateColumns: '20% 1fr', gap: 1, mb: 1, mt: 1 }}>
<img src={queueItem.image} height='200px' width='100%' alt="Thumbnail" /> <img src={queueItem.image} height='auto' width='100%' alt="Thumbnail" />
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
<Box sx={{ display: 'flex', flexDirection: 'column' }}> <Box sx={{ display: 'flex', flexDirection: 'column' }}>
<Box sx={{ display: 'grid', gridTemplateColumns: '1fr 200px' }}> <Box sx={{ display: 'grid', gridTemplateColumns: '1fr 200px' }}>

View file

@ -17,7 +17,8 @@ export class RandomEventHandler {
} = { } = {
progress: [], progress: [],
finish: [], finish: [],
queueChange: [] queueChange: [],
current: []
}; };
public on<T extends keyof RandomEvents>(name: T, listener: Handler<T>) { public on<T extends keyof RandomEvents>(name: T, listener: Handler<T>) {

View file

@ -109,6 +109,7 @@ export default class Base {
this.sendMessage({ name: 'queueChange', data: this.queue }); this.sendMessage({ name: 'queueChange', data: this.queue });
if (this.workOnQueue && this.queue.length > 0 && !await this.isDownloading()) { if (this.workOnQueue && this.queue.length > 0 && !await this.isDownloading()) {
this.setDownloading(true); this.setDownloading(true);
this.sendMessage({ name: 'current', data: this.queue[0] });
this.downloadItem(this.queue[0]); this.downloadItem(this.queue[0]);
this.queue = this.queue.slice(1); this.queue = this.queue.slice(1);
this.queueChange(); this.queueChange();
@ -116,6 +117,7 @@ export default class Base {
} }
public async onFinish() { public async onFinish() {
this.sendMessage({ name: 'current', data: undefined });
this.queueChange(); this.queueChange();
} }