private void OnCancel() { if (AddMode) { RepeatedScheduleCollection.Remove(SelectedRepeatedSchedule); SelectedRepeatedSchedule = null; AddMode = false; } else if (EditMode) { SelectedRepeatedSchedule.CancelEdit(); EditMode = false; } else if (RemoveMode) { foreach (var item in RepeatedScheduleCollection) { if (item.IsChecked) { item.IsChecked = false; } } RemoveMode = false; } CanUseDatagrid = true; CanEditFields = false; }
private bool CanSave() { if (RemoveMode) { if (RepeatedScheduleCollection.Any(x => x.IsChecked)) { return(true); } return(false); } return(AddMode || EditMode); }
private void OnAdd() { AddMode = true; CanEditFields = true; CanUseDatagrid = false; SelectedRepeatedSchedule = new RepeatedScheduleModel { Description = "", AudioLocation = "", PlayerLocation = Global.PlayerLocation, IsChecked = false, StartDateTime = DateTime.Now, Repetition = Repetition.Daily }; RepeatedScheduleCollection.Add(SelectedRepeatedSchedule); }
private async void OnSave() { if (AddMode) { var repeatedSchedule = Converter.ConvertFromRepeatedScheduleModel(SelectedRepeatedSchedule); bool success = true; string error = ""; int id = 0; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <RepeatedSchedule> repo = new Repository <RepeatedSchedule>()) { var result = repo.InsertWithResult(repeatedSchedule); if (result.Status.Success) { id = result.Data.ID; } else { success = false; error = result.Status.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured.Message: {ex.Message}"; } }); if (success) { SelectedRepeatedSchedule.Id = id; OnInformationRequested("Data added successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { RepeatedScheduleCollection.Remove(SelectedRepeatedSchedule); SelectedRepeatedSchedule = null; OnErrorOccured(error); } AddMode = false; } else if (EditMode) { var repeatedSchedule = Converter.ConvertFromRepeatedScheduleModel(SelectedRepeatedSchedule); bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <RepeatedSchedule> repo = new Repository <RepeatedSchedule>()) { var result = repo.Update(repeatedSchedule); if (!result.Success) { success = false; error = result.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured.Message: {ex.Message}"; } }); if (success) { SelectedRepeatedSchedule.EndEdit(); OnInformationRequested("Data updated successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { SelectedRepeatedSchedule.CancelEdit(); OnErrorOccured(error); } EditMode = false; } else if (RemoveMode) { var temp = from item in RepeatedScheduleCollection where item.IsChecked select item; var repeatedSchedulesToRemove = temp.Select(x => Converter.ConvertFromRepeatedScheduleModel(x)); bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <RepeatedSchedule> repo = new Repository <RepeatedSchedule>()) { var result = repo.Delete(repeatedSchedulesToRemove); if (!result.Success) { success = false; error = result.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured.Message: {ex.Message}"; } }); if (success) { //to avoid Invalid Operation Exception for (int i = 0; i < RepeatedScheduleCollection.Count; i++) { if (RepeatedScheduleCollection[i].IsChecked) { RepeatedScheduleCollection.RemoveAt(i); i--; } } OnInformationRequested("Data removed successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { OnErrorOccured(error); } RemoveMode = false; } CanUseDatagrid = true; CanEditFields = false; }