Skip to content

Commit 4d25227

Browse files
refactor: enhance NativeDownloadModule to support download progress events
1 parent 4ce6dea commit 4d25227

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

android/src/main/java/com/mendix/mendixnative/react/download/NativeDownloadModule.kt

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.mendix.mendixnative.react.download
22

33
import com.facebook.react.bridge.*
4-
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
54
import okhttp3.OkHttpClient
65
import java.io.IOException
76
import java.net.ConnectException
87
import java.util.concurrent.TimeUnit
98

10-
class NativeDownloadModule(val context: ReactApplicationContext) {
9+
class NativeDownloadModule(
10+
val context: ReactApplicationContext,
11+
private val eventEmitter: ((Double, Double) -> Unit)? = null
12+
) {
1113
val client = OkHttpClient()
1214

1315
fun download(
@@ -66,30 +68,15 @@ class NativeDownloadModule(val context: ReactApplicationContext) {
6668
}
6769
}
6870
) { receivedBytes, totalBytes ->
69-
postProgressEvent(
70-
receivedBytes,
71-
totalBytes
72-
)
71+
eventEmitter?.invoke(receivedBytes, totalBytes)
7372
}
7473
}
7574

76-
private fun postProgressEvent(receivedBytes: Double, totalBytes: Double) {
77-
val params = Arguments.createMap()
78-
params.putDouble("receivedBytes", receivedBytes)
79-
params.putDouble("totalBytes", totalBytes)
80-
context
81-
.getJSModule(RCTDeviceEventEmitter::class.java)
82-
.emit(DOWNLOAD_PROGRESS_EVENT, params)
83-
}
84-
8575
companion object {
86-
val supportedEvents: Array<String> = arrayOf(DOWNLOAD_PROGRESS_EVENT)
87-
8876
const val TIMEOUT_KEY = "connectionTimeout"
8977
const val MIME_TYPE_KEY = "mimeType"
9078
const val TIMEOUT = 10000
9179

92-
const val DOWNLOAD_PROGRESS_EVENT = "NDM_DOWNLOAD_PROGRESS_EVENT"
9380
const val ERROR_DOWNLOAD_FAILED = "ERROR_DOWNLOAD_FAILED"
9481
const val FILE_ALREADY_EXISTS = "FILE_ALREADY_EXISTS"
9582
const val ERROR_CONNECTION_FAILED = "ERROR_CONNECTION_FAILED"

android/src/main/java/com/mendixnative/download/MxDownloadModule.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mendixnative.download
22

3+
import com.facebook.react.bridge.Arguments
34
import com.facebook.react.bridge.Promise
45
import com.facebook.react.bridge.ReactApplicationContext
56
import com.facebook.react.bridge.ReadableMap
@@ -11,14 +12,35 @@ import com.mendixnative.NativeMxDownloadSpec
1112
class MxDownloadModule(reactContext: ReactApplicationContext) :
1213
NativeMxDownloadSpec(reactContext) {
1314

14-
private val downloadModule = NativeDownloadModule(reactContext)
15+
// Pass event emitter callback to NativeDownloadModule
16+
private val downloadModule = NativeDownloadModule(
17+
reactContext,
18+
eventEmitter = { receivedBytes, totalBytes ->
19+
emitOnDownloadProgress(receivedBytes, totalBytes)
20+
}
21+
)
1522

1623
override fun getName(): String = NAME
1724

1825
override fun download(url: String, downloadPath: String, config: ReadableMap, promise: Promise) {
1926
downloadModule.download(url, downloadPath, config, promise)
2027
}
2128

29+
/**
30+
* Emit download progress event.
31+
* This matches the codegen pattern: readonly onDownloadProgress: EventEmitter<DownloadProgress>
32+
* Codegen generates the base addListener/removeListeners methods automatically.
33+
*/
34+
private fun emitOnDownloadProgress(receivedBytes: Double, totalBytes: Double) {
35+
val params = Arguments.createMap().apply {
36+
putDouble("receivedBytes", receivedBytes)
37+
putDouble("totalBytes", totalBytes)
38+
}
39+
// Emit via the codegen-generated event emitter
40+
// Event name matches the spec: onDownloadProgress
41+
emitOnDownloadProgress(params)
42+
}
43+
2244
companion object {
2345
const val NAME = "MxDownload"
2446
}

ios/TurboModules/MxDownload/MxDownload.mm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ - (void)download:(nonnull NSString *)url
2626
connectionTimeout = @(config.connectionTimeout().value());
2727
}
2828
NSString *mimeType = config.mimeType();;
29-
[[[NativeDownloadModule alloc] init] download:url downloadPath:downloadPath connectionTimeout:connectionTimeout mimeType:mimeType onProgress:nil promise:promise];
29+
NativeDownloadModule *downloader = [[NativeDownloadModule alloc] init];
30+
[downloader download:url downloadPath:downloadPath connectionTimeout:connectionTimeout mimeType:mimeType onProgress:^(NSDictionary* progress) {
31+
// Uncomment the line below to track progress events.
32+
// [self emitOnDownloadProgress: progress];
33+
} promise:promise];
3034
}
3135

3236
@end

src/download-handler/NativeMxDownload.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ type DownloadConfig = {
66
mimeType?: string;
77
};
88

9+
type DownloadProgress = {
10+
receivedBytes: CodegenTypes.Double;
11+
totalBytes: CodegenTypes.Double;
12+
};
13+
914
export interface Spec extends TurboModule {
1015
download(
1116
url: string,
1217
downloadPath: string,
1318
config: DownloadConfig
1419
): Promise<void>;
20+
21+
// Event emitter for download progress
22+
readonly onDownloadProgress: CodegenTypes.EventEmitter<DownloadProgress>;
1523
}
1624

1725
export default TurboModuleRegistry.getEnforcing<Spec>('MxDownload');
1826

19-
export type { DownloadConfig };
27+
export type { DownloadConfig, DownloadProgress };

0 commit comments

Comments
 (0)