public OperEditorViewModel(ICachedService cachedService, IMapper mapper, IShell shell) { Shell = shell as CachedServiceShell; this.cachedService = cachedService; this.mapper = mapper; IsValid = true; validator = new OperEditorValidator(); var operTemplates = new SourceList <string>(); var canSave = this.WhenAnyValue(tvm => tvm.IsDirty, isd => isd == true).Concat(Shell.CanEdit); SaveChangesCommand = ReactiveCommand.CreateFromTask(async() => await Save(), canSave); CancelCommand = ReactiveCommand.CreateFromTask(async() => { if (IsDirty) { if (!await Shell.ShowWarningAffirmativeDialogAsync ("All unsaved changes will be lost. Close window?")) { return; } } Close(); }); this.ObservableForProperty(s => s.Mode) .Subscribe(mode => { var templates = mode.Value == OperMode.Exporter ? DataExporters.Select(pair => pair.Key) : DataImporters.Select(pair => pair.Key); operTemplates.ClearAndAddRange(templates); OperTemplates = operTemplates.SpawnCollection(); ImplementationType = operTemplates.First(); }); this.ObservableForProperty(s => s.ImplementationType) .Where(type => type.Value != null) .Subscribe(type => { var operType = Mode == OperMode.Exporter ? DataExporters[type.Value] : DataImporters[type.Value]; if (operType == null) { return; } Configuration = Activator.CreateInstance(operType); mapper.Map(cachedService, Configuration); }); this.WhenAnyObservable(s => s.AllErrors.CountChanged) .Subscribe(_ => IsValid = !AllErrors.Items.Any()); }
public TaskEditorViewModel(ICachedService service, IMapper mapper, IShell shell) { cachedService = service; this.mapper = mapper; validator = new TaskEditorValidator(); IsValid = true; Shell = shell as CachedServiceShell; taskParameters = new SourceList <TaskParameter>(); taskDependencies = new SourceList <DesktopTaskDependence>(); bindedOpers = new SourceList <DesktopOperation>(); incomingPackages = new SourceList <string>(); tasks = new SourceList <DesktopTaskNameId>(); DataImporters = cachedService.DataImporters; DataExporters = cachedService.DataExporters; implementationTypes = new SourceList <string>(); var canSave = this.WhenAnyValue(tvm => tvm.IsDirty, tvm => tvm.IsValid, (isd, isv) => isd && isv).Concat(Shell.CanEdit); SaveChangesCommand = ReactiveCommand.CreateFromTask(async() => await Save(), canSave); CancelCommand = ReactiveCommand.CreateFromTask(Cancel); ClipBoardFillCommand = ReactiveCommand.Create <string>(Clipboard.SetText); OpenCurrentTaskViewCommand = ReactiveCommand //todo: make without interface blocking .CreateFromTask(async() => cachedService.OpenPageInBrowser( await cachedService.GetCurrentTaskViewById(Id))); RemoveOperationCommand = ReactiveCommand.Create <DesktopOperation>(to => { if (SelectedOperation?.Id == to.Id) { ClearSelections(); } bindedOpers.Remove(to); }, Shell.CanEdit); RemoveParameterCommand = ReactiveCommand .Create <TaskParameter>(par => taskParameters.Remove(par), Shell.CanEdit); RemoveDependenceCommand = ReactiveCommand .Create <DesktopTaskDependence>(dep => taskDependencies.Remove(dep), Shell.CanEdit); AddParameterCommand = ReactiveCommand.Create(() => taskParameters.Add(new TaskParameter { Name = "@RepPar" }), Shell.CanEdit); AddDependenceCommand = ReactiveCommand.CreateFromTask(async() => { if (tasks.Count == 0) { await Shell.ShowMessageAsync("No any existing tasks in service"); return; } tasks .Connect() .Bind(out var cachedTasks) .Subscribe(); var dependence = new DesktopTaskDependence { Tasks = cachedTasks, TaskId = cachedTasks.First().Id }; taskDependencies.Add(dependence); }, Shell.CanEdit); AddOperationCommand = ReactiveCommand.CreateFromTask(AddOperation, Shell.CanEdit); CreateOperConfigCommand = ReactiveCommand.CreateFromTask(CreateOperConfig, Shell.CanEdit); OpenTemplatesListCommand = ReactiveCommand.CreateFromTask(async() => { if (SelectedOperationConfig != null && Shell.Role == ServiceUserRole.Editor) { if (!await Shell.ShowWarningAffirmativeDialogAsync ("All unsaved operation configuration changes will be lost. Close window?")) { return; } } await SelectOperFromTemplates(); }, Shell.CanEdit); SelectOperationCommand = ReactiveCommand.CreateFromTask <DesktopOperation> (SelectOperation); this.ObservableForProperty(s => s.Mode) .Subscribe(mode => { var templates = mode?.Value == OperMode.Exporter ? DataExporters.Select(pair => pair.Key) : DataImporters.Select(pair => pair.Key); implementationTypes.ClearAndAddRange(templates); Type = implementationTypes.Items.FirstOrDefault(); }); this.ObservableForProperty(s => s.Type) .Where(type => type.Value != null) .Subscribe(type => { var operType = Mode == OperMode.Exporter ? DataExporters[type.Value] : DataImporters[type.Value]; if (operType == null) { return; } SelectedOperationConfig = Activator.CreateInstance(operType); mapper.Map(cachedService, SelectedOperationConfig); }); this.WhenAnyValue(tvm => tvm.SelectedOperationConfig) .Where(selop => selop != null) .Subscribe(conf => IncomingPackages = incomingPackages.SpawnCollection()); }