diff --git a/App.tsx b/App.tsx index 5fcb68047..cf96a91a1 100644 --- a/App.tsx +++ b/App.tsx @@ -10,6 +10,7 @@ import LottieSplashScreen from 'react-native-lottie-splash-screen'; import { SafeAreaProvider } from 'react-native-safe-area-context'; import { Provider as PaperProvider } from 'react-native-paper'; import * as Notifications from 'expo-notifications'; +import BackgroundService from 'react-native-background-actions'; import { createTables } from '@database/db'; import AppErrorBoundary from '@components/AppErrorBoundary/AppErrorBoundary'; @@ -18,6 +19,8 @@ import { collectPlugins } from '@plugins/pluginManager'; import Main from './src/navigators/Main'; import { BottomSheetModalProvider } from '@gorhom/bottom-sheet'; import { usePlugins } from '@hooks/persisted'; +import { MMKVStorage } from '@utils/mmkv/mmkv'; +import { BACKGROUND_ACTION } from '@services/constants'; Notifications.setNotificationHandler({ handleNotification: async () => { @@ -35,6 +38,9 @@ const App = () => { createTables(); collectPlugins().then(() => LottieSplashScreen.hide()); refreshPlugins(); + if (!BackgroundService.isRunning()) { + MMKVStorage.delete(BACKGROUND_ACTION); + } }, []); return ( diff --git a/src/hooks/persisted/useDownload.ts b/src/hooks/persisted/useDownload.ts index f885a7719..b6090424b 100644 --- a/src/hooks/persisted/useDownload.ts +++ b/src/hooks/persisted/useDownload.ts @@ -53,6 +53,7 @@ const downloadChapterAction = async (taskData?: TaskData) => { } } } finally { + MMKVStorage.delete(BACKGROUND_ACTION); await Notifications.scheduleNotificationAsync({ content: { title: 'Downloader', @@ -60,7 +61,6 @@ const downloadChapterAction = async (taskData?: TaskData) => { }, trigger: null, }); - MMKVStorage.delete(BACKGROUND_ACTION); BackgroundService.stop(); } }; diff --git a/src/services/backup/drive/index.ts b/src/services/backup/drive/index.ts index 46cb068f4..660055392 100644 --- a/src/services/backup/drive/index.ts +++ b/src/services/backup/drive/index.ts @@ -87,10 +87,10 @@ const driveBackupAction = async (taskData?: TaskData) => { }, trigger: null, }); - await BackgroundService.stop(); } } finally { MMKVStorage.delete(BACKGROUND_ACTION); + await BackgroundService.stop(); } }; @@ -172,10 +172,10 @@ const driveRestoreAction = async (taskData?: TaskData) => { }, trigger: null, }); - await BackgroundService.stop(); } } finally { MMKVStorage.delete(BACKGROUND_ACTION); + await BackgroundService.stop(); } }; diff --git a/src/services/backup/legacy/index.ts b/src/services/backup/legacy/index.ts index 5e83080b2..e2790c436 100644 --- a/src/services/backup/legacy/index.ts +++ b/src/services/backup/legacy/index.ts @@ -50,6 +50,7 @@ export const createBackup = async () => { showToast(error.message); } finally { MMKVStorage.delete(BACKGROUND_ACTION); + BackgroundService.stop(); } }; @@ -122,6 +123,7 @@ export const restoreBackup = async (filePath?: string) => { }, trigger: null, }); + MMKVStorage.delete(BACKGROUND_ACTION); resolve(); } @@ -170,8 +172,6 @@ export const restoreBackup = async (filePath?: string) => { } } catch (error: any) { showToast(error.message); - } finally { - MMKVStorage.delete(BACKGROUND_ACTION); } }; diff --git a/src/services/migrate/migrateNovel.ts b/src/services/migrate/migrateNovel.ts index 7b3aa9c44..306fcae0d 100644 --- a/src/services/migrate/migrateNovel.ts +++ b/src/services/migrate/migrateNovel.ts @@ -225,6 +225,7 @@ export const migrateNovel = async ( } } finally { MMKVStorage.delete(BACKGROUND_ACTION); + BackgroundService.stop(); } }; await BackgroundService.start(veryIntensiveTask, options); diff --git a/src/services/updates/index.ts b/src/services/updates/index.ts index 88bbe69ff..0bc5475e8 100644 --- a/src/services/updates/index.ts +++ b/src/services/updates/index.ts @@ -109,6 +109,7 @@ const updateLibrary = async (categoryId?: number) => { } } finally { MMKVStorage.delete(BACKGROUND_ACTION); + BackgroundService.stop(); } };