feat: download history

This commit is contained in:
Adrian Castro
2024-03-20 19:32:39 +01:00
parent bc9116237f
commit fe93b9a92f
3 changed files with 49 additions and 8 deletions

View File

@@ -1,9 +1,11 @@
import type { ReactNode } from "react";
import React, { createContext, useContext, useState } from "react";
import React, { createContext, useContext, useEffect, useState } from "react";
import * as FileSystem from "expo-file-system";
import * as MediaLibrary from "expo-media-library";
interface DownloadItem {
import { loadDownloadHistory, saveDownloadHistory } from "~/settings";
export interface DownloadItem {
id: string;
filename: string;
progress: number;
@@ -39,6 +41,21 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
}) => {
const [downloads, setDownloads] = useState<DownloadItem[]>([]);
useEffect(() => {
const initializeDownloads = async () => {
const storedDownloads = await loadDownloadHistory();
if (storedDownloads) {
setDownloads(storedDownloads);
}
};
void initializeDownloads();
}, []);
useEffect(() => {
void saveDownloadHistory(downloads.slice(0, 10));
}, [downloads]);
const startDownload = async (url: string, type: "mp4" | "hls") => {
const newDownload: DownloadItem = {
id: `download-${Date.now()}-${Math.random().toString(16).slice(2)}`,