public SSHConnectionInfoManagementViewModel() : base() { SelectPrivateKeyFileCommand = IsItemEditing.ToReactiveCommand(); SelectPrivateKeyFileCommand.Subscribe(async() => { var openFileDialog = new OpenFileDialog { FilterIndex = 1, Filter = "Private Key Files (*.*)|*.*" }; if (openFileDialog.ShowDialog() == true) { EditingItem.Value.PrivateKeyFilePath = openFileDialog.FileName; } else { var result = await MainWindow.ShowConfirmationDialog( Resources.SSHConnectionInfo_Dialog_ClearPrivateKeyFilePath_Message, Resources.SSHConnectionInfo_Dialog_ClearPrivateKeyFilePath_Title); if (result) { EditingItem.Value.PrivateKeyFilePath = null; } } }).AddTo(Disposable); AddNewPortForwardingCommand = IsItemEditing.ToReactiveCommand(); AddNewPortForwardingCommand.Subscribe(() => { EditingItem.Value.PortForwardingCollection.Add(new PortForwarding()); }).AddTo(Disposable); RemovePortForwardingCommand = IsItemEditing.ToReactiveCommand <PortForwarding>(); RemovePortForwardingCommand.Subscribe(item => { EditingItem.Value.PortForwardingCollection.Remove(item); }).AddTo(Disposable); }
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); }