This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRepositoryPublishViewModel.cs
More file actions
195 lines (167 loc) · 8.34 KB
/
RepositoryPublishViewModel.cs
File metadata and controls
195 lines (167 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Extensions.Reactive;
using GitHub.Factories;
using GitHub.Logging;
using GitHub.Models;
using GitHub.Services;
using GitHub.UserErrors;
using GitHub.Validation;
using ReactiveUI;
using Serilog;
namespace GitHub.ViewModels.TeamExplorer
{
[Export(typeof(IRepositoryPublishViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class RepositoryPublishViewModel : RepositoryFormViewModel, IRepositoryPublishViewModel
{
static readonly ILogger log = LogManager.ForContext<RepositoryPublishViewModel>();
readonly IRepositoryPublishService repositoryPublishService;
readonly INotificationService notificationService;
readonly IModelServiceFactory modelServiceFactory;
readonly IDialogService dialogService;
readonly ObservableAsPropertyHelper<IReadOnlyList<IAccount>> accounts;
readonly ObservableAsPropertyHelper<bool> isHostComboBoxVisible;
readonly IUsageTracker usageTracker;
[ImportingConstructor]
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;
// });
}
public ReactiveCommand<Unit, ProgressState> PublishRepository { get; private set; }
public ReactiveCommand<Unit, Unit> LoginAsDifferentUser { get; private set; }
public IReadOnlyObservableCollection<IConnection> Connections { get; private set; }
bool isBusy;
public bool IsBusy
{
get { return isBusy; }
private set { this.RaiseAndSetIfChanged(ref isBusy, value); }
}
IConnection selectedConnection;
public IConnection SelectedConnection
{
get { return selectedConnection; }
set { this.RaiseAndSetIfChanged(ref selectedConnection, value); }
}
public IReadOnlyList<IAccount> Accounts
{
get { return accounts.Value; }
}
public bool IsHostComboBoxVisible
{
get { return isHostComboBoxVisible.Value; }
}
async Task LoginAsDifferentUserAsync()
{
if (await dialogService.ShowLoginDialog() is IConnection connection)
{
SelectedConnection = connection;
}
}
ReactiveCommand<Unit, ProgressState> InitializePublishRepositoryCommand()
{
var canCreate = this.WhenAny(x => x.RepositoryNameValidator.ValidationResult.IsValid, x => x.Value);
return ReactiveCommand.CreateFromObservable(OnPublishRepository, canCreate);
}
IObservable<ProgressState> OnPublishRepository()
{
var newRepository = GatherRepositoryInfo();
var account = SelectedAccount;
var modelService = modelServiceFactory.CreateBlocking(SelectedConnection);
return repositoryPublishService.PublishRepository(newRepository, account, modelService.ApiClient)
.Do(_ => usageTracker.IncrementCounter(x => x.NumberOfReposPublished).Forget())
.Select(_ => ProgressState.Success)
.Catch<ProgressState, Exception>(ex =>
{
if (!ex.IsCriticalException())
{
log.Error(ex, "Error Publishing Repository");
var error = new PublishRepositoryUserError(ex.Message);
notificationService.ShowError((error.ErrorMessage + Environment.NewLine + error.ErrorCauseOrResolution).TrimEnd());
}
return Observable.Return(ProgressState.Fail);
});
}
void InitializeValidation()
{
var nameValidationConditions = this.WhenAny(model => model.RepositoryName,
model => model.SelectedConnection,
model => model.SelectedAccount,
(repositoryName, connection, account) => (repositoryName: repositoryName.Value,
connection: connection.Value, account: account.Value))
.Where(tuple => tuple.repositoryName != null);
RepositoryNameValidator = ReactivePropertyValidator.ForObservable(nameValidationConditions)
.IfTrue(tuple => string.IsNullOrEmpty(tuple.repositoryName), Resources.RepositoryNameValidatorEmpty)
.IfTrue(tuple => tuple.repositoryName.Length > 100, Resources.RepositoryNameValidatorTooLong);
SafeRepositoryNameWarningValidator = ReactivePropertyValidator.ForObservable(nameValidationConditions)
.Add(tuple =>
{
var parsedReference = GetSafeRepositoryName(tuple.repositoryName);
return parsedReference != tuple.repositoryName ? String.Format(CultureInfo.CurrentCulture, Resources.SafeRepositoryNameWarning, parsedReference) : null;
});
}
}
}