Fix: add check multiplicable tasks & repace download queue by task queue

This commit is contained in:
nyagami
2024-07-14 00:42:52 +07:00
parent 03e3816ab8
commit ea5fde4156
6 changed files with 77 additions and 50 deletions
+3 -3
View File
@@ -112,9 +112,9 @@ function NovelCover<TNovel extends CoverItemLibrary | CoverItemPlugin>({
},
styles.standardNovelCover,
isSelected && {
backgroundColor: theme.primary,
} &&
styles.selectedNovelCover,
backgroundColor: theme.primary,
...styles.selectedNovelCover,
},
]}
>
<Pressable
+2 -2
View File
@@ -10,7 +10,7 @@ import ReaderSettings from '../screens/settings/SettingsReaderScreen/SettingsRea
import BackupSettings from '../screens/settings/SettingsBackupScreen';
import AdvancedSettings from '../screens/settings/SettingsAdvancedScreen';
import GeneralSettings from '../screens/settings/SettingsGeneralScreen/SettingsGeneralScreen';
import DownloadQueue from '../screens/more/DownloadQueueScreen';
import TaskQueue from '../screens/more/TaskQueueScreen';
import Downloads from '../screens/more/DownloadsScreen';
import AppearanceSettings from '../screens/settings/SettingsAppearanceScreen';
import CategoriesScreen from '@screens/Categories/CategoriesScreen';
@@ -43,7 +43,7 @@ const MoreStack = () => (
<Stack.Navigator screenOptions={stackNavigatorConfig}>
<Stack.Screen name="SettingsStack" component={SettingsStack} />
<Stack.Screen name="About" component={About} />
<Stack.Screen name="DownloadQueue" component={DownloadQueue} />
<Stack.Screen name="TaskQueue" component={TaskQueue} />
<Stack.Screen name="Downloads" component={Downloads} />
<Stack.Screen name="Categories" component={CategoriesScreen} />
<Stack.Screen name="Statistics" component={StatsScreen} />
+3 -3
View File
@@ -65,7 +65,7 @@ export type MoreStackScreenProps = CompositeScreenProps<
export type MoreStackParamList = {
SettingsStack: NavigatorScreenParams<SettingsStackParamList>;
About: undefined;
DownloadQueue: undefined;
TaskQueue: undefined;
Downloads: undefined;
Categories: undefined;
Statistics: undefined;
@@ -94,9 +94,9 @@ export type DownloadsScreenProps = StackScreenProps<
MoreStackParamList,
'Downloads'
>;
export type DownloadQueueScreenProps = StackScreenProps<
export type TaskQueueScreenProps = StackScreenProps<
MoreStackParamList,
'DownloadQueue'
'TaskQueue'
>;
export type BrowseSourceScreenProps = StackScreenProps<
RootStackParamList,
+11 -5
View File
@@ -5,13 +5,17 @@ import { getString } from '@strings/translations';
import { List } from '@components';
import { MoreHeader } from './components/MoreHeader';
import { useDownload, useLibrarySettings, useTheme } from '@hooks/persisted';
import { useLibrarySettings, useTheme } from '@hooks/persisted';
import { MoreStackScreenProps } from '@navigators/types';
import Switch from '@components/Switch/Switch';
import { useMMKVObject } from 'react-native-mmkv';
import ServiceManager, { BackgroundTask } from '@services/ServiceManager';
const MoreScreen = ({ navigation }: MoreStackScreenProps) => {
const theme = useTheme();
const { downloadQueue } = useDownload();
const [taskQueue] = useMMKVObject<BackgroundTask[]>(
ServiceManager.manager.STORE_KEY,
);
const {
incognitoMode = false,
downloadedOnlyMode = false,
@@ -107,14 +111,16 @@ const MoreScreen = ({ navigation }: MoreStackScreenProps) => {
</Pressable>
<List.Divider theme={theme} />
<List.Item
title={getString('moreScreen.downloadQueue')}
title={'Task Queue'}
description={
downloadQueue.length > 0 ? downloadQueue.length + ' remaining' : ''
taskQueue && taskQueue.length > 0
? taskQueue.length + ' remaining'
: ''
}
icon="progress-download"
onPress={() =>
navigation.navigate('MoreStack', {
screen: 'DownloadQueue',
screen: 'TaskQueue',
})
}
theme={theme}
@@ -8,36 +8,36 @@ import {
overlay,
} from 'react-native-paper';
import { useDownload, useTheme } from '@hooks/persisted';
import { useTheme } from '@hooks/persisted';
import { showToast } from '../../utils/showToast';
import { getString } from '@strings/translations';
import { Appbar, EmptyView } from '@components';
import { DownloadQueueScreenProps } from '@navigators/types';
import ServiceManager from '@services/ServiceManager';
import { TaskQueueScreenProps } from '@navigators/types';
import ServiceManager, { BackgroundTask } from '@services/ServiceManager';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useMMKVObject } from 'react-native-mmkv';
const DownloadQueue = ({ navigation }: DownloadQueueScreenProps) => {
const DownloadQueue = ({ navigation }: TaskQueueScreenProps) => {
const theme = useTheme();
const { bottom } = useSafeAreaInsets();
const { downloadQueue, resumeDowndload, pauseDownload, cancelDownload } =
useDownload();
const [isDownloading, setIsDownloading] = useState(
ServiceManager.manager.isRunning,
const [taskQueue] = useMMKVObject<BackgroundTask[]>(
ServiceManager.manager.STORE_KEY,
);
const [isRunning, setIsRunning] = useState(ServiceManager.manager.isRunning);
const [visible, setVisible] = useState(false);
const openMenu = () => setVisible(true);
const closeMenu = () => setVisible(false);
useEffect(() => {
if (downloadQueue.length === 0) {
setIsDownloading(false);
if (taskQueue?.length === 0) {
setIsRunning(false);
}
}, [downloadQueue]);
}, [taskQueue]);
return (
<>
<Appbar
title={getString('moreScreen.downloadQueue')}
title={'Task Queue'}
handleGoBack={navigation.goBack}
theme={theme}
>
@@ -45,7 +45,7 @@ const DownloadQueue = ({ navigation }: DownloadQueueScreenProps) => {
visible={visible}
onDismiss={closeMenu}
anchor={
downloadQueue.length ? (
taskQueue?.length ? (
<MaterialAppbar.Action
icon="dots-vertical"
iconColor={theme.onSurface}
@@ -57,8 +57,8 @@ const DownloadQueue = ({ navigation }: DownloadQueueScreenProps) => {
>
<Menu.Item
onPress={() => {
cancelDownload();
setIsDownloading(false);
ServiceManager.manager.stop();
setIsRunning(false);
showToast(getString('downloadScreen.cancelled'));
closeMenu();
}}
@@ -69,19 +69,15 @@ const DownloadQueue = ({ navigation }: DownloadQueueScreenProps) => {
</Appbar>
<FlatList
contentContainerStyle={{ flexGrow: 1, paddingBottom: 100 }}
keyExtractor={item => 'download_chapter_' + item.data.chapterId}
data={downloadQueue}
renderItem={({ item }) => (
keyExtractor={(item, index) => 'task_' + index}
data={taskQueue || []}
renderItem={({ item, index }) => (
<View style={{ padding: 16 }}>
<Text style={{ color: theme.onSurface }}>
{item.data.chapterName}
{item.name} - {ServiceManager.manager.getTaskDescription(item)}
</Text>
<ProgressBar
indeterminate={
isDownloading &&
downloadQueue.length > 0 &&
downloadQueue[0].data.chapterId === item.data.chapterId
}
indeterminate={taskQueue && taskQueue.length > 0 && index === 0}
color={theme.primary}
style={{ marginTop: 8 }}
/>
@@ -90,29 +86,27 @@ const DownloadQueue = ({ navigation }: DownloadQueueScreenProps) => {
ListEmptyComponent={
<EmptyView
icon="(・o・;)"
description={getString('downloadScreen.noDownloads')}
description={'No running tasks'}
theme={theme}
/>
}
/>
{downloadQueue.length > 0 ? (
{taskQueue && taskQueue.length > 0 ? (
<FAB
style={[styles.fab, { backgroundColor: theme.primary, bottom }]}
color={theme.onPrimary}
label={
isDownloading
? getString('common.pause')
: getString('common.resume')
isRunning ? getString('common.pause') : getString('common.resume')
}
uppercase={false}
icon={isDownloading ? 'pause' : 'play'}
icon={isRunning ? 'pause' : 'play'}
onPress={() => {
if (isDownloading) {
pauseDownload();
setIsDownloading(false);
if (isRunning) {
ServiceManager.manager.pause();
setIsRunning(false);
} else {
resumeDowndload();
setIsDownloading(true);
ServiceManager.manager.resume();
setIsRunning(true);
}
}}
/>
+29 -2
View File
@@ -50,6 +50,13 @@ export default class ServiceManager {
get isRunning() {
return BackgroundService.isRunning();
}
isMultiplicableTask(task: BackgroundTask) {
return (
['DOWNLOAD_CHAPTER', 'IMPORT_EPUB', 'MIGRATE_NOVEL'] as Array<
BackgroundTask['name']
>
).includes(task.name);
}
start() {
if (!this.isRunning) {
BackgroundService.start(ServiceManager.lauch, {
@@ -141,12 +148,32 @@ export default class ServiceManager {
});
}
}
getTaskDescription(task: BackgroundTask) {
switch (task.name) {
case 'DOWNLOAD_CHAPTER':
return task.data.chapterName;
case 'IMPORT_EPUB':
return task.data.filename;
case 'MIGRATE_NOVEL':
return task.data.fromNovel.name;
default:
return task.data?.toString() || 'No data';
}
}
getTaskList() {
return getMMKVObject<Array<BackgroundTask>>(this.STORE_KEY) || [];
}
addTask(tasks: BackgroundTask | BackgroundTask[]) {
setMMKVObject(this.STORE_KEY, this.getTaskList().concat(tasks));
this.start();
const currentTasks = this.getTaskList();
const addableTasks = (Array.isArray(tasks) ? tasks : [tasks]).filter(
task =>
this.isMultiplicableTask(task) ||
!currentTasks.some(_t => _t.name === task.name),
);
if (addableTasks.length) {
setMMKVObject(this.STORE_KEY, currentTasks.concat(addableTasks));
this.start();
}
}
removeTasksByName(name: BackgroundTask['name']) {
const taskList = this.getTaskList();