Revision: 28336
          http://sourceforge.net/p/bibdesk/svn/28336
Author:   hofman
Date:     2023-09-12 21:52:15 +0000 (Tue, 12 Sep 2023)
Log Message:
-----------
Get cached dates for smart group conditions lazily, the document may not be set 
yet when the group is set on the condition

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

Modified: trunk/bibdesk/BDSKCondition.m
===================================================================
--- trunk/bibdesk/BDSKCondition.m       2023-09-12 15:35:25 UTC (rev 28335)
+++ trunk/bibdesk/BDSKCondition.m       2023-09-12 21:52:15 UTC (rev 28336)
@@ -55,7 +55,8 @@
 - (void)setCachedEndDate:(NSDate *)newCachedDate;
 - (NSDate *)cachedStartDate;
 - (void)setCachedStartDate:(NSDate *)newCachedDate;
-- (void)updateCachedDates;
+- (void)invalidateCachedDates;
+- (void)getCachedDatesIfNeeded;
 - (void)getStartDate:(NSDate **)startDate endDate:(NSDate **)endDate;
 - (void)refreshCachedDate:(NSTimer *)timer;
 @end
@@ -216,6 +217,7 @@
                return YES; // empty condition matches anything
        
     if ([self isDateCondition]) {
+        [self getCachedDatesIfNeeded];
         
         NSDate *date = nil;
         if ([key isEqualToString:BDSKDateAddedString])
@@ -409,7 +411,7 @@
         [key release];
         key = [(newKey ?: @"") copy];
         if ([self isDateCondition] != wasDateCondition)
-            [self updateCachedDates]; // remove the cached date and stop the 
timer
+            [self invalidateCachedDates]; // remove the cached date and stop 
the timer
     }
 }
 
@@ -542,7 +544,7 @@
 - (void)setDateComparison:(BDSKDateComparison)newComparison {
     if (dateComparison != newComparison) {
         dateComparison = newComparison;
-        [self updateCachedDates];
+        [self invalidateCachedDates];
     }
 }
 
@@ -549,7 +551,7 @@
 - (void)setNumberValue:(NSInteger)newNumber {
     if (numberValue != newNumber) {
         numberValue = newNumber;
-        [self updateCachedDates];
+        [self invalidateCachedDates];
     }
 }
 
@@ -556,7 +558,7 @@
 - (void)setAndNumberValue:(NSInteger)newNumber {
     if (andNumberValue != newNumber) {
         andNumberValue = newNumber;
-        [self updateCachedDates];
+        [self invalidateCachedDates];
     }
 }
 
@@ -563,7 +565,7 @@
 - (void)setPeriodValue:(BDSKPeriod)newPeriod {
     if (periodValue != newPeriod) {
         periodValue = newPeriod;
-        [self updateCachedDates];
+        [self invalidateCachedDates];
     }
 }
 
@@ -575,7 +577,7 @@
     if (dateValue != newDate) {
         [dateValue release];
         dateValue = [newDate retain];
-        [self updateCachedDates];
+        [self invalidateCachedDates];
     }
 }
 
@@ -587,7 +589,7 @@
     if (toDateValue != newDate) {
         [toDateValue release];
         toDateValue = [newDate retain];
-        [self updateCachedDates];
+        [self invalidateCachedDates];
     }
 }
 
@@ -655,7 +657,7 @@
     if (group != newGroup) {
         group = newGroup;
         if ([self isDateCondition])
-            [self updateCachedDates];
+            [self invalidateCachedDates];
     }
 }
 
@@ -697,23 +699,29 @@
        }
 }
 
-- (void)updateCachedDates {
+- (void)invalidateCachedDates {
+    [cacheTimer invalidate];
+    cacheTimer = nil;
+    
+    [self setCachedStartDate:nil];
+    [self setCachedEndDate:nil];
+}
+
+- (void)getCachedDatesIfNeeded {
+    if ([self isDateCondition] == NO || group == nil || cachedStartDate || 
cachedEndDate)
+        return;
+    
     NSDate *startDate = nil;
     NSDate *endDate = nil;
     
-    [cacheTimer invalidate];
-    cacheTimer = nil;
-    
-    if ([self isDateCondition]) {
-        [self getStartDate:&startDate endDate:&endDate];
-        if (dateComparison < BDSKDate && group) {
-            // 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(refreshCachedDate:) 
userInfo:NULL repeats:YES];
-            [[NSRunLoop currentRunLoop] addTimer:cacheTimer 
forMode:NSDefaultRunLoopMode];
-            [cacheTimer release];
-        }
+    [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(refreshCachedDate:) 
userInfo:NULL repeats:YES];
+        [[NSRunLoop currentRunLoop] addTimer:cacheTimer 
forMode:NSDefaultRunLoopMode];
+        [cacheTimer release];
     }
     
     [self setCachedStartDate:startDate];

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