Переглянути джерело

Gdrive and Dropbox: show remaning downloadtime

Carola Nitz 11 роки тому
батько
коміт
bb845d9835

BIN
Resources/en.lproj/Localizable.strings


+ 1 - 0
Sources/VLCDropboxController.h

@@ -19,6 +19,7 @@
 @optional
 - (void)operationWithProgressInformationStarted;
 - (void)currentProgressInformation:(float)progress;
+- (void)updateRemainingTime:(NSString *)time;
 - (void)operationWithProgressInformationStopped;
 
 - (void)numberOfFilesWaitingToBeDownloadedChanged;

+ 30 - 2
Sources/VLCDropboxController.m

@@ -24,6 +24,11 @@
     BOOL _downloadInProgress;
 
     NSInteger _outstandingNetworkRequests;
+
+    CGFloat _averageSpeed;
+    CGFloat _fileSize;
+    NSTimeInterval _startDL;
+    NSTimeInterval _lastStatsUpdate;
 }
 
 @end
@@ -96,7 +101,8 @@
 {
     NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
-
+    _startDL = [NSDate timeIntervalSinceReferenceDate];
+    _fileSize = file.totalBytes;
     [[self restClient] loadFile:file.path intoPath:filePath];
 
     if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
@@ -160,12 +166,16 @@
     if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
         [self.delegate operationWithProgressInformationStopped];
     _downloadInProgress = NO;
-
     [self _triggerNextDownload];
 }
 
 - (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
 {
+    if ((_lastStatsUpdate > 0 && ([NSDate timeIntervalSinceReferenceDate] - _lastStatsUpdate > .5)) || _lastStatsUpdate <= 0) {
+        [self calculateRemainingTime:progress*_fileSize expectedDownloadSize:_fileSize];
+        _lastStatsUpdate = [NSDate timeIntervalSinceReferenceDate];
+    }
+
     if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
         [self.delegate currentProgressInformation:progress];
 }
@@ -211,6 +221,24 @@
 
 #pragma mark - VLC internal communication and delegate
 
+- (void)calculateRemainingTime:(CGFloat)receivedDataSize expectedDownloadSize:(CGFloat)expectedDownloadSize
+{
+    CGFloat lastSpeed = receivedDataSize / ([NSDate timeIntervalSinceReferenceDate] - _startDL);
+    CGFloat smoothingFactor = 0.005;
+    _averageSpeed = isnan(_averageSpeed) ? lastSpeed : smoothingFactor * lastSpeed + (1 - smoothingFactor) * _averageSpeed;
+
+    CGFloat RemainingInSeconds = (expectedDownloadSize - receivedDataSize)/_averageSpeed;
+
+    NSDate *date = [NSDate dateWithTimeIntervalSince1970:RemainingInSeconds];
+    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+    [formatter setDateFormat:@"HH:mm:ss"];
+    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
+
+    NSString  *remaingTime = [formatter stringFromDate:date];
+    if ([self.delegate respondsToSelector:@selector(updateRemainingTime:)])
+        [self.delegate updateRemainingTime:remaingTime];
+}
+
 - (NSArray *)currentListFiles
 {
     return _currentFileList;

+ 23 - 9
Sources/VLCDropboxTableViewController.m

@@ -34,7 +34,8 @@
     UIBarButtonItem *_numberOfFilesBarButtonItem;
     UIBarButtonItem *_progressBarButtonItem;
     UIBarButtonItem *_downloadingBarLabel;
-    UIProgressView *_progressView;
+    UIProgressView *_progressBar;
+    UILabel *_progressLabel;
 
     UIActivityIndicatorView *_activityIndicator;
     DBMetadata *_selectedFile;
@@ -69,11 +70,18 @@
 
     _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
     [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
+    _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
+    _progressLabel = [[UILabel alloc] init];
+    _progressLabel.textColor = [UIColor whiteColor];
+    _progressLabel.font = [UIFont systemFontOfSize:11.];
 
-    _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
-    _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
-    _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
-    [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
+    UIView *progressView = [[UIView alloc] init];
+    [progressView addSubview:_progressBar];
+    [progressView addSubview:_progressLabel];
+    [progressView addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
+    progressView.translatesAutoresizingMaskIntoConstraints = NO;
+
+    _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:progressView];
 
     [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
     if (!SYSTEM_RUNS_IOS7_OR_LATER) {
@@ -123,8 +131,8 @@
     if (!value)
         [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
     else {
-        _progressView.progress = 0.;
-        [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
+        _progressBar.progress = 0.;
+        [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
     }
 }
 
@@ -233,9 +241,15 @@
     [self _showProgressInToolbar:YES];
 }
 
-- (void)currentProgressInformation:(float)progress
+- (void)updateRemainingTime:(NSString *)time
 {
-    [_progressView setProgress: progress animated:YES];
+    [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
+    CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
+    [_progressLabel setFrame:CGRectMake(0, 2, size.width, size.height)];
+}
+
+- (void)currentProgressInformation:(float)progress {
+    [_progressBar setProgress:progress animated:YES];
 }
 
 - (void)operationWithProgressInformationStopped

+ 1 - 0
Sources/VLCGoogleDriveController.h

@@ -22,6 +22,7 @@
 @optional
 - (void)operationWithProgressInformationStarted;
 - (void)currentProgressInformation:(float)progress;
+- (void)updateRemainingTime:(NSString *)time;
 - (void)operationWithProgressInformationStopped;
 - (void)numberOfFilesWaitingToBeDownloadedChanged;
 @end

+ 28 - 3
Sources/VLCGoogleDriveController.m

@@ -27,6 +27,10 @@
     BOOL _downloadInProgress;
 
     NSString *_nextPageToken;
+
+    CGFloat _averageSpeed;
+    NSTimeInterval _startDL;
+    NSTimeInterval _lastStatsUpdate;
 }
 
 @end
@@ -211,12 +215,15 @@
 
         // Fetcher logging can include comments.
         [fetcher setCommentWithFormat:@"Downloading \"%@\"", file.title];
-
         __weak GTMHTTPFetcher *weakFetcher = fetcher;
-
+        _startDL = [NSDate timeIntervalSinceReferenceDate];
         fetcher.receivedDataBlock = ^(NSData *receivedData) {
-            float progress = (float)weakFetcher.downloadedLength / (float)[file.fileSize longLongValue];
+            if ((_lastStatsUpdate > 0 && ([NSDate timeIntervalSinceReferenceDate] - _lastStatsUpdate > .5)) || _lastStatsUpdate <= 0) {
+                [self calculateRemainingTime:weakFetcher.downloadedLength expectedDownloadSize:[file.fileSize floatValue]];
+                _lastStatsUpdate = [NSDate timeIntervalSinceReferenceDate];
+            }
 
+            CGFloat progress = (CGFloat)weakFetcher.downloadedLength / (CGFloat)[file.fileSize unsignedLongValue];
             if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
                 [self.delegate currentProgressInformation:progress];
         };
@@ -233,6 +240,24 @@
     }
 }
 
+- (void)calculateRemainingTime:(CGFloat)receivedDataSize expectedDownloadSize:(CGFloat)expectedDownloadSize
+{
+    CGFloat lastSpeed = receivedDataSize / ([NSDate timeIntervalSinceReferenceDate] - _startDL);
+    CGFloat smoothingFactor = 0.005;
+    _averageSpeed = isnan(_averageSpeed) ? lastSpeed : smoothingFactor * lastSpeed + (1 - smoothingFactor) * _averageSpeed;
+
+    CGFloat RemainingInSeconds = (expectedDownloadSize - receivedDataSize) / _averageSpeed;
+
+    NSDate *date = [NSDate dateWithTimeIntervalSince1970:RemainingInSeconds];
+    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+    [formatter setDateFormat:@"HH:mm:ss"];
+    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
+
+    NSString  *remaingTime = [formatter stringFromDate:date];
+    if ([self.delegate respondsToSelector:@selector(updateRemainingTime:)])
+        [self.delegate updateRemainingTime:remaingTime];
+}
+
 - (void)downloadSucessfull
 {
     /* update library now that we got a file */

+ 24 - 9
Sources/VLCGoogleDriveTableViewController.m

@@ -32,7 +32,8 @@
     UIBarButtonItem *_numberOfFilesBarButtonItem;
     UIBarButtonItem *_progressBarButtonItem;
     UIBarButtonItem *_downloadingBarLabel;
-    UIProgressView *_progressView;
+    UIProgressView *_progressBar;
+    UILabel *_progressLabel;
 
     UIActivityIndicatorView *_activityIndicator;
 
@@ -69,10 +70,18 @@
     _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
     [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
 
-    _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
-    _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
-    _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
-    [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
+    _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
+    _progressLabel = [[UILabel alloc] init];
+    _progressLabel.textColor = [UIColor whiteColor];
+    _progressLabel.font = [UIFont systemFontOfSize:11.];
+
+    UIView *progressView = [[UIView alloc] init];
+    [progressView addSubview:_progressBar];
+    [progressView addSubview:_progressLabel];
+    [progressView addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
+    progressView.translatesAutoresizingMaskIntoConstraints = NO;
+
+    _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:progressView];
 
     [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
 
@@ -161,8 +170,8 @@
     if (!value)
         [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
     else {
-        _progressView.progress = 0.;
-        [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
+        _progressBar.progress = 0.;
+        [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
     }
 }
 
@@ -272,9 +281,15 @@
     [self _showProgressInToolbar:YES];
 }
 
-- (void)currentProgressInformation:(float)progress
+- (void)updateRemainingTime:(NSString *)time
 {
-    [_progressView setProgress: progress animated:YES];
+    [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
+    CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
+    [_progressLabel setFrame:CGRectMake(0, 2, size.width, size.height)];
+}
+
+- (void)currentProgressInformation:(float)progress {
+    [_progressBar setProgress:progress animated:YES];
 }
 
 - (void)operationWithProgressInformationStopped