diff --git a/components/NativeDownloadButton.tsx b/components/NativeDownloadButton.tsx index 0091079a..745b46d6 100644 --- a/components/NativeDownloadButton.tsx +++ b/components/NativeDownloadButton.tsx @@ -144,12 +144,9 @@ export const NativeDownloadButton: React.FC = ({ if (res.url.includes("master.m3u8")) { // TODO: Download with custom native module - downloadHLSAsset( - item.Id!, - res.url, - `${FileSystem.documentDirectory}${item.Name}.mkv` - ); console.log("TODO: Download with custom native module"); + if (!item.Id || !item.Name) throw new Error("No item id found"); + downloadHLSAsset(item.Id, res.url, item.Name); } else { // Download with reac-native-background-downloader const destination = `${FileSystem.documentDirectory}${item.Name}.mkv`; @@ -201,12 +198,16 @@ export const NativeDownloadButton: React.FC = ({ ]); useEffect(() => { - const progressListener = addProgressListener((item) => { + const progressListener = addProgressListener((_item) => { console.log("progress ~", item); - setActiveDownload({ - id: activeDownload?.id!, - progress: item.progress, - state: "DOWNLOADING", + if (item.Id !== _item.id) return; + setActiveDownload((prev) => { + if (!prev) return undefined; + return { + ...prev, + progress: _item.progress, + state: _item.state, + }; }); }); @@ -215,8 +216,11 @@ export const NativeDownloadButton: React.FC = ({ "AVAssetDownloadURLSession ~ checkForExistingDownloads ~", downloads ); + const firstDownload = downloads?.[0]; - if (!download) return; + + if (!firstDownload) return; + if (firstDownload.id !== item.Id) return; setActiveDownload({ id: firstDownload?.id, diff --git a/modules/hls-downloader/ios/HlsDownloaderModule.swift b/modules/hls-downloader/ios/HlsDownloaderModule.swift index 91b9bd03..dfd151cb 100644 --- a/modules/hls-downloader/ios/HlsDownloaderModule.swift +++ b/modules/hls-downloader/ios/HlsDownloaderModule.swift @@ -11,6 +11,8 @@ public class HlsDownloaderModule: Module { Events("onProgress", "onError", "onComplete") Function("downloadHLSAsset") { (providedId: String, url: String, assetTitle: String) -> Void in + print("Starting download - ID: \(providedId), URL: \(url), Title: \(assetTitle)") + guard let assetURL = URL(string: url) else { self.sendEvent("onError", ["id": providedId, "error": "Invalid URL", "state": "FAILED"]) return @@ -47,6 +49,7 @@ public class HlsDownloaderModule: Module { ]) task.resume() + print("Download task started with identifier: \(task.taskIdentifier)") } Function("checkForExistingDownloads") { diff --git a/modules/hls-downloader/src/HlsDownloader.types.ts b/modules/hls-downloader/src/HlsDownloader.types.ts index 1dba0467..3adb48b1 100644 --- a/modules/hls-downloader/src/HlsDownloader.types.ts +++ b/modules/hls-downloader/src/HlsDownloader.types.ts @@ -1,5 +1,9 @@ export type OnProgressEventPayload = { progress: number; + state: "PENDING" | "DOWNLOADING" | "PAUSED" | "DONE" | "FAILED" | "STOPPED"; + id: string; + bytesDownloaded: number; + bytesTotal: number; }; export type OnErrorEventPayload = {