Skip to content

Commit 60d998b

Browse files
committed
feat: Implement numeric comparisons for notification checks
1 parent 822fab7 commit 60d998b

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -369,35 +369,12 @@ private bool CheckAllMatch(List<Criteria> criteriaList)
369369
{
370370
foreach (var c in criteriaList)
371371
{
372-
if (c is Contains) // other criteria might be added such as NoLessThan and NoMoreThan.
373-
{
374-
if (CheckContains(c, true) == false) return false;
375-
}
376-
if (c is NotContains)
377-
{
378-
if (CheckContains(c, false) == false) return false;
379-
}
372+
var result = c.IsMatch();
373+
AppendToLog($"Check: {result}: '{c.Property}' {c.GetType().Name} '{c.Value}'");
374+
if (!result) return false;
380375
}
381376
return true;
382377
}
383-
384-
/// <summary>
385-
/// Returns true if a match is found and sense = true. For NotContains, use sense = false.
386-
/// </summary>
387-
/// <param name="criteria"></param>
388-
/// <param name="sense"></param>
389-
/// <returns></returns>
390-
private bool CheckContains(Criteria criteria, bool sense)
391-
{
392-
// If Property was a parameter, then use the expansion
393-
var content = ParameterDictionary.ContainsKey(criteria.Property)
394-
? ParameterDictionary[criteria.Property]
395-
: criteria.Property;
396-
397-
var result = content.IndexOf(criteria.Value, StringComparison.OrdinalIgnoreCase) > -1 == sense;
398-
LogCheckContains(criteria.Value, sense, content, result);
399-
return result;
400-
}
401378
#endregion
402379

403380
private void AddItemToPage(NotificationPage page, Item item)
@@ -668,7 +645,7 @@ public void ChangePage(int step)
668645
}
669646

670647
#region Logging
671-
public void LogOverrideParameters()
648+
void LogOverrideParameters()
672649
{
673650
if (Log == false) return;
674651

@@ -680,7 +657,7 @@ public void LogOverrideParameters()
680657
}
681658
}
682659

683-
public void LogParameters()
660+
void LogParameters()
684661
{
685662
if (Log == false) return;
686663

@@ -692,18 +669,12 @@ public void LogParameters()
692669
}
693670
}
694671

695-
public void LogNotification(Notification n)
672+
void LogNotification(Notification n)
696673
{
697674
AppendToLog($"\r\nNotification: {n.Title}");
698675
}
699676

700-
public void LogCheckContains(string value, bool sense, string content, bool result)
701-
{
702-
var negation = sense ? "" : "NOT ";
703-
AppendToLog($"Check: {result} = '{value}' {negation}contained in '{content}'");
704-
}
705-
706-
public void AppendToLog(string record)
677+
void AppendToLog(string record)
707678
{
708679
if (Log == false) return;
709680

Source/Menu/Notifications/Notifications.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,30 @@ public class AnyOf
8585
public List<Criteria> AllOfList { get; set; }
8686
}
8787

88-
class Contains : Criteria { }
89-
class NotContains : Criteria { }
88+
// These criteria are all doing an actual comparison
89+
class Contains : Criteria { public override bool IsMatch() => Property.Contains(Value); }
90+
class Equals : Criteria { public override bool IsMatch() => Property == Value; }
91+
class LessThan : NumericCriteria { public override bool IsMatch() => PropertyAsInt < ValueAsInt; }
92+
class MoreThan : NumericCriteria { public override bool IsMatch() => PropertyAsInt > ValueAsInt; }
9093

91-
// Not implemented as not needed yet
92-
// String comparison, not numerical
93-
class NoLessThan : Criteria { }
94-
class NoMoreThan : Criteria { }
95-
96-
public class Criteria
94+
// These criteria are all negated versions of those above
95+
class NotContains : Contains { public override bool IsMatch() => !base.IsMatch(); }
96+
class NotEquals : Equals { public override bool IsMatch() => !base.IsMatch(); }
97+
class MoreThanOrEquals : LessThan { public override bool IsMatch() => !base.IsMatch(); }
98+
class LessThanOrEquals : MoreThan { public override bool IsMatch() => !base.IsMatch(); }
99+
100+
abstract class NumericCriteria : Criteria
101+
{
102+
public int? PropertyAsInt => int.TryParse(Property, out var value) ? value : (int?)null;
103+
public int? ValueAsInt => int.TryParse(Value, out var value) ? value : (int?)null;
104+
}
105+
106+
public abstract class Criteria
97107
{
98-
// System Information "examples"
108+
// System Information "examples"
99109
public string Property { get; set; } // installed_version, direct3d, runtime, system, memory, cpu, gpu
100110
public string Value { get; set; } // {{new_version}}, {{10_0}}
111+
public abstract bool IsMatch();
101112
}
102113

103114
public class ParameterValue

0 commit comments

Comments
 (0)