mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 14:23:28 +00:00
feat: download history
This commit is contained in:
@@ -5,7 +5,7 @@ import ContextMenu from "react-native-context-menu-view";
|
|||||||
import { useRouter } from "expo-router";
|
import { useRouter } from "expo-router";
|
||||||
import { Image, Text, View } from "tamagui";
|
import { Image, Text, View } from "tamagui";
|
||||||
|
|
||||||
// import { useDownloadManager } from "~/hooks/DownloadManagerContext";
|
import { useDownloadManager } from "~/hooks/DownloadManagerContext";
|
||||||
import { usePlayerStore } from "~/stores/player/store";
|
import { usePlayerStore } from "~/stores/player/store";
|
||||||
|
|
||||||
export interface ItemData {
|
export interface ItemData {
|
||||||
@@ -19,7 +19,7 @@ export interface ItemData {
|
|||||||
export default function Item({ data }: { data: ItemData }) {
|
export default function Item({ data }: { data: ItemData }) {
|
||||||
const resetVideo = usePlayerStore((state) => state.resetVideo);
|
const resetVideo = usePlayerStore((state) => state.resetVideo);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
// const { startDownload } = useDownloadManager();
|
const { startDownload } = useDownloadManager();
|
||||||
|
|
||||||
const { title, type, year, posterUrl } = data;
|
const { title, type, year, posterUrl } = data;
|
||||||
|
|
||||||
@@ -41,10 +41,10 @@ export default function Item({ data }: { data: ItemData }) {
|
|||||||
e: NativeSyntheticEvent<ContextMenuOnPressNativeEvent>,
|
e: NativeSyntheticEvent<ContextMenuOnPressNativeEvent>,
|
||||||
) => {
|
) => {
|
||||||
console.log(e.nativeEvent.name);
|
console.log(e.nativeEvent.name);
|
||||||
// startDownload(
|
startDownload(
|
||||||
// "https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
|
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
|
||||||
// "mp4",
|
"mp4",
|
||||||
// ).catch(console.error);
|
).catch(console.error);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
import type { ReactNode } from "react";
|
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 FileSystem from "expo-file-system";
|
||||||
import * as MediaLibrary from "expo-media-library";
|
import * as MediaLibrary from "expo-media-library";
|
||||||
|
|
||||||
interface DownloadItem {
|
import { loadDownloadHistory, saveDownloadHistory } from "~/settings";
|
||||||
|
|
||||||
|
export interface DownloadItem {
|
||||||
id: string;
|
id: string;
|
||||||
filename: string;
|
filename: string;
|
||||||
progress: number;
|
progress: number;
|
||||||
@@ -39,6 +41,21 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const [downloads, setDownloads] = useState<DownloadItem[]>([]);
|
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 startDownload = async (url: string, type: "mp4" | "hls") => {
|
||||||
const newDownload: DownloadItem = {
|
const newDownload: DownloadItem = {
|
||||||
id: `download-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
id: `download-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
|
|
||||||
|
import type { DownloadItem } from "~/hooks/DownloadManagerContext";
|
||||||
import type { ThemeStoreOption } from "~/stores/theme";
|
import type { ThemeStoreOption } from "~/stores/theme";
|
||||||
|
|
||||||
interface ThemeSettings {
|
interface ThemeSettings {
|
||||||
@@ -31,3 +32,26 @@ export const saveTheme = async (newTheme: ThemeStoreOption) => {
|
|||||||
settings.themes.theme = newTheme;
|
settings.themes.theme = newTheme;
|
||||||
await saveSettings(settings);
|
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<DownloadItem[]> => {
|
||||||
|
const json = await AsyncStorage.getItem(downloadHistoryKey);
|
||||||
|
const settings = json
|
||||||
|
? (JSON.parse(json) as DownloadHistory)
|
||||||
|
: { downloads: [] };
|
||||||
|
return settings.downloads;
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user