You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/09/21 05:15:05 UTC

[3/7] ios commit: [CB-622] Progress events for downloads

[CB-622] Progress events for downloads


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/31f894cf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/31f894cf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/31f894cf

Branch: refs/heads/master
Commit: 31f894cf5e6c546904cc036f98384078c9de3393
Parents: d1ff5a7
Author: Cory Thompson <co...@gmail.com>
Authored: Tue Sep 4 18:08:44 2012 +1000
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Sep 20 22:35:11 2012 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVFileTransfer.h |    3 ++-
 CordovaLib/Classes/CDVFileTransfer.m |   24 +++++++++++++++++++-----
 2 files changed, 21 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/31f894cf/CordovaLib/Classes/CDVFileTransfer.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFileTransfer.h b/CordovaLib/Classes/CDVFileTransfer.h
index 7ca96a3..d29d423 100755
--- a/CordovaLib/Classes/CDVFileTransfer.h
+++ b/CordovaLib/Classes/CDVFileTransfer.h
@@ -70,7 +70,8 @@ extern NSString* const kOptionsKeyCookie;
 @property (nonatomic, copy) NSString* source;
 @property (nonatomic, copy) NSString* target;
 @property (assign) int responseCode; // atomic
-@property (nonatomic, assign) NSInteger bytesWritten;
+@property (nonatomic, assign) NSInteger bytesTransfered;
+@property (nonatomic, assign) NSInteger bytesExpected;
 
 
 @end;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/31f894cf/CordovaLib/Classes/CDVFileTransfer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFileTransfer.m b/CordovaLib/Classes/CDVFileTransfer.m
index 3660486..055b66c 100755
--- a/CordovaLib/Classes/CDVFileTransfer.m
+++ b/CordovaLib/Classes/CDVFileTransfer.m
@@ -290,7 +290,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
     [delegate.connection cancel];
     [activeTransfers removeObjectForKey:objectId];
     
-    CDVPlugin Result *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [self createFileTransferError:CONNECTION_ABORTED AndSource:delegate.source AndTarget:delegate.target]];
+    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: [self createFileTransferError:CONNECTION_ABORTED AndSource:delegate.source AndTarget:delegate.target]];
     
     [self writeJavascript:[result toErrorCallbackString:command.callbackId]];
 
@@ -380,7 +380,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
 
 @implementation CDVFileTransferDelegate
 
-@synthesize callbackId, connection, source, target, responseData, command, bytesWritten, direction, responseCode, objectId;
+@synthesize callbackId, connection, source, target, responseData, command, bytesTransfered, bytesExpected, direction, responseCode, objectId;
 
 
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
@@ -407,7 +407,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
             if (uploadResponse != nil) {
                 [uploadResult setObject: [uploadResponse stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey: @"response"];
             }
-            [uploadResult setObject:[NSNumber numberWithInt: self.bytesWritten] forKey:@"bytesSent"];
+            [uploadResult setObject:[NSNumber numberWithInt: self.bytesTransfered] forKey:@"bytesSent"];
             [uploadResult setObject:[NSNumber numberWithInt:self.responseCode] forKey: @"responseCode"];
             result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: uploadResult];            
         } 
@@ -469,6 +469,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
 {
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
     self.responseCode = [httpResponse statusCode];
+    self.bytesExpected = [response expectedContentLength];
 }
 
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
@@ -484,7 +485,20 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
 
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
-        [self.responseData appendData:data];
+    self.bytesTransfered += data.length;
+    [self.responseData appendData:data];
+    
+    BOOL lengthComputable = (self.bytesExpected != NSURLResponseUnknownLength);
+    NSMutableDictionary* downloadProgress = [NSMutableDictionary dictionaryWithCapacity:3];
+    [downloadProgress setObject:[NSNumber numberWithBool: lengthComputable] forKey:@"lengthComputable"];
+    [downloadProgress setObject:[NSNumber numberWithInt: self.bytesTransfered] forKey:@"loaded"];
+    [downloadProgress setObject:[NSNumber numberWithInt: self.bytesExpected] forKey:@"total"];
+    
+    CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: downloadProgress ];
+    [result setKeepCallbackAsBool:true];
+    [self.command writeJavascript:[result toSuccessCallbackString: callbackId ]];
+
+    
 }
 - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
 {
@@ -497,7 +511,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
     [result setKeepCallbackAsBool:true]; 
     [self.command writeJavascript:[result toSuccessCallbackString: callbackId ]];
     
-    self.bytesWritten = totalBytesWritten;
+    self.bytesTransfered = totalBytesWritten;
 }
 /* TESTING ONLY CODE
 // use ONLY for testing with self signed certificates