private void SaveTask(object sender, RoutedEventArgs e) { string type = ""; Priority priority = Priority.Low; DateTime due = DateTime.Now; string title = tblkTitle.Text; string description = tblkDescription.Text; try { type = comboTaskType.SelectedItem.ToString(); priority = GetPriority(); } catch (NullReferenceException err) { Toastr.Warning("Warning", "Please choose an option in all dropdown menus"); return; } try { due = dueDate.SelectedDate.Value.Date; due = due.Date + new TimeSpan(23, 59, 59); } catch (InvalidOperationException err) { Toastr.Warning("Warning", "Please select a valid date"); return; } Task newTask = CreateNewTask(type, title, description, priority, due); if (newTask != null) { int weekNumber = DateService.GetWeekNumber(newTask.DueDatetime); SaveNewTask(newTask, weekNumber); NavigationService.Navigate(new MyPlanner()); } else { Toastr.Error("Error", "Failed to create new task"); } }
private Task CreateNewTask(string type, string title, string description, Priority priority, DateTime due) { if (title.Length < 3) { Toastr.Warning("Warning", "Title length is too short"); return(null); } if (description.Length < 10) { Toastr.Warning("Warning", "Description length is too short."); return(null); } if (!DateService.DateAfterToday(due)) { Toastr.Warning("Warning", "The due date must be in the future."); return(null); } Task newTask = null; Toastr.TurnOffNotifications(); // Prevent multiple 'update' notifications DateTime now = DateTime.Now; switch (type) { case "Assignment": string subject = tbxSubject.Text; int percentage = int.Parse(tbxPercentage.Text); newTask = new AssignmentTask(title, description, priority, due, now, subject, percentage); break; case "Exam": string subjectExam = tbxSubjectExam.Text; string materials = tbxMaterials.Text; int percentageExam = int.Parse(tbxPercentExam.Text); newTask = new ExamTask(title, description, priority, due, now, subjectExam, percentageExam, new List <string>(materials.Split(','))); break; case "Event": string location = tbxLocation.Text; newTask = new EventTask(title, description, priority, due, now, location); break; case "Payment": decimal amount = decimal.Parse(tbxAmount.Text); newTask = new PaymentTask(title, description, priority, due, now, amount); break; default: Toastr.Error("Error", "Invalid task type."); break; } Toastr.TurnOnNotifications(); if (newTask != null) { Toastr.Success("Created", "The '" + title + "' task has been created"); } return(newTask); }