Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Check for division by zero when calculating download throughput. (#23)
Browse files Browse the repository at this point in the history
* Check for division by zero when calculating download throughput.
* Bump version to 1.4.
  • Loading branch information
nguyen-phillip committed Feb 28, 2018
1 parent ac50a06 commit 59f2f3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
17 changes: 12 additions & 5 deletions Restor/Controllers/ViewControllers/DownloadImageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,26 @@ - (MOLAuthenticatingURLSession *)createDownloadSession {
}

- (void)updateProgressDescription {
NSByteCountFormatter *bf = [[NSByteCountFormatter alloc] init];
bf.zeroPadsFractionDigits = YES;

// Calculate throughput, while guarding against division by zero.
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];
uint64_t currBytes = self.progress.completedUnitCount;
double throughput = (currBytes - self.startBytes) / (currTime - self.startTime);
NSTimeInterval dt = currTime - self.startTime;
NSString *throughput = @"";
if (dt > 0) {
throughput = [NSString stringWithFormat:@"(%@/sec)",
[bf stringFromByteCount:(currBytes - self.startBytes) / dt]];
}

// Update the localized description that will be displayed by DownloadImageViewController.
// NSProgress's localizedAdditionalDescription can auto format this same info, but it won't zero
// pad the fraction digits and updates too frequently, resulting in horrible vibrating text.
NSByteCountFormatter *bf = [[NSByteCountFormatter alloc] init];
bf.zeroPadsFractionDigits = YES;
self.progress.localizedDescription = [NSString stringWithFormat:@"%@ of %@ (%@/sec)",
self.progress.localizedDescription = [NSString stringWithFormat:@"%@ of %@ %@",
[bf stringFromByteCount:self.progress.completedUnitCount],
[bf stringFromByteCount:self.progress.totalUnitCount],
[bf stringFromByteCount:throughput]];
throughput];

// Reset values for next call.
self.startTime = currTime;
Expand Down
4 changes: 2 additions & 2 deletions Restor/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3</string>
<string>1.4</string>
<key>CFBundleVersion</key>
<string>1.3</string>
<string>1.4</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
4 changes: 2 additions & 2 deletions restord/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3</string>
<string>1.4</string>
<key>CFBundleVersion</key>
<string>4</string>
<string>5</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down

0 comments on commit 59f2f3e

Please sign in to comment.