From f626cc193304a9fa9727a098e458bf1cf7827485 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Thu, 24 Sep 2020 19:31:27 +0800 Subject: [PATCH] Fix download event and remove unzip event --- .../modules/update/DownloadTask.java | 12 ++++++--- ios/RCTPushy/RCTPushy.m | 25 +++++++++++-------- lib/index.d.ts | 1 - lib/index.js | 9 ------- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java b/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java index efddcda..36ec1b1 100644 --- a/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java +++ b/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java @@ -97,6 +97,7 @@ class DownloadTask extends AsyncTask { long bytesRead = 0; long received = 0; + int currentPercentage = 0; while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) { received += bytesRead; sink.emit(); @@ -104,11 +105,16 @@ class DownloadTask extends AsyncTask { Log.d("RNUpdate", "Progress " + received + "/" + contentLength); } - publishProgress(new long[]{received, contentLength}); + int percentage = (int)(received * 100.0 / contentLength + 0.5); + if (percentage > currentPercentage) { + currentPercentage = percentage; + publishProgress(new long[]{received, contentLength}); + } } if (received != contentLength) { - throw new Error("Unexpected eof while reading ppk"); + throw new Error("Unexpected eof while reading downloaded update"); } + publishProgress(new long[]{received, contentLength}); sink.writeAll(source); sink.close(); @@ -123,7 +129,7 @@ class DownloadTask extends AsyncTask { WritableMap params = Arguments.createMap(); params.putDouble("received", (values[0][0])); params.putDouble("total", (values[0][1])); - params.putString("hashname", this.hash); + params.putString("hash", this.hash); sendEvent("RCTPushyDownloadProgress", params); } diff --git a/ios/RCTPushy/RCTPushy.m b/ios/RCTPushy/RCTPushy.m index cdcd8db..6fd95d9 100644 --- a/ios/RCTPushy/RCTPushy.m +++ b/ios/RCTPushy/RCTPushy.m @@ -43,8 +43,8 @@ static NSString * const ERROR_FILE_OPERATION = @"file operation error"; // event def static NSString * const EVENT_PROGRESS_DOWNLOAD = @"RCTPushyDownloadProgress"; -static NSString * const EVENT_PROGRESS_UNZIP = @"RCTPushyUnzipProgress"; -static NSString * const PARAM_PROGRESS_HASHNAME = @"hashname"; +// static NSString * const EVENT_PROGRESS_UNZIP = @"RCTPushyUnzipProgress"; +static NSString * const PARAM_PROGRESS_HASHNAME = @"hash"; static NSString * const PARAM_PROGRESS_RECEIVED = @"received"; static NSString * const PARAM_PROGRESS_TOTAL = @"total"; @@ -305,7 +305,10 @@ RCT_EXPORT_METHOD(markSuccess) #pragma mark - private - (NSArray *)supportedEvents { - return @[EVENT_PROGRESS_DOWNLOAD, EVENT_PROGRESS_UNZIP]; + return @[ + EVENT_PROGRESS_DOWNLOAD, + // EVENT_PROGRESS_UNZIP + ]; } // Will be called when this module's first listener is added. @@ -362,14 +365,14 @@ RCT_EXPORT_METHOD(markSuccess) RCTLogInfo(@"RCTPushy -- unzip file %@", zipFilePath); NSString *unzipFilePath = [dir stringByAppendingPathComponent:hashName]; [self->_fileManager unzipFileAtPath:zipFilePath toDestination:unzipFilePath progressHandler:^(NSString *entry,long entryNumber, long total) { - if (self->hasListeners) { - [self sendEventWithName:EVENT_PROGRESS_UNZIP - body:@{ - PARAM_PROGRESS_HASHNAME:hashName, - PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber], - PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total] - }]; - } + // if (self->hasListeners) { + // [self sendEventWithName:EVENT_PROGRESS_UNZIP + // body:@{ + // PARAM_PROGRESS_HASHNAME:hashName, + // PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber], + // PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total] + // }]; + // } } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) { dispatch_async(self->_methodQueue, ^{ diff --git a/lib/index.d.ts b/lib/index.d.ts index 129df65..a1fbe01 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -34,7 +34,6 @@ export function downloadUpdate( info: UpdateAvailableResult, eventListeners?: { onDownloadProgress?: (data: ProgressData) => void; - onUnzipProgress?: (data: ProgressData) => void; }, ): Promise; diff --git a/lib/index.js b/lib/index.js index 37e1756..dd18d05 100644 --- a/lib/index.js +++ b/lib/index.js @@ -153,14 +153,6 @@ export async function downloadUpdate(options, eventListeners) { } }); } - if (eventListeners.onUnzipProgress) { - const unzipCallback = eventListeners.onUnzipProgress; - eventEmitter.addListener('RCTPushyUnzipProgress', (progressData) => { - if (progressData.hash === options.hash) { - unzipCallback(progressData); - } - }); - } } if (options.diffUrl) { logger('downloading diff'); @@ -177,7 +169,6 @@ export async function downloadUpdate(options, eventListeners) { }); } eventEmitter.removeAllListeners('RCTPushyDownloadProgress'); - eventEmitter.removeAllListeners('RCTPushyUnzipProgress'); return options.hash; }