public ProjectViewModel(ILifetimeScope lifetimeScope, IUIDispatcher dispatcher, LocLocalizer localizer,
                                ProjectFileWorkspace workspace)
            : base(lifetimeScope, dispatcher)
        {
            #region Init

            Languages = this.RegisterUiCollection <ProjectViewLanguageModel>(nameof(Languages))
                        .BindToList(out var languages);
            ProjectEntrys = this.RegisterUiCollection <ProjectEntryModel>(nameof(ProjectEntrys))
                            .BindToList(out var projectEntrys);
            ImportetProjects = this.RegisterUiCollection <string>(nameof(ImportetProjects))
                               .BindToList(out var importprojects);

            var loadTrigger = new Subject <Unit>();

            this.Receive <IncommingEvent>(e => e.Action());

            IsEnabled     = RegisterProperty <bool>(nameof(IsEnabled)).WithDefaultValue(!workspace.ProjectFile.IsEmpty);
            SelectedIndex = RegisterProperty <int>(nameof(SelectedIndex));

            var self = Context.Self;

            void TryUpdateEntry((string ProjectName, string EntryName, ActiveLanguage Lang, string Content) data)
            {
                var(projectName, entryName, lang, content) = data;
                self.Tell(new UpdateRequest(entryName, lang, content, projectName));
            }

            void TryRemoveEntry((string ProjectName, string EntryName) data)
            {
                var(projectName, entryName) = data;
                self.Tell(new RemoveRequest(entryName, projectName));
            }

            Start.Subscribe(_ => Self.Tell(new InitProjectViewModel(workspace.Get(_project))));

            void InitProjectViewModel(InitProjectViewModel obj)
            {
                _project = obj.Project.ProjectName;

                languages.Edit(l =>
                {
                    l.Add(new ProjectViewLanguageModel(localizer.ProjectViewLanguageBoxFirstLabel, true));
                    l.AddRange(obj.Project.ActiveLanguages.Select(al => new ProjectViewLanguageModel(al.Name, false)));
                });
                SelectedIndex += 0;

                projectEntrys.AddRange(obj.Project.Entries.OrderBy(le => le.Key).Select(le
                                                                                        => new ProjectEntryModel(obj.Project, le, TryUpdateEntry, TryRemoveEntry)));

                importprojects.AddRange(obj.Project.Imports);
                loadTrigger.OnNext(Unit.Default);
            }

            this.Receive <InitProjectViewModel>(InitProjectViewModel);

            #endregion

            #region New Entry

            IEnumerable <NewEntryInfoBase> GetEntrys()
            {
                var list = ImportetProjects !.ToList();

                list.Add(_project);

                var allEntrys = list.SelectMany(pro => workspace.Get(pro).Entries.Select(e => e.Key)).ToArray();

                return(allEntrys.Select(e => new NewEntryInfo(e)).OfType <NewEntryInfoBase>()
                       .Concat(allEntrys
                               .Select(s => s.Split('_', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault())
                               .Where(s => !string.IsNullOrWhiteSpace(s))
                               .Distinct(StringComparer.Ordinal)
                               .Select(s => new NewEntrySuggestInfo(s !))));
            }

            void AddEntry(EntryAdd entry)
            {
                if (_project != entry.Entry.Project)
                {
                    return;
                }

                projectEntrys.Add(new ProjectEntryModel(workspace.Get(_project), entry.Entry, TryUpdateEntry,
                                                        TryRemoveEntry));
            }

            NewCommad.WithFlow(ob => ob.Select(_ => GetEntrys())
                               .Dialog(this).Of <INewEntryDialog, NewEntryDialogResult?>()
                               .NotNull()
                               .Mutate(workspace.Entrys).With(em => em.EntryAdd, em => res => em.NewEntry(_project, res.Name))
                               .ObserveOnSelf()
                               .Subscribe(AddEntry))
            .ThenRegister("NewEntry");

            #endregion

            #region Remove Request

            void RemoveEntry(EntryRemove entry)
            {
                if (_project != entry.Entry.Project)
                {
                    return;
                }

                var index = ProjectEntrys !.FindIndex(em => em.EntryName == entry.Entry.Key);

                if (index == -1)
                {
                    return;
                }

                projectEntrys !.RemoveAt(index);
            }

            Receive <RemoveRequest>(obs => obs.Mutate(workspace.Entrys)
                                    .With(em => em.EntryRemove, em => rr => em.RemoveEntry(rr.ProjectName, rr.EntryName))
                                    .ObserveOnSelf()
                                    .Subscribe(RemoveEntry));

            #endregion

            #region Update Request

            void UpdateEntry(EntryUpdate obj)
            {
                if (_project != obj.Entry.Project)
                {
                    return;
                }

                var model = ProjectEntrys !.FirstOrDefault(m => m.EntryName == obj.Entry.Key);

                model?.Update(obj.Entry);
            }

            Receive <UpdateRequest>(obs => obs.Mutate(workspace.Entrys).With(em => em.EntryUpdate,
                                                                             em => ur => em.UpdateEntry(ur.ProjectName, ur.Language, ur.EntryName, ur.Content))
                                    .ObserveOnSelf()
                                    .Subscribe(UpdateEntry));

            #endregion

            #region Imports

            void AddImport(AddImport obj)
            {
                var(projectName, import) = obj;
                if (projectName != _project)
                {
                    return;
                }
                importprojects !.Add(import);
            }

            IEnumerable <string> GetImportableProjects()
            {
                var pro = workspace.Get(_project);

                return(workspace.ProjectFile.Projects.Select(p => p.ProjectName)
                       .Where(p => p != _project && !pro.Imports.Contains(p)));
            }

            ImportSelectIndex = RegisterProperty <int>(nameof(ImportSelectIndex)).WithDefaultValue(-1);

            NewCommad.WithCanExecute(from trigger in loadTrigger
                                     select GetImportableProjects().Any())
            .WithFlow(ob => ob.Select(_ => GetImportableProjects())
                      .Dialog(this).Of <IImportProjectDialog, ImportProjectDialogResult?>()
                      .NotNull()
                      .Mutate(workspace.Projects).With(pm => pm.NewImport, pm => r => pm.AddImport(_project, r.Project))
                      .ObserveOnSelf()
                      .Subscribe(AddImport))
            .ThenRegister("AddImport");

            void RemoveImport(RemoveImport import)
            {
                var(targetProject, toRemove) = import;
                if (_project != targetProject)
                {
                    return;
                }

                importprojects !.Remove(toRemove);
            }

            NewCommad.WithCanExecute(ImportSelectIndex.Select(i => i != -1))
            .WithFlow(ob => ob.Select(_ => new InitImportRemove(ImportetProjects[ImportSelectIndex]))
                      .Mutate(workspace.Projects).With(pm => pm.RemoveImport,
                                                       pm => ir => pm.TryRemoveImport(_project, ir.ToRemove))
                      .Subscribe(RemoveImport))
            .ThenRegister("RemoveImport");

            workspace.Projects.NewImport.ToUnit().Subscribe(loadTrigger).DisposeWith(this);
            workspace.Projects.RemoveImport.ToUnit().Subscribe(loadTrigger).DisposeWith(this);

            #endregion

            #region AddLanguage

            void AddActiveLanguage(AddActiveLanguage language)
            {
                if (language.ProjectName != _project)
                {
                    return;
                }

                languages.Add(new ProjectViewLanguageModel(language.ActiveLanguage.Name, false));

                foreach (var model in ProjectEntrys)
                {
                    model.AddLanguage(language.ActiveLanguage);
                }
            }

            NewCommad.WithFlow(ob => ob
                               .Select(_ => workspace.Get(_project).ActiveLanguages.Select(al => al.ToCulture()).ToArray())
                               .Dialog(this).Of <ILanguageSelectorDialog, AddLanguageDialogResult?>()
                               .NotNull()
                               .Mutate(workspace.Projects)
                               .With(pm => pm.NewLanguage, pm => d => pm.AddLanguage(_project, d.CultureInfo))
                               .Subscribe(AddActiveLanguage))
            .ThenRegister("AddLanguage");

            #endregion
        }
Exemplo n.º 2
0
        public MainWindowViewModel(ILifetimeScope lifetimeScope, IUIDispatcher dispatcher, AppSettings settings, ITauronEnviroment enviroment,
                                   ProfileManager profileManager, CalculationManager calculation, SystemClock clock, IEventAggregator aggregator)
            : base(lifetimeScope, dispatcher)
        {
            SnackBarQueue = RegisterProperty <SnackbarMessageQueue?>(nameof(SnackBarQueue));
            dispatcher.InvokeAsync(() => new SnackbarMessageQueue(TimeSpan.FromSeconds(10)))
            .Subscribe(SnackBarQueue);

            CurrentProfile = RegisterProperty <string>(nameof(CurrentProfile));
            AllProfiles    = this.RegisterUiCollection <string>(nameof(AllProfiles))
                             .BindToList(settings.AllProfiles, out var list);

            aggregator.ConsumeErrors().Subscribe(ReportError).DisposeWith(this);

            #region Profile Selection

            IsProcessable = RegisterProperty <bool>(nameof(IsProcessable));
            profileManager.IsProcessable
            .Subscribe(IsProcessable)
            .DisposeWith(this);

            ProfileState = RegisterProperty <string>(nameof(ProfileState));
            profileManager.IsProcessable
            .Select(b => b
                                      ? $"{CurrentProfile.Value} Geladen"
                                      : "nicht Geladen")
            .AutoSubscribe(ProfileState, ReportError);

            var loadTrigger = new Subject <string>();

            (from newProfile in CurrentProfile
             where !list.Items.Contains(newProfile)
             select newProfile)
            .Throttle(TimeSpan.FromSeconds(5))
            .AutoSubscribe(s =>
            {
                if (string.IsNullOrWhiteSpace(s))
                {
                    return;
                }

                settings.AllProfiles = settings.AllProfiles.Add(s);
                list.Add(s);
                loadTrigger.OnNext(s);
            }, ReportError)
            .DisposeWith(this);

            (from profile in CurrentProfile
             where list.Items.Contains(profile)
             select profile)
            .Subscribe(loadTrigger)
            .DisposeWith(this);

            #endregion

            Configurate = NewCommad.WithCanExecute(profileManager.IsProcessable)
                          .WithFlow(obs => (from _ in obs
                                            from res in this.ShowDialogAsync <ConfigurationDialog, Unit, ConfigurationManager>(() => profileManager.ConfigurationManager)
                                            select res)
                                    .AutoSubscribe(ReportError))
                          .ThenRegister(nameof(Configurate));

            profileManager.CreateFileLoadPipeline(loadTrigger).DisposeWith(this);

            HoursAll = RegisterProperty <string>(nameof(HoursAll));
            //profileManager.ProcessableData
            //              .Select(pd => pd.AllHours)
            //              .DistinctUntilChanged()
            //              .Select(i => i.ToString())
            //              .ObserveOn(Scheduler.Default)
            //              .Get(HoursAll).DisposeWith(this);

            #region Entrys

            var isHere = false.ToRx().DisposeWith(this);

            ProfileEntries = this.RegisterUiCollection <UiProfileEntry>(nameof(ProfileEntries))
                             .BindTo(profileManager.ConnectCache()
                                     .Select(entry => new UiProfileEntry(entry, profileManager.ProcessableData, ReportError)));

            profileManager.ConnectCache()
            .AutoSubscribe(_ => CheckHere(), aggregator.ReportError)
            .DisposeWith(this);

            dispatcher.InvokeAsync(() =>
            {
                var view        = (ListCollectionView)CollectionViewSource.GetDefaultView(ProfileEntries.Property.Value);
                view.CustomSort = Comparer <UiProfileEntry> .Default;
            });

            (from data in profileManager.ProcessableData.DistinctUntilChanged(pd => pd.Entries)
             where data.Entries.Count != 0
             select Unit.Default)
            .AutoSubscribe(_ => CheckHere(), ReportError)
            .DisposeWith(this);


            Come = NewCommad
                   .WithCanExecute(profileManager.IsProcessable)
                   .WithCanExecute(from here in isHere
                                   select !here)
                   .WithParameterFlow <ModiferBox>(
                obs => profileManager.Come(from box in obs
                                           select new ComeParameter(clock.NowDate, box != null && box.Keys.HasFlag(ModifierKeys.Control)))
                .AutoSubscribe(b =>
            {
                if (b)
                {
                    CheckHere();
                }
                else
                {
                    SnackBarQueue.Value?.Enqueue("Tag Schon eingetragen");
                }
            }, ReportError))
                   .ThenRegister(nameof(Come));

            Go = NewCommad
                 .WithCanExecute(profileManager.IsProcessable)
                 .WithCanExecute(isHere)
                 .WithFlow(() => clock.NowDate,
                           obs => profileManager.Go(obs)
                           .AutoSubscribe(b =>
            {
                if (b)
                {
                    CheckHere();
                }
                else
                {
                    SnackBarQueue.Value?.Enqueue("Tag nicht gefunden");
                }
            }, ReportError))
                 .ThenRegister(nameof(Go));

            void CheckHere()
            {
                (from item in profileManager.Entries
                 where item.Start != null && item.Finish == null
                 orderby item.Date
                 select item).FirstOrDefault()
                .OptionNotNull()
                .Run(_ => isHere !.Value = true, () => isHere !.Value = false);
            }

            Vacation = NewCommad
                       .WithFlow(obs => (from _ in obs
                                         from data in profileManager.ProcessableData.Take(1)
                                         from result in this.ShowDialogAsync <VacationDialog, DateTime[]?, DateTime>(() => data.CurrentMonth)
                                         where result != null
                                         from res in profileManager.AddVacation(result)
                                         select res)
                                 .AutoSubscribe(ReportError))
                       .WithCanExecute(IsProcessable)
                       .ThenRegister(nameof(Vacation));

            #endregion

            #region Correction

            CurrentEntry = RegisterProperty <UiProfileEntry?>(nameof(CurrentEntry));

            Correct = NewCommad
                      .WithCanExecute(profileManager.IsProcessable)
                      .WithCanExecute(from entry in CurrentEntry
                                      select entry != null)
                      .WithFlow(obs => (from _ in obs
                                        let oldEntry = CurrentEntry.Value
                                                       where oldEntry != null
                                                       from dialogResult in this.ShowDialogAsync <CorrectionDialog, CorrectionResult, ProfileEntry>(() => oldEntry.Entry)
                                                       from result in dialogResult switch
            {
                UpdateCorrectionResult update => profileManager.UpdateEntry(update.Entry, oldEntry.Entry.Date),
                DeleteCorrectionResult delete => profileManager.DeleteEntry(delete.Key).Select(_ => string.Empty),
                _ => Observable.Return(string.Empty)
            }
                                        select result)
                                .AutoSubscribe(s =>
            {
                if (!string.IsNullOrWhiteSpace(s))
                {
                    SnackBarQueue.Value?.Enqueue(s);
                }
                else
                {
                    CheckHere();
                }
            }, aggregator.ReportError))
                      .ThenRegister(nameof(Correct));

            AddEntry = NewCommad
                       .WithCanExecute(IsProcessable)
                       .WithFlow(obs => (from _ in obs
                                         from data in profileManager.ProcessableData.Take(1)
                                         let parameter = new AddEntryParameter(data.Entries.Select(pe => pe.Value.Date.Day).ToHashSet(), data.CurrentMonth)
                                                         from result in this.ShowDialogAsync <AddEntryDialog, AddEntryResult, AddEntryParameter>(() => parameter)
                                                         from u in result switch
            {
                NewAddEntryResult entry => profileManager.AddEntry(entry.Entry),
                _ => Observable.Return(Unit.Default)
            }
                                         select u)
                                 .AutoSubscribe(_ => CheckHere(), ReportError))
                       .ThenRegister(nameof(AddEntry));

            #endregion

            #region Calculation

            Remaining = RegisterProperty <int>(nameof(Remaining));

            CurrentState = RegisterProperty <MonthState>(nameof(CurrentState))
                           .WithDefaultValue(MonthState.Minus);

            calculation.AllHours
            .Select(ts => ts.TotalHours)
            .Select(h =>
            {
                return(h switch
                {
                    0 when InValidConfig() => "Konfiguration!",
                    0 => string.Empty,
                    _ => h.ToString("F0")
                });

                bool InValidConfig()
                => profileManager.ConfigurationManager.DailyHours == 0 && profileManager.ConfigurationManager.MonthHours == 0;
            })