From fe93b9a92f2aa3d42a5d266a1304c223e69cc3ba Mon Sep 17 00:00:00 2001 From: Adrian Castro <22133246+castdrian@users.noreply.github.com> Date: Wed, 20 Mar 2024 19:32:39 +0100 Subject: [PATCH] feat: download history --- apps/expo/src/components/item/item.tsx | 12 +++++----- .../expo/src/hooks/DownloadManagerContext.tsx | 21 ++++++++++++++-- apps/expo/src/settings/index.ts | 24 +++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/apps/expo/src/components/item/item.tsx b/apps/expo/src/components/item/item.tsx index 380f0c9..0b1cd53 100644 --- a/apps/expo/src/components/item/item.tsx +++ b/apps/expo/src/components/item/item.tsx @@ -5,7 +5,7 @@ import ContextMenu from "react-native-context-menu-view"; import { useRouter } from "expo-router"; import { Image, Text, View } from "tamagui"; -// import { useDownloadManager } from "~/hooks/DownloadManagerContext"; +import { useDownloadManager } from "~/hooks/DownloadManagerContext"; import { usePlayerStore } from "~/stores/player/store"; export interface ItemData { @@ -19,7 +19,7 @@ export interface ItemData { export default function Item({ data }: { data: ItemData }) { const resetVideo = usePlayerStore((state) => state.resetVideo); const router = useRouter(); - // const { startDownload } = useDownloadManager(); + const { startDownload } = useDownloadManager(); const { title, type, year, posterUrl } = data; @@ -41,10 +41,10 @@ export default function Item({ data }: { data: ItemData }) { e: NativeSyntheticEvent, ) => { console.log(e.nativeEvent.name); - // startDownload( - // "https://samplelib.com/lib/preview/mp4/sample-5s.mp4", - // "mp4", - // ).catch(console.error); + startDownload( + "https://samplelib.com/lib/preview/mp4/sample-5s.mp4", + "mp4", + ).catch(console.error); }; return ( diff --git a/apps/expo/src/hooks/DownloadManagerContext.tsx b/apps/expo/src/hooks/DownloadManagerContext.tsx index 083d066..d15318d 100644 --- a/apps/expo/src/hooks/DownloadManagerContext.tsx +++ b/apps/expo/src/hooks/DownloadManagerContext.tsx @@ -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([]); + 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)}`, diff --git a/apps/expo/src/settings/index.ts b/apps/expo/src/settings/index.ts index 5e62240..4beac57 100644 --- a/apps/expo/src/settings/index.ts +++ b/apps/expo/src/settings/index.ts @@ -1,5 +1,6 @@ import AsyncStorage from "@react-native-async-storage/async-storage"; +import type { DownloadItem } from "~/hooks/DownloadManagerContext"; import type { ThemeStoreOption } from "~/stores/theme"; interface ThemeSettings { @@ -31,3 +32,26 @@ export const saveTheme = async (newTheme: ThemeStoreOption) => { settings.themes.theme = newTheme; await saveSettings(settings); }; + +interface DownloadHistory { + downloads: DownloadItem[]; +} + +const downloadHistoryKey = "downloadHistory"; + +export const saveDownloadHistory = async (downloads: DownloadItem[]) => { + const json = await AsyncStorage.getItem(downloadHistoryKey); + const settings = json + ? (JSON.parse(json) as DownloadHistory) + : { downloads: [] }; + settings.downloads = downloads; + await AsyncStorage.setItem(downloadHistoryKey, JSON.stringify(settings)); +}; + +export const loadDownloadHistory = async (): Promise => { + const json = await AsyncStorage.getItem(downloadHistoryKey); + const settings = json + ? (JSON.parse(json) as DownloadHistory) + : { downloads: [] }; + return settings.downloads; +};