Beispiel #1
0
        public ConfigurationVM(MainVM mvm)
        {
            MainVM          = mvm;
            ProfilesDisplay = Profiles.Connect().ToObservableCollection(this);
            PatchersDisplay = this.WhenAnyValue(x => x.SelectedProfile)
                              .Select(p => p?.Patchers.Connect() ?? Observable.Empty <IChangeSet <PatcherVM> >())
                              .Switch()
                              .ToObservableCollection(this);

            CompleteConfiguration = ReactiveCommand.CreateFromTask(
                async() =>
            {
                var initializer = this.NewPatcher;
                if (initializer == null)
                {
                    return;
                }
                AddNewPatchers(await initializer.Construct().ToListAsync());
            },
                canExecute: this.WhenAnyValue(x => x.NewPatcher)
                .Select(patcher =>
            {
                if (patcher == null)
                {
                    return(Observable.Return(false));
                }
                return(patcher.WhenAnyValue(x => x.CanCompleteConfiguration)
                       .Select(e => e.Succeeded));
            })
                .Switch());

            CancelConfiguration = ReactiveCommand.Create(
                () =>
            {
                NewPatcher?.Cancel();
                NewPatcher = null;
            });

            // Dispose any old patcher initializations
            this.WhenAnyValue(x => x.NewPatcher)
            .DisposePrevious()
            .Subscribe()
            .DisposeWith(this);

            _DisplayedObject = Observable.CombineLatest(
                this.WhenAnyValue(x => x.SelectedProfile !.DisplayedObject),
                this.WhenAnyValue(x => x.NewPatcher),
                (selected, newConfig) => (newConfig as object) ?? selected)
                               .ToGuiProperty(this, nameof(DisplayedObject), default);

            RunPatchers = NoggogCommand.CreateFromJob(
                extraInput: this.WhenAnyValue(x => x.SelectedProfile),
                jobCreator: (profile) =>
            {
                if (SelectedProfile == null)
                {
                    return(default(PatchersRunVM?), Observable.Return(Unit.Default));
                }
                var ret            = new PatchersRunVM(this, SelectedProfile);
                var completeSignal = ret.WhenAnyValue(x => x.Running)
                                     .TurnedOff()
                                     .FirstAsync();
                return(ret, completeSignal);
            },
                createdJobs: out var createdRuns,
                canExecute: this.WhenAnyFallback(x => x.SelectedProfile !.BlockingError, fallback: ErrorResponse.Failure)
                .Select(err => err.Succeeded))
                          .DisposeWith(this);

            _CurrentRun = createdRuns
                          .ToGuiProperty(this, nameof(CurrentRun), default);

            this.WhenAnyValue(x => x.CurrentRun)
            .NotNull()
            .Do(run => MainVM.ActivePanel = run)
            .ObserveOn(RxApp.TaskpoolScheduler)
            .Subscribe(r => r.Run())
            .DisposeWith(this);

            ShowHelpToggleCommand = ReactiveCommand.Create(() => ShowHelp = !ShowHelp);
        }