From 147906f067728c0083821acde171ba49cbfe8f33 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Mon, 27 Jul 2015 16:45:02 -0700 Subject: [PATCH] Use NSByteCountFormatter if available. --- Sparkle/SUUIBasedUpdateDriver.m | 54 ++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index b1e09370..9a3524c4 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -14,6 +14,21 @@ #import "SUStatusController.h" #import "SUConstants.h" +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1080 +@interface NSByteCountFormatter : NSFormatter { +@private + unsigned int _allowedUnits; + char _countStyle; + BOOL _allowsNonnumericFormatting, _includesUnit, _includesCount, _includesActualByteCount, + _adaptive, _zeroPadsFractionDigits; + int _formattingContext; + int _reserved[5]; +} ++ (NSString *)stringFromByteCount:(long long)byteCount + countStyle:(NSByteCountFormatterCountStyle)countStyle; +@end +#endif + @interface SUUIBasedUpdateDriver () @property (strong) SUStatusController *statusController; @@ -111,30 +126,41 @@ [self.statusController setMaxProgressValue:[response expectedContentLength]]; } -- (NSString *)humanReadableSizeFromDouble:(double)value +- (NSString *)localizedStringFromByteCount:(long long)value { - if (value < 1000) { - return [NSString stringWithFormat:@"%.0lf %@", value, SULocalizedString(@"B", @"the unit for bytes")]; + SInt32 minor = 0; + Gestalt(gestaltSystemVersionMinor, &minor); + if (minor < 8) { + if (value < 1000) { + return [NSString stringWithFormat:@"%.0lf %@", value / 1.0, + SULocalizedString(@"B", @"the unit for bytes")]; + } + + if (value < 1000 * 1000) { + return [NSString stringWithFormat:@"%.0lf %@", value / 1000.0, + SULocalizedString(@"KB", @"the unit for kilobytes")]; + } + + if (value < 1000 * 1000 * 1000) { + return [NSString stringWithFormat:@"%.1lf %@", value / 1000.0 / 1000.0, + SULocalizedString(@"MB", @"the unit for megabytes")]; + } + + return [NSString stringWithFormat:@"%.2lf %@", value / 1000.0 / 1000.0 / 1000.0, + SULocalizedString(@"GB", @"the unit for gigabytes")]; } - if (value < 1000 * 1000) { - return [NSString stringWithFormat:@"%.0lf %@", value / 1000.0, SULocalizedString(@"KB", @"the unit for kilobytes")]; - } - - if (value < 1000 * 1000 * 1000) { - return [NSString stringWithFormat:@"%.1lf %@", value / 1000.0 / 1000.0, SULocalizedString(@"MB", @"the unit for megabytes")]; - } - - return [NSString stringWithFormat:@"%.2lf %@", value / 1000.0 / 1000.0 / 1000.0, SULocalizedString(@"GB", @"the unit for gigabytes")]; + return [NSByteCountFormatter stringFromByteCount:value + countStyle:NSByteCountFormatterCountStyleFile]; } - (void)download:(NSURLDownload *)__unused download didReceiveDataOfLength:(NSUInteger)length { [self.statusController setProgressValue:[self.statusController progressValue] + (double)length]; if ([self.statusController maxProgressValue] > 0.0) - [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ of %@", nil), [self humanReadableSizeFromDouble:[self.statusController progressValue]], [self humanReadableSizeFromDouble:[self.statusController maxProgressValue]]]]; + [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ of %@", nil), [self localizedStringFromByteCount:(long long)self.statusController.progressValue], [self localizedStringFromByteCount:(long long)self.statusController.maxProgressValue]]]; else - [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ downloaded", nil), [self humanReadableSizeFromDouble:[self.statusController progressValue]]]]; + [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ downloaded", nil), [self localizedStringFromByteCount:(long long)self.statusController.progressValue]]]; } - (IBAction)cancelDownload:(id)__unused sender