3434//TODO Add caching of criteria
3535//TODO Add "includeIf" for individual notifications
3636
37+ // Notifications are read only once as a background task at start into NotificationList.
38+ // Every time the notifications page is re-visited, the options may have changed, so
39+ // the visibility of each notification in NotificationList is re-assessed. Also its date.
40+ // A SortedPageList is created which contains a list of indexes for the notifications to be shown sorted as latest first.
41+ // Every time the notifications page is re-visited or the page is incremented up or down,
42+ // the current notification is re-assessed and
43+ // the panel is re-loaded with Controls for the current page.
44+
3745namespace ORTS
3846{
3947 class NotificationManager
@@ -44,17 +52,18 @@ class NotificationManager
4452 // We don't track the reading of each notification but set the NewNotificationCount = 0 after the last of the new ones has been read.
4553 public int NewPageCount = 1 ;
4654 public int LastPageViewed = 0 ;
47- public int Index = 0 ;
55+ // public int Index = 0;
4856
4957 public Notifications Notifications ;
50- public List < NotificationPage > PageList = new List < NotificationPage > ( ) ;
5158 private Exception Error ;
5259 private Dictionary < string , string > ParameterDictionary ;
5360
5461 private readonly MainForm MainForm ; // Needed so we can add controls to the NotificationPage
5562 private readonly UpdateManager UpdateManager ;
5663 private readonly UserSettings Settings ;
5764
65+ public NotificationPage Page { get ; private set ; }
66+
5867 public NotificationManager ( MainForm mainForm , UpdateManager updateManager , UserSettings settings )
5968 {
6069 MainForm = mainForm ;
@@ -78,7 +87,7 @@ public void CheckNotifications()
7887 ReplaceParameters ( ) ;
7988 LogParameters ( ) ;
8089
81- PopulatePageList ( ) ;
90+ PopulatePage ( ) ;
8291 ArePagesVisible = false ;
8392 }
8493 catch ( WebException ex )
@@ -135,24 +144,22 @@ private string GetRemoteJson()
135144 return client . DownloadString ( new Uri ( "https://wepp.co.uk/openrails/notifications2/menu.json" ) ) ;
136145 }
137146
138- public void PopulatePageList ( )
147+ public void PopulatePage ( )
139148 {
140- SetUpdateNotificationPage ( ) ;
141-
142- var NotificationPage = PageList . LastOrDefault ( ) ;
143- new NTextControl ( NotificationPage , "" ) . Add ( ) ;
144- new NTextControl ( NotificationPage , "(Toggle icon to hide these notifications.)" ) . Add ( ) ;
149+ Page = UpdateNotificationPage ( ) ;
150+ new NTextControl ( Page , "" ) . Add ( ) ;
151+ new NTextControl ( Page , "(Toggle icon to hide these notifications.)" ) . Add ( ) ;
145152 }
146153
147154 /// <summary>
148155 /// Ultimately there will be a list of notifications downloaded from https://static.openrails.org/api/notifications/menu.json .
149156 /// Until then, there is a single notification announcing either that a new update is available or the installation is up to date.
150157 /// </summary>
151- private void SetUpdateNotificationPage ( )
158+ private NotificationPage UpdateNotificationPage ( )
152159 {
160+ var page = MainForm . CreateNotificationPage ( ) ;
161+
153162 MainForm . UpdateNotificationPageAlert ( ) ;
154- PageList . Clear ( ) ;
155- var page = MainForm . CreateNotificationPage ( Notifications ) ;
156163
157164 if ( UpdateManager . LastCheckError != null || Error != null )
158165 {
@@ -173,24 +180,23 @@ private void SetUpdateNotificationPage()
173180 new NTextControl ( page , "Is your Internet connected?" ) . Add ( ) ;
174181
175182 new NRetryControl ( page , "Retry" , 140 , "Try again to fetch notifications" , MainForm ) . Add ( ) ;
176- PageList . Add ( page ) ;
177- return ;
178183 }
179184
180185 NewPageCount = 1 ;
181186 var list = Notifications . NotificationList ;
182- var n = list [ Index ] ;
187+ var n = list [ MainForm . CurrentNotificationNo ] ;
183188 LogNotification ( n ) ;
184189
185- var skipPage = false ;
186- new NTitleControl ( page , Index + 1 , list . Count , n . Date , n . Title ) . Add ( ) ;
190+ // var skipPage = false;
191+ new NTitleControl ( page , MainForm . CurrentNotificationNo + 1 , list . Count , n . Date , n . Title ) . Add ( ) ;
187192
188193 // Check constraints for each item
189- foreach ( var item in n . ItemList )
194+ foreach ( var item in n . ItemList )
190195 {
191196 if ( AreChecksMet ( item ) ) AddItemToPage ( page , item ) ;
192197 }
193- if ( skipPage == false ) PageList . Add ( page ) ;
198+
199+ return page ;
194200 }
195201
196202 #region Process Criteria
@@ -337,9 +343,6 @@ public void ReplaceParameters()
337343 {
338344 n . Title = ReplaceParameter ( n . Title ) ;
339345 n . Date = ReplaceParameter ( n . Date ) ;
340- //n.PrefixItemList?.ForEach(item => ReplaceItemParameter(item));
341- //n.Met.ItemList?.ForEach(item => ReplaceItemParameter(item));
342- //n.SuffixItemList?.ForEach(item => ReplaceItemParameter(item));
343346 n . ItemList ? . ForEach ( item => ReplaceItemParameter ( item ) ) ;
344347 }
345348 foreach ( var list in Notifications . CheckList )
0 commit comments