-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/modify download settings (#429)
* [Codegen] Update settings fragment * Extract logic to change a number setting * Show slider for global update interval setting * Add download settings
- Loading branch information
Showing
10 changed files
with
562 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* 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/. | ||
*/ | ||
|
||
import Dialog from '@mui/material/Dialog'; | ||
import DialogContent from '@mui/material/DialogContent'; | ||
import DialogTitle from '@mui/material/DialogTitle'; | ||
import TextField from '@mui/material/TextField'; | ||
import { InputAdornment, ListItemText } from '@mui/material'; | ||
import DialogActions from '@mui/material/DialogActions'; | ||
import Button from '@mui/material/Button'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import ListItemButton from '@mui/material/ListItemButton'; | ||
import * as React from 'react'; | ||
import ListItemIcon from '@mui/material/ListItemIcon'; | ||
import Slider from '@mui/material/Slider'; | ||
|
||
type BaseProps = { | ||
settingTitle: string; | ||
settingValue: string; | ||
settingIcon?: React.ReactNode; | ||
value: number; | ||
defaultValue?: number; | ||
minValue?: number; | ||
maxValue?: number; | ||
stepSize?: number; | ||
dialogTitle: string; | ||
valueUnit: string; | ||
handleUpdate: (value: number) => void; | ||
showSlider?: never; | ||
}; | ||
|
||
type PropsWithSlider = Omit<BaseProps, 'defaultValue' | 'minValue' | 'maxValue' | 'showSlider'> & | ||
Required<Pick<BaseProps, 'defaultValue' | 'minValue' | 'maxValue'>> & { showSlider: true }; | ||
|
||
type Props = BaseProps | PropsWithSlider; | ||
|
||
export const NumberSetting = ({ | ||
settingTitle, | ||
settingValue, | ||
settingIcon, | ||
value, | ||
defaultValue, | ||
minValue, | ||
maxValue, | ||
stepSize, | ||
dialogTitle, | ||
valueUnit, | ||
handleUpdate, | ||
showSlider, | ||
}: Props) => { | ||
const { t } = useTranslation(); | ||
|
||
const [isDialogOpen, setIsDialogOpen] = useState(false); | ||
const [dialogValue, setDialogValue] = useState(value); | ||
|
||
const closeDialog = useCallback( | ||
(resetValue: boolean) => { | ||
setIsDialogOpen(false); | ||
|
||
if (resetValue) { | ||
setDialogValue(value); | ||
} | ||
}, | ||
[value], | ||
); | ||
|
||
const closeDialogWithReset = useCallback(() => closeDialog(true), [closeDialog]); | ||
|
||
const updateSetting = useCallback( | ||
(newValue: number, shouldCloseDialog: boolean = true) => { | ||
if (shouldCloseDialog) { | ||
closeDialog(false); | ||
} | ||
|
||
const didValueChange = value !== newValue; | ||
if (!didValueChange) { | ||
return; | ||
} | ||
|
||
handleUpdate(newValue); | ||
}, | ||
[value, handleUpdate, closeDialog], | ||
); | ||
|
||
useEffect(() => { | ||
setDialogValue(value); | ||
}, [value]); | ||
|
||
return ( | ||
<> | ||
<ListItemButton onClick={() => setIsDialogOpen(true)}> | ||
{settingIcon ? <ListItemIcon>{settingIcon}</ListItemIcon> : null} | ||
<ListItemText | ||
primary={settingTitle} | ||
secondary={settingValue} | ||
secondaryTypographyProps={{ style: { display: 'flex', flexDirection: 'column' } }} | ||
/> | ||
</ListItemButton> | ||
|
||
<Dialog open={isDialogOpen} onClose={closeDialogWithReset}> | ||
<DialogContent> | ||
<DialogTitle sx={{ paddingLeft: 0 }}>{dialogTitle}</DialogTitle> | ||
<TextField | ||
sx={{ | ||
width: '100%', | ||
margin: 'auto', | ||
}} | ||
InputProps={{ | ||
inputProps: { min: minValue, max: maxValue, step: stepSize }, | ||
startAdornment: <InputAdornment position="start">{valueUnit}</InputAdornment>, | ||
}} | ||
autoFocus | ||
value={dialogValue} | ||
type="number" | ||
onChange={(e) => setDialogValue(Number(e.target.value))} | ||
/> | ||
{showSlider ? ( | ||
<Slider | ||
aria-label="number-setting-slider" | ||
defaultValue={defaultValue} | ||
value={dialogValue} | ||
step={stepSize} | ||
min={minValue} | ||
max={maxValue} | ||
onChange={(_, newValue) => setDialogValue(newValue as number)} | ||
/> | ||
) : null} | ||
</DialogContent> | ||
<DialogActions> | ||
{defaultValue !== undefined ? ( | ||
<Button | ||
onClick={() => { | ||
setDialogValue(defaultValue); | ||
updateSetting(defaultValue, false); | ||
}} | ||
color="primary" | ||
> | ||
{t('global.button.reset_to_default')} | ||
</Button> | ||
) : null} | ||
<Button onClick={closeDialogWithReset} color="primary"> | ||
{t('global.button.cancel')} | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
updateSetting(dialogValue); | ||
}} | ||
color="primary" | ||
> | ||
{t('global.button.ok')} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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/. | ||
*/ | ||
|
||
import { Button, Dialog, DialogTitle, ListItemText } from '@mui/material'; | ||
import DialogContent from '@mui/material/DialogContent'; | ||
import TextField from '@mui/material/TextField'; | ||
import DialogActions from '@mui/material/DialogActions'; | ||
import ListItemButton from '@mui/material/ListItemButton'; | ||
import { useEffect, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import DialogContentText from '@mui/material/DialogContentText'; | ||
|
||
export const ServerDirSetting = ({ | ||
settingName, | ||
dialogDescription, | ||
dirPath, | ||
handlePathChange, | ||
}: { | ||
settingName: string; | ||
dialogDescription: string; | ||
dirPath?: string; | ||
handlePathChange: (path: string) => void; | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
const [isDialogOpen, setIsDialogOpen] = useState(false); | ||
const [dialogDirPath, setDialogDirPath] = useState(dirPath ?? ''); | ||
|
||
useEffect(() => { | ||
if (!dirPath) { | ||
return; | ||
} | ||
|
||
setDialogDirPath(dirPath); | ||
}, [dirPath]); | ||
|
||
const closeDialog = () => { | ||
setIsDialogOpen(false); | ||
}; | ||
|
||
const updateSetting = () => { | ||
closeDialog(); | ||
handlePathChange(dialogDirPath); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ListItemButton onClick={() => setIsDialogOpen(true)}> | ||
<ListItemText | ||
primary={settingName} | ||
secondary={dirPath ?? t('global.label.loading')} | ||
secondaryTypographyProps={{ style: { display: 'flex', flexDirection: 'column' } }} | ||
/> | ||
</ListItemButton> | ||
|
||
<Dialog open={isDialogOpen} onClose={closeDialog} fullWidth> | ||
<DialogContent> | ||
<DialogTitle sx={{ paddingLeft: 0 }}>{settingName}</DialogTitle> | ||
<DialogContentText sx={{ paddingBottom: '10px' }}>{dialogDescription}</DialogContentText> | ||
<TextField | ||
sx={{ | ||
width: '100%', | ||
margin: 'auto', | ||
}} | ||
autoFocus | ||
value={dialogDirPath} | ||
type="text" | ||
onChange={(e) => setDialogDirPath(e.target.value)} | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={closeDialog} color="primary"> | ||
{t('global.button.cancel')} | ||
</Button> | ||
<Button onClick={() => updateSetting()} color="primary"> | ||
{t('global.button.ok')} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</> | ||
); | ||
}; |
68 changes: 68 additions & 0 deletions
68
src/components/settings/downloads/DownloadAheadSetting.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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/. | ||
*/ | ||
|
||
import { useTranslation } from 'react-i18next'; | ||
import { List, ListItem, ListItemText, Switch } from '@mui/material'; | ||
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction'; | ||
import { useCallback } from 'react'; | ||
import { requestManager } from '@/lib/requests/RequestManager.ts'; | ||
import { NumberSetting } from '@/components/settings/NumberSetting.tsx'; | ||
|
||
const DEFAULT_LIMIT = 5; | ||
const MIN_LIMIT = 2; | ||
const MAX_LIMIT = 10; | ||
|
||
export const DownloadAheadSetting = () => { | ||
const { t } = useTranslation(); | ||
|
||
const { data } = requestManager.useGetServerSettings(); | ||
const downloadAheadLimit = data?.settings.autoDownloadAheadLimit ?? 0; | ||
const shouldDownloadAhead = !!downloadAheadLimit; | ||
const [mutateSettings] = requestManager.useUpdateServerSettings(); | ||
|
||
const updateSetting = useCallback((autoDownloadAheadLimit: number) => { | ||
mutateSettings({ variables: { input: { settings: { autoDownloadAheadLimit } } } }); | ||
}, []); | ||
|
||
const setDoAutoUpdates = (enable: boolean) => { | ||
const globalUpdateInterval = enable ? DEFAULT_LIMIT : 0; | ||
updateSetting(globalUpdateInterval); | ||
}; | ||
|
||
return ( | ||
<List> | ||
<ListItem> | ||
<ListItemText primary={t('download.settings.download_ahead.label.while_reading')} /> | ||
<ListItemSecondaryAction> | ||
<Switch | ||
edge="end" | ||
checked={shouldDownloadAhead} | ||
onChange={(e) => setDoAutoUpdates(e.target.checked)} | ||
/> | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
{shouldDownloadAhead ? ( | ||
<NumberSetting | ||
settingTitle={t('download.settings.download_ahead.label.unread_chapters_to_download')} | ||
settingValue={t('download.settings.download_ahead.label.value', { | ||
chapters: downloadAheadLimit, | ||
count: downloadAheadLimit, | ||
})} | ||
value={downloadAheadLimit} | ||
minValue={MIN_LIMIT} | ||
maxValue={MAX_LIMIT} | ||
defaultValue={DEFAULT_LIMIT} | ||
showSlider | ||
dialogTitle={t('download.settings.download_ahead.label.unread_chapters_to_download')} | ||
valueUnit={t('chapter.title')} | ||
handleUpdate={updateSetting} | ||
/> | ||
) : null} | ||
</List> | ||
); | ||
}; |
Oops, something went wrong.