Skip to content

Commit f1bca8b

Browse files
committed
some re-factoring
1 parent 318043a commit f1bca8b

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

Source/Menu/MainForm.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,9 @@ orderby tool.Text
242242
LoadDocuments(docs, codePath, code);
243243
}
244244
}
245-
else
246-
buttonDocuments.Enabled = false;
247-
248-
NotificationManager = new NotificationManager(this, UpdateManager, Settings, panelDetails
249-
, (Image)Resources.GetObject("Notification_previous")
250-
, (Image)Resources.GetObject("Notification_next")
251-
, (Image)Resources.GetObject("Notification_first")
252-
, (Image)Resources.GetObject("Notification_last")
253-
);
245+
else buttonDocuments.Enabled = false;
246+
247+
NotificationManager = new NotificationManager(this, this.Resources, UpdateManager, Settings, panelDetails);
254248
}
255249

256250
ShowEnvironment();
@@ -1468,8 +1462,8 @@ private void ToggleNotificationPages()
14681462
if (NotificationManager.ArePagesVisible == false)
14691463
{
14701464
NotificationManager.ArePagesVisible = true; // Set before calling ShowNotifcations()
1465+
//UpdateNotificationPageAlert();
14711466
ShowNotificationPages();
1472-
UpdateNotificationPageAlert();
14731467
}
14741468
else
14751469
{
@@ -1509,7 +1503,7 @@ public void Button0_Click(object sender, EventArgs e)
15091503
if (NotificationManager.Notifications == null) // button0 is "Retry"
15101504
{
15111505
NotificationManager.CheckNotifications();
1512-
UpdateNotificationPageAlert();
1506+
//UpdateNotificationPageAlert();
15131507
ShowNotificationPages();
15141508
}
15151509
else NotificationManager.Page.DoButton(UpdateManager, 0);

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
using System.Windows.Forms;
3131
using System.Linq;
3232
using System.Diagnostics;
33+
using System.Resources;
34+
using ORTS.Properties;
3335

3436
// Notifications are read only once as a background task at start into NotificationList.
3537
// Every time the notifications page is re-visited, the position is discarded and first page is shown.
@@ -72,22 +74,19 @@ public class PageTracking
7274
public Image LastImage { get; private set; }
7375
public PageTracking NewPages { get; private set; }
7476

75-
public NotificationManager(MainForm mainForm, UpdateManager updateManager, UserSettings settings, Panel panel
76-
, Image previousImage
77-
, Image nextImage
78-
, Image firstImage
79-
, Image lastImage
80-
)
81-
{
77+
public NotificationManager(MainForm mainForm, ResourceManager resources, UpdateManager updateManager, UserSettings settings, Panel panel)
78+
{
8279
MainForm = mainForm;
8380
this.UpdateManager = updateManager;
8481
this.Settings = settings;
8582
Panel = panel;
86-
PreviousImage = previousImage;
87-
NextImage = nextImage;
88-
FirstImage = firstImage;
89-
LastImage = lastImage;
9083
NewPages = new PageTracking();
84+
85+
// Load images of arrows
86+
PreviousImage = (Image)resources.GetObject("Notification_previous");
87+
NextImage = (Image)resources.GetObject("Notification_next");
88+
FirstImage = (Image)resources.GetObject("Notification_first");
89+
LastImage = (Image)resources.GetObject("Notification_last");
9190
}
9291

9392
//TODO Make this a background task
@@ -166,10 +165,16 @@ private string GetRemoteJson()
166165
// Helpful to supply server with data for its log file.
167166
client.Headers[HttpRequestHeader.UserAgent] = $"{System.Windows.Forms.Application.ProductName}/{VersionInfo.VersionOrBuild}";
168167

168+
//TODO
169169
return client.DownloadString(new Uri("https://wepp.co.uk/openrails/notifications2/menu.json"));
170170
//return client.DownloadString(new Uri("https://static.openrails.org/api/notifications/menu.json"));
171171
}
172172

173+
/// <summary>
174+
/// Returns a list of Notifications excluding any that fail IncludeIf. Sorts Update channel "none" to the end.
175+
/// </summary>
176+
/// <param name="list"></param>
177+
/// <returns></returns>
173178
private List<Notification> IncludeValid(List<Notification> list)
174179
{
175180
NewPages.Count = 0;
@@ -197,36 +202,21 @@ private List<Notification> SortByDate(List<Notification> list)
197202
}
198203

199204
/// <summary>
200-
/// Adds details of the current notifications to the panel
205+
/// Adds details of the current notification to the panel
201206
/// </summary>
202207
public void PopulatePage()
203208
{
204-
Settings.LastViewNotificationDate = $"{DateTime.Today:yyyy-MM-dd}";
205-
Settings.Save("LastViewNotificationDate"); // Saves the date on any viewing of notifications
206209
Page = new NotificationPage(MainForm, Panel, this);
207210

208211
if (UpdateManager.LastCheckError != null || Error != null)
209212
{
210-
NewPages.Count = 0;
211-
var message = (UpdateManager.LastCheckError != null)
212-
? UpdateManager.LastCheckError.Message
213-
: Error.Message;
214-
215-
// Reports notifications are not available.
216-
var channelName = UpdateManager.ChannelName == "" ? "None" : UpdateManager.ChannelName;
217-
var today = DateTime.Now.Date;
218-
Page.NDetailList.Add(new NTitleControl(Page, 1, 1, $"{today:dd-MMM-yy}", "Notifications are not available"));
219-
Page.NDetailList.Add(new NRecordControl(Page, "Update mode", 140, channelName));
220-
Page.NDetailList.Add(new NRecordControl(Page, "Installed version", 140, VersionInfo.VersionOrBuild));
221-
222-
Page.NDetailList.Add(new NHeadingControl(Page, "Notifications are not available", "red"));
223-
Page.NDetailList.Add(new NTextControl(Page, $"Error: {message}"));
224-
Page.NDetailList.Add(new NTextControl(Page, "Is your Internet connected?"));
225-
226-
Page.NDetailList.Add(new NRetryControl(Page, "Retry", 140, "Try again to fetch notifications", MainForm));
213+
PopulateRetryPage();
227214
}
228215
else
229216
{
217+
Settings.LastViewNotificationDate = $"{DateTime.Today:yyyy-MM-dd}";
218+
Settings.Save("LastViewNotificationDate"); // Saves the date on any viewing of notifications
219+
230220
var list = Notifications.NotificationList;
231221
var n = list[CurrentNotificationNo];
232222
LogNotification(n);
@@ -244,6 +234,27 @@ public void PopulatePage()
244234
Page.NDetailList.Add(new NTextControl(Page, "(Toggle icon to hide these notifications.)"));
245235
}
246236

237+
private void PopulateRetryPage()
238+
{
239+
NewPages.Count = 0;
240+
241+
// Reports notifications are not available.
242+
var channelName = UpdateManager.ChannelName == "" ? "None" : UpdateManager.ChannelName;
243+
var today = DateTime.Now.Date;
244+
Page.NDetailList.Add(new NTitleControl(Page, 1, 1, $"{today:dd-MMM-yy}", "Notifications are not available"));
245+
Page.NDetailList.Add(new NRecordControl(Page, "Update mode", 140, channelName));
246+
Page.NDetailList.Add(new NRecordControl(Page, "Installed version", 140, VersionInfo.VersionOrBuild));
247+
248+
Page.NDetailList.Add(new NHeadingControl(Page, "Notifications are not available", "red"));
249+
var message = (UpdateManager.LastCheckError != null)
250+
? UpdateManager.LastCheckError.Message
251+
: Error.Message;
252+
Page.NDetailList.Add(new NTextControl(Page, $"Error: {message}"));
253+
Page.NDetailList.Add(new NTextControl(Page, "Is your Internet connected?"));
254+
255+
Page.NDetailList.Add(new NRetryControl(Page, "Retry", 140, "Try again to fetch notifications", MainForm));
256+
}
257+
247258
#region Process Criteria
248259
private bool AreNotificationChecksMet(Notification notification)
249260
{

0 commit comments

Comments
 (0)