Skip to content

Commit

Permalink
add publication date in Criteria+NSPredicate.swift
Browse files Browse the repository at this point in the history
this allows the predicate editor to correctly re-load "date published" predicates.
Also, make the SmartFolder code better by extracting the repeated array of Date expressions and remove superfluous space in predicate translation strings
  • Loading branch information
TAKeanice committed Aug 25, 2024
1 parent 4bdb0d6 commit d692efb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Vienna/Resources/Base.lproj/Predicates.strings
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"%[All,Any,None]@ of the following are true" = "Article matches %1$[all,any,none]@ of the following conditions";
"%[Author,Folder,Subject,Text]@ %[is,is not]@ %@" = "%1$[Author,Folder,Subject,Content]@ %2$[is,is not]@ %3$@";
"%[Author,Subject,Text]@ %[contains,does not contain]@ %@" = "%1$[Author,Subject,Content]@ %2$[contains,does not contain]@ %3$@";
"%[Date, PublicationDate]@ %[is,is greater than or equal to,is less than,is less than or equal to]@ %[yesterday]@" = "%1$[Last update, Date Published]@ %2$[is,is after or is,is before,is before or is]@ %3$[yesterday]@";
"%[Date, PublicationDate]@ %[is,is greater than,is greater than or equal to,is less than,is less than or equal to]@ %[last week]@" = "%1$[Last update, Date Published]@ %2$[is,is after,is after or is,is before,is before or is]@ %3$[last week]@";
"%[Date, PublicationDate]@ %[is,is less than,is less than or equal to]@ %[today]@" = "%1$[Last update, Date Published]@ %2$[is,is before,is before or is]@ %3$[today]@";
"%[Date, PublicationDate]@ %[less than ago,more than ago]@ %[days,hours,minutes,months,weeks,years]@ %@" = "%1$[Last update, Date Published]@ %2$[is in the last,is not in the last]@ %4$@ %3$[days,hours,minutes,months,weeks,years]@";
"%[Date,PublicationDate]@ %[is,is greater than or equal to,is less than,is less than or equal to]@ %[yesterday]@" = "%1$[Last update, Date Published]@ %2$[is,is after or is,is before,is before or is]@ %3$[yesterday]@";
"%[Date,PublicationDate]@ %[is,is greater than,is greater than or equal to,is less than,is less than or equal to]@ %[last week]@" = "%1$[Last update,Date Published]@ %2$[is,is after,is after or is,is before,is before or is]@ %3$[last week]@";
"%[Date,PublicationDate]@ %[is,is less than,is less than or equal to]@ %[today]@" = "%1$[Last update,Date Published]@ %2$[is,is before,is before or is]@ %3$[today]@";
"%[Date,PublicationDate]@ %[less than ago,more than ago]@ %[days,hours,minutes,months,weeks,years]@ %@" = "%1$[Last update,Date Published]@ %2$[is in the last,is not in the last]@ %4$@ %3$[days,hours,minutes,months,weeks,years]@";
"%[Deleted,Flagged,HasEnclosure,Read]@ is %[No,Yes]@" = "%1$[Is deleted,Is flagged,Has enclosure,Is read]@ %2$[No,Yes]@";
21 changes: 9 additions & 12 deletions Vienna/Sources/Alerts/SmartFolder.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,14 @@ - (void)prepareTemplates

[rowTemplates addObject:[VNASeparatorPredicateEditorRowTemplate new]];


NSArray *dateLeftExpressions = @[
[NSExpression expressionForConstantValue:MA_Field_LastUpdate],
[NSExpression expressionForConstantValue:MA_Field_PublicationDate]];

// date < / > days / weeks / months / years old
NSPredicateEditorRowTemplate *dateCompareTemplate = [[VNADateWithUnitPredicateEditorRowTemplate alloc]
initWithLeftExpressions:@[
[NSExpression expressionForConstantValue:MA_Field_LastUpdate],
[NSExpression expressionForConstantValue:MA_Field_PublicationDate]]];
initWithLeftExpressions:dateLeftExpressions];
[rowTemplates addObject:dateCompareTemplate];

// date = / < / <= today / yesterday / lastWeek
Expand All @@ -189,9 +192,7 @@ - (void)prepareTemplates
@(NSLessThanOrEqualToPredicateOperatorType),
];
NSPredicateEditorRowTemplate *todayTemplate = [[NSPredicateEditorRowTemplate alloc]
initWithLeftExpressions:@[
[NSExpression expressionForConstantValue:MA_Field_LastUpdate],
[NSExpression expressionForConstantValue:MA_Field_PublicationDate]]
initWithLeftExpressions:dateLeftExpressions
rightExpressions:todayRightExpressions
modifier:NSDirectPredicateModifier
operators:todayOperators
Expand All @@ -209,9 +210,7 @@ - (void)prepareTemplates
@(NSGreaterThanOrEqualToPredicateOperatorType)
];
NSPredicateEditorRowTemplate *yesterdayTemplate = [[NSPredicateEditorRowTemplate alloc]
initWithLeftExpressions:@[
[NSExpression expressionForConstantValue:MA_Field_LastUpdate],
[NSExpression expressionForConstantValue:MA_Field_PublicationDate]]
initWithLeftExpressions:dateLeftExpressions
rightExpressions:yesterdayRightExpressions
modifier:NSDirectPredicateModifier
operators:yesterdayOperators
Expand All @@ -230,9 +229,7 @@ - (void)prepareTemplates
@(NSLessThanOrEqualToPredicateOperatorType)
];
NSPredicateEditorRowTemplate *dateTemplate = [[NSPredicateEditorRowTemplate alloc]
initWithLeftExpressions:@[
[NSExpression expressionForConstantValue:MA_Field_LastUpdate],
[NSExpression expressionForConstantValue:MA_Field_PublicationDate]]
initWithLeftExpressions:dateLeftExpressions
rightExpressions:weekRightExpressions
modifier:NSDirectPredicateModifier
operators:weekOperators
Expand Down
10 changes: 6 additions & 4 deletions Vienna/Sources/Criteria/Criteria+NSPredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ extension Criteria: PredicateConvertible {
fallback = true
criteriaOperator = .equalTo
}
case MA_Field_LastUpdate:
case MA_Field_LastUpdate, MA_Field_PublicationDate:
switch predicate.predicateOperatorType {
case .lessThan:
criteriaOperator = .before
Expand Down Expand Up @@ -231,14 +231,15 @@ extension Criteria: PredicateConvertible {
let value = self.value
let operatorType = self.operatorType

if field == MA_Field_LastUpdate, let unit = DateUnit.allCases.first(where: { value.hasSuffix($0.rawValue) }) {
if field == MA_Field_LastUpdate || field == MA_Field_PublicationDate,
let unit = DateUnit.allCases.first(where: { value.hasSuffix($0.rawValue) }) {
let countString = value
.replacingOccurrences(of: unit.rawValue, with: "")
.trimmingCharacters(in: CharacterSet.whitespaces)
guard let count = UInt(countString) else {
fatalError("malformed criteria value \(value)")
}
return DatePredicateWithUnit(field: MA_Field_LastUpdate, comparisonOperator: operatorType, count: count, unit: unit)
return DatePredicateWithUnit(field: field, comparisonOperator: operatorType, count: count, unit: unit)
} else {
return buildComparisonPredicate(field, value, operatorType)
}
Expand Down Expand Up @@ -291,7 +292,8 @@ extension Criteria: PredicateConvertible {
// TODO: constants for fixed criteria values also for Criteria+SQL,
// e.g. YES, NO, yesterday, today, last week, ...

if field == MA_Field_LastUpdate && operatorType == .after && value == DateOffset.yesterday.rawValue {
if (field == MA_Field_LastUpdate || field == MA_Field_PublicationDate)
&& operatorType == .after && value == DateOffset.yesterday.rawValue {
// Use canonical "is today" instead of "is after yesterday"
comparisonPredicate = NSComparisonPredicate(leftExpression: left, rightExpression: NSExpression(forConstantValue: DateOffset.today), modifier: .direct, type: .equalTo)
} else if operatorType == .notEqualTo && (value == "No" || value == "Yes") {
Expand Down

0 comments on commit d692efb

Please sign in to comment.