Skip to content

Commit 0f6de51

Browse files
committed
handles fetch errors for Update and Notifications
1 parent 55dfb28 commit 0f6de51

File tree

4 files changed

+114
-103
lines changed

4 files changed

+114
-103
lines changed

Source/Menu/MainForm.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ void CheckForUpdate()
325325
});
326326
}
327327

328+
// Event raised by Retry button in NotificationPages so user can retry updates following an error notification.
329+
public event EventHandler CheckUpdatesAgain;
330+
331+
public virtual void OnCheckUpdatesAgain(EventArgs e)
332+
{
333+
CheckForUpdate();
334+
}
335+
328336
void LoadLanguage()
329337
{
330338
if (Settings.Language.Length > 0)
@@ -1524,7 +1532,7 @@ NotificationPage GetCurrentNotificationPage()
15241532

15251533
public NotificationPage CreateNotificationPage(Notifications notifications)
15261534
{
1527-
return new NotificationPage(notifications, panelDetails);
1535+
return new NotificationPage(this, panelDetails);
15281536
}
15291537

15301538
// 3 should be enough, but is there a way to get unlimited buttons?
@@ -1540,6 +1548,7 @@ public void Button2_Click(object sender, EventArgs e)
15401548
{
15411549
GetCurrentNotificationPage().DoButton(UpdateManager, 2);
15421550
}
1551+
15431552
#endregion NotificationPages
15441553
}
15451554
}

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
using System.Linq;
2121
using System.Net;
2222
using System.Text;
23-
using System.Threading.Tasks;
2423
using Newtonsoft.Json;
25-
using Orts.Formats.OR;
2624
using ORTS.Common;
2725
using ORTS.Updater;
2826
using 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

Comments
 (0)