Ignore:
Timestamp:
10/20/02 06:40:54 (20 years ago)
Author:
Nicholas Riley
Message:

Pester 1.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/Pester/Source/PSAlarms.m

    r26 r28  
    1111
    1212NSString * const PSAlarmsDidChangeNotification = @"PSAlarmsDidChangeNotification";
     13NSString * const PSAlarmsNextAlarmDidChangeNotification = @"PSAlarmsNextAlarmDidChangeNotification";
    1314
    1415static NSString * const PSPendingAlarms = @"Pester pending alarms"; // NSUserDefaults key
     
    1617static PSAlarms *PSAlarmsAllAlarms = nil;
    1718
     19@interface PSAlarms (Private)
     20
     21- (void)_updateNextAlarm;
     22
     23@end
     24
    1825@implementation PSAlarms
    1926
    20 + (void)setUp; // XXX if you can think of a better name, please be my guest.
     27+ (void)setUp;
    2128{
    2229    if (PSAlarmsAllAlarms == nil) {
    2330        PSAlarmsAllAlarms = [[self alloc] init];
     31        [PSAlarmsAllAlarms _updateNextAlarm]; // only generate notifications after singleton established
    2432    }
    2533}
     
    2735+ (PSAlarms *)allAlarms;
    2836{
     37    NSAssert(PSAlarmsAllAlarms != nil, @"Attempt to use +[PSAlarms allAlarms] before setup complete");
    2938    return PSAlarmsAllAlarms;
     39}
     40
     41- (void)_updateNextAlarm;
     42{
     43    NSEnumerator *e;
     44    PSAlarm *alarm, *oldNextAlarm = nextAlarm;
     45    [nextAlarm release];
     46    nextAlarm = nil;
     47    // sort alarms so earliest is first
     48    [alarms sortUsingSelector: @selector(compare:)];
     49    // find first un-expired alarm
     50    e = [alarms objectEnumerator];
     51    while ( (alarm = [e nextObject]) != nil) {
     52        if ([alarm isValid]) {
     53            nextAlarm = [alarm retain];
     54            break;
     55        }
     56    }
     57    if (oldNextAlarm != nextAlarm)
     58        [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmsNextAlarmDidChangeNotification object: nextAlarm];
    3059}
    3160
     
    3362{
    3463    if ( (self = [super init]) != nil) {
     64        alarms = [[NSMutableArray alloc] init];
    3565        NS_DURING
    36             NSData *alarmData = [[NSUserDefaults standardUserDefaults] dataForKey: PSPendingAlarms];
    37             if (alarmData != nil) {
    38                 alarms = [[NSUnarchiver unarchiveObjectWithData: alarmData] retain];
     66            NSArray *alarmsData = [[NSUserDefaults standardUserDefaults] arrayForKey: PSPendingAlarms];
     67            NSEnumerator *e = [alarmsData objectEnumerator];
     68            NSData *alarmData;
     69            PSAlarm *alarm;
     70            while ( (alarmData = [e nextObject]) != nil) {
     71                alarm = [NSUnarchiver unarchiveObjectWithData: alarmData];
     72                if (alarm != nil)
     73                    [alarms addObject: alarm];
    3974            }
    4075        NS_HANDLER
     
    4277            NSLog(@"An error occurred while attempting to restore the alarm list: %@", localException);
    4378        NS_ENDHANDLER
    44         if (alarms == nil) alarms = [[NSMutableArray alloc] init];
    4579        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmTimerSet:) name: PSAlarmTimerSetNotification object: nil];
    4680        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmTimerExpired:) name: PSAlarmTimerExpiredNotification object: nil];
     
    5185- (void)dealloc;
    5286{
    53     [alarms dealloc];
     87    [alarms release];
    5488    [[NSNotificationCenter defaultCenter] removeObserver: self];
    5589    [super dealloc];
     
    5892- (void)_changed;
    5993{
    60     NSData *alarmData;
     94    NSMutableArray *alarmsData = [[NSMutableArray alloc] init];
    6195    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    62     [alarms sortUsingSelector: @selector(compare:)];
    63     alarmData = [NSArchiver archivedDataWithRootObject: alarms];
    64     [defaults setObject: alarmData forKey: PSPendingAlarms];
     96    NSEnumerator *e;
     97    PSAlarm *alarm;
     98    [self _updateNextAlarm];
     99    // NSLog(@"PSAlarms _changed:\n%@", alarms);
     100    // archive
     101    e = [alarms objectEnumerator];
     102    while ( (alarm = [e nextObject]) != nil) {
     103        [alarmsData addObject: [NSArchiver archivedDataWithRootObject: alarm]];
     104    }
     105    [defaults setObject: alarmsData forKey: PSPendingAlarms];
    65106    [defaults synchronize];
    66     // NSLog(@"PSAlarms changed: %@", alarms);
     107    [alarmsData release];
    67108    [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmsDidChangeNotification object: self];
    68109}
     
    80121}
    81122
     123- (PSAlarm *)nextAlarm;
     124{
     125    return nextAlarm;
     126}
     127
    82128- (int)alarmCount;
    83129{
     
    92138- (void)removeAlarmAtIndex:(int)index;
    93139{
     140    [(PSAlarm *)[alarms objectAtIndex: index] cancel];
    94141    [alarms removeObjectAtIndex: index];
    95142}
     
    99146    NSEnumerator *e = [indices objectEnumerator];
    100147    NSNumber *n;
    101     int indexCount = [indices count], i = 0;
     148    int indexCount = [indices count], i = 0, alarmIndex;
    102149    int *indexArray = (int *)malloc(indexCount * sizeof(int));
    103150    NS_DURING
    104151        while ( (n = [e nextObject]) != nil) {
    105             indexArray[i] = [n intValue];
     152            alarmIndex = [n intValue];
     153            [(PSAlarm *)[alarms objectAtIndex: alarmIndex] cancel];
     154            indexArray[i] = alarmIndex;
    106155            i++;
    107156        }
Note: See TracChangeset for help on using the changeset viewer.