Skip to content

Commit 6426c6f

Browse files
committed
add next & previous controls
1 parent e267870 commit 6426c6f

File tree

10 files changed

+262
-59
lines changed

10 files changed

+262
-59
lines changed

Source/Menu/MainForm.Designer.cs

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Menu/MainForm.cs

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,13 @@ void comboBoxTimetable_EnabledChanged(object sender, EventArgs e)
14431443
}
14441444

14451445
#region NotificationPages
1446+
public int CurrentNotificationNo = 0;
1447+
private bool firstVisible = true;
1448+
private bool previousVisible = false;
1449+
private bool lastVisible = false;
1450+
private bool nextVisible = true;
1451+
1452+
14461453
private void pbNotificationsNone_Click(object sender, EventArgs e)
14471454
{
14481455
ToggleNotificationPages();
@@ -1473,7 +1480,7 @@ private void ToggleNotificationPages()
14731480

14741481
private void FiddleNewNotificationPageCount()
14751482
{
1476-
NotificationManager.LastPageViewed = 1;
1483+
//NotificationManager.LastPageViewed = 1;
14771484
UpdateNotificationPageAlert();
14781485
}
14791486

@@ -1490,7 +1497,7 @@ void ShowNotificationPages()
14901497
{
14911498
Win32.LockWindowUpdate(Handle);
14921499
ClearPanel();
1493-
NotificationManager.PopulatePageList();
1500+
NotificationManager.PopulatePage();
14941501
var notificationPage = GetCurrentNotificationPage();
14951502
notificationPage.FlowNDetails();
14961503
Win32.LockWindowUpdate(IntPtr.Zero);
@@ -1502,12 +1509,23 @@ void ShowNotificationPages()
15021509
/// <returns></returns>
15031510
NotificationPage GetCurrentNotificationPage()
15041511
{
1505-
return NotificationManager.PageList[0];
1512+
return NotificationManager.Page;
15061513
}
15071514

1508-
public NotificationPage CreateNotificationPage(Notifications notifications)
1515+
/// <summary>
1516+
/// Returns a new notificationPage with default images and label
1517+
/// </summary>
1518+
/// <returns></returns>
1519+
public NotificationPage CreateNotificationPage()
1520+
// Located in MainForm to get access to MainForm.Resources
15091521
{
1510-
return new NotificationPage(this, panelDetails);
1522+
var previousImage = (Image)Resources.GetObject("Notification_previous");
1523+
var nextImage = (Image)Resources.GetObject("Notification_next");
1524+
var firstImage = (Image)Resources.GetObject("Notification_first");
1525+
var lastImage = (Image)Resources.GetObject("Notification_last");
1526+
var pageCount = $"{CurrentNotificationNo + 1}/{NotificationManager.Notifications.NotificationList.Count}";
1527+
return new NotificationPage(this, panelDetails, nextImage, previousImage, firstImage, lastImage, pageCount,
1528+
previousVisible, firstVisible, nextVisible, lastVisible);
15111529
}
15121530

15131531
// 3 should be enough, but is there a way to get unlimited buttons?
@@ -1524,6 +1542,51 @@ public void Button2_Click(object sender, EventArgs e)
15241542
GetCurrentNotificationPage().DoButton(UpdateManager, 2);
15251543
}
15261544

1545+
public void Next_Click(object sender, EventArgs e)
1546+
{
1547+
ChangePage(1);
1548+
// GetCurrentNotificationPage().DoNext(1);
1549+
}
1550+
1551+
public void Previous_Click(object sender, EventArgs e)
1552+
{
1553+
ChangePage(-1);
1554+
//GetCurrentNotificationPage().DoNext(-1);
1555+
}
1556+
1557+
private void ChangePage(int step)
1558+
{
1559+
SetVisibility(step);
1560+
CurrentNotificationNo += step;
1561+
ShowNotificationPages();
1562+
}
1563+
1564+
private void SetVisibility(int step)
1565+
{
1566+
if (step < 0)
1567+
{
1568+
if (CurrentNotificationNo + step <= 0)
1569+
{
1570+
previousVisible = false;
1571+
firstVisible = true;
1572+
return;
1573+
}
1574+
}
1575+
else
1576+
{
1577+
if (CurrentNotificationNo + step >= NotificationManager.Notifications.NotificationList.Count - 1)
1578+
{
1579+
nextVisible = false;
1580+
lastVisible = true;
1581+
return;
1582+
}
1583+
}
1584+
nextVisible = true;
1585+
lastVisible = false;
1586+
previousVisible = true;
1587+
firstVisible = false;
1588+
}
1589+
15271590
#endregion NotificationPages
15281591
}
15291592
}

Source/Menu/Notifications/NotificationManager.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
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+
3745
namespace 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

Comments
 (0)