From ce88971789fc9a26a2a0bbc9c53502f6dda18ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wei=C3=9F?= Date: Thu, 6 May 2021 14:26:47 +0200 Subject: [PATCH] Replace custom time formatting code in TimeField with SecondsFormatter. --- Window/TimeField.m | 80 ++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/Window/TimeField.m b/Window/TimeField.m index 277ad90cf..ad2f04356 100644 --- a/Window/TimeField.m +++ b/Window/TimeField.m @@ -8,61 +8,43 @@ #import "TimeField.h" +#import "SecondsFormatter.h" + + static NSString *kTimerModeKey = @"timerShowTimeRemaining"; -NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) { - const int64_t signed_total_seconds = (int64_t)timeInterval; - const bool need_minus_sign = signbit(timeInterval); - const int64_t total_seconds = (need_minus_sign ? -1 : 1) * signed_total_seconds; - const int64_t seconds = total_seconds % 60; - const int64_t total_minutes = (total_seconds - seconds) / 60; - const int64_t minutes = total_minutes % 60; - const int64_t total_hours = (total_minutes - minutes) / 60; - const int64_t hours = total_hours % 24; - const int64_t days = (total_hours - hours) / 24; - - NSString *timeString = nil; - - if (days > 0) { - timeString = - [NSString localizedStringWithFormat:@"%s" "%" PRIi64 ":" "%02" PRIi64 ":" "%02" PRIi64 ":" "%02" PRIi64, - need_minus_sign ? "-" : "", - days, - hours, - minutes, - seconds]; - } - else if (hours > 0) { - timeString = - [NSString localizedStringWithFormat:@"%s" "%" PRIi64 ":" "%02" PRIi64 ":" "%02" PRIi64, - need_minus_sign ? "-" : "", - hours, - minutes, - seconds]; - } - else if (minutes > 0) { - timeString = - [NSString localizedStringWithFormat:@"%s" "%" PRIi64 ":" "%02" PRIi64, - need_minus_sign ? "-" : "", - minutes, - seconds]; - } - else { - timeString = - [NSString localizedStringWithFormat:@"%s" "0" ":" "%02" PRIi64, - need_minus_sign ? "-" : "", - seconds]; - } - - return timeString; -} - @implementation TimeField { BOOL showTimeRemaining; NSDictionary *fontAttributes; + SecondsFormatter *secondsFormatter; } +- (instancetype)initWithCoder:(NSCoder *)aDecoder +{ + if (self = [super initWithCoder:aDecoder]) + { + [self commonInit]; + } + + return self; +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) + { + [self commonInit]; + } + + return self; +} + +- (void)commonInit +{ + secondsFormatter = [[SecondsFormatter alloc] init]; +} + - (void)awakeFromNib { showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey]; @@ -74,7 +56,7 @@ NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) { if (showTimeRemaining == NO) { NSTimeInterval sec = self.currentTime; - text = timeStringForTimeInterval(sec); + text = [secondsFormatter stringForTimeInterval:sec]; } else { @@ -82,7 +64,7 @@ NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) { // NOTE: The floating point standard has support for negative zero. // We use that to enforce the sign prefix. if (sec == 0.0) { sec = -0.0; } - text = timeStringForTimeInterval(sec); + text = [secondsFormatter stringForTimeInterval:sec]; } NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:fontAttributes];