mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
feat: background tasks ios setup
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import type { DownloadTask } from "@kesha-antonov/react-native-background-downloader";
|
import type { DownloadTask } from "@kesha-antonov/react-native-background-downloader";
|
||||||
import type { Asset } from "expo-media-library";
|
import type { Asset } from "expo-media-library";
|
||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
import React, { createContext, useContext, useEffect, useState } from "react";
|
import React, { createContext, useCallback, useContext, useEffect, useState } from "react";
|
||||||
import * as FileSystem from "expo-file-system";
|
import * as FileSystem from "expo-file-system";
|
||||||
import * as MediaLibrary from "expo-media-library";
|
import * as MediaLibrary from "expo-media-library";
|
||||||
import {
|
import {
|
||||||
@@ -75,9 +75,9 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initializeDownloads = () => {
|
const initializeDownloads = () => {
|
||||||
const { downloads: storedDownloads } = useDownloadHistoryStore.getState();
|
const { downloads } = useDownloadHistoryStore.getState();
|
||||||
if (storedDownloads) {
|
if (downloads) {
|
||||||
setDownloads(storedDownloads);
|
setDownloads(downloads);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -88,6 +88,27 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
useDownloadHistoryStore.setState({ downloads });
|
useDownloadHistoryStore.setState({ downloads });
|
||||||
}, [downloads]);
|
}, [downloads]);
|
||||||
|
|
||||||
|
const checkRunningTasks = useCallback(async () => {
|
||||||
|
const existingTasks = await checkForExistingDownloads();
|
||||||
|
existingTasks.forEach((task) => {
|
||||||
|
task
|
||||||
|
.progress(({ bytesDownloaded, bytesTotal }) => {
|
||||||
|
const progress = bytesDownloaded / bytesTotal;
|
||||||
|
updateDownloadItem(task.id, { progress });
|
||||||
|
})
|
||||||
|
.done(() => {
|
||||||
|
completeHandler(task.id);
|
||||||
|
})
|
||||||
|
.error(({ error, errorCode }) => {
|
||||||
|
console.error(`Download error: ${errorCode} - ${error}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
void checkRunningTasks();
|
||||||
|
}, [checkRunningTasks]);
|
||||||
|
|
||||||
const cancellationFlags = useState<Record<string, boolean>>({})[0];
|
const cancellationFlags = useState<Record<string, boolean>>({})[0];
|
||||||
|
|
||||||
const setCancellationFlag = (downloadId: string, flag: boolean): void => {
|
const setCancellationFlag = (downloadId: string, flag: boolean): void => {
|
||||||
@@ -111,35 +132,6 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// const initializeDownloader = async () => {
|
|
||||||
// setConfig({ isLogsEnabled: true }); // Set any global configs here
|
|
||||||
|
|
||||||
// const existingTasks = await checkForExistingDownloads();
|
|
||||||
// existingTasks.forEach(task => {
|
|
||||||
// // Reattach event listeners to existing tasks
|
|
||||||
// processTask(task);
|
|
||||||
// });
|
|
||||||
// };
|
|
||||||
|
|
||||||
const _processTask = (task: DownloadTask) => {
|
|
||||||
task
|
|
||||||
.progress(({ bytesDownloaded, bytesTotal }) => {
|
|
||||||
const progress = bytesDownloaded / bytesTotal;
|
|
||||||
updateDownloadItem(task.id, { progress });
|
|
||||||
})
|
|
||||||
.done(() => {
|
|
||||||
completeHandler(task.id);
|
|
||||||
})
|
|
||||||
.error(({ error, errorCode }) => {
|
|
||||||
console.error(`Download error: ${errorCode} - ${error}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
const downloadItem = downloads.find((d) => d.id === task.id);
|
|
||||||
if (downloadItem) {
|
|
||||||
updateDownloadItem(task.id, { downloadTask: task });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const startDownload = async (
|
const startDownload = async (
|
||||||
url: string,
|
url: string,
|
||||||
type: "mp4" | "hls",
|
type: "mp4" | "hls",
|
||||||
|
Reference in New Issue
Block a user