1717
1818using System ;
1919using System . Collections . Generic ;
20+ using System . Drawing ;
2021using System . IO ;
22+ using System . Linq ;
2123using System . Net ;
24+ using System . Resources ;
2225using System . Text ;
26+ using System . Windows . Forms ;
2327using Newtonsoft . Json ;
2428using ORTS . Common ;
2529using ORTS . Settings ;
2630using ORTS . Updater ;
2731using static ORTS . Common . SystemInfo ;
2832using static ORTS . NotificationPage ;
29- using System . Drawing ;
30- using System . Windows . Forms ;
31- using System . Linq ;
32- using System . Diagnostics ;
33- using System . Resources ;
34- using ORTS . Properties ;
3533
36- // Notifications are read only once as a background task at start into NotificationList.
34+ // Behaviour
35+ // Notifications are read only once as a background task at start into Notifications.
3736// Every time the notifications page is re-visited, the position is discarded and first page is shown.
3837// Every time the notifications page is re-visited, the Update Mode option may have changed, so
3938// the visibility of each notification in NotificationList is re-assessed. Also its date.
40- // Every time the notifications page is re-visited or the page is incremented up or down,
41- // the panel is re-loaded with items for the current page.
39+ // Every time the user selects a different notification page, the panel is re-loaded with items for that page.
4240
4341namespace ORTS
4442{
4543 public class NotificationManager
4644 {
47- // The Page points to the MainForm.Panel which holds the items for the current notification.
45+ public Notifications Notifications ; // An object defined by the JSON schema
46+
47+ // The Page points to the MainForm.Panel and also holds details of the items for the current notification.
4848 public NotificationPage Page { get ; private set ; }
49+ public bool ArePagesVisible = false ;
50+ public int CurrentPageIndex = 0 ;
4951
5052 public class PageTracking
5153 {
@@ -55,13 +57,11 @@ public class PageTracking
5557 public int Viewed { get ; set ; } // Viewing always starts at the first, newest notification. The user sees (Count - Viewed)
5658 }
5759
58- public Notifications Notifications ; // An object defined by the JSON schema
59- public int CurrentNotificationNo = 0 ;
60- public bool ArePagesVisible = false ;
61-
62- private Exception Error ;
60+ // Parameters (e.g. {{installed_version}} are saved in the dictionary alongside their value.
61+ // They are written once and potentially read many times.
6362 private Dictionary < string , string > ParameterDictionary ;
64-
63+
64+ private Exception Error ;
6565 private bool Log = false ;
6666 private const string LogFile = "notifications_trial_log.txt" ;
6767
@@ -90,15 +90,14 @@ public NotificationManager(MainForm mainForm, ResourceManager resources, UpdateM
9090 FirstImage = ( Image ) resources . GetObject ( "Notification_first" ) ;
9191 LastImage = ( Image ) resources . GetObject ( "Notification_last" ) ;
9292 }
93-
94- //TODO Make this a background task
93+
9594 public void CheckNotifications ( )
9695 {
9796 try
9897 {
9998 Error = null ;
10099 ArePagesVisible = false ;
101- CurrentNotificationNo = 0 ;
100+ CurrentPageIndex = 0 ;
102101 Notifications = GetNotifications ( ) ;
103102 ParameterDictionary = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
104103
@@ -121,18 +120,18 @@ public void CheckNotifications()
121120 public Notifications GetNotifications ( )
122121 {
123122 string notificationsSerial ;
123+
124124 // To support testing of a new remote notifications.json file before it is published,
125125 // GetNotifications() tests first for a local file notifications_trial.json
126126 // and uses that if present, else it uses the remote file.
127-
128127 var filename = @"notifications_trial.json" ;
129- if ( System . IO . File . Exists ( filename ) )
128+ if ( File . Exists ( filename ) )
130129 {
131130 // Input from local file into a string
132- notificationsSerial = System . IO . File . ReadAllText ( filename ) ;
133-
134- // Turn on logging
135- Log = true ;
131+ notificationsSerial = File . ReadAllText ( filename ) ;
132+
133+ Log = true ; // Turn on logging to LogFile
134+ if ( File . Exists ( LogFile ) ) File . Delete ( LogFile ) ;
136135 }
137136 else
138137 {
@@ -148,8 +147,6 @@ public Notifications GetNotifications()
148147 NewPages . LastViewDate = Settings . LastViewNotificationDate ;
149148 if ( NewPages . LastViewDate == "" ) NewPages . LastViewDate = "2024-01-01" ; // Date of this code - i.e. before Notifications went public
150149
151- //TODO Cache jsonInput
152-
153150 return jsonInput ;
154151 }
155152
@@ -167,9 +164,7 @@ private string GetRemoteJson()
167164 // Helpful to supply server with data for its log file.
168165 client . Headers [ HttpRequestHeader . UserAgent ] = $ "{ System . Windows . Forms . Application . ProductName } /{ VersionInfo . VersionOrBuild } ";
169166
170- //TODO
171- return client . DownloadString ( new Uri ( "https://wepp.co.uk/openrails/notifications2/menu.json" ) ) ;
172- //return client.DownloadString(new Uri("https://static.openrails.org/api/notifications/menu.json"));
167+ return client . DownloadString ( new Uri ( "https://static.openrails.org/api/notifications/menu.json" ) ) ;
173168 }
174169
175170 /// <summary>
@@ -220,12 +215,12 @@ public void PopulatePage()
220215 Settings . Save ( "LastViewNotificationDate" ) ; // Saves the date on any viewing of notifications
221216
222217 var list = Notifications . NotificationList ;
223- var n = list [ CurrentNotificationNo ] ;
218+ var n = list [ CurrentPageIndex ] ;
224219 LogNotification ( n ) ;
225220
226- Page . NDetailList . Add ( new NTitleControl ( Panel , CurrentNotificationNo + 1 , list . Count , n . Date , n . Title ) ) ;
221+ Page . NDetailList . Add ( new NTitleControl ( Panel , CurrentPageIndex + 1 , list . Count , n . Date , n . Title ) ) ;
227222
228- // Check constraints for each item
223+ // Check constraints foPageNoem
229224 foreach ( var item in n . ItemList )
230225 {
231226 if ( AreItemChecksMet ( item ) ) AddItemToPage ( Page , item ) ;
@@ -490,6 +485,7 @@ private string ReplaceParameter(string field)
490485 {
491486 switch ( lowerCaseTarget )
492487 {
488+ // Update parameters
493489 // Using "none" instead of "" so that records are readable.
494490 case "update_mode" :
495491 replacement = ( UpdateManager . ChannelName == "" )
@@ -508,6 +504,7 @@ private string ReplaceParameter(string field)
508504 : $ "{ UpdateManager . LastUpdate . Date : yyyy-MM-dd} ";
509505 break ;
510506
507+ // System parameters
511508 case "installed_version" :
512509 replacement = SystemInfo . Application . Version ;
513510 break ;
@@ -538,10 +535,10 @@ private string ReplaceParameter(string field)
538535 replacement = string . Join ( "," , Direct3DFeatureLevels ) ;
539536 break ;
540537
538+ // Routes, User Settings and Not Recognised
541539 case "installed_routes" :
542540 replacement = GetInstalledRoutes ( ) ;
543541 break ;
544-
545542 default :
546543 var propertyValue = GetSetting ( target ) ;
547544 replacement = ( propertyValue == "" )
@@ -569,7 +566,7 @@ private bool ContainsParameter(string field)
569566 /// <returns></returns>
570567 string GetSetting ( string settingText )
571568 {
572- var nameArray = settingText . Split ( '.' ) ; // 2 elements: "Settings, " <property>", e.g. "SimpleControlPhysics"
569+ var nameArray = settingText . Split ( '.' ) ; // 2 elements: "Settings. <property>", e.g. "SimpleControlPhysics"
573570 if ( nameArray [ 0 ] == "Settings" && nameArray . Length == 2 )
574571 {
575572 return Settings . GetType ( ) . GetProperty ( nameArray [ 1 ] ) ? . GetValue ( Settings ) . ToString ( ) ?? "" ;
@@ -589,7 +586,7 @@ private string GetInstalledRoutes()
589586 foreach ( var routePath in Directory . GetDirectories ( path ) )
590587 {
591588 // Extract the last folder in the path - the route folder name, e.g. "SCE"
592- var routeName = System . IO . Path . GetFileName ( routePath ) . ToLower ( ) ;
589+ var routeName = Path . GetFileName ( routePath ) . ToLower ( ) ;
593590 installedRouteList += routeName + "," ;
594591 }
595592 }
@@ -604,10 +601,10 @@ public OverrideParameterList GetOverrideParameters()
604601 // and uses that if present to override the current program values, else it extracts these from the program.
605602
606603 var filename = @"notifications_trial_parameters.json" ;
607- if ( System . IO . File . Exists ( filename ) )
604+ if ( File . Exists ( filename ) )
608605 {
609606 // Input from local file into a string
610- var overrideParametersSerial = System . IO . File . ReadAllText ( filename ) ;
607+ var overrideParametersSerial = File . ReadAllText ( filename ) ;
611608
612609 var jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling . Auto } ;
613610 var jsonInput = JsonConvert . DeserializeObject < OverrideParameterList > ( overrideParametersSerial , jsonSettings ) ;
@@ -620,10 +617,10 @@ public OverrideParameterList GetOverrideParameters()
620617
621618 public void ChangePage ( int step )
622619 {
623- CurrentNotificationNo += step ;
620+ CurrentPageIndex += step ;
624621 if ( step > 0
625- && CurrentNotificationNo > NewPages . Viewed // and this is a new unviewed page
626- && CurrentNotificationNo <= NewPages . Count ) // and there are still new unviewed pages to be viewed
622+ && CurrentPageIndex > NewPages . Viewed // and this is a new unviewed page
623+ && CurrentPageIndex <= NewPages . Count ) // and there are still new unviewed pages to be viewed
627624 {
628625 NewPages . Viewed ++ ;
629626 }
@@ -635,15 +632,10 @@ public void LogOverrideParameters()
635632 {
636633 if ( Log == false ) return ;
637634
638- if ( File . Exists ( LogFile ) ) File . Delete ( LogFile ) ;
639-
640635 using ( StreamWriter sw = File . CreateText ( LogFile ) )
641636 {
642637 sw . WriteLine ( "Parameters overridden:" ) ;
643- foreach ( var p in ParameterDictionary )
644- {
645- sw . WriteLine ( $ "{ p . Key } = { p . Value } ") ;
646- }
638+ foreach ( var p in ParameterDictionary ) sw . WriteLine ( $ "{ p . Key } = { p . Value } ") ;
647639 sw . WriteLine ( ) ;
648640 }
649641 }
@@ -655,10 +647,7 @@ public void LogParameters()
655647 using ( StreamWriter sw = File . AppendText ( LogFile ) )
656648 {
657649 sw . WriteLine ( "Parameters used:" ) ;
658- foreach ( var p in ParameterDictionary )
659- {
660- sw . WriteLine ( $ "{ p . Key } = { p . Value } ") ;
661- }
650+ foreach ( var p in ParameterDictionary ) sw . WriteLine ( $ "{ p . Key } = { p . Value } ") ;
662651 sw . WriteLine ( ) ;
663652 }
664653 }
@@ -667,10 +656,7 @@ public void LogNotification(Notification n)
667656 {
668657 AppendToLog ( $ "\r \n Notification: { n . Title } ") ;
669658 }
670- public void LogChecks ( string checkName )
671- {
672- AppendToLog ( $ "CheckId: { checkName } ") ;
673- }
659+
674660 public void LogCheckContains ( string value , bool sense , string content , bool result )
675661 {
676662 var negation = sense ? "" : "NOT " ;
@@ -681,10 +667,7 @@ public void AppendToLog(string record)
681667 {
682668 if ( Log == false ) return ;
683669
684- using ( StreamWriter sw = File . AppendText ( LogFile ) )
685- {
686- sw . WriteLine ( record ) ;
687- }
670+ using ( StreamWriter sw = File . AppendText ( LogFile ) ) sw . WriteLine ( record ) ;
688671 }
689672 #endregion
690673 }
0 commit comments