Esempio n. 1
0
        private Uri GetRequestUri <T>(ReplicateCommand <T> command)
        {
            string requestUri = this.GetMessageBase <T>();

            requestUri += "/replicate";
            List <KeyValuePair <string, string> > queryStringParameters = new List <KeyValuePair <string, string> >();

            queryStringParameters.AddRange(this.GetAdditionalParameters(command.AdditionalParameters));
            queryStringParameters.ForEach(x => { requestUri = QueryHelpers.AddQueryString(requestUri, x.Key, x.Value); });
            return(new Uri(requestUri));
        }
Esempio n. 2
0
        public ConnectionInfoManagementViewModel()
        {
            AddNewItemCommand = IsItemEditing.Inverse().ToReactiveCommand();
            AddNewItemCommand.Subscribe(() =>
            {
                SelectedItem.Value = null;
                EditingItem.Value  = new T();
                if (IsGroupSelected?.Value == true)
                {
                    EditingItem.Value.GroupName = SelectedGroup.Value?.Name;
                }
                IsItemEditing.Value = true;
            }).AddTo(Disposable);

            ConnectCommand.Subscribe(async item => await ConfirmConnect(item)).AddTo(Disposable);

            IsItemSelected = SelectedItem.Select(x => x != null).ToReadOnlyReactiveProperty();

            IsNotItemEditing = IsItemEditing.Inverse().ToReadOnlyReactiveProperty();

            SelectedItem.Subscribe(x =>
            {
                EditingItem.Value   = SelectedItem.Value;
                IsItemEditing.Value = false;
            }).AddTo(Disposable);

            StartEditCommand = IsItemSelected
                               .CombineLatest(IsItemEditing.Inverse(), (a, b) => a && b)
                               .ToReactiveCommand();
            StartEditCommand.Subscribe(() =>
            {
                EditingItem.Value   = SelectedItem.Value.CloneDeep();
                IsItemEditing.Value = true;
            }).AddTo(Disposable);

            ReplicateCommand = IsItemSelected
                               .CombineLatest(IsItemEditing.Inverse(), (a, b) => a && b)
                               .ToReactiveCommand();
            ReplicateCommand.Subscribe(() =>
            {
                var replicated      = SelectedItem.Value.CloneDeep();
                replicated.Id       = -1;
                SelectedItem.Value  = null;
                EditingItem.Value   = replicated;
                IsItemEditing.Value = true;
            }).AddTo(Disposable);

            RemoveCommand = IsItemSelected
                            .CombineLatest(IsItemEditing.Inverse(), (a, b) => a && b)
                            .ToAsyncReactiveCommand();
            RemoveCommand.Subscribe(async() =>
            {
                if (await Remove(SelectedItem.Value))
                {
                    Items.Remove(SelectedItem.Value);
                    // Renew Windows JumpList
                    JumpListHelper.RenewJumpList(await MainWindow.DbContext.EnumerateAllConnectionInfos());
                }
            }).AddTo(Disposable);

            DiscardChangesCommand = IsItemEditing.ToReactiveCommand();
            DiscardChangesCommand.Subscribe(() =>
            {
                EditingItem.Value   = SelectedItem.Value ?? new T();
                IsItemEditing.Value = false;
            }).AddTo(Disposable);

            SaveChangesCommand = IsItemEditing.ToReactiveCommand();
            SaveChangesCommand.Subscribe(async() =>
            {
                var selectedItem = SelectedItem.Value;
                var item         = EditingItem.Value;
                try
                {
                    var(result, resultItem) = await Save(item);
                    if (resultItem == null)
                    {
                        return;        // FAILED
                    }
                    item = resultItem; // Replace with the saved item
                    if (result)        // ADDED
                    {
                        Items.Add(item);
                    }
                    else // UPDATED
                    {
                        var oldItem = Items.FirstOrDefault(x => x.Id == item.Id);
                        if (oldItem != null)
                        {
                            var index = Items.IndexOf(oldItem);
                            if (index >= 0)
                            {
                                Items.RemoveAt(index);
                                Items.Insert(index, item);
                            }
                        }
                    }
                    // Renew Windows JumpList
                    JumpListHelper.RenewJumpList(await MainWindow.DbContext.EnumerateAllConnectionInfos());
                }
                catch (OperationCanceledException) // User manually canceled
                {
                    return;
                }
                SelectedItem.Value  = item;
                IsItemEditing.Value = false;
            }).AddTo(Disposable);

            // Connection info filterings
            FilterText
            .Throttle(TimeSpan.FromMilliseconds(500))
            .ObserveOnDispatcher()
            .Subscribe(_ => RefreshCollectionView())
            .AddTo(Disposable);
            SelectedGroup
            .ObserveOnDispatcher()
            .Subscribe(_ => RefreshCollectionView())
            .AddTo(Disposable);

            // If any group is selected or not (except for "All")
            IsGroupSelected = SelectedGroup
                              .Select(x => x?.Name != AllGroupName)
                              .ToReadOnlyReactivePropertySlim()
                              .AddTo(Disposable);

            // Group list extraction on connection info events
            Observable.CombineLatest(
                // When Add, Remove or Update
                Items.CollectionChangedAsObservable()
                .Select(_ => Unit.Default)
                .StartWith(Unit.Default),
                // When GroupName property in each element changed
                Items.ObserveElementPropertyChanged()
                .Where(x => x.EventArgs.PropertyName == nameof(ConnectionInfoBase.GroupName))
                .Select(_ => Unit.Default)
                .StartWith(Unit.Default)
                )
            .Throttle(TimeSpan.FromMilliseconds(500))     // Once 500 ms
            .ObserveOnDispatcher()
            .Subscribe(_ =>
            {
                var selectedGroup = SelectedGroup.Value;
                // Reload group list
                Groups.Clear();
                EnumerateGroups().ToList().ForEach(Groups.Add);
                // Reset selected group
                SelectedGroup.Value = (selectedGroup is null) ? Groups.FirstOrDefault() : selectedGroup;
            })
            .AddTo(Disposable);
        }
Esempio n. 3
0
 public HttpRequestMessage GetRequestMessage <T>(ReplicateCommand <T> command)
 {
     return(this.GetRequestMessage <T>(this.GetRequestUri <T>(command), this.GetHttpContent <T>(command), HttpMethod));
 }
Esempio n. 4
0
 private HttpContent GetHttpContent <T>(ReplicateCommand <T> command)
 {
     return(new StringContent(this.serializerService.Serialize(command.Replica)));
 }
 public ReplicateHttpApiCommand(ReplicateCommand <T> command, IRequestMessageBuilderFactory requestMessageBuilderFactory)
 {
     this.command        = command;
     this.requestBuilder = requestMessageBuilderFactory.GetRequestMessageBuilder <ReplicateRequestMessageBuilder>();
 }