fix: working state updates for active/non-active downloads

This commit is contained in:
Fredrik Burmester
2025-02-17 20:28:05 +01:00
parent ec153ebfc6
commit 82b981f15c
3 changed files with 96 additions and 45 deletions

View File

@@ -20,9 +20,17 @@ import {
import { useQuery } from "@tanstack/react-query";
import * as FileSystem from "expo-file-system";
import { useAtomValue } from "jotai";
import { createContext, useContext, useEffect, useState } from "react";
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import { toast } from "sonner-native";
import { apiAtom, userAtom } from "./JellyfinProvider";
import { useFocusEffect } from "expo-router";
import { AppState, AppStateStatus } from "react-native";
type DownloadOptionsData = {
selectedAudioStream: number;
@@ -120,38 +128,47 @@ export const NativeDownloadProvider: React.FC<{
});
useEffect(() => {
const _getActiveDownloads = async () => {
const activeDownloads = await getActiveDownloads();
setDownloads((prev) => {
const newDownloads = { ...prev };
activeDownloads.forEach((download) => {
newDownloads[download.id] = {
id: download.id,
progress: download.progress,
state: download.state,
secondsDownloaded: download.secondsDownloaded,
secondsTotal: download.secondsTotal,
metadata: download.metadata,
startTime: download.startTime,
};
});
return newDownloads;
});
const handleAppStateChange = (state: AppStateStatus) => {
if (state === "background" || state === "inactive") {
setDownloads({});
} else if (state === "active") {
const _getActiveDownloads = async () => {
const activeDownloads = await getActiveDownloads();
setDownloads((prev) => {
const newDownloads = { ...prev };
activeDownloads.forEach((download) => {
newDownloads[download.id] = {
id: download.id,
progress: download.progress,
state: download.state,
secondsDownloaded: download.secondsDownloaded,
secondsTotal: download.secondsTotal,
metadata: download.metadata,
startTime: download.startTime,
};
});
return newDownloads;
});
};
_getActiveDownloads();
refetchDownloadedFiles();
}
};
_getActiveDownloads();
const subscription = AppState.addEventListener(
"change",
handleAppStateChange
);
return () => {
subscription.remove();
};
}, [getActiveDownloads]);
useEffect(() => {
const progressListener = addProgressListener((download) => {
if (!download.metadata) throw new Error("No metadata found in download");
console.log(
"[HLS] Download progress:",
download.metadata.item.Id,
download.progress,
download.state,
download.taskId
);
setDownloads((prev) => ({
...prev,
[download.id]: {