This commit is contained in:
Fredrik Burmester
2025-02-15 21:15:52 +01:00
parent 10bf8fa19a
commit 179f6c02ca
3 changed files with 22 additions and 11 deletions

View File

@@ -144,12 +144,9 @@ export const NativeDownloadButton: React.FC<NativeDownloadButton> = ({
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<NativeDownloadButton> = ({
]);
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<NativeDownloadButton> = ({
"AVAssetDownloadURLSession ~ checkForExistingDownloads ~",
downloads
);
const firstDownload = downloads?.[0];
if (!download) return;
if (!firstDownload) return;
if (firstDownload.id !== item.Id) return;
setActiveDownload({
id: firstDownload?.id,

View File

@@ -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") {

View File

@@ -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 = {