feat: remove download history items on long press

This commit is contained in:
Adrian Castro
2024-03-20 20:07:23 +01:00
parent fe93b9a92f
commit 66344d552b
3 changed files with 49 additions and 33 deletions

View File

@@ -6,13 +6,13 @@ import ScreenLayout from "~/components/layout/ScreenLayout";
import { useDownloadManager } from "~/hooks/DownloadManagerContext"; import { useDownloadManager } from "~/hooks/DownloadManagerContext";
const DownloadsScreen: React.FC = () => { const DownloadsScreen: React.FC = () => {
const { downloads } = useDownloadManager(); const { downloads, removeDownload } = useDownloadManager();
return ( return (
<ScreenLayout title="Downloads"> <ScreenLayout title="Downloads">
<ScrollView> <ScrollView>
{downloads.map((item) => ( {downloads.map((item) => (
<DownloadItem key={item.id} {...item} /> <DownloadItem key={item.id} {...item} onLongPress={removeDownload} />
))} ))}
</ScrollView> </ScrollView>
</ScreenLayout> </ScreenLayout>

View File

@@ -1,13 +1,16 @@
import React from "react"; import React from "react";
import { TouchableOpacity } from "react-native-gesture-handler";
import { Progress, Text, View } from "tamagui"; import { Progress, Text, View } from "tamagui";
export interface DownloadItemProps { export interface DownloadItemProps {
id: string;
filename: string; filename: string;
progress: number; progress: number;
speed: number; speed: number;
fileSize: number; fileSize: number;
downloaded: number; downloaded: number;
isFinished: boolean; isFinished: boolean;
onLongPress: (id: string) => void;
} }
const formatBytes = (bytes: number, decimals = 2) => { const formatBytes = (bytes: number, decimals = 2) => {
@@ -20,51 +23,55 @@ const formatBytes = (bytes: number, decimals = 2) => {
}; };
export const DownloadItem: React.FC<DownloadItemProps> = ({ export const DownloadItem: React.FC<DownloadItemProps> = ({
id,
filename, filename,
progress, progress,
speed, speed,
fileSize, fileSize,
downloaded, downloaded,
isFinished, isFinished,
onLongPress,
}) => { }) => {
const percentage = progress * 100; const percentage = progress * 100;
const formattedFileSize = formatBytes(fileSize); const formattedFileSize = formatBytes(fileSize);
const formattedDownloaded = formatBytes(downloaded); const formattedDownloaded = formatBytes(downloaded);
return ( return (
<View marginBottom={16} borderRadius={8} borderColor="white" padding={16}> <TouchableOpacity onLongPress={() => onLongPress(id)} activeOpacity={0.7}>
<Text marginBottom={4} fontSize={16}> <View marginBottom={16} borderRadius={8} borderColor="white" padding={16}>
{filename} <Text marginBottom={4} fontSize={16}>
</Text> {filename}
<Progress
value={percentage}
height={10}
backgroundColor="$progressBackground"
>
<Progress.Indicator
animation="bounce"
backgroundColor="$progressFilled"
/>
</Progress>
<View
marginTop={8}
flexDirection="row"
alignItems="center"
justifyContent="space-between"
>
<Text fontSize={12} color="gray">
{percentage}% - {formattedDownloaded} of {formattedFileSize}
</Text> </Text>
{isFinished ? ( <Progress
value={percentage}
height={10}
backgroundColor="$progressBackground"
>
<Progress.Indicator
animation="bounce"
backgroundColor="$progressFilled"
/>
</Progress>
<View
marginTop={8}
flexDirection="row"
alignItems="center"
justifyContent="space-between"
>
<Text fontSize={12} color="gray"> <Text fontSize={12} color="gray">
Finished {percentage}% - {formattedDownloaded} of {formattedFileSize}
</Text> </Text>
) : ( {isFinished ? (
<Text fontSize={12} color="gray"> <Text fontSize={12} color="gray">
{speed.toFixed(2)} MB/s Finished
</Text> </Text>
)} ) : (
<Text fontSize={12} color="gray">
{speed.toFixed(2)} MB/s
</Text>
)}
</View>
</View> </View>
</View> </TouchableOpacity>
); );
}; };

View File

@@ -20,6 +20,7 @@ export interface DownloadItem {
interface DownloadManagerContextType { interface DownloadManagerContextType {
downloads: DownloadItem[]; downloads: DownloadItem[];
startDownload: (url: string, type: "mp4" | "hls") => Promise<void>; startDownload: (url: string, type: "mp4" | "hls") => Promise<void>;
removeDownload: (id: string) => void;
} }
const DownloadManagerContext = createContext< const DownloadManagerContext = createContext<
@@ -168,8 +169,16 @@ export const DownloadManagerProvider: React.FC<{ children: ReactNode }> = ({
} }
}; };
const removeDownload = (id: string) => {
const updatedDownloads = downloads.filter((download) => download.id !== id);
setDownloads(updatedDownloads);
void saveDownloadHistory(updatedDownloads);
};
return ( return (
<DownloadManagerContext.Provider value={{ downloads, startDownload }}> <DownloadManagerContext.Provider
value={{ downloads, startDownload, removeDownload }}
>
{children} {children}
</DownloadManagerContext.Provider> </DownloadManagerContext.Provider>
); );