Skip to content

Commit

Permalink
Fixing time display when there is no quote to be more normally formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
mmattozzi committed Mar 29, 2023
1 parent 0b5637a commit 3eb6ece
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions LiteraryClockScreenSaver/LiteraryClockScreenSaverView.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- (void) setupTextLayerPropertiesWithTextLayer:(CATextLayer *)layer textContents:(HighlightedQuote *)quote;
- (void) setupCitatationTextLayerPropertiesWithTextLayer:(CATextLayer *)layer textContents:(HighlightedQuote *)quote;
- (NSString*) createFormattedTime;
- (NSString*) createHumanFormattedTime;
- (BOOL) selectNextQuote;
- (void) resumeAnimation;

Expand Down
23 changes: 22 additions & 1 deletion LiteraryClockScreenSaver/LiteraryClockScreenSaverView.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ - (void) setupTextLayerPropertiesWithTextLayer:(CATextLayer *)layer textContents
[highlightedString addAttribute:NSFontAttributeName value:boldFontName range:[quote rangeOfHighlight]];
[highlightedString endEditing];
} else {
highlightedString = [[NSMutableAttributedString alloc] initWithString:[self createFormattedTime]];
highlightedString = [[NSMutableAttributedString alloc] initWithString:[self createHumanFormattedTime]];
[highlightedString beginEditing];
[self addBasicMainTextAttributes:highlightedString];
[highlightedString endEditing];
Expand Down Expand Up @@ -282,6 +282,27 @@ - (NSString*) createFormattedTime {
return formattedTime;
}

- (NSString*) createHumanFormattedTime {
NSDate* now = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];

NSString *ampm = @"am";
if (hour > 12) {
hour = hour - 12;
ampm = @"pm";
}

NSString *paddedHour = [NSString stringWithFormat:@"%02ld", hour];
NSString *paddedMinute = [NSString stringWithFormat:@"%02ld", minute];
NSString *formattedTime = [NSString stringWithFormat:@"%@:%@ %@", paddedHour, paddedMinute, ampm];

return formattedTime;
}

- (BOOL) selectNextQuote {
NSString *formattedTime = [self createFormattedTime];
BOOL quoteChanged = NO;
Expand Down

0 comments on commit 3eb6ece

Please sign in to comment.