|
| 1 | +// |
| 2 | +// PBGitCommitDateFormatter.m |
| 3 | +// GitX |
| 4 | +// |
| 5 | + |
| 6 | +#import "PBGitCommitDateFormatter.h" |
| 7 | + |
| 8 | +NSString *const PBGitCommitDateFormatDidChangeNotification = @"PBGitCommitDateFormatDidChangeNotification"; |
| 9 | + |
| 10 | +// The history list makes one of these per visible row, so the built formatter is |
| 11 | +// shared by all of them and rebuilt only when the preference actually moves. |
| 12 | +static PBCommitDateFormatSetting builtSetting; |
| 13 | +static NSString *builtCustomFormat; |
| 14 | +static NSDateFormatter *builtDateFormatter; |
| 15 | + |
| 16 | +static PBCommitDateFormatSetting announcedSetting; |
| 17 | +static NSString *announcedCustomFormat; |
| 18 | + |
| 19 | +static BOOL formatsAreEqual(NSString *one, NSString *other) |
| 20 | +{ |
| 21 | + return (one == other) || [one isEqualToString:other]; |
| 22 | +} |
| 23 | + |
| 24 | +@implementation PBGitCommitDateFormatter |
| 25 | + |
| 26 | ++ (void)initialize |
| 27 | +{ |
| 28 | + if (self != [PBGitCommitDateFormatter class]) |
| 29 | + return; |
| 30 | + |
| 31 | + announcedSetting = [PBGitDefaults commitDateFormat]; |
| 32 | + announcedCustomFormat = [[PBGitDefaults commitDateCustomFormat] copy]; |
| 33 | + |
| 34 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 35 | + selector:@selector(userDefaultsChanged:) |
| 36 | + name:NSUserDefaultsDidChangeNotification |
| 37 | + object:nil]; |
| 38 | +} |
| 39 | + |
| 40 | +// Every defaults write reaches here, so the date preference is compared against |
| 41 | +// the one last announced before anything is said about it. |
| 42 | ++ (void)userDefaultsChanged:(NSNotification *)notification |
| 43 | +{ |
| 44 | + PBCommitDateFormatSetting setting = [PBGitDefaults commitDateFormat]; |
| 45 | + NSString *customFormat = [PBGitDefaults commitDateCustomFormat]; |
| 46 | + |
| 47 | + if ((setting == announcedSetting) && formatsAreEqual(customFormat, announcedCustomFormat)) |
| 48 | + return; |
| 49 | + |
| 50 | + announcedSetting = setting; |
| 51 | + announcedCustomFormat = [customFormat copy]; |
| 52 | + |
| 53 | + [[NSNotificationCenter defaultCenter] postNotificationName:PBGitCommitDateFormatDidChangeNotification object:nil]; |
| 54 | +} |
| 55 | + |
| 56 | ++ (NSDateFormatter *)dateFormatterForSetting:(PBCommitDateFormatSetting)setting customFormat:(NSString *)customFormat |
| 57 | +{ |
| 58 | + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; |
| 59 | + |
| 60 | + NSString *pattern = [customFormat stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; |
| 61 | + if ((setting == PBCommitDateFormatCustom) && pattern.length) { |
| 62 | + formatter.dateFormat = customFormat; |
| 63 | + return formatter; |
| 64 | + } |
| 65 | + |
| 66 | + formatter.timeStyle = NSDateFormatterShortStyle; |
| 67 | + |
| 68 | + switch (setting) { |
| 69 | + case PBCommitDateFormatShort: |
| 70 | + formatter.dateStyle = NSDateFormatterShortStyle; |
| 71 | + break; |
| 72 | + case PBCommitDateFormatMedium: |
| 73 | + formatter.dateStyle = NSDateFormatterMediumStyle; |
| 74 | + break; |
| 75 | + default: |
| 76 | + formatter.dateStyle = NSDateFormatterLongStyle; |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + return formatter; |
| 81 | +} |
| 82 | + |
| 83 | +// The preference is read rather than waited for, so a formatter built here is |
| 84 | +// the one the preference asks for however the setting got there. |
| 85 | ++ (NSDateFormatter *)currentDateFormatter |
| 86 | +{ |
| 87 | + PBCommitDateFormatSetting setting = [PBGitDefaults commitDateFormat]; |
| 88 | + NSString *customFormat = (setting == PBCommitDateFormatCustom) ? [PBGitDefaults commitDateCustomFormat] : nil; |
| 89 | + |
| 90 | + if (!builtDateFormatter || (setting != builtSetting) || !formatsAreEqual(customFormat, builtCustomFormat)) { |
| 91 | + builtSetting = setting; |
| 92 | + builtCustomFormat = [customFormat copy]; |
| 93 | + builtDateFormatter = [self dateFormatterForSetting:setting customFormat:customFormat]; |
| 94 | + } |
| 95 | + |
| 96 | + return builtDateFormatter; |
| 97 | +} |
| 98 | + |
| 99 | +- (NSString *)stringForObjectValue:(id)date |
| 100 | +{ |
| 101 | + if (![date isKindOfClass:[NSDate class]]) |
| 102 | + return nil; |
| 103 | + |
| 104 | + return [[[self class] currentDateFormatter] stringFromDate:date]; |
| 105 | +} |
| 106 | + |
| 107 | +@end |
0 commit comments