private void btnPreviousPage_Click(object sender, EventArgs e) { BLIO.Log("btnPreviousPage_Click"); if (pageNumber <= 1) //Can't go to the previous page if we're on the first one { return; } List <Reminder> reminders = BLReminder.GetOrderedReminders(); int reminderItemCounter = 0; for (int i = (pageNumber - 2) * 7; i < ((pageNumber - 2) * 7) + 7; i++) { if (reminders.Count - 1 >= i) //Safely within index numbers { //Get the user control item from the panel. There's 7 user controls in the panel, so we have another counter for those MUCReminderItem itm = (MUCReminderItem)pnlReminders.Controls[reminderItemCounter]; itm.Visible = true; //Update the reminder object inside the user control, that's waay faster than removing and re-drawing a new control. itm.Reminder = reminders[i]; } reminderItemCounter++; if (reminderItemCounter == 7) { break; } } pageNumber--; SetPageButtonIcons(reminders); foreach (MUCReminderItem itm in pnlReminders.Controls) { itm.RefreshLabelFont(); } GC.Collect(); }
private void btnNextPage_Click(object sender, EventArgs e) { BLIO.Log("btnNextPage_Click"); List <Reminder> reminders = BLReminder.GetOrderedReminders(); if ((pageNumber * 7) + 1 > reminders.Count) { return; //No reminders left } int reminderItemCounter = 0; for (int i = pageNumber * 7; i < (pageNumber * 7) + 7; i++) { if (reminders.Count - 1 >= i) //Safely within index numbers { //Get the user control item from the panel. There's 7 user controls in the panel, so we have another counter for those MUCReminderItem itm = (MUCReminderItem)pnlReminders.Controls[reminderItemCounter]; itm.Visible = true; //Update the reminder object inside the user control, that's waay faster than removing and re-drawing a new control. itm.Reminder = reminders[i]; } else //Fill all remaining controls that can't be filled with reminders with "empty", since there are no reminders left { for (int ii = reminderItemCounter; ii < 7; ii++) { ((MUCReminderItem)pnlReminders.Controls[ii]).Reminder = null; } } reminderItemCounter++; } MaterialSkin.MaterialSkinManager.Themes theme = MaterialSkin.MaterialSkinManager.Instance.Theme; pageNumber++; if ((pageNumber * 7) + 1 > reminders.Count) { btnNextPage.Icon = Properties.Resources.nextDisabledDark; } else { if (theme == MaterialSkin.MaterialSkinManager.Themes.DARK) { btnNextPage.Icon = Properties.Resources.NextWhite; } else { btnNextPage.Icon = Properties.Resources.nextDark; } } if (pageNumber > 1) { if (theme == MaterialSkin.MaterialSkinManager.Themes.DARK) { btnPreviousPage.Icon = Properties.Resources.PreviousWhite; } else { btnPreviousPage.Icon = Properties.Resources.previousDark; } } MaterialForm1.Instance.UpdatePageNumber(pageNumber); foreach (MUCReminderItem itm in pnlReminders.Controls) { itm.RefreshLabelFont(); } GC.Collect(); }
/// <summary> /// Display changes on the current page. (For example a deleted or enabled/disabled reminder) /// </summary> /// <param name="editedReminder">If a reminder has been edited, this object will contain that reminder</param> public void UpdateCurrentPage(Reminder editedReminder = null) { MaterialSkin.MaterialSkinManager.Themes theme = MaterialSkin.MaterialSkinManager.Instance.Theme; BLIO.Log("Starting UpdateCurrentPage()..."); //Reminder list containing normal reminders and conditional reminders, enabled and disabled List <Reminder> reminders = BLReminder.GetOrderedReminders(); //^ All reminders in one list with the disabled ones at the end of the list BLIO.Log(reminders.Count + " reminders loaded"); startMethod: if ((pageNumber * 7) + 1 > reminders.Count) { if (theme == MaterialSkin.MaterialSkinManager.Themes.DARK) { btnNextPage.Icon = Properties.Resources.nextDisabledDark; } else { btnNextPage.Icon = Properties.Resources.nextDisabledDark; } } else { if (theme == MaterialSkin.MaterialSkinManager.Themes.DARK) { btnNextPage.Icon = Properties.Resources.NextWhite; } else { btnNextPage.Icon = Properties.Resources.nextDark; } } int reminderItemCounter = 0; for (int i = (pageNumber - 1) * 7; i < ((pageNumber) * 7); i++) { if (reminders.Count - 1 >= i) //Safely within index numbers { if (reminderItemCounter >= pnlReminders.Controls.Count) { return; } //Get the user control item from the panel. There's 7 user controls in the panel, so we have another counter for those MUCReminderItem itm = (MUCReminderItem)pnlReminders.Controls[reminderItemCounter]; //Update the reminder object inside the user control, that's waay faster than removing and re-drawing a new control. itm.Reminder = reminders[i]; itm.RefreshLabelFont(); } else { //User deleted a reminder, which was the last one out of the list from that page. Navigate to the previous page. if (i % 7 == 0 && pageNumber > 1) { BLIO.Log("navigating to the previous page after deletion of an reminder..."); pageNumber--; goto startMethod; } for (int ii = i; ii < 7; ii++) { if (ii >= pnlReminders.Controls.Count) { break; } MUCReminderItem itm = (MUCReminderItem)pnlReminders.Controls[ii]; itm.Reminder = null; } //This happens when an reminder has been deleted, and there are less than 7 reminders on that page. Empty out the remaining reminder items. while (reminderItemCounter <= 6) { BLIO.Log("Detected the deletion of an reminder on the current page."); //Get the user control item from the panel. There's 7 user controls in the panel, so we have another counter for those try { MUCReminderItem itm = (MUCReminderItem)pnlReminders.Controls[reminderItemCounter]; if (itm.Reminder != null) { BLIO.Log("Emptying ReminderItem with ID " + itm.Reminder.Id); } //Update the reminder object inside the user control, that's waay faster than removing and re-drawing a new control. itm.Reminder = null; reminderItemCounter++; } catch (Exception ex) { BLIO.Log("Setting new Reminder value failed. -> " + ex.GetType().ToString()); } } break; } reminderItemCounter++; if (reminderItemCounter == 7) { break; } } if (reminders.Count <= 7) { MaterialForm1.Instance.UpdatePageNumber(-1); } else { MaterialForm1.Instance.UpdatePageNumber(pageNumber); } if (Instance != null) { Instance.tmrCheckReminder.Start(); } if (editedReminder != null && editedReminder.HttpId != null) { //This object has been altered. Deleted, Perma-deleted, edited OR disabled if (BLReminder.GetReminderById(editedReminder.Id) == null || editedReminder.Deleted > 0 || editedReminder.Enabled == 0) { //perma-deleted, soft-deleted or turned off if (GetTimer(editedReminder) != null) { GetTimer(editedReminder).Stop(); } RemoveTimer(editedReminder); } else //Reminder is still active, so it probably has been edited { HttpRequests httpObj = BLLocalDatabase.HttpRequest.GetHttpRequestById((long)editedReminder.Id); var kvp = httpTimers.Where(r => r.Key.Id == editedReminder.Id).FirstOrDefault(); if (kvp.Key != null) { //Already exist, stop timer, change & restart RemoveTimer(editedReminder); var timer = new System.Windows.Forms.Timer(); timer.Interval = Convert.ToInt32(httpObj.Interval * 60000); timer.Tick += (object s, EventArgs a) => ExecuteHttpRequest(s, a, httpObj, editedReminder); timer.Start(); httpTimers.Add(editedReminder, timer); } else if (editedReminder.Enabled == 1) //Reminder has been re-enabled { System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); httpTimers.Add(editedReminder, timer); timer.Interval = Convert.ToInt32(httpObj.Interval * 60000); timer.Tick += (object s, EventArgs a) => ExecuteHttpRequest(s, a, httpObj, editedReminder); timer.Start(); } } } else { //Http requests foreach (Reminder rem in BLReminder.GetReminders(true).Where(r => r.HttpId != null).Where(r => r.Enabled == 1)) { HttpRequests httpObj = BLLocalDatabase.HttpRequest.GetHttpRequestById((long)rem.Id); if (GetTimer(rem) == null) { //Don't add duplicates System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); httpTimers.Add(rem, timer); timer.Interval = Convert.ToInt32(httpObj.Interval * 60000); timer.Tick += (object s, EventArgs a) => ExecuteHttpRequest(s, a, httpObj, rem); timer.Start(); } } } BLIO.Log("UpdateCurrentPage() completed."); }