Revision: 28348
          http://sourceforge.net/p/bibdesk/svn/28348
Author:   hofman
Date:     2023-09-17 15:32:04 +0000 (Sun, 17 Sep 2023)
Log Message:
-----------
fire cached date timer for condition 1 sec after the end of the current period 
rather than  each day. Retain the timer to be sure.

Modified Paths:
--------------
    trunk/bibdesk/BDSKCondition.m
    trunk/bibdesk/NSDate_BDSKExtensions.h
    trunk/bibdesk/NSDate_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKCondition.m
===================================================================
--- trunk/bibdesk/BDSKCondition.m       2023-09-14 21:47:53 UTC (rev 28347)
+++ trunk/bibdesk/BDSKCondition.m       2023-09-17 15:32:04 UTC (rev 28348)
@@ -161,7 +161,7 @@
 - (void)dealloc {
        //NSLog(@"dealloc condition");
     [cacheTimer invalidate];
-    cacheTimer = nil;
+    BDSKDESTROY(cacheTimer);
     BDSKDESTROY(key);
     BDSKDESTROY(stringValue);
     BDSKDESTROY(authorValue);
@@ -724,8 +724,7 @@
 
 - (void)invalidateCachedDates {
     [cacheTimer invalidate];
-    cacheTimer = nil;
-    
+    BDSKDESTROY(cacheTimer);
     BDSKDESTROY(cachedStartDate);
     BDSKDESTROY(cachedEndDate);
 }
@@ -756,6 +755,12 @@
         changed = YES;
     }
     
+    NSDate *fireDate = [[NSDate date] startOfPeriod:periodValue byAdding:1 
seconds:1];
+    [cacheTimer invalidate];
+    BDSKDESTROY(cacheTimer);
+    cacheTimer = [[NSTimer alloc] initWithFireDate:fireDate interval:0.0 
target:self selector:@selector(refreshCachedDates:) userInfo:NULL repeats:NO];
+    [[NSRunLoop currentRunLoop] addTimer:cacheTimer 
forMode:NSDefaultRunLoopMode];
+    
     if (changed && group) {
         [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKFilterChangedNotification object:group];
     }
@@ -769,13 +774,11 @@
     NSDate *endDate = nil;
     
     [self getStartDate:&startDate endDate:&endDate];
-    if (dateComparison < BDSKDate) {
-        // we fire every day at 1 second past midnight, because the condition 
changes at midnight
-        NSTimeInterval refreshInterval = 24 * 3600;
-        NSDate *fireDate = [[[NSDate date] startOfPeriod:BDSKPeriodDay] 
dateByAddingTimeInterval:refreshInterval + 1];
-        cacheTimer = [[NSTimer alloc] initWithFireDate:fireDate 
interval:refreshInterval target:self selector:@selector(refreshCachedDates:) 
userInfo:NULL repeats:YES];
+    if (dateComparison < BDSKDate && (startDate || endDate)) {
+        // we fire 1 second past midnight after the end of the current period, 
because the condition changes at midnight
+        NSDate *fireDate = [[NSDate date] startOfPeriod:periodValue byAdding:1 
seconds:1];
+        cacheTimer = [[NSTimer alloc] initWithFireDate:fireDate interval:0.0 
target:self selector:@selector(refreshCachedDates:) userInfo:NULL repeats:NO];
         [[NSRunLoop currentRunLoop] addTimer:cacheTimer 
forMode:NSDefaultRunLoopMode];
-        [cacheTimer release];
     }
     
     cachedStartDate = [startDate retain];

Modified: trunk/bibdesk/NSDate_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/NSDate_BDSKExtensions.h       2023-09-14 21:47:53 UTC (rev 
28347)
+++ trunk/bibdesk/NSDate_BDSKExtensions.h       2023-09-17 15:32:04 UTC (rev 
28348)
@@ -73,5 +73,6 @@
 
 - (NSDate *)startOfPeriod:(BDSKPeriod)period;
 - (NSDate *)startOfPeriod:(BDSKPeriod)period byAdding:(NSInteger)offset;
+- (NSDate *)startOfPeriod:(BDSKPeriod)period byAdding:(NSInteger)offset 
seconds:(NSInteger)seconds;
 
 @end

Modified: trunk/bibdesk/NSDate_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSDate_BDSKExtensions.m       2023-09-14 21:47:53 UTC (rev 
28347)
+++ trunk/bibdesk/NSDate_BDSKExtensions.m       2023-09-17 15:32:04 UTC (rev 
28348)
@@ -265,7 +265,7 @@
     return [formatter stringFromDate:self];
 }
 
-- (NSDate *)startOfPeriod:(BDSKPeriod)period byAdding:(NSInteger)offset {
+- (NSDate *)startOfPeriod:(BDSKPeriod)period byAdding:(NSInteger)offset 
seconds:(NSInteger)seconds {
     NSCalendar *calendar = [NSCalendar currentCalendar];
     NSUInteger unitFlags;
     if (period == BDSKPeriodWeek)
@@ -299,33 +299,40 @@
     NSDate *date = [calendar dateFromComponents:components];
     
     if (offset != 0) {
-        NSCalendarUnit unit = 0;
+        components = [[NSDateComponents alloc] init];
+        [components setHour:0];
+        [components setMinute:0];
+        [components setSecond:seconds];
         switch (period) {
             case BDSKPeriodDay:
-                unit = NSCalendarUnitDay;
+                [components setDay:offset];
                 break;
             case BDSKPeriodWeek:
-                unit = NSCalendarUnitWeekOfYear;
+                [components setWeekOfYear:offset];
                 break;
             case BDSKPeriodMonth:
-                unit = NSCalendarUnitMonth;
+                [components setMonth:offset];
                 break;
             case BDSKPeriodYear:
-                unit = NSCalendarUnitYear;
+                [components setYear:offset];
                 break;
             default:
                 NSLog(@"Unknown period %ld", (long)period);
                 break;
         }
-        if (unit != 0)
-            date = [calendar dateByAddingUnit:unit value:offset toDate:date 
options:0];
+        date = [calendar dateByAddingComponents:components toDate:date 
options:0];
+        [components release];
     }
     
     return date;
 }
 
+- (NSDate *)startOfPeriod:(BDSKPeriod)period byAdding:(NSInteger)offset {
+    return [self startOfPeriod:period byAdding:offset seconds:0];
+}
+
 - (NSDate *)startOfPeriod:(BDSKPeriod)period {
-    return [self startOfPeriod:period byAdding:0];
+    return [self startOfPeriod:period byAdding:0 seconds:0];
 }
 
 @end

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to