async void ExecuteDeleteCommand(object param) { try { var setting = BaseAlarmSettings.Instance.Alarms.Single(s => s.Id == this.Id); AlarmManager.Instance.DisableAlarm(setting); if (Enabled) { AlarmManager.Instance.EnableAlarm(setting); // anable it when it should be (toasts will be updated = created again) } // synchronize with model setting.Enabled = Enabled; // check all alarms and if there is at least one enabled // then show badge var numberOfActiveAlarms = BaseAlarmSettings.Instance.Alarms.Count(a => a.Enabled); if (numberOfActiveAlarms > 0) { BadgeManager.ShowBadge((uint)numberOfActiveAlarms); } else { BadgeManager.HideBadge(); } } catch (Exception ex) { string message = $"There was a problem enabling/disabling alarm.{Environment.NewLine}Error: {ex.Message}"; await new MessageDialog(message).ShowAsync(); } }
private async void btnSave_Click(object sender, RoutedEventArgs e) { #region Validation var settingVM = DataContext as AlarmSettingVM; if (string.IsNullOrWhiteSpace(settingVM.AudioFilename)) { await new MessageDialog("You have to select audio file.").ShowAsync(); return; } // no day selected if (settingVM.Repeatedly && (!chbMonday.IsChecked.Value) && (!chbTuesday.IsChecked.Value) && (!chbWednesday.IsChecked.Value) && (!chbThursday.IsChecked.Value) && (!chbFriday.IsChecked.Value) && (!chbSaturday.IsChecked.Value) && (!chbSunday.IsChecked.Value)) { await new MessageDialog("You have to select at least one day").ShowAsync(); return; } #endregion // synchronize VM to Model var setting = (settingVM.State == AlarmSettingState.New ? new BaseAlarmSetting() : BaseAlarmSettings.Instance.Alarms.Single(s => s.Id == settingVM.Id)); setting.Time = settingVM.Time; setting.Enabled = settingVM.Enabled; setting.AudioFilename = settingVM.AudioFilename; setting.ImageFilename = settingVM.ImageFilename; if (rbOnlyOnce.IsChecked.Value) { setting.Occurrence = OccurrenceType.OnlyOnce; } if (rbRepeatedly.IsChecked.Value) { setting.Occurrence = OccurrenceType.Repeatedly; } setting.DateTimeOffset = settingVM.DateTimeOffset; setting.IgnoreHolidays = settingVM.IgnoreHolidays; setting.DaysOfWeek = DayOfWeekType.None; if (chbMonday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Monday; } if (chbTuesday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Tuesday; } if (chbWednesday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Wednesday; } if (chbThursday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Thursday; } if (chbFriday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Friday; } if (chbSaturday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Saturday; } if (chbSunday.IsChecked.Value) { setting.DaysOfWeek = setting.DaysOfWeek | DayOfWeekType.Sunday; } if (settingVM.State == AlarmSettingState.New) { setting.Id = settingVM.Id; BaseAlarmSettings.Instance.Alarms.Add(setting); var alarmVM = new AlarmSettingVM(); alarmVM.Initialize(setting); AlarmSettingsVM.Instance.Alarms.Add(alarmVM); // add VM for alarm } else { var alarmVM = AlarmSettingsVM.Instance.Alarms.Single(a => a.Id == setting.Id); alarmVM.Initialize(setting); } // TODO: similar code as in ExecuteDeleteCommand method. Use one method for both! #region Refresh Toasts // if alarm was enabled then remove previous alarm toasts and plan it again (maybe time/date or other condition was changed) // so previous toasts can be invalid AlarmManager.Instance.DisableAlarm(setting); if (setting.Enabled) { AlarmManager.Instance.EnableAlarm(setting); // anable it if it should be (toasts will be updated = created again) } // check all alarms and if there is at least one enabled // then show badge var numberOfActiveAlarms = BaseAlarmSettings.Instance.Alarms.Count(a => a.Enabled); if (numberOfActiveAlarms > 0) { BadgeManager.ShowBadge((uint)numberOfActiveAlarms); } else { BadgeManager.HideBadge(); } #endregion Frame.GoBack(); }