2020using System . Linq ;
2121using System . Net ;
2222using System . Text ;
23- using System . Threading . Tasks ;
2423using Newtonsoft . Json ;
25- using Orts . Formats . OR ;
2624using ORTS . Common ;
2725using ORTS . Updater ;
2826using static ORTS . Common . SystemInfo ;
@@ -42,7 +40,9 @@ class NotificationManager
4240
4341 public Notifications Notifications ;
4442 public List < NotificationPage > PageList = new List < NotificationPage > ( ) ;
45- private MainForm MainForm ;
43+ private Exception Error ;
44+
45+ private MainForm MainForm ; // Needed so we can add controls to the NotificationPage
4646 private UpdateManager UpdateManager ;
4747
4848 public NotificationManager ( MainForm mainForm , UpdateManager updateManager )
@@ -56,16 +56,17 @@ public void CheckNotifications()
5656 {
5757 try
5858 {
59+ Error = null ;
5960 Notifications = GetNotifications ( ) ;
60- Notifications . Available = true ;
6161 DropUnusedUpdateNotifications ( ) ;
6262 ReplaceParameters ( ) ;
6363
6464 PopulatePageList ( ) ;
65+ ArePagesVisible = false ;
6566 }
6667 catch ( WebException ex )
6768 {
68- Notifications . Available = false ;
69+ Error = ex ;
6970 }
7071 }
7172
@@ -118,97 +119,98 @@ public void PopulatePageList()
118119 /// </summary>
119120 void SetUpdateNotificationPage ( )
120121 {
121- NewPageCount = ( IsUpdateAvailable ( ) ) ? 1 : 0 ;
122122 MainForm . UpdateNotificationPageAlert ( ) ;
123123 PageList . Clear ( ) ;
124124 var page = MainForm . CreateNotificationPage ( Notifications ) ;
125125
126- if ( Notifications . Available )
126+ if ( UpdateManager . LastCheckError != null || Error != null )
127127 {
128- NewPageCount = 1 ;
129- if ( ArePagesVisible )
128+ NewPageCount = 0 ;
129+ // if (ArePagesVisible)
130130 {
131- var list = Notifications . NotificationList ;
132- var n = list [ Index ] ;
131+ var message = ( UpdateManager . LastCheckError != null )
132+ ? UpdateManager . LastCheckError . Message
133+ : Error . Message ;
133134
134- new NTitleControl ( page , Index + 1 , list . Count , n . Date , n . Title ) . Add ( ) ;
135+ // Reports notifications are not available.
136+ var channelName = UpdateManager . ChannelName == "" ? "None" : UpdateManager . ChannelName ;
137+ var today = DateTime . Now . Date ;
138+ new NTitleControl ( page , 1 , 1 , $ "{ today : dd-MMM-yy} ", "Notifications are not available" ) . Add ( ) ;
139+ new NRecordControl ( page , "Update mode" , 140 , channelName ) . Add ( ) ;
140+ new NRecordControl ( page , "Installed version" , 140 , VersionInfo . VersionOrBuild ) . Add ( ) ;
135141
136- foreach ( var item in n . PrefixItemList )
137- {
138- AddItemToPage ( page , item ) ;
139- }
142+ new NHeadingControl ( page , "Notifications are not available" , "red" ) . Add ( ) ;
143+ new NTextControl ( page , $ "Error: { message } ") . Add ( ) ;
144+ new NTextControl ( page , "Is your Internet connected?" ) . Add ( ) ;
140145
141- // Check constraints
142- var excludesMet = true ;
143- var includesMet = true ;
144- foreach ( var nc in n . MetLists . CheckIdList )
146+ new NRetryControl ( page , "Retry" , 140 , "Try again to fetch notifications" , MainForm ) . Add ( ) ;
147+ PageList . Add ( page ) ;
148+ }
149+ return ;
150+ }
151+
152+ NewPageCount = 1 ;
153+ var list = Notifications . NotificationList ;
154+ var n = list [ Index ] ;
155+
156+ new NTitleControl ( page , Index + 1 , list . Count , n . Date , n . Title ) . Add ( ) ;
157+
158+ foreach ( var item in n . PrefixItemList )
159+ {
160+ AddItemToPage ( page , item ) ;
161+ }
162+
163+ // Check constraints
164+ var excludesMet = true ;
165+ var includesMet = true ;
166+ foreach ( var nc in n . MetLists . CheckIdList )
167+ {
168+ foreach ( var c in Notifications . CheckList . Where ( c => c . Id == nc . Id ) )
169+ {
170+ var checkFailed = CheckExcludes ( c ) ;
171+ excludesMet = ( checkFailed == null ) ;
172+ if ( excludesMet == false )
145173 {
146- foreach ( var c in Notifications . CheckList . Where ( c => c . Id == nc . Id ) )
174+ foreach ( var item in checkFailed . UnmetItemList )
147175 {
148- var checkFailed = CheckExcludes ( c ) ;
149- excludesMet = ( checkFailed == null ) ;
150- if ( excludesMet == false )
151- {
152- foreach ( var item in checkFailed . UnmetItemList )
153- {
154- AddItemToPage ( page , item ) ;
155- }
156- break ;
157- }
158-
159- includesMet = ( c . IncludesAnyOf . Count == 0 || CheckIncludes ( c ) != null ) ;
160- if ( includesMet == false )
161- {
162- foreach ( var item in c . UnmetItemList )
163- {
164- AddItemToPage ( page , item ) ;
165- }
166- break ;
167- }
176+ AddItemToPage ( page , item ) ;
168177 }
169- if ( excludesMet == false || includesMet == false )
170- break ;
178+ break ;
171179 }
172- if ( excludesMet && includesMet )
180+
181+ includesMet = ( c . IncludesAnyOf . Count == 0 || CheckIncludes ( c ) != null ) ;
182+ if ( includesMet == false )
173183 {
174- foreach ( var item in n . MetLists . ItemList )
184+ foreach ( var item in c . UnmetItemList )
175185 {
176186 AddItemToPage ( page , item ) ;
177187 }
178- }
179-
180- foreach ( var item in n . SuffixItemList )
181- {
182- AddItemToPage ( page , item ) ;
188+ break ;
183189 }
184190 }
191+ if ( excludesMet == false || includesMet == false )
192+ break ;
185193 }
186- else
194+ if ( excludesMet && includesMet )
187195 {
188- NewPageCount = 0 ;
189- if ( ArePagesVisible )
196+ foreach ( var item in n . MetLists . ItemList )
190197 {
191- // Reports notifications are not available.
192- var channelName = UpdateManager . ChannelName == "" ? "None" : UpdateManager . ChannelName ;
193- var today = DateTime . Now . Date ;
194- new NTitleControl ( page , 1 , 1 , $ "{ today : dd-MMM-yy} ", "Notifications are not available" ) . Add ( ) ;
195- new NRecordControl ( page , "Update mode" , 140 , channelName ) . Add ( ) ;
196- new NRecordControl ( page , "Installed version" , 140 , VersionInfo . VersionOrBuild ) . Add ( ) ;
197-
198- new NHeadingControl ( page , "Notifications are not available" , "red" ) . Add ( ) ;
199- new NTextControl ( page , "Is your Internet connected?" ) . Add ( ) ;
200-
201- new NRetryControl ( page , "Retry" , 140 , "Try again to fetch notifications" , MainForm ) . Add ( ) ;
198+ AddItemToPage ( page , item ) ;
202199 }
203200 }
201+
202+ foreach ( var item in n . SuffixItemList )
203+ {
204+ AddItemToPage ( page , item ) ;
205+ }
204206 PageList . Add ( page ) ;
205207 }
206208
207- private bool IsUpdateAvailable ( )
208- {
209- return UpdateManager . LastUpdate != null
210- && UpdateManager . LastUpdate . Version != VersionInfo . Version ;
211- }
209+ // private bool IsUpdateAvailable()
210+ // {
211+ // return UpdateManager.LastUpdate != null
212+ // && UpdateManager.LastUpdate.Version != VersionInfo.Version;
213+ // }
212214
213215 private void AddItemToPage ( NotificationPage page , Item item )
214216 {
0 commit comments