private void ApplicationBarSaveButton_Click(object sender, EventArgs e) { // Get all the values from the forum string title = this.titleTextBox.Text; DateTime date = (DateTime)beginDatePicker.Value; DateTime time = (DateTime)beginTimePicker.Value; DateTime beginTime = date + time.TimeOfDay; if (beginTime < DateTime.Now) { MessageBox.Show("Sorry, Can't start an alarm in the past. Please select a different time"); return; } AlarmSound sound = this.soundPicker.SelectedItem as AlarmSound; int noOfOccurences = 0; RecurrenceInterval recurrence = RecurrenceInterval.None; List<DayOfWeek> daysOfWeek = new List<DayOfWeek>(); List<int> monthsOfYear = new List<int>(); DateTime? expirationTime = null; if (this.RepeatRadioBtn.IsChecked.Value) { // Get the value from repeat picker recurrence = (RecurrenceInterval)Enum.Parse(typeof(RecurrenceInterval), this.RepeatPicker.SelectedItem.ToString(), true); if (recurrence == RecurrenceInterval.Weekly) { // Get the days that are checked daysOfWeek = this.weekPanel.AllChildren<CheckBox>().Where(x => x.IsChecked.Value).Select(x => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x.Tag.ToString())).ToList(); } else if (recurrence == RecurrenceInterval.Monthly) { monthsOfYear = this.monthPanel.AllChildren<CheckBox>().Where(x => x.IsChecked.Value).Select(x => Int32.Parse(x.Tag.ToString())).ToList(); } if (!this.NeverRadioBtn.IsChecked.Value) { if (this.AfterRadioBtn.IsChecked.Value) { noOfOccurences = Convert.ToInt32(this.AfterTextBox.Text); } else if (this.EndsRadioBtn.IsChecked.Value) { expirationTime = endDatePicker.Value; if (expirationTime.HasValue && expirationTime.Value < beginTime) { MessageBox.Show("Sorry, The End Date should be after the Start Date. Please select a different date"); return; } } } } // Create an alarm schedule based on the selected options AlarmSchedule schedule = new AlarmSchedule(recurrence, expirationTime, noOfOccurences, daysOfWeek, monthsOfYear); // Create the super alarm with the schedule if not it edit mode if (!this.isEditMode) { SelectedAlarm = new SuperAlarm(title, beginTime, schedule, sound); } else { // Delete the existing simple alarms and create new ones with the new schedule this.ApplicationBarDeleteButton_Click(null, null); SelectedAlarm.StartTime = beginTime; SelectedAlarm.Name = title; SelectedAlarm.Sound = sound; SelectedAlarm.Schedule = schedule; // Create the simple alarms SelectedAlarm.Alarms = SelectedAlarm.CreateAlarms(); } // Create the alarm SelectedAlarm.Alarms.ForEach(x => ScheduledActionService.Add(x.Create(SelectedAlarm.Schedule))); SelectedAlarm.IsScheduled = true; // also save the super alarm in isolate storage with the id as the key var settings = IsolatedStorageSettings.ApplicationSettings; if (settings.Contains(SelectedAlarm.ID)) { settings[SelectedAlarm.ID] = SelectedAlarm; } else { settings.Add(SelectedAlarm.ID, SelectedAlarm); } // Save the settings settings.Save(); // Navigate back to the main reminder list page. NavigationService.GoBack(); }
/// <summary> /// Super alarm /// </summary> /// <param name="name"></param> /// <param name="startTime"></param> /// <param name="schedule"></param> /// <param name="sound"></param> public SuperAlarm(string name, DateTime startTime, AlarmSchedule schedule, AlarmSound sound) { this.Name = name; this.StartTime = startTime; this.Sound = sound; this.Schedule = schedule; // Unique ID for the super alarm. this.ID = Guid.NewGuid().ToString(); // Create the alarms based on the schedule this.Alarms = this.CreateAlarms(); }
/// <summary> /// Get the start time for the alarm /// </summary> /// <param name="startTime"></param> /// <param name="schedule"></param> /// <returns></returns> private DateTime GetBeginTime(DateTime startTime, AlarmSchedule schedule) { return startTime; }
/// <summary> /// Gets the expiration team based on the end time or no of occurences /// A schedule cant have both no of occurences and /// </summary> /// <param name="startTime"></param> /// <param name="schedule"></param> /// <returns></returns> private DateTime? GetExpirationTime(DateTime startTime, AlarmSchedule schedule) { if (schedule.EndTime != null) { return schedule.EndTime; } if (schedule.Occurrences != 0) { switch (schedule.Repeats) { case RecurrenceInterval.Daily: { return startTime.AddDays(schedule.Occurrences); } case RecurrenceInterval.Weekly: { return startTime.AddDays(7 * schedule.Occurrences); } case RecurrenceInterval.Monthly: case RecurrenceInterval.EndOfMonth: { return startTime.AddMonths(schedule.Occurrences); } case RecurrenceInterval.Yearly: { return startTime.AddYears(schedule.Occurrences); } default: { return null; } } } return null; }
/// <summary> /// Create alarm based on schedule /// </summary> /// <param name="schedule"></param> /// <returns></returns> public Alarm Create(AlarmSchedule schedule) { Alarm alarm = new Alarm(this.Name); alarm.Content = this.Content; alarm.Sound = new Uri(this.Sound.Path, UriKind.RelativeOrAbsolute); alarm.BeginTime = this.GetBeginTime(this.Start, schedule); alarm.RecurrenceType = schedule.Repeats; DateTime? endTime = this.GetExpirationTime(this.Start, schedule); // Set the expiration if available if (endTime != null) { alarm.ExpirationTime = endTime.Value; } return alarm; }