private void SelectPrevious() { if (_spells.Any()) { SpellListItemViewModel selected = _spells.FirstOrDefault(x => x.IsSelected); foreach (SpellListItemViewModel spell in _spells) { spell.IsSelected = false; } if (selected == null) { _spells[_spells.Count - 1].IsSelected = true; _selectedSpell = new SpellViewModel(_spells[_spells.Count - 1].SpellModel); } else { int index = Math.Max(_spells.IndexOf(selected) - 1, 0); _spells[index].IsSelected = true; _selectedSpell = new SpellViewModel(_spells[index].SpellModel); } OnPropertyChanged(nameof(SelectedSpell)); } }
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(SpellsViewModel.SelectedSpell)) { SpellListItemViewModel selected = _viewModel.Spells.FirstOrDefault(x => x.IsSelected); if (selected != null) { if (_tree.ItemContainerGenerator.ContainerFromItem(selected) is TreeViewItem item) { item.BringIntoView(); } } } }
private void SelectSpell(SpellListItemViewModel spellItem) { bool selectSpell = true; if (_editSpellXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedSpell.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 (!SaveEditSpell()) { selectSpell = false; } } else if (result == false) { CancelEditSpell(); } else { selectSpell = false; } } else { CancelEditSpell(); } } if (selectSpell) { foreach (SpellListItemViewModel item in _spells) { item.IsSelected = false; } spellItem.IsSelected = true; _selectedSpell = new SpellViewModel(spellItem.SpellModel); OnPropertyChanged(nameof(SelectedSpell)); } }
private void SortSpells() { if (_spells != null && _spells.Count > 0) { List <SpellModel> spells = _spellSearchService.Sort(_spells.Select(x => x.SpellModel), _spellSearchInput.SortOption.Key); for (int i = 0; i < spells.Count; ++i) { if (spells[i].Id != _spells[i].SpellModel.Id) { SpellListItemViewModel spell = _spells.FirstOrDefault(x => x.SpellModel.Id == spells[i].Id); if (spell != null) { _spells.Move(_spells.IndexOf(spell), i); } } } } }
/// <summary> /// Searches, applying current sorting and filtering /// </summary> public void Search() { _spells.Clear(); foreach (SpellModel spellModel in _spellSearchService.Search(_spellSearchInput)) { _spells.Add(new SpellListItemViewModel(spellModel, _stringService)); } if (_selectedSpell != null) { SpellListItemViewModel spell = _spells.FirstOrDefault(x => x.SpellModel.Id == _selectedSpell.SpellModel.Id); if (spell != null) { spell.IsSelected = true; } } OnPropertyChanged(nameof(SortAndFilterHeader)); }
private void SpellListItemViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(SpellListItemViewModel.IsSelected)) { SpellListItemViewModel spellListSpellViewModel = sender as SpellListItemViewModel; if (spellListSpellViewModel.IsSelected) { if (!_multiSelect) { _selectedSpells.Clear(); } if (!_selectedSpells.Any(x => x.Id == spellListSpellViewModel.SpellModel.Id)) { _selectedSpells.Add(spellListSpellViewModel.SpellModel); } } else { _selectedSpells.RemoveAll(x => x.Id == spellListSpellViewModel.SpellModel.Id); } } }
private void SelectSpell(SpellListItemViewModel spell) { _dialogService.ShowDetailsDialog(new SpellViewModel(spell.SpellModel)); }
private void Delete() { if (_selectedSpell != null) { bool canDelete = true; foreach (CharacterModel character in _compendium.Characters) { foreach (SpellbookModel spellbook in character.Spellbooks) { foreach (SpellbookEntryModel spellbookEntry in spellbook.Spells) { if (spellbookEntry.Spell != null && spellbookEntry.Spell.Id == _selectedSpell.SpellModel.Id) { canDelete = false; break; } } if (!canDelete) { break; } } if (!canDelete) { break; } } if (canDelete) { string message = String.Format("Are you sure you want to delete {0}?", _selectedSpell.Name); bool?result = _dialogService.ShowConfirmationDialog("Delete Spell", message, "Yes", "No", null); if (result == true) { _compendium.DeleteSpell(_selectedSpell.SpellModel.Id); SpellListItemViewModel listItem = _spells.FirstOrDefault(x => x.SpellModel.Id == _selectedSpell.SpellModel.Id); if (listItem != null) { _spells.Remove(listItem); } _selectedSpell = null; _compendium.SaveSpells(); OnPropertyChanged(nameof(SelectedSpell)); if (_editSpellXML != null) { CancelEditSpell(); } } } else { _dialogService.ShowConfirmationDialog("Unable Delete Spell", "Spell is in use by a character.", "OK", null, null); } } }
private void Copy() { if (_selectedSpell != null) { bool copySpell = true; if (_editSpellXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedSpell.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 (!SaveEditSpell()) { copySpell = false; } } else if (result == false) { CancelEditSpell(); } else { copySpell = false; } } else { CancelEditSpell(); } } if (copySpell) { SpellModel newSpell = new SpellModel(_selectedSpell.SpellModel); newSpell.Name += " (copy)"; newSpell.Id = Guid.NewGuid(); newSpell.XML = newSpell.XML.Replace("<name>" + _selectedSpell.SpellModel.Name + "</name>", "<name>" + newSpell.Name + "</name>"); _compendium.AddSpell(newSpell); if (_spellSearchService.SearchInputApplies(_spellSearchInput, newSpell)) { SpellListItemViewModel listItem = new SpellListItemViewModel(newSpell, _stringService); _spells.Add(listItem); foreach (SpellListItemViewModel item in _spells) { item.IsSelected = false; } listItem.IsSelected = true; } _selectedSpell = new SpellViewModel(newSpell); SortSpells(); _compendium.SaveSpells(); OnPropertyChanged(nameof(SelectedSpell)); } } }
private void Add() { bool addSpell = true; if (_editSpellXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedSpell.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 (!SaveEditSpell()) { addSpell = false; } } else if (result == false) { CancelEditSpell(); } else { addSpell = false; } } else { CancelEditSpell(); } } if (addSpell) { string xml = @"<name>New Spell</name> <level></level> <school></school> <ritual></ritual> <time></time> <range></range> <components></components> <duration></duration> <classes></classes> <text></text> <roll></roll>" ; SpellModel spellModel = _xmlImporter.GetSpell(xml); _compendium.AddSpell(spellModel); if (_spellSearchService.SearchInputApplies(_spellSearchInput, spellModel)) { SpellListItemViewModel listItem = new SpellListItemViewModel(spellModel, _stringService); _spells.Add(listItem); foreach (SpellListItemViewModel item in _spells) { item.IsSelected = false; } listItem.IsSelected = true; } _selectedSpell = new SpellViewModel(spellModel); _editSpellXML = spellModel.XML; SortSpells(); _compendium.SaveSpells(); OnPropertyChanged(nameof(EditingSpellXML)); OnPropertyChanged(nameof(IsEditingSpell)); OnPropertyChanged(nameof(SelectedSpell)); } }
private bool SaveEditSpell() { bool saved = false; try { SpellModel model = _xmlImporter.GetSpell(_editSpellXML); if (model != null) { model.Id = _selectedSpell.SpellModel.Id; _compendium.UpdateSpell(model); _selectedSpell = new SpellViewModel(model); SpellListItemViewModel oldListItem = _spells.FirstOrDefault(x => x.SpellModel.Id == model.Id); if (oldListItem != null) { if (_spellSearchService.SearchInputApplies(_spellSearchInput, model)) { oldListItem.UpdateModel(model); } else { _spells.Remove(oldListItem); } } _editSpellXML = null; _editHasUnsavedChanges = false; SortSpells(); _compendium.SaveSpells(); OnPropertyChanged(nameof(SelectedSpell)); OnPropertyChanged(nameof(EditingSpellXML)); OnPropertyChanged(nameof(IsEditingSpell)); 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); }