Пример #1
0
        public ReportsViewModel(ITogglDataSource dataSource,
                                ITimeService timeService,
                                INavigationService navigationService,
                                IInteractorFactory interactorFactory,
                                IAnalyticsService analyticsService,
                                ISchedulerProvider schedulerProvider,
                                IRxActionFactory rxActionFactory)
            : base(navigationService)
        {
            Ensure.Argument.IsNotNull(dataSource, nameof(dataSource));
            Ensure.Argument.IsNotNull(timeService, nameof(timeService));
            Ensure.Argument.IsNotNull(rxActionFactory, nameof(rxActionFactory));
            Ensure.Argument.IsNotNull(analyticsService, nameof(analyticsService));
            Ensure.Argument.IsNotNull(interactorFactory, nameof(interactorFactory));
            Ensure.Argument.IsNotNull(schedulerProvider, nameof(schedulerProvider));
            Ensure.Argument.IsNotNull(navigationService, nameof(navigationService));

            this.dataSource        = dataSource;
            this.timeService       = timeService;
            this.analyticsService  = analyticsService;
            this.interactorFactory = interactorFactory;

            CalendarViewModel = new ReportsCalendarViewModel(timeService, dataSource, rxActionFactory, navigationService);

            var totalsObservable = reportSubject
                                   .SelectMany(_ => interactorFactory.GetReportsTotals(userId, workspaceId, startDate, endDate).Execute())
                                   .Catch <ITimeEntriesTotals, OfflineException>(_ => Observable.Return <ITimeEntriesTotals>(null))
                                   .Where(report => report != null);

            BarChartViewModel = new ReportsBarChartViewModel(schedulerProvider, dataSource.Preferences, totalsObservable, navigationService);

            IsLoadingObservable = isLoading.AsObservable().StartWith(true).AsDriver(schedulerProvider);
            StartDate           = startDateSubject.AsObservable().AsDriver(schedulerProvider);
            EndDate             = endDateSubject.AsObservable().AsDriver(schedulerProvider);

            SelectWorkspace = rxActionFactory.FromAsync(selectWorkspace);

            WorkspaceNameObservable = workspaceSubject
                                      .Select(workspace => workspace?.Name ?? string.Empty)
                                      .DistinctUntilChanged()
                                      .AsDriver(schedulerProvider);

            WorkspaceHasBillableFeatureEnabled = workspaceSubject
                                                 .Where(workspace => workspace != null)
                                                 .SelectMany(workspace => interactorFactory.GetWorkspaceFeaturesById(workspace.Id).Execute())
                                                 .Select(workspaceFeatures => workspaceFeatures.IsEnabled(WorkspaceFeatureId.Pro))
                                                 .StartWith(false)
                                                 .DistinctUntilChanged()
                                                 .AsDriver(schedulerProvider);

            CurrentDateRange = currentDateRangeStringSubject
                               .Select(text => !string.IsNullOrEmpty(text) ? $"{text} ▾" : "")
                               .DistinctUntilChanged()
                               .AsDriver(schedulerProvider);

            WorkspacesObservable = interactorFactory.ObserveAllWorkspaces().Execute()
                                   .Select(list => list.Where(w => !w.IsInaccessible))
                                   .Select(readOnlyWorkspaceSelectOptions)
                                   .AsDriver(schedulerProvider);

            DurationFormatObservable = dataSource.Preferences.Current
                                       .Select(prefs => prefs.DurationFormat)
                                       .AsDriver(schedulerProvider);

            SegmentsObservable        = segmentsSubject.CombineLatest(DurationFormatObservable, applyDurationFormat);
            GroupedSegmentsObservable = SegmentsObservable.CombineLatest(DurationFormatObservable, groupSegments);
            ShowEmptyStateObservable  = SegmentsObservable.CombineLatest(IsLoadingObservable, shouldShowEmptyState);
        }