예제 #1
0
        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));
        }