private void SelectPrevious() { if (_conditions.Any()) { ConditionListItemViewModel selected = _conditions.FirstOrDefault(x => x.IsSelected); foreach (ConditionListItemViewModel condition in _conditions) { condition.IsSelected = false; } if (selected == null) { _conditions[_conditions.Count - 1].IsSelected = true; _selectedCondition = new ConditionViewModel(_conditions[_conditions.Count - 1].ConditionModel); } else { int index = Math.Max(_conditions.IndexOf(selected) - 1, 0); _conditions[index].IsSelected = true; _selectedCondition = new ConditionViewModel(_conditions[index].ConditionModel); } OnPropertyChanged(nameof(SelectedCondition)); } }
private void Delete() { if (_selectedCondition != null) { bool canDelete = true; foreach (CharacterModel character in _compendium.Characters) { foreach (AppliedConditionModel condition in character.Conditions) { if (condition.ConditionModel != null && condition.ConditionModel.Id == _selectedCondition.ConditionModel.Id) { canDelete = false; break; } } if (!canDelete) { break; } } if (canDelete) { string message = String.Format("Are you sure you want to delete {0}?", _selectedCondition.Name); bool?result = _dialogService.ShowConfirmationDialog("Delete Condition", message, "Yes", "No", null); if (result == true) { _compendium.DeleteCondition(_selectedCondition.ConditionModel.Id); ConditionListItemViewModel listItem = _conditions.FirstOrDefault(x => x.ConditionModel.Id == _selectedCondition.ConditionModel.Id); if (listItem != null) { _conditions.Remove(listItem); } _selectedCondition = null; _compendium.SaveConditions(); OnPropertyChanged(nameof(SelectedCondition)); if (_editConditionCommand != null) { CancelEditCondition(); } } } else { _dialogService.ShowConfirmationDialog("Unable Delete Condition", "Condition is in use by a character.", "OK", null, null); } } }
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(ConditionsViewModel.SelectedCondition)) { ConditionListItemViewModel selected = _viewModel.Conditions.FirstOrDefault(x => x.IsSelected); if (selected != null) { if (_tree.ItemContainerGenerator.ContainerFromItem(selected) is TreeViewItem item) { item.BringIntoView(); } } } }
private void SelectCondition(ConditionListItemViewModel conditionItem) { bool selectCondition = true; if (_editConditionXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedCondition.Name, Environment.NewLine + Environment.NewLine); string accept = "Save and Continue"; string reject = "Discard Changes"; string cancel = "Cancel Navigation"; bool? result = _dialogService.ShowConfirmationDialog("Unsaved Changes", body, accept, reject, cancel); if (result == true) { if (!SaveEditCondition()) { selectCondition = false; } } else if (result == false) { CancelEditCondition(); } else { selectCondition = false; } } else { CancelEditCondition(); } } if (selectCondition) { foreach (ConditionListItemViewModel item in _conditions) { item.IsSelected = false; } conditionItem.IsSelected = true; _selectedCondition = new ConditionViewModel(conditionItem.ConditionModel); OnPropertyChanged(nameof(SelectedCondition)); } }
/// <summary> /// Searches, applying current sorting and filtering /// </summary> public void Search() { _conditions.Clear(); foreach (ConditionModel conditionModel in _conditionSearchService.Search(_conditionSearchInput)) { _conditions.Add(new ConditionListItemViewModel(conditionModel)); } if (_selectedCondition != null) { ConditionListItemViewModel condition = _conditions.FirstOrDefault(x => x.ConditionModel.Id == _selectedCondition.ConditionModel.Id); if (condition != null) { condition.IsSelected = true; } } }
private void SortConditions() { if (_conditions != null && _conditions.Count > 0) { List <ConditionModel> conditions = _conditionSearchService.Sort(_conditions.Select(x => x.ConditionModel), _conditionSearchInput.SortOption); for (int i = 0; i < conditions.Count; ++i) { if (conditions[i].Id != _conditions[i].ConditionModel.Id) { ConditionListItemViewModel conditionListItem = _conditions.FirstOrDefault(x => x.ConditionModel.Id == conditions[i].Id); if (conditionListItem != null) { _conditions.Move(_conditions.IndexOf(conditionListItem), i); } } } } }
private void Copy() { if (_selectedCondition != null) { bool copyCondition = true; if (_editConditionXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedCondition.Name, Environment.NewLine + Environment.NewLine); string accept = "Save and Continue"; string reject = "Discard Changes"; string cancel = "Cancel Navigation"; bool? result = _dialogService.ShowConfirmationDialog("Unsaved Changes", body, accept, reject, cancel); if (result == true) { if (!SaveEditCondition()) { copyCondition = false; } } else if (result == false) { CancelEditCondition(); } else { copyCondition = false; } } else { CancelEditCondition(); } } if (copyCondition) { ConditionModel newCondition = new ConditionModel(_selectedCondition.ConditionModel); newCondition.Name += " (copy)"; newCondition.Id = Guid.NewGuid(); newCondition.XML = newCondition.XML.Replace("<name>" + _selectedCondition.ConditionModel.Name + "</name>", "<name>" + newCondition.Name + "</name>"); _compendium.AddCondition(newCondition); if (_conditionSearchService.SearchInputApplies(_conditionSearchInput, newCondition)) { ConditionListItemViewModel listItem = new ConditionListItemViewModel(newCondition); _conditions.Add(listItem); foreach (ConditionListItemViewModel item in _conditions) { item.IsSelected = false; } listItem.IsSelected = true; } _selectedCondition = new ConditionViewModel(newCondition); SortConditions(); _compendium.SaveConditions(); OnPropertyChanged(nameof(SelectedCondition)); } } }
private void Add() { bool addCondition = true; if (_editConditionXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedCondition.Name, Environment.NewLine + Environment.NewLine); string accept = "Save and Continue"; string reject = "Discard Changes"; string cancel = "Cancel Navigation"; bool? result = _dialogService.ShowConfirmationDialog("Unsaved Changes", body, accept, reject, cancel); if (result == true) { if (!SaveEditCondition()) { addCondition = false; } } else if (result == false) { CancelEditCondition(); } else { addCondition = false; } } else { CancelEditCondition(); } } if (addCondition) { string xml = "<name>New Condition</name><text></text>"; ConditionModel conditionModel = _xmlImporter.GetCondition(xml); _compendium.AddCondition(conditionModel); if (_conditionSearchService.SearchInputApplies(_conditionSearchInput, conditionModel)) { ConditionListItemViewModel listItem = new ConditionListItemViewModel(conditionModel); _conditions.Add(listItem); foreach (ConditionListItemViewModel item in _conditions) { item.IsSelected = false; } listItem.IsSelected = true; } _selectedCondition = new ConditionViewModel(conditionModel); _editConditionXML = conditionModel.XML; SortConditions(); _compendium.SaveConditions(); OnPropertyChanged(nameof(EditingConditionXML)); OnPropertyChanged(nameof(IsEditingCondition)); OnPropertyChanged(nameof(SelectedCondition)); } }
private bool SaveEditCondition() { bool saved = false; try { ConditionModel model = _xmlImporter.GetCondition(_editConditionXML); if (model != null) { model.Id = _selectedCondition.ConditionModel.Id; _compendium.UpdateCondition(model); _selectedCondition = new ConditionViewModel(model); ConditionListItemViewModel oldListItem = _conditions.FirstOrDefault(x => x.ConditionModel.Id == model.Id); if (oldListItem != null) { if (_conditionSearchService.SearchInputApplies(_conditionSearchInput, model)) { oldListItem.UpdateModel(model); } else { _conditions.Remove(oldListItem); } } _editConditionXML = null; _editHasUnsavedChanges = false; SortConditions(); _compendium.SaveConditions(); OnPropertyChanged(nameof(SelectedCondition)); OnPropertyChanged(nameof(EditingConditionXML)); OnPropertyChanged(nameof(IsEditingCondition)); OnPropertyChanged(nameof(HasUnsavedChanges)); saved = true; } else { string message = String.Format("Something went wrong...{0}{1}{2}{3}", Environment.NewLine + Environment.NewLine, "The following are required:", Environment.NewLine, "-name"); _dialogService.ShowConfirmationDialog("Unable To Save", message, "OK", null, null); } } catch (Exception ex) { string message = String.Format("Something went wrong...{0}{1}", Environment.NewLine + Environment.NewLine, ex.Message); _dialogService.ShowConfirmationDialog("Unable To Save", message, "OK", null, null); } return(saved); }