Beispiel #1
0
        public MainWindowViewModel(ILifetimeScope lifetimeScope, Dispatcher dispatcher, IOperationManager operationManager, LocLocalizer localizer, IDialogCoordinator dialogCoordinator,
                                   AppConfig config, IDialogFactory dialogFactory, IViewModel <CenterViewModel> model, IMainWindowCoordinator mainWindowCoordinator, ProjectFileWorkspace workspace)
            : base(lifetimeScope, dispatcher)
        {
            Receive <IncommingEvent>(e => e.Action());

            var last             = QueryProperty.Create <ProjectFile?>();
            var loadingOperation = QueryProperty.Create <OperationController?>();

            var self = Self;

            CenterView = this.RegisterViewModel(nameof(CenterView), model);
            workspace.Source.ProjectReset.RespondOn(null, pr => last.Value = pr.ProjectFile);

            #region Restarting

            OnPreRestart += (exception, o) =>
            {
                if (last != null)
                {
                    Self.Tell(last);
                }
            };
            Receive <ProjectFile>(workspace.Reset);

            #endregion

            #region Operation Manager

            RunningOperations = RegisterProperty <IEnumerable <RunningOperation> >(nameof(RunningOperations)).WithDefaultValue(operationManager.RunningOperations);
            RenctFiles        = RegisterProperty <RenctFilesCollection>(nameof(RenctFiles)).WithDefaultValue(new RenctFilesCollection(config, s => self.Tell(new InternlRenctFile(s))));

            NewCommad.WithExecute(operationManager.Clear, b => operationManager.ShouldClear(b, AddResource)).ThenRegister("ClearOp");
            NewCommad.WithExecute(operationManager.CompledClear, b => operationManager.ShouldCompledClear(b, AddResource)).ThenRegister("ClearAllOp");

            #endregion

            #region Save As

            UpdateSource?SaveAsProject()
            {
                var targetFile = dialogFactory.ShowSaveFileDialog(null, true, false, true, "transp", true,
                                                                  localizer.OpenFileDialogViewDialogFilter, true, true, localizer.MainWindowMainMenuFileSaveAs, Directory.GetCurrentDirectory(), out var result);

                if (result != true && CheckSourceOk(targetFile))
                {
                    return(null);
                }

                return(new UpdateSource(targetFile !));
            }

            bool CheckSourceOk(string?source)
            {
                if (!string.IsNullOrWhiteSpace(source))
                {
                    return(false);
                }
                UICall(() => dialogCoordinator.ShowMessage(localizer.CommonError, localizer.MainWindowModelLoadProjectSourceEmpty !));
                return(true);
            }

            NewCommad.WithCanExecute(b => b.FromProperty(last, file => file != null && !file.IsEmpty))
            .ThenFlow(SaveAsProject, b => b.Send.ToModel(CenterView))
            .ThenRegister("SaveAs");

            #endregion

            #region Open File

            Receive <InternlRenctFile>(o => OpentFileSource(o.File));

            async Task <LoadedProjectFile?> SourceSelectedFunc(SourceSelected s)
            {
                if (s.Mode != OpenFileMode.OpenExistingFile)
                {
                    return(await NewFileSource(s.Source));
                }
                OpentFileSource(s.Source);
                return(null);
            }

            void OpentFileSource(string?source)
            {
                if (CheckSourceOk(source))
                {
                    return;
                }

                mainWindowCoordinator.IsBusy = true;
                loadingOperation.Value       = operationManager.StartOperation(string.Format(localizer.MainWindowModelLoadProjectOperation, Path.GetFileName(source) ?? source));

                if (!workspace.ProjectFile !.IsEmpty)
                {
                    workspace.ProjectFile.Operator.Tell(ForceSave.Force(workspace.ProjectFile));
                }

                ProjectFile.BeginLoad(Context, loadingOperation.Value.Id, source !, "Project_Operator");
            }

            SupplyNewProjectFile?ProjectLoaded(LoadedProjectFile obj)
            {
                if (loadingOperation !.Value != null)
                {
                    if (obj.Ok)
                    {
                        loadingOperation.Value.Compled();
                    }
                    else
                    {
                        mainWindowCoordinator.IsBusy = false;
                        loadingOperation.Value.Failed(obj.ErrorReason?.Message ?? localizer.CommonError);
                        return(null);
                    }
                }

                loadingOperation.Value = null;
                if (obj.Ok)
                {
                    RenctFiles.Value.AddNewFile(obj.ProjectFile.Source);
                }

                last !.Value = obj.ProjectFile;

                return(new SupplyNewProjectFile(obj.ProjectFile));
            }

            NewCommad.WithCanExecute(b => b.NotNull(loadingOperation))
            .ThenFlow(
                SourceSelected.From(this.ShowDialog <IOpenFileDialog, string?>(TypedParameter.From(OpenFileMode.OpenExistingFile)), OpenFileMode.OpenExistingFile),
                b =>
            {
                b.Func(SourceSelectedFunc).ToSelf()
                .Then(b2 => b2.Func(ProjectLoaded !).ToModel(CenterView));
            })
Beispiel #2
0
        public MainWindowViewModel(ILifetimeScope lifetimeScope, IUIDispatcher dispatcher,
                                   IOperationManager operationManager, LocLocalizer localizer, IDialogCoordinator dialogCoordinator,
                                   AppConfig config, IDialogFactory dialogFactory, IViewModel <CenterViewModel> model,
                                   IMainWindowCoordinator mainWindowCoordinator, ProjectFileWorkspace workspace)
            : base(lifetimeScope, dispatcher)
        {
            this.Receive <IncommingEvent>(e => e.Action());

            var last             = new RxVar <ProjectFile?>(null);
            var loadingOperation = new RxVar <OperationController?>(null);

            var self = Self;

            CenterView = this.RegisterViewModel(nameof(CenterView), model);
            workspace.Source.ProjectReset.RespondOn(null, pr => last.Value = pr.ProjectFile);

            #region Restarting

            Start.Subscribe(_ =>
            {
                if (last != null)
                {
                    Self.Tell(last);
                }
            });
            this.Receive <ProjectFile>(workspace.Reset);

            #endregion

            #region Operation Manager

            RunningOperations = RegisterProperty <IEnumerable <RunningOperation> >(nameof(RunningOperations))
                                .WithDefaultValue(operationManager.RunningOperations);
            RenctFiles = RegisterProperty <RenctFilesCollection>(nameof(RenctFiles))
                         .WithDefaultValue(new RenctFilesCollection(config, s => self.Tell(new InternlRenctFile(s))));

            NewCommad.WithExecute(operationManager.Clear, operationManager.ShouldClear()).ThenRegister("ClearOp");
            NewCommad.WithExecute(operationManager.CompledClear, operationManager.ShouldCompledClear())
            .ThenRegister("ClearAllOp");

            #endregion

            #region Save As

            IObservable <UpdateSource> SaveAsProject()
            {
                var targetFile = dialogFactory.ShowSaveFileDialog(null, true, false, true, "transp", true,
                                                                  localizer.OpenFileDialogViewDialogFilter, true, true, localizer.MainWindowMainMenuFileSaveAs,
                                                                  Directory.GetCurrentDirectory());

                return(targetFile.NotEmpty()
                       .SelectMany(CheckSourceOk)
                       .Where(d => d.Item1)
                       .Select(r => new UpdateSource(r.Item2)));
            }

            async Task <(bool, string)> CheckSourceOk(string source)
            {
                if (!string.IsNullOrWhiteSpace(source))
                {
                    return(true, source);
                }
                await UICall(()
                             => dialogCoordinator.ShowMessage(localizer.CommonError,
                                                              localizer.MainWindowModelLoadProjectSourceEmpty !));

                return(false, source);
            }

            NewCommad.WithCanExecute(last.Select(pf => pf != null && !pf.IsEmpty))
            .WithFlow(ob => ob.SelectMany(_ => SaveAsProject())
                      .ToModel(CenterView))
            .ThenRegister("SaveAs");

            #endregion

            #region Open File

            IDisposable NewProjectFile(IObservable <SourceSelected> source) => source
            .SelectMany(SourceSelectedFunc)
            .NotNull()
            .ObserveOnSelf()
            .Select(ProjectLoaded)
            .ToModel(CenterView !);


            this.Receive <InternlRenctFile>(o => OpentFileSource(o.File));

            IObservable <LoadedProjectFile?> SourceSelectedFunc(SourceSelected s)
            {
                if (s.Mode != OpenFileMode.OpenExistingFile)
                {
                    return(NewFileSource(s.Source));
                }
                OpentFileSource(s.Source);
                return(Observable.Return <LoadedProjectFile?>(null));
            }

            void OpentFileSource(string?rawSource)
            {
                Observable.Return(rawSource)
                .NotEmpty()
                .SelectMany(CheckSourceOk)
                .Select(p => p.Item2)
                .Do(_ => mainWindowCoordinator.IsBusy = true)
                .SelectMany(source => operationManager
                            .StartOperation(string.Format(localizer.MainWindowModelLoadProjectOperation,
                                                          Path.GetFileName(source)))
                            .Do(op => loadingOperation !.Value = op)
                            .Select(operationController => (operationController, source)))
                .Do(_ =>
                {
                    if (!workspace.ProjectFile.IsEmpty)
                    {
                        workspace.ProjectFile.Operator.Tell(ForceSave.Force(workspace.ProjectFile));
                    }
                })
                .ObserveOnSelf()
                .Subscribe(pair
                           => ProjectFile.BeginLoad(Context, pair.operationController.Id, pair.source,
                                                    "Project_Operator"));
            }

            SupplyNewProjectFile?ProjectLoaded(LoadedProjectFile obj)
            {
                try
                {
                    if (loadingOperation !.Value != null)
                    {
                        if (obj.Ok)
                        {
                            loadingOperation.Value.Compled();
                        }
                        else
                        {
                            loadingOperation.Value.Failed(obj.ErrorReason?.Message ?? localizer.CommonError);
                            return(null);
                        }
                    }

                    loadingOperation.Value = null;
                    if (obj.Ok)
                    {
                        RenctFiles !.Value.AddNewFile(obj.ProjectFile.Source);
                    }

                    last !.Value = obj.ProjectFile;

                    return(new SupplyNewProjectFile(obj.ProjectFile));
                }
                finally
                {
                    mainWindowCoordinator.IsBusy = false;
                }
            }

            NewCommad.WithCanExecute(loadingOperation.Select(oc => oc == null))
            .WithFlow(obs => NewProjectFile(obs.Dialog(this, TypedParameter.From(OpenFileMode.OpenExistingFile))
                                            .Of <IOpenFileDialog, string?>()
                                            .Select(s => SourceSelected.From(s, OpenFileMode.OpenExistingFile))))
            .ThenRegister("OpenFile");

            NewProjectFile(Receive <SourceSelected>()).DisposeWith(this);
            Receive <LoadedProjectFile>(ob => ob.Select(ProjectLoaded).ToModel(CenterView !));

            #endregion

            #region New File

            IObservable <LoadedProjectFile?> NewFileSource(string?source)
            {
                source ??= string.Empty;
                var data = new LoadedProjectFile(string.Empty,
                                                 ProjectFile.NewProjectFile(Context, source, "Project_Operator"), null, true);

                if (File.Exists(source))
                {
                    //TODO NewFile Filog Message
                    var result = UICall(async()
                                        => await dialogCoordinator.ShowMessage(localizer.CommonError !, "", null));

                    return(result.Where(b => b == true).Do(_ => mainWindowCoordinator.IsBusy = true).Select(_ => data));
                }

                mainWindowCoordinator.IsBusy = true;
                return(Observable.Return(data));
            }

            NewCommad.WithCanExecute(loadingOperation.Select(oc => oc == null))
            .WithFlow(obs => obs.Dialog(this, TypedParameter.From(OpenFileMode.OpenNewFile))
                      .Of <IOpenFileDialog, string?>()
                      .Select(s => SourceSelected.From(s, OpenFileMode.OpenNewFile))
                      .ToSelf())
            .ThenRegister("NewFile");

            #endregion

            #region Analyzing

            AnalyzerEntries = this.RegisterUiCollection <AnalyzerEntry>(nameof(AnalyzerEntries))
                              .BindToList(out var analyterList);

            var builder = new AnalyzerEntryBuilder(localizer);

            void IssuesChanged(IssuesEvent obj)
            {
                analyterList.Edit(l =>
                {
                    var(ruleName, issues) = obj;
                    l.Remove(AnalyzerEntries.Where(e => e.RuleName == ruleName));
                    l.AddRange(issues.Select(builder.Get));
                });
            }

            this.RespondOnEventSource(workspace.Analyzer.Issues, IssuesChanged);

            #endregion

            #region Build

            var buildModel = lifetimeScope.Resolve <IViewModel <BuildViewModel> >();
            buildModel.InitModel(Context, "Build-View");

            BuildModel = RegisterProperty <IViewModel <BuildViewModel> >(nameof(BuildModel)).WithDefaultValue(buildModel);

            #endregion
        }