This commit is contained in:
Fredrik Burmester
2025-02-15 17:20:46 +01:00
parent 4644d2ab58
commit a25c01b7a8
9 changed files with 384 additions and 73 deletions

View File

@@ -14,7 +14,7 @@ import {
useDownloadComplete,
useDownloadError,
useDownloadProgress,
} from "../src/ExpoHlsDownloader";
} from "..";
/**
* Parses boot.xml in the root download directory to extract the stream folder name.

View File

@@ -6,15 +6,15 @@ import type {
OnCompleteEventPayload,
OnErrorEventPayload,
OnProgressEventPayload,
} from "./ExpoHlsDownloader.types";
import ExpoHlsDownloaderModule from "./ExpoHlsDownloaderModule";
} from "./src/ExpoHlsDownloader.types";
import ExpoHlsDownloaderModule from "./src/ExpoHlsDownloaderModule";
/**
* Initiates an HLS download.
* @param url - The HLS stream URL.
* @param assetTitle - A title for the asset.
*/
export function downloadHLSAsset(url: string, assetTitle: string): void {
function downloadHLSAsset(url: string, assetTitle: string): void {
ExpoHlsDownloaderModule.downloadHLSAsset(url, assetTitle);
}
@@ -23,7 +23,7 @@ export function downloadHLSAsset(url: string, assetTitle: string): void {
* @param listener A callback invoked with progress updates.
* @returns A subscription that can be removed.
*/
export function addProgressListener(
function addProgressListener(
listener: (event: OnProgressEventPayload) => void
): EventSubscription {
return ExpoHlsDownloaderModule.addListener("onProgress", listener);
@@ -34,7 +34,7 @@ export function addProgressListener(
* @param listener A callback invoked with error details.
* @returns A subscription that can be removed.
*/
export function addErrorListener(
function addErrorListener(
listener: (event: OnErrorEventPayload) => void
): EventSubscription {
return ExpoHlsDownloaderModule.addListener("onError", listener);
@@ -45,7 +45,7 @@ export function addErrorListener(
* @param listener A callback invoked when the download completes.
* @returns A subscription that can be removed.
*/
export function addCompleteListener(
function addCompleteListener(
listener: (event: OnCompleteEventPayload) => void
): EventSubscription {
return ExpoHlsDownloaderModule.addListener("onComplete", listener);
@@ -54,7 +54,7 @@ export function addCompleteListener(
/**
* React hook that returns the current download progress (01).
*/
export function useDownloadProgress(): number {
function useDownloadProgress(): number {
const [progress, setProgress] = useState(0);
useEffect(() => {
const subscription = addProgressListener((event) => {
@@ -68,7 +68,7 @@ export function useDownloadProgress(): number {
/**
* React hook that returns the latest download error (or null if none).
*/
export function useDownloadError(): string | null {
function useDownloadError(): string | null {
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const subscription = addErrorListener((event) => {
@@ -111,9 +111,7 @@ async function persistDownloadedFile(
* @param destinationFileName Optional filename (with extension) to persist the file.
* @returns The final file URI or null if not completed.
*/
export function useDownloadComplete(
destinationFileName?: string
): string | null {
function useDownloadComplete(destinationFileName?: string): string | null {
const [location, setLocation] = useState<string | null>(null);
useEffect(() => {
@@ -161,3 +159,10 @@ export function useDownloadComplete(
return location;
}
export {
downloadHLSAsset,
useDownloadComplete,
useDownloadError,
useDownloadProgress,
};

View File

@@ -1,38 +0,0 @@
import ExpoModulesCore
import WebKit
// This view will be used as a native component. Make sure to inherit from `ExpoView`
// to apply the proper styling (e.g. border radius and shadows).
class ExpoHlsDownloaderView: ExpoView {
let webView = WKWebView()
let onLoad = EventDispatcher()
var delegate: WebViewDelegate?
required init(appContext: AppContext? = nil) {
super.init(appContext: appContext)
clipsToBounds = true
delegate = WebViewDelegate { url in
self.onLoad(["url": url])
}
webView.navigationDelegate = delegate
addSubview(webView)
}
override func layoutSubviews() {
webView.frame = bounds
}
}
class WebViewDelegate: NSObject, WKNavigationDelegate {
let onUrlChange: (String) -> Void
init(onUrlChange: @escaping (String) -> Void) {
self.onUrlChange = onUrlChange
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation) {
if let url = webView.url {
onUrlChange(url.absoluteString)
}
}
}

View File

@@ -1,10 +1,3 @@
import { NativeModule, requireNativeModule } from "expo";
import { ExpoHlsDownloaderModuleEvents } from "./ExpoHlsDownloader.types";
import { requireNativeModule } from "expo";
declare class ExpoHlsDownloaderModule extends NativeModule<ExpoHlsDownloaderModuleEvents> {
downloadHLSAsset(url: string, assetTitle: string): void;
}
export default requireNativeModule<ExpoHlsDownloaderModule>(
"ExpoHlsDownloader"
);
export default requireNativeModule("ExpoHlsDownloader");

View File

@@ -1,11 +0,0 @@
import { requireNativeView } from 'expo';
import * as React from 'react';
import { ExpoHlsDownloaderViewProps } from './ExpoHlsDownloader.types';
const NativeView: React.ComponentType<ExpoHlsDownloaderViewProps> =
requireNativeView('ExpoHlsDownloader');
export default function ExpoHlsDownloaderView(props: ExpoHlsDownloaderViewProps) {
return <NativeView {...props} />;
}

View File

@@ -4,6 +4,6 @@
"compilerOptions": {
"outDir": "./build"
},
"include": ["./src"],
"include": ["./src", "index.ts"],
"exclude": ["**/__mocks__/*", "**/__tests__/*", "**/__rsc_tests__/*"]
}