Changeset 14341


Ignore:
Timestamp:
Oct 17, 2014, 5:12:00 AM (7 years ago)
Author:
livings124
Message:

Yosemite: use NSDateComponentsFormatter instead of custom time string code

Location:
trunk/macosx
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/InfoActivityViewController.m

    r14052 r14341  
    2424
    2525#import "InfoActivityViewController.h"
     26#import "NSApplicationAdditions.h"
    2627#import "NSStringAdditions.h"
    2728#import "PiecesView.h"
     
    186187        [fDateAddedField setObjectValue: [torrent dateAdded]];
    187188       
    188         [fDownloadTimeField setStringValue: [NSString timeString: [torrent secondsDownloading] showSeconds: YES]];
    189         [fSeedTimeField setStringValue: [NSString timeString: [torrent secondsSeeding] showSeconds: YES]];
     189        if ([NSApp isOnYosemiteOrBetter]) {
     190            static NSDateComponentsFormatter *timeFormatter;
     191            static dispatch_once_t onceToken;
     192            dispatch_once(&onceToken, ^{
     193                timeFormatter = [NSDateComponentsFormatter new];
     194                timeFormatter.unitsStyle = NSDateComponentsFormatterUnitsStyleShort;
     195                timeFormatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
     196                timeFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropLeading;
     197            });
     198           
     199            [fDownloadTimeField setStringValue: [timeFormatter stringFromTimeInterval:[torrent secondsDownloading]]];
     200            [fSeedTimeField setStringValue: [timeFormatter stringFromTimeInterval:[torrent secondsSeeding]]];
     201        }
     202        else {
     203            [fDownloadTimeField setStringValue: [NSString timeString: [torrent secondsDownloading] includesTimeRemainingPhrase:NO showSeconds: YES]];
     204            [fSeedTimeField setStringValue: [NSString timeString: [torrent secondsSeeding] includesTimeRemainingPhrase:NO showSeconds: YES]];
     205        }
    190206       
    191207        [fPiecesView updateView];
  • trunk/macosx/NSStringAdditions.h

    r14045 r14341  
    4141+ (NSString *) percentString: (CGFloat) progress longDecimals: (BOOL) longDecimals;
    4242
    43 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds;
    44 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max;
     43+ (NSString *) timeString: (uint64_t) seconds includesTimeRemainingPhrase: (BOOL) includesTimeRemainingPhrase showSeconds: (BOOL) showSeconds;
     44+ (NSString *) timeString: (uint64_t) seconds includesTimeRemainingPhrase: (BOOL) includesTimeRemainingPhrase showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max;
    4545
    4646- (NSComparisonResult) compareNumeric: (NSString *) string; //simple compare method for strings with numbers (works for IP addresses)
  • trunk/macosx/NSStringAdditions.m

    r14045 r14341  
    154154}
    155155
    156 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
    157 {
    158     return [NSString timeString: seconds showSeconds: showSeconds maxFields: NSUIntegerMax];
    159 }
    160 
    161 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
    162 {
     156+ (NSString *) timeString: (uint64_t) seconds includesTimeRemainingPhrase: (BOOL) includesTimeRemainingPhrase showSeconds: (BOOL) showSeconds
     157{
     158    return [NSString timeString: seconds
     159    includesTimeRemainingPhrase: includesTimeRemainingPhrase
     160                    showSeconds: showSeconds
     161                      maxFields: NSUIntegerMax];
     162}
     163
     164+ (NSString *) timeString: (uint64_t) seconds includesTimeRemainingPhrase: (BOOL) includesTimeRemainingPhrase showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
     165{
     166    NSAssert(![NSApp isOnYosemiteOrBetter], @"you should be using NSDateComponentsFormatter on >= 10.10");
    163167    NSParameterAssert(max > 0);
    164168   
     
    201205        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
    202206   
    203     return [timeArray componentsJoinedByString: @" "];
     207    NSString * timeString = [timeArray componentsJoinedByString: @" "];
     208   
     209    if (includesTimeRemainingPhrase) {
     210        timeString = [NSString stringWithFormat: NSLocalizedString(@"%@ remaining", "time remaining string"), timeString];
     211    }
     212   
     213    return timeString;
    204214}
    205215
  • trunk/macosx/StatsWindowController.m

    r13526 r14341  
    197197    [fRatioAllField setStringValue: totalRatioString];
    198198   
    199     [fTimeField setStringValue: [NSString timeString: statsSession.secondsActive showSeconds: NO]];
    200     [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [NSString timeString: statsAll.secondsActive showSeconds: NO]]];
     199    if ([NSApp isOnYosemiteOrBetter]) {
     200        static NSDateComponentsFormatter *timeFormatter;
     201        static dispatch_once_t onceToken;
     202        dispatch_once(&onceToken, ^{
     203            timeFormatter = [NSDateComponentsFormatter new];
     204            timeFormatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
     205            timeFormatter.maximumUnitCount = 3;
     206            timeFormatter.allowedUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
     207            timeFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropLeading;
     208        });
     209       
     210        [fTimeField setStringValue: [timeFormatter stringFromTimeInterval:statsSession.secondsActive]];
     211        [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [timeFormatter stringFromTimeInterval:statsAll.secondsActive]]];
     212    }
     213    else {
     214        [fTimeField setStringValue: [NSString timeString: statsSession.secondsActive includesTimeRemainingPhrase:NO showSeconds: NO]];
     215        [fTimeAllField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%@ total", "stats total"), [NSString timeString: statsAll.secondsActive includesTimeRemainingPhrase:NO showSeconds: NO]]];
     216    }
    201217   
    202218    if (statsAll.sessionCount == 1)
  • trunk/macosx/Torrent.m

    r14290 r14341  
    2626#import "GroupsController.h"
    2727#import "FileListNode.h"
     28#import "NSApplicationAdditions.h"
    2829#import "NSStringAdditions.h"
    2930#import "TrackerNode.h"
     
    19811982        return NSLocalizedString(@"remaining time unknown", "Torrent -> eta string");
    19821983   
    1983     NSString * idleString = [NSString stringWithFormat: NSLocalizedString(@"%@ remaining", "Torrent -> eta string"),
    1984                                 [NSString timeString: eta showSeconds: YES maxFields: 2]];
    1985     if (fromIdle)
     1984    NSString * idleString;
     1985   
     1986    if ([NSApp isOnYosemiteOrBetter]) {
     1987        static NSDateComponentsFormatter *formatter;
     1988        static dispatch_once_t onceToken;
     1989        dispatch_once(&onceToken, ^{
     1990            formatter = [NSDateComponentsFormatter new];
     1991            formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleShort;
     1992            formatter.maximumUnitCount = 2;
     1993            formatter.collapsesLargestUnit = YES;
     1994            formatter.includesTimeRemainingPhrase = YES;
     1995        });
     1996       
     1997        idleString = [formatter stringFromTimeInterval: eta];
     1998    }
     1999    else {
     2000        idleString = [NSString timeString: eta includesTimeRemainingPhrase: YES showSeconds: YES maxFields: 2];
     2001    }
     2002   
     2003    if (fromIdle) {
    19862004        idleString = [idleString stringByAppendingFormat: @" (%@)", NSLocalizedString(@"inactive", "Torrent -> eta string")];
     2005    }
    19872006   
    19882007    return idleString;
  • trunk/macosx/TrackerNode.m

    r13251 r14341  
    2424
    2525#import "TrackerNode.h"
     26#import "NSApplicationAdditions.h"
    2627#import "NSStringAdditions.h"
    2728
     
    157158       
    158159        case TR_TRACKER_WAITING:
     160        {
     161            const NSTimeInterval nextAnnounceTimeLeft = fStat.nextAnnounceTime - [[NSDate date] timeIntervalSince1970];
     162           
     163            NSString *timeString;
     164            if ([NSApp isOnYosemiteOrBetter]) {
     165                static NSDateComponentsFormatter *formatter;
     166                static dispatch_once_t onceToken;
     167                dispatch_once(&onceToken, ^{
     168                    formatter = [NSDateComponentsFormatter new];
     169                    formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated;
     170                    formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropLeading;
     171                    formatter.collapsesLargestUnit = YES;
     172                });
     173               
     174                timeString = [formatter stringFromTimeInterval: nextAnnounceTimeLeft];
     175            }
     176            else {
     177                timeString = [NSString timeString: nextAnnounceTimeLeft includesTimeRemainingPhrase: NO showSeconds: YES];
     178            }
    159179            return [NSString stringWithFormat: NSLocalizedString(@"Next announce in %@", "Tracker next announce"),
    160                     [NSString timeString: fStat.nextAnnounceTime - [[NSDate date] timeIntervalSince1970] showSeconds: YES]];
    161        
     180                    timeString];
     181        }
    162182        case TR_TRACKER_QUEUED:
    163183            return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
Note: See TracChangeset for help on using the changeset viewer.