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,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<DownloadItem[]> => {
const json = await AsyncStorage.getItem(downloadHistoryKey);
const settings = json
? (JSON.parse(json) as DownloadHistory)
: { downloads: [] };
return settings.downloads;
};