2021-09-13 19:18:21 +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-09-13 19:18:21 +04:30
|
|
|
|
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import ListItem from '@mui/material/ListItem';
|
|
|
|
|
import ListItemText from '@mui/material/ListItemText';
|
|
|
|
|
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
|
|
|
|
|
import Switch from '@mui/material/Switch';
|
|
|
|
|
import Checkbox from '@mui/material/Checkbox';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { CheckBoxPreferenceProps, SwitchPreferenceCompatProps, TwoStatePreferenceProps } from 'typings';
|
2021-09-13 19:18:21 +04:30
|
|
|
|
|
|
|
|
function getTwoStateType(type: 'Checkbox' | 'Switch') {
|
2023-02-06 10:06:33 +01:00
|
|
|
if (type === 'Switch') {
|
|
|
|
|
return Switch;
|
|
|
|
|
}
|
2021-09-13 19:18:21 +04:30
|
|
|
return Checkbox;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function TwoSatePreference(props: TwoStatePreferenceProps) {
|
2023-02-06 10:06:33 +01:00
|
|
|
const { title, summary, currentValue, updateValue, type } = props;
|
2021-09-13 19:18:21 +04:30
|
|
|
const [internalCurrentValue, setInternalCurrentValue] = useState<boolean>(currentValue);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setInternalCurrentValue(currentValue);
|
|
|
|
|
}, [currentValue]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ListItem>
|
|
|
|
|
<ListItemText primary={title} secondary={summary} />
|
|
|
|
|
<ListItemSecondaryAction>
|
2023-02-06 10:06:33 +01:00
|
|
|
{React.createElement(getTwoStateType(type), {
|
|
|
|
|
edge: 'end',
|
|
|
|
|
checked: internalCurrentValue,
|
|
|
|
|
onChange: () => {
|
|
|
|
|
updateValue(!currentValue);
|
|
|
|
|
|
|
|
|
|
// appear smooth
|
|
|
|
|
setInternalCurrentValue(!currentValue);
|
|
|
|
|
},
|
|
|
|
|
})}
|
2021-09-13 19:18:21 +04:30
|
|
|
</ListItemSecondaryAction>
|
|
|
|
|
</ListItem>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function CheckBoxPreference(props: CheckBoxPreferenceProps) {
|
|
|
|
|
return <TwoSatePreference {...props} type="Checkbox" />;
|
|
|
|
|
}
|
2023-02-06 10:06:33 +01:00
|
|
|
|
2021-09-13 19:18:21 +04:30
|
|
|
export function SwitchPreferenceCompat(props: SwitchPreferenceCompatProps) {
|
|
|
|
|
return <TwoSatePreference {...props} type="Switch" />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default { CheckBoxPreference, SwitchPreferenceCompat };
|