Skip to content

Commit 05d5892

Browse files
authored
Merge pull request #1058 from twpol/refactor/notification-parameters
Refactor notification parameters
2 parents 273f2ef + 626ebed commit 05d5892

File tree

2 files changed

+67
-81
lines changed

2 files changed

+67
-81
lines changed

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void CheckNotifications()
133133
}
134134
}
135135

136-
public Notifications GetNotifications()
136+
Notifications GetNotifications()
137137
{
138138
string notificationsSerial;
139139

@@ -437,68 +437,37 @@ private string GetUrl(Link link)
437437
return url;
438438
}
439439

440-
public void ReplaceParameters()
440+
void ReplaceParameters()
441441
{
442-
foreach (var n in Notifications.NotificationList)
443-
{
444-
n.Title = ReplaceParameter(n.Title);
445-
n.Date = ReplaceParameter(n.Date);
446-
n.ItemList?.ForEach(item => ReplaceItemParameter(item));
447-
}
448-
foreach (var list in Notifications.CheckList)
449-
{
450-
foreach(var c in list?.AnyOfList)
451-
{
452-
c?.AllOfList.ForEach(criteria => ReplaceCriteriaPropertyParameter(criteria));
453-
c?.AllOfList.ForEach(criteria => ReplaceCriteriaValueParameter(criteria));
454-
}
455-
}
442+
Notifications.ReplaceParameters(ReplaceParameterValues);
456443
}
457444

458-
private void ReplaceItemParameter(Item item)
459-
{
460-
if (item is Record record)
461-
record.Value = ReplaceParameter(record.Value);
462-
if (item is Link link)
463-
link.Value = ReplaceParameter(link.Value);
464-
if (item is Update update)
465-
update.Value = ReplaceParameter(update.Value);
466-
}
467-
468-
/// <summary>
469-
/// If Property is a parameter, remove {{..}} and add it and its replacement to the dictionary.
470-
/// </summary>
471-
/// <param name="criteria"></param>
472-
private void ReplaceCriteriaPropertyParameter(Criteria criteria)
445+
string ReplaceParameterValues(string value)
473446
{
474-
if (ContainsParameter(criteria.Property))
447+
if (value == null) return value;
448+
var start = 0;
449+
while ((start = value.IndexOf("{{", start)) >= 0)
475450
{
476-
criteria.Property = ReplaceParameter(criteria.Property);
451+
var end = value.IndexOf("}}", start);
452+
if (end == -1) break;
453+
var variable = value.Substring(start + 2, end - start - 2);
454+
var replacement = GetParameterValue(variable);
455+
value = value.Substring(0, start) + replacement + value.Substring(end + 2);
456+
start += replacement.Length;
477457
}
458+
return value;
478459
}
479460

480-
private void ReplaceCriteriaValueParameter(Criteria criteria)
481-
{
482-
criteria.Value = ReplaceParameter(criteria.Value);
483-
}
484-
485-
private string ReplaceParameter(string field)
461+
string GetParameterValue(string parameter)
486462
{
487-
if (ContainsParameter(field) == false) return field;
488-
489-
var parameterArray = field.Split('{', '}'); // 5 elements: prefix, "", target, "", suffix
490-
var target = parameterArray[2];
491-
var lowerCaseTarget = parameterArray[2].ToLower();
492463
string replacement;
493-
494-
// If found in dictionary, then use that else extract it from program
495-
if (ParameterDictionary.ContainsKey(lowerCaseTarget))
464+
if (ParameterDictionary.ContainsKey(parameter))
496465
{
497-
replacement = ParameterDictionary[lowerCaseTarget];
466+
replacement = ParameterDictionary[parameter];
498467
}
499468
else
500469
{
501-
switch (lowerCaseTarget)
470+
switch (parameter)
502471
{
503472
// Update parameters
504473
// Using "none" instead of "" so that records are readable.
@@ -555,23 +524,15 @@ private string ReplaceParameter(string field)
555524
replacement = GetInstalledRoutes();
556525
break;
557526
default:
558-
var propertyValue = GetSetting(target);
527+
var propertyValue = GetSetting(parameter);
559528
replacement = (propertyValue == "")
560-
? field // strings that are not recognised are not replaced.
529+
? "{{" + parameter + "}}" // strings that are not recognised are not replaced.
561530
: propertyValue.ToLower().Replace("false", "off").Replace("true", "on");
562531
break;
563532
}
564-
ParameterDictionary.Add(lowerCaseTarget, replacement);
533+
ParameterDictionary.Add(parameter, replacement);
565534
}
566-
567-
return parameterArray[0] + replacement + parameterArray[4];
568-
}
569-
570-
private bool ContainsParameter(string field)
571-
{
572-
if (field.Contains("{{") == false) return false;
573-
if (field.Contains("}}") == false) return false;
574-
return true;
535+
return replacement;
575536
}
576537

577538
/// <summary>
@@ -617,7 +578,7 @@ private string GetInstalledRoutes()
617578
return installedRouteList;
618579
}
619580

620-
public OverrideParameterList GetOverrideParameters()
581+
OverrideParameterList GetOverrideParameters()
621582
{
622583
// To support testing of a new remote notifications.json file before it is published,
623584
// GetNotifications tests first for a local file notifications_override_values.json

Source/Menu/Notifications/Notifications.cs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,38 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18+
using System;
1819
using System.Collections.Generic;
1920

20-
2121
namespace Menu.Notifications
2222
{
2323
public class Notifications
2424
{
2525
public List<Notification> NotificationList = new List<Notification>();
2626
public List<Check> CheckList = new List<Check>();
27-
}
28-
29-
class JsonInput
30-
{
31-
public List<Notification> NotificationList { get; set; }
32-
public List<Check> CheckList { get; set; }
27+
internal void ReplaceParameters(Func<string, string> replaceFunc)
28+
{
29+
NotificationList?.ForEach(item => item.ReplaceParameters(replaceFunc));
30+
CheckList?.ForEach(item => item.ReplaceParameters(replaceFunc));
31+
}
3332
}
3433

3534
public class Notification
3635
{
3736
public string Date { get; set; }
3837
public string Title { get; set; }
39-
public string UpdateMode { get; set; }
4038
public List<string> IncludeIf { get; set; }
4139
public List<string> IncludeIfNot { get; set; }
4240
public List<Item> ItemList { get; set; }
41+
internal void ReplaceParameters(Func<string, string> replaceFunc)
42+
{
43+
Date = replaceFunc(Date);
44+
Title = replaceFunc(Title);
45+
ItemList?.ForEach(item => item.ReplaceParameters(replaceFunc));
46+
}
4347
}
44-
class Record : Item
48+
class Record : ValueItem
4549
{
46-
public string Value { get; set; }
4750
}
4851
class Text : Item
4952
{
@@ -52,42 +55,59 @@ class Heading : Item
5255
{
5356
public new string Color { get; set; } = "blue";
5457
}
55-
class Link : Item
58+
class Link : ValueItem
5659
{
57-
public string Value { get; set; }
5860
public string Url { get; set; }
5961
public string StableUrl { get; set; }
6062
public string TestingUrl { get; set; }
6163
public string UnstableUrl { get; set; }
6264
}
63-
class Dialog : Item
65+
class Dialog : ValueItem
6466
{
65-
public string Value { get; set; }
6667
public string Form { get; set; }
6768
}
68-
class Update : Item
69+
class Update : ValueItem
70+
{
71+
}
72+
abstract class ValueItem : Item
6973
{
7074
public string Value { get; set; }
71-
public string UpdateMode { get; set; }
75+
internal override void ReplaceParameters(Func<string, string> replaceFunc)
76+
{
77+
base.ReplaceParameters(replaceFunc);
78+
Value = replaceFunc(Value);
79+
}
7280
}
73-
public class Item
81+
public abstract class Item
7482
{
7583
public List<string> IncludeIf { get; set; }
7684
public List<string> IncludeIfNot { get; set; }
7785
public string Label { get; set; }
7886
public string Color { get; set; } = "black";
7987
public int Indent { get; set; } = 140;
88+
internal virtual void ReplaceParameters(Func<string, string> replaceFunc)
89+
{
90+
Label = replaceFunc(Label);
91+
}
8092
}
8193

8294
public class Check
8395
{
8496
public string Id { get; set; }
8597
public List<AnyOf> AnyOfList { get; set; }
98+
internal void ReplaceParameters(Func<string, string> replaceFunc)
99+
{
100+
AnyOfList?.ForEach(item => item.ReplaceParameters(replaceFunc));
101+
}
86102
}
87103

88104
public class AnyOf
89105
{
90106
public List<Criteria> AllOfList { get; set; }
107+
internal void ReplaceParameters(Func<string, string> replaceFunc)
108+
{
109+
AllOfList?.ForEach(item => item.ReplaceParameters(replaceFunc));
110+
}
91111
}
92112

93113
// These criteria are all doing an actual comparison
@@ -114,15 +134,20 @@ public abstract class Criteria
114134
public string Property { get; set; } // installed_version, direct3d, runtime, system, memory, cpu, gpu
115135
public string Value { get; set; } // {{new_version}}, {{10_0}}
116136
public abstract bool IsMatch();
137+
internal void ReplaceParameters(Func<string, string> replaceFunc)
138+
{
139+
Property = replaceFunc(Property);
140+
Value = replaceFunc(Value);
141+
}
117142
}
118143

119-
public class ParameterValue
144+
class ParameterValue
120145
{
121146
public string Parameter { get; set; } // installed_version, direct3d, runtime, system, memory, cpu, gpu
122147
public string Value { get; set; } // {{new_version}}, {{10_0}}
123148
}
124149

125-
public class OverrideParameterList
150+
class OverrideParameterList
126151
{
127152
public List<ParameterValue> ParameterValueList = new List<ParameterValue>();
128153
}

0 commit comments

Comments
 (0)