//add a new tag from the context menu and then apply it to the selected instruments private async void NewTagTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Enter) { return; } var newTagTextBox = (TextBox)sender; string newTagName = newTagTextBox.Text; //add the tag ApiResponse <Tag> addTagResult = await _client.AddTag(new Tag { Name = newTagName }).ConfigureAwait(true); if (!addTagResult.WasSuccessful) { await this.ShowMessageAsync("Error", "Could not add tag").ConfigureAwait(true); return; } Tag newTag = addTagResult.Result; //apply the tag to the selected instruments IEnumerable <Instrument> selectedInstruments = InstrumentsGrid.SelectedItems.Cast <Instrument>(); foreach (Instrument i in selectedInstruments) { i.Tags.Add(newTag); await _client.UpdateInstrument(i).ConfigureAwait(true); } //update the tag menu ApiResponse <List <Tag> > allTags = await _client.GetTags().ConfigureAwait(true); if (allTags.WasSuccessful) { BuildTagContextMenu(allTags.Result); } newTagTextBox.Text = ""; CollectionViewSource.GetDefaultView(InstrumentsGrid.ItemsSource).Refresh(); }
public TagsViewModel(IDataClient client, IDialogCoordinator dialogCoordinator) { _client = client; _dialogCoordinator = dialogCoordinator; Tags = new ObservableCollection <TagViewModel>(); //Load all tags from db LoadTags = ReactiveCommand.CreateFromTask(async _ => await _client.GetTags().ConfigureAwait(true)); LoadTags.Subscribe(async result => { if (!result.WasSuccessful) { await _dialogCoordinator.ShowMessageAsync(this, "Error", string.Join("\n", result.Errors)).ConfigureAwait(true); return; } foreach (var tag in result.Result) { Tags.Add(new TagViewModel(tag)); } }); //Add new tag var addCanExecute = NewTag .WhenAnyValue(x => x.Name) .Select(newName => !string.IsNullOrEmpty(newName)); Add = ReactiveCommand.CreateFromTask(async _ => { var tag = await client.AddTag(NewTag.Model).ConfigureAwait(true); NewTag.Name = ""; return(tag); }, addCanExecute); Add.Subscribe(async result => { if (!result.WasSuccessful) { await _dialogCoordinator.ShowMessageAsync(this, "Error", string.Join("\n", result.Errors)).ConfigureAwait(true); return; } Tags.Add(new TagViewModel(result.Result)); }); //When changing the selected tag, reset the delete confirmation this.WhenAnyValue(x => x.SelectedTag) .Buffer(1, 1) .Subscribe(x => { var tagVm = x.FirstOrDefault(); if (tagVm != null) { tagVm.ConfirmDelete = false; } }); //Delete Tag Delete = ReactiveCommand.CreateFromTask(async _ => { if (SelectedTag.ConfirmDelete != true) { SelectedTag.ConfirmDelete = true; return(null); } return(await client.DeleteTag(SelectedTag?.Model).ConfigureAwait(true)); }, this.WhenAnyValue(x => x.SelectedTag).Select(x => x != null)); Delete.Subscribe(async result => { if (result == null) { return; } if (await result.DisplayErrors(this, _dialogCoordinator)) { return; } Tags.Remove(Tags.FirstOrDefault(x => x.Model.ID == result.Result.ID)); }); //Update Tag var saveCanExecute = this .WhenAnyValue(x => x.SelectedTag, x => x.SelectedTagHasErrors, (tag, hasError) => new { tag, hasError }) .Select(x => x.tag != null && x.hasError == false); Save = ReactiveCommand.CreateFromTask( async _ => await client.UpdateTag(SelectedTag?.Model).ConfigureAwait(true), saveCanExecute); Save.Subscribe(async result => await result.DisplayErrors(this, _dialogCoordinator)); }