Skip to content

Commit 8d1b8a2

Browse files
authored
fix: ios wrong progress and status about pause (#743)
1 parent 802d1aa commit 8d1b8a2

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

ios/Classes/FlutterDownloaderPlugin.m

+22-5
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,18 @@ - (void)pauseTaskWithId: (NSString*)taskId
215215
NSURLSessionTaskState state = download.state;
216216
NSString *taskIdValue = [weakSelf identifierForTask:download];
217217
if ([taskId isEqualToString:taskIdValue] && (state == NSURLSessionTaskStateRunning)) {
218-
int64_t bytesReceived = download.countOfBytesReceived;
219-
int64_t bytesExpectedToReceive = download.countOfBytesExpectedToReceive;
220-
int progress = round(bytesReceived * 100 / (double)bytesExpectedToReceive);
221218
NSDictionary *task = [weakSelf loadTaskWithId:taskIdValue];
219+
220+
int progress = 0;
221+
if (download.countOfBytesExpectedToReceive > 0) {
222+
int64_t bytesReceived = download.countOfBytesReceived;
223+
int64_t bytesExpectedToReceive = download.countOfBytesExpectedToReceive;
224+
progress = round(bytesReceived * 100 / (double)bytesExpectedToReceive);
225+
} else {
226+
NSNumber *progressNumOfTask = task[@"progress"];
227+
progress = progressNumOfTask.intValue;
228+
}
229+
222230
[download cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
223231
// Save partial downloaded data to a file
224232
NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -913,10 +921,19 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
913921
int progress = round(totalBytesWritten * 100 / (double)totalBytesExpectedToWrite);
914922
NSNumber *lastProgress = _runningTaskById[taskId][KEY_PROGRESS];
915923
if (([lastProgress intValue] == 0 || (progress > ([lastProgress intValue] + _step)) || progress == 100) && progress != [lastProgress intValue]) {
916-
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_RUNNING) andProgress:@(progress)];
924+
925+
NSNumber *status;
926+
if (downloadTask.state == NSURLSessionTaskStateRunning) {
927+
status = @(STATUS_RUNNING);
928+
} else {
929+
NSDictionary *taskDict = [self loadTaskWithId:taskId];
930+
status = taskDict[@"status"];
931+
}
932+
933+
[self sendUpdateProgressForTaskId:taskId inStatus:status andProgress:@(progress)];
917934
__typeof__(self) __weak weakSelf = self;
918935
[self executeInDatabaseQueueForTask:^{
919-
[weakSelf updateTask:taskId status:STATUS_RUNNING progress:progress];
936+
[weakSelf updateTask:taskId status:status.intValue progress:progress];
920937
}];
921938
_runningTaskById[taskId][KEY_PROGRESS] = @(progress);
922939
}

0 commit comments

Comments
 (0)