Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
dependencies {
implementation "com.facebook.react:react-android"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.tpstreams:tpstreams-player:0.0.6"
implementation "com.tpstreams:tpstreams-player:0.0.8"
}

react {
Expand Down
105 changes: 105 additions & 0 deletions android/src/main/java/com/tpstreams/TPStreamsDownloadModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.tpstreams

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.Arguments
import com.tpstreams.player.download.DownloadTracker

class TPStreamsDownloadModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

private val downloadTracker: DownloadTracker by lazy {
try {
DownloadTracker.getInstance(reactApplicationContext)
} catch (e: Exception) {
throw RuntimeException("Failed to initialize DownloadTracker. Make sure TPStreams library is properly initialized.", e)
}
}

override fun getName(): String {
return "TPStreamsDownload"
}

@ReactMethod
fun getDownloads(promise: Promise) {
try {
val downloadItems = downloadTracker.getAllDownloadItems()
val result = Arguments.createArray()

downloadItems.forEach { item ->
val itemMap = Arguments.createMap()

itemMap.putString("videoId", item.assetId)
itemMap.putString("title", item.title)

item.thumbnailUrl?.let { thumbnailUrl ->
itemMap.putString("thumbnailUrl", thumbnailUrl)
}
Comment thread
syed-tp marked this conversation as resolved.

itemMap.putDouble("totalBytes", item.totalBytes.toDouble())
itemMap.putDouble("downloadedBytes", item.downloadedBytes.toDouble())
itemMap.putDouble("progressPercentage", item.progressPercentage.toDouble())
itemMap.putInt("state", item.state)

result.pushMap(itemMap)
}

promise.resolve(result)
} catch (e: Exception) {
promise.reject("GET_DOWNLOADS_ERROR", "Failed to get downloads: ${e.message}", e)
}
}

@ReactMethod
fun getDownloadStatus(videoId: String, promise: Promise) {
try {
val status = downloadTracker.getDownloadStatus(videoId)
promise.resolve(status)
} catch (e: Exception) {
promise.reject("GET_STATUS_ERROR", "Failed to get download status for video $videoId: ${e.message}", e)
}
}

@ReactMethod
fun pauseDownload(videoId: String, promise: Promise) {
try {
val success = downloadTracker.pauseDownload(videoId)
promise.resolve(success)
} catch (e: Exception) {
promise.reject("PAUSE_DOWNLOAD_ERROR", "Failed to pause download for video $videoId: ${e.message}", e)
}
}

@ReactMethod
fun resumeDownload(videoId: String, promise: Promise) {
try {
val success = downloadTracker.resumeDownload(videoId)
promise.resolve(success)
} catch (e: Exception) {
promise.reject("RESUME_DOWNLOAD_ERROR", "Failed to resume download for video $videoId: ${e.message}", e)
}
}

@ReactMethod
fun cancelDownload(videoId: String, promise: Promise) {
try {
val success = downloadTracker.removeDownload(videoId)
promise.resolve(success)
} catch (e: Exception) {
promise.reject("CANCEL_DOWNLOAD_ERROR", "Failed to cancel download for video $videoId: ${e.message}", e)
}
}

@ReactMethod
fun isDownloaded(videoId: String, promise: Promise) {
try {
val downloaded = downloadTracker.isDownloaded(videoId)
promise.resolve(downloaded)
} catch (e: Exception) {
promise.reject("IS_DOWNLOADED_ERROR", "Failed to check download status for video $videoId: ${e.message}", e)
}
}
}
1 change: 1 addition & 0 deletions android/src/main/java/com/tpstreams/TPStreamsRNPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TPStreamsRNPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
val modules: MutableList<NativeModule> = ArrayList()
modules.add(TPStreamsRNModule(reactContext))
modules.add(TPStreamsDownloadModule(reactContext))
return modules
}
}
39 changes: 39 additions & 0 deletions src/TPStreamsDownload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NativeModules } from 'react-native';

const { TPStreamsDownload } = NativeModules;

export interface DownloadItem {
videoId: string;
title: string;
thumbnailUrl?: string;
totalBytes: number;
downloadedBytes: number;
progressPercentage: number;
state: number;
}

export default {
getDownloads(): Promise<DownloadItem[]> {
return TPStreamsDownload.getDownloads();
},

getDownloadStatus(videoId: string): Promise<number> {
return TPStreamsDownload.getDownloadStatus(videoId);
},

pauseDownload(videoId: string): Promise<boolean> {
return TPStreamsDownload.pauseDownload(videoId);
},

resumeDownload(videoId: string): Promise<boolean> {
return TPStreamsDownload.resumeDownload(videoId);
},

cancelDownload(videoId: string): Promise<boolean> {
return TPStreamsDownload.cancelDownload(videoId);
},

isDownloaded(videoId: string): Promise<boolean> {
return TPStreamsDownload.isDownloaded(videoId);
},
};
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export * from './TPStreamsPlayerViewNativeComponent';
export { default as TPStreamsPlayerView } from './TPStreamsPlayer';
export type { TPStreamsPlayerRef } from './TPStreamsPlayer';

export { default as TPStreamsDownload } from './TPStreamsDownload';
export type { DownloadItem } from './TPStreamsDownload';

const TPStreamsModule = NativeModules.TPStreams;

export const TPStreams = {
Expand Down