public RepositoryPublishViewModel(
            IRepositoryHosts hosts,
            IRepositoryPublishService repositoryPublishService,
            INotificationService notificationService,
            IConnectionManager connectionManager,
            IUsageTracker usageTracker)
        {
            this.notificationService = notificationService;
            this.hosts        = hosts;
            this.usageTracker = usageTracker;

            title = this.WhenAny(
                x => x.SelectedHost,
                x => x.Value != null ?
                string.Format(CultureInfo.CurrentCulture, Resources.PublishToTitle, x.Value.Title) :
                Resources.PublishTitle
                )
                    .ToProperty(this, x => x.Title);

            Connections = connectionManager.Connections;
            this.repositoryPublishService = repositoryPublishService;

            if (Connections.Any())
            {
                SelectedConnection = Connections.FirstOrDefault(x => x.HostAddress.IsGitHubDotCom()) ?? Connections[0];
            }

            accounts = this.WhenAny(x => x.SelectedConnection, x => x.Value != null ? hosts.LookupHost(x.Value.HostAddress) : RepositoryHosts.DisconnectedRepositoryHost)
                       .Where(x => !(x is DisconnectedRepositoryHost))
                       .SelectMany(host => host.ModelService.GetAccounts())
                       .ObserveOn(RxApp.MainThreadScheduler)
                       .ToProperty(this, x => x.Accounts, initialValue: new ReadOnlyCollection <IAccount>(new IAccount[] {}));

            this.WhenAny(x => x.Accounts, x => x.Value)
            .WhereNotNull()
            .Where(accts => accts.Any())
            .Subscribe(accts => {
                var selectedAccount = accts.FirstOrDefault();
                if (selectedAccount != null)
                {
                    SelectedAccount = accts.FirstOrDefault();
                }
            });

            isHostComboBoxVisible = this.WhenAny(x => x.Connections, x => x.Value)
                                    .WhereNotNull()
                                    .Select(h => h.Count > 1)
                                    .ToProperty(this, x => x.IsHostComboBoxVisible);

            InitializeValidation();

            PublishRepository = InitializePublishRepositoryCommand();

            canKeepPrivate = CanKeepPrivateObservable.CombineLatest(PublishRepository.IsExecuting,
                                                                    (canKeep, publishing) => canKeep && !publishing)
                             .ToProperty(this, x => x.CanKeepPrivate);

            isPublishing = PublishRepository.IsExecuting
                           .ToProperty(this, x => x.IsPublishing);

            var defaultRepositoryName = repositoryPublishService.LocalRepositoryName;

            if (!string.IsNullOrEmpty(defaultRepositoryName))
            {
                RepositoryName = defaultRepositoryName;
            }

            this.WhenAny(x => x.SelectedConnection, x => x.SelectedAccount,
                         (a, b) => true)
            .Where(x => RepositoryNameValidator.ValidationResult != null && SafeRepositoryNameWarningValidator.ValidationResult != null)
            .Subscribe(async _ =>
            {
                var name       = RepositoryName;
                RepositoryName = null;
                await RepositoryNameValidator.ResetAsync();
                await SafeRepositoryNameWarningValidator.ResetAsync();
                RepositoryName = name;
            });
        }
Esempio n. 2
0
        public RepositoryPublishViewModel(
            IRepositoryPublishService repositoryPublishService,
            INotificationService notificationService,
            IConnectionManager connectionManager,
            IModelServiceFactory modelServiceFactory,
            IDialogService dialogService,
            IUsageTracker usageTracker)
        {
            Guard.ArgumentNotNull(repositoryPublishService, nameof(repositoryPublishService));
            Guard.ArgumentNotNull(notificationService, nameof(notificationService));
            Guard.ArgumentNotNull(connectionManager, nameof(connectionManager));
            Guard.ArgumentNotNull(usageTracker, nameof(usageTracker));
            Guard.ArgumentNotNull(modelServiceFactory, nameof(modelServiceFactory));
            Guard.ArgumentNotNull(dialogService, nameof(dialogService));

            this.notificationService = notificationService;
            this.usageTracker        = usageTracker;
            this.modelServiceFactory = modelServiceFactory;
            this.dialogService       = dialogService;

            Connections = connectionManager.Connections;
            this.repositoryPublishService = repositoryPublishService;

            if (Connections.Any())
            {
                SelectedConnection = Connections.FirstOrDefault(x => x.HostAddress.IsGitHubDotCom()) ?? Connections[0];
            }

            accounts = this.WhenAnyValue(x => x.SelectedConnection)
                       .Where(x => x != null)
                       .SelectMany(async c => (await modelServiceFactory.CreateAsync(c)).GetAccounts())
                       .Switch()
                       .ObserveOn(RxApp.MainThreadScheduler)
                       .ToProperty(this, x => x.Accounts, initialValue: new ReadOnlyCollection <IAccount>(Array.Empty <IAccount>()));

            this.WhenAny(x => x.Accounts, x => x.Value)
            .WhereNotNull()
            .Where(accts => accts.Any())
            .Subscribe(accts => {
                var selectedAccount = accts.FirstOrDefault();
                if (selectedAccount != null)
                {
                    SelectedAccount = accts.FirstOrDefault();
                }
            });

            isHostComboBoxVisible = this.WhenAny(x => x.Connections, x => x.Value)
                                    .WhereNotNull()
                                    .Select(h => h.Count > 1)
                                    .ToProperty(this, x => x.IsHostComboBoxVisible);

            InitializeValidation();

            PublishRepository = InitializePublishRepositoryCommand();
            PublishRepository.IsExecuting.Subscribe(x => IsBusy = x);

            LoginAsDifferentUser = ReactiveCommand.CreateFromTask(LoginAsDifferentUserAsync);

            var defaultRepositoryName = repositoryPublishService.LocalRepositoryName;

            if (!string.IsNullOrEmpty(defaultRepositoryName))
            {
                RepositoryName = defaultRepositoryName;
            }

            this.WhenAny(x => x.SelectedConnection, x => x.SelectedAccount,
                         (a, b) => true)
            .Where(x => RepositoryNameValidator.ValidationResult != null && SafeRepositoryNameWarningValidator.ValidationResult != null)
            .Subscribe(async _ =>
            {
                var name       = RepositoryName;
                RepositoryName = null;
                await RepositoryNameValidator.ResetAsync();
                await SafeRepositoryNameWarningValidator.ResetAsync();
                RepositoryName = name;
            });
        }