コード例 #1
0
    static GitHubPaneViewModel CreateTarget(
        IViewViewModelFactory viewModelFactory   = null,
        ISimpleApiClientFactory apiClientFactory = null,
        IConnectionManager connectionManager     = null,
        ITeamExplorerContext teamExplorerContext = null,
        IVisualStudioBrowser browser             = null,
        IUsageTracker usageTracker     = null,
        INavigationViewModel navigator = null,
        ILoggedOutViewModel loggedOut  = null,
        INotAGitHubRepositoryViewModel notAGitHubRepository = null,
        INotAGitRepositoryViewModel notAGitRepository       = null,
        ILoginFailedViewModel loginFailed = null)
    {
        viewModelFactory     = viewModelFactory ?? Substitute.For <IViewViewModelFactory>();
        connectionManager    = connectionManager ?? Substitute.For <IConnectionManager>();
        teamExplorerContext  = teamExplorerContext ?? CreateTeamExplorerContext(ValidGitHubRepo);
        browser              = browser ?? Substitute.For <IVisualStudioBrowser>();
        usageTracker         = usageTracker ?? Substitute.For <IUsageTracker>();
        loggedOut            = loggedOut ?? Substitute.For <ILoggedOutViewModel>();
        notAGitHubRepository = notAGitHubRepository ?? Substitute.For <INotAGitHubRepositoryViewModel>();
        notAGitRepository    = notAGitRepository ?? Substitute.For <INotAGitRepositoryViewModel>();
        loginFailed          = loginFailed ?? Substitute.For <ILoginFailedViewModel>();

        if (navigator == null)
        {
            navigator = CreateNavigator();
            navigator.Content.Returns((IPanePageViewModel)null);
        }

        if (apiClientFactory == null)
        {
            var validGitHubRepoClient     = Substitute.For <ISimpleApiClient>();
            var validEnterpriseRepoClient = Substitute.For <ISimpleApiClient>();
            var invalidRepoClient         = Substitute.For <ISimpleApiClient>();

            validGitHubRepoClient.GetRepository().Returns(new Octokit.Repository(1));
            validEnterpriseRepoClient.GetRepository().Returns(new Octokit.Repository(1));
            validEnterpriseRepoClient.IsEnterprise().Returns(true);

            apiClientFactory = Substitute.For <ISimpleApiClientFactory>();
            apiClientFactory.Create(null).ReturnsForAnyArgs(invalidRepoClient);
            apiClientFactory.Create(ValidGitHubRepo).Returns(validGitHubRepoClient);
            apiClientFactory.Create(ValidEnterpriseRepo).Returns(validEnterpriseRepoClient);
        }

        return(new GitHubPaneViewModel(
                   viewModelFactory,
                   apiClientFactory,
                   connectionManager,
                   teamExplorerContext,
                   browser,
                   usageTracker,
                   navigator,
                   loggedOut,
                   notAGitHubRepository,
                   notAGitRepository,
                   loginFailed));
    }
コード例 #2
0
        public GitHubPaneViewModel(
            IViewViewModelFactory viewModelFactory,
            ISimpleApiClientFactory apiClientFactory,
            IConnectionManager connectionManager,
            ITeamExplorerContext teamExplorerContext,
            IVisualStudioBrowser browser,
            IUsageTracker usageTracker,
            INavigationViewModel navigator,
            ILoggedOutViewModel loggedOut,
            INotAGitHubRepositoryViewModel notAGitHubRepository,
            INotAGitRepositoryViewModel notAGitRepository,
            INoRemoteOriginViewModel noRemoteOrigin,
            ILoginFailedViewModel loginFailed)
        {
            Guard.ArgumentNotNull(viewModelFactory, nameof(viewModelFactory));
            Guard.ArgumentNotNull(apiClientFactory, nameof(apiClientFactory));
            Guard.ArgumentNotNull(connectionManager, nameof(connectionManager));
            Guard.ArgumentNotNull(teamExplorerContext, nameof(teamExplorerContext));
            Guard.ArgumentNotNull(browser, nameof(browser));
            Guard.ArgumentNotNull(usageTracker, nameof(usageTracker));
            Guard.ArgumentNotNull(navigator, nameof(navigator));
            Guard.ArgumentNotNull(loggedOut, nameof(loggedOut));
            Guard.ArgumentNotNull(notAGitHubRepository, nameof(notAGitHubRepository));
            Guard.ArgumentNotNull(notAGitRepository, nameof(notAGitRepository));
            Guard.ArgumentNotNull(noRemoteOrigin, nameof(noRemoteOrigin));
            Guard.ArgumentNotNull(loginFailed, nameof(loginFailed));

            this.viewModelFactory     = viewModelFactory;
            this.apiClientFactory     = apiClientFactory;
            this.connectionManager    = connectionManager;
            this.teamExplorerContext  = teamExplorerContext;
            this.navigator            = navigator;
            this.loggedOut            = loggedOut;
            this.notAGitHubRepository = notAGitHubRepository;
            this.notAGitRepository    = notAGitRepository;
            this.noRemoteOrigin       = noRemoteOrigin;
            this.loginFailed          = loginFailed;

            var contentAndNavigatorContent = Observable.CombineLatest(
                this.WhenAnyValue(x => x.Content),
                navigator.WhenAnyValue(x => x.Content),
                (c, nc) => new { Content = c, NavigatorContent = nc });

            contentOverride = contentAndNavigatorContent
                              .SelectMany(x =>
            {
                if (x.Content == null)
                {
                    return(Observable.Return(ContentOverride.Spinner));
                }
                else if (x.Content == navigator && x.NavigatorContent != null)
                {
                    return(x.NavigatorContent.WhenAnyValue(
                               y => y.IsLoading,
                               y => y.Error,
                               (l, e) =>
                    {
                        if (l)
                        {
                            return ContentOverride.Spinner;
                        }
                        if (e != null)
                        {
                            return ContentOverride.Error;
                        }
                        else
                        {
                            return ContentOverride.None;
                        }
                    }));
                }
                else
                {
                    return(Observable.Return(ContentOverride.None));
                }
            })
                              .ToProperty(this, x => x.ContentOverride);

            // Returns navigator.Content if Content == navigator, otherwise null.
            var currentPage = contentAndNavigatorContent
                              .Select(x => x.Content == navigator ? x.NavigatorContent : null);

            title = currentPage
                    .SelectMany(x => x?.WhenAnyValue(y => y.Title) ?? Observable.Return <string>(null))
                    .Select(x => x ?? "GitHub")
                    .ToProperty(this, x => x.Title);

            isSearchEnabled = currentPage
                              .Select(x => x is ISearchablePageViewModel)
                              .ToProperty(this, x => x.IsSearchEnabled);

            refresh = ReactiveCommand.CreateFromTask(
                () => navigator.Content.Refresh(),
                currentPage.SelectMany(x => x?.WhenAnyValue(
                                           y => y.IsLoading,
                                           y => y.IsBusy,
                                           (loading, busy) => !loading && !busy)
                                       ?? Observable.Return(false)));
            refresh.ThrownExceptions.Subscribe();

            showPullRequests = ReactiveCommand.CreateFromTask(
                ShowPullRequests,
                this.WhenAny(x => x.Content, x => x.Value == navigator));

            openInBrowser = ReactiveCommand.Create(
                () =>
            {
                var url = ((IOpenInBrowser)navigator.Content).WebUrl;
                if (url != null)
                {
                    browser.OpenUrl(url);
                }
            },
                currentPage.Select(x => x is IOpenInBrowser));

            BrowseRepository = ReactiveCommand.Create(
                () =>
            {
                var url = LocalRepository.CloneUrl.ToRepositoryUrl();
                if (url != null)
                {
                    browser.OpenUrl(url);
                }
            },
                currentPage.Select(x => x is IOpenInBrowser));

            help = ReactiveCommand.Create(() => { });
            help.Subscribe(_ =>
            {
                browser.OpenUrl(new Uri(GitHubUrls.Documentation));
                usageTracker.IncrementCounter(x => x.NumberOfGitHubPaneHelpClicks).Forget();
            });

            navigator.WhenAnyObservable(x => x.Content.NavigationRequested)
            .Subscribe(x => NavigateTo(x).Forget());

            this.WhenAnyValue(x => x.SearchQuery)
            .Where(x => navigator.Content is ISearchablePageViewModel)
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(x => ((ISearchablePageViewModel)navigator.Content).SearchQuery = x);
        }