Changeset 608 for trunk/Cocoa


Ignore:
Timestamp:
12/21/09 22:13:21 (15 years ago)
Author:
Nicholas Riley
Message:

Display "today" or "tomorrow" when setting an alarm.

Location:
trunk/Cocoa/Pester/Source
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/Pester/Source/PSAlarm.h

    r366 r608  
    4848
    4949- (NSCalendarDate *)date;
     50- (NSDate *)midnightOnDate;
    5051- (NSDate *)time;
     52- (int)daysFromToday;
    5153- (NSTimeInterval)interval;
    5254- (NSTimeInterval)timeRemaining;
  • trunk/Cocoa/Pester/Source/PSAlarm.m

    r602 r608  
    2929
    3030static NSDateFormatter *dateFormatter, *shortDateFormatter, *timeFormatter;
     31
     32static NSCalendar *gregorianCalendar;
     33
     34static NSDate *midnightOnDate(NSDate *date) {
     35    return [gregorianCalendar dateFromComponents:
     36            [gregorianCalendar components: NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit fromDate: date]];
     37}
    3138
    3239@implementation PSAlarm
     
    4653    [timeFormatter setTimeStyle: NSDateFormatterMediumStyle];
    4754    [timeFormatter setDateStyle: NSDateFormatterNoStyle];
     55    gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    4856}
    4957
     
    246254}
    247255
     256- (NSDate *)midnightOnDate;
     257{
     258    if (alarmType == PSAlarmInterval) [self _setDateFromInterval];
     259   
     260    return midnightOnDate(alarmDate);
     261}
     262
    248263- (NSDate *)time;
    249264{
     
    251266    if (alarmType == PSAlarmInterval) [self _setDateFromInterval];
    252267
    253     NSCalendar *calendar = [NSCalendar currentCalendar];
    254 
    255     return [calendar dateFromComponents:
    256             [calendar components: NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate: alarmDate]];
     268    return [gregorianCalendar dateFromComponents:
     269            [gregorianCalendar components: NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate: alarmDate]];
     270}
     271
     272- (int)daysFromToday;
     273{
     274    if (alarmType == PSAlarmInterval) [self _setDateFromInterval];
     275   
     276    return [[gregorianCalendar components: NSDayCalendarUnit fromDate: midnightOnDate([NSDate date]) toDate: alarmDate options: 0] day];
    257277}
    258278
  • trunk/Cocoa/Pester/Source/PSAlarmSetController.m

    r606 r608  
    165165{
    166166    // NSLog(@"updateDateDisplay: %@", sender);
    167     if ([alarm isValid]) {
    168         [self setStatus: [NSString stringWithFormat: @"Alarm will be set for %@\non %@.", [alarm timeString], [alarm dateString]]];
    169         [setButton setEnabled: YES];
    170         if (updateTimer == nil || ![updateTimer isValid]) {
    171             // XXX this logic (and the timer) should really go into PSAlarm, to send notifications for status updates instead.  Timer starts when people are watching, stops when people aren't.
    172             // NSLog(@"setting timer");
    173             if (isInterval) {
    174                 updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: YES];
    175             } else {
    176                 updateTimer = [NSTimer scheduledTimerWithTimeInterval: [alarm interval] target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: NO];
    177             }
    178             [updateTimer retain];
    179         }
    180     } else {
    181         [setButton setEnabled: NO];
    182         [self setStatus: [alarm invalidMessage]];
    183         [self _stopUpdateTimer];
     167    if (![alarm isValid]) {
     168        [setButton setEnabled: NO];
     169        [self setStatus: [alarm invalidMessage]];
     170        [self _stopUpdateTimer];
     171        return;
     172    }
     173   
     174    const int day = 60 * 60 * 24;
     175    int daysUntilAlarm = [alarm daysFromToday];
     176    NSString *onString;
     177    switch (daysUntilAlarm) {
     178        case 0: onString = @"today,"; break;
     179        case 1: onString = @"tomorrow,"; break;
     180        default: onString = @"on";
     181    }
     182   
     183    [self setStatus: [NSString stringWithFormat: @"Alarm will be set for %@\n%@ %@.", [alarm timeString], onString, [alarm dateString]]];
     184    [setButton setEnabled: YES];
     185    if (updateTimer == nil || ![updateTimer isValid]) {
     186        // XXX this logic (and the timer) should really go into PSAlarm, to send notifications for status updates instead.  Timer starts when people are watching, stops when people aren't.
     187        // NSLog(@"setting timer");
     188        if (isInterval) {
     189            updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: YES];
     190        } else {
     191            // XXX time/time zone change
     192            NSTimeInterval interval = [alarm interval];
     193            if (daysUntilAlarm < 2 && interval > day)
     194                interval = [[alarm midnightOnDate] timeIntervalSinceNow];
     195            updateTimer = [NSTimer scheduledTimerWithTimeInterval: interval target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: NO];
     196        }
     197        [updateTimer retain];
    184198    }
    185199}
Note: See TracChangeset for help on using the changeset viewer.