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 pathCommentViewModel.cs
More file actions
311 lines (265 loc) · 9.72 KB
/
Copy pathCommentViewModel.cs
File metadata and controls
311 lines (265 loc) · 9.72 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Logging;
using GitHub.Models;
using GitHub.Services;
using ReactiveUI;
using Serilog;
namespace GitHub.ViewModels
{
/// <summary>
/// An issue or pull request comment.
/// </summary>
[Export(typeof(ICommentViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class CommentViewModel : ViewModelBase, ICommentViewModel
{
static readonly ILogger log = LogManager.ForContext<CommentViewModel>();
readonly ICommentService commentService;
readonly ObservableAsPropertyHelper<bool> canCancel;
readonly ObservableAsPropertyHelper<bool> canDelete;
ObservableAsPropertyHelper<string> commitCaption;
string id;
IActorViewModel author;
IActorViewModel currentUser;
string body;
string errorMessage;
bool isReadOnly;
bool isSubmitting;
CommentEditState state;
DateTimeOffset createdAt;
ICommentThreadViewModel thread;
string undoBody;
/// <summary>
/// Initializes a new instance of the <see cref="CommentViewModel"/> class.
/// </summary>
/// <param name="commentService">The comment service.</param>
/// <param name="autoCompleteAdvisor">The auto complete advisor.</param>
[ImportingConstructor]
public CommentViewModel(ICommentService commentService, IAutoCompleteAdvisor autoCompleteAdvisor)
{
Guard.ArgumentNotNull(commentService, nameof(commentService));
Guard.ArgumentNotNull(autoCompleteAdvisor, nameof(autoCompleteAdvisor));
AutoCompleteAdvisor = autoCompleteAdvisor;
this.commentService = commentService;
var canDeleteObservable = this.WhenAnyValue(
x => x.EditState,
x => x.Author,
x => x.CurrentUser,
(editState, author, currentUser) => editState == CommentEditState.None && author?.Login == currentUser?.Login);
canDelete = canDeleteObservable.ToProperty(this, x => x.CanDelete);
Delete = ReactiveCommand.CreateFromTask(DoDelete, canDeleteObservable);
var canEdit = this.WhenAnyValue(
x => x.EditState,
x => x.Author,
x => x.CurrentUser,
(editState, author, currentUser) => editState == CommentEditState.Placeholder ||
(editState == CommentEditState.None && author?.Login == currentUser?.Login));
BeginEdit = ReactiveCommand.Create(DoBeginEdit, canEdit);
AddErrorHandler(BeginEdit);
CommitEdit = ReactiveCommand.CreateFromTask(
DoCommitEdit,
this.WhenAnyValue(
x => x.IsReadOnly,
x => x.Body,
(ro, body) => !ro && !string.IsNullOrWhiteSpace(body)));
AddErrorHandler(CommitEdit);
canCancel = this.WhenAnyValue(x => x.Id)
.Select(id => id != null)
.ToProperty(this, x => x.CanCancel);
CancelEdit = ReactiveCommand.Create(DoCancelEdit, CommitEdit.IsExecuting.Select(x => !x));
AddErrorHandler(CancelEdit);
OpenOnGitHub = ReactiveCommand.Create(
() => { },
this.WhenAnyValue(x => x.Id).Select(x => x != null));
}
/// <inheritdoc/>
public string Id
{
get => id;
private set => this.RaiseAndSetIfChanged(ref id, value);
}
/// <inheritdoc/>
public int DatabaseId { get; private set; }
/// <inheritdoc/>
public int PullRequestId { get; private set; }
/// <inheritdoc/>
public IActorViewModel Author
{
get => author;
private set => this.RaiseAndSetIfChanged(ref author, value);
}
/// <inheritdoc/>
public IActorViewModel CurrentUser
{
get => currentUser;
private set => this.RaiseAndSetIfChanged(ref currentUser, value);
}
/// <inheritdoc/>
public string Body
{
get => body;
set => this.RaiseAndSetIfChanged(ref body, value);
}
/// <inheritdoc/>
public string ErrorMessage
{
get => errorMessage;
private set => this.RaiseAndSetIfChanged(ref errorMessage, value);
}
/// <inheritdoc/>
public CommentEditState EditState
{
get => state;
private set => this.RaiseAndSetIfChanged(ref state, value);
}
/// <inheritdoc/>
public bool IsReadOnly
{
get => isReadOnly;
set => this.RaiseAndSetIfChanged(ref isReadOnly, value);
}
/// <inheritdoc/>
public bool IsSubmitting
{
get => isSubmitting;
protected set => this.RaiseAndSetIfChanged(ref isSubmitting, value);
}
/// <inheritdoc/>
public bool CanCancel => canCancel.Value;
/// <inheritdoc/>
public bool CanDelete => canDelete.Value;
/// <inheritdoc/>
public DateTimeOffset CreatedAt
{
get => createdAt;
private set => this.RaiseAndSetIfChanged(ref createdAt, value);
}
/// <inheritdoc/>
public string CommitCaption => commitCaption.Value;
/// <inheritdoc/>
public ICommentThreadViewModel Thread
{
get => thread;
private set => this.RaiseAndSetIfChanged(ref thread, value);
}
/// <inheritdoc/>
public Uri WebUrl { get; private set; }
/// <inheritdoc/>
public ReactiveCommand<Unit, Unit> BeginEdit { get; }
/// <inheritdoc/>
public ReactiveCommand<Unit, Unit> CancelEdit { get; }
/// <inheritdoc/>
public ReactiveCommand<Unit, Unit> CommitEdit { get; }
/// <inheritdoc/>
public ReactiveCommand<Unit, Unit> OpenOnGitHub { get; }
/// <inheritdoc/>
public ReactiveCommand<Unit, Unit> Delete { get; }
/// <inheritdoc/>
public IAutoCompleteAdvisor AutoCompleteAdvisor { get; }
/// <inheritdoc/>
public Task InitializeAsync(
ICommentThreadViewModel thread,
ActorModel currentUser,
CommentModel comment,
CommentEditState state)
{
Guard.ArgumentNotNull(thread, nameof(thread));
Guard.ArgumentNotNull(currentUser, nameof(currentUser));
Thread = thread;
CurrentUser = new ActorViewModel(currentUser);
Id = comment?.Id;
DatabaseId = comment?.DatabaseId ?? 0;
PullRequestId = (comment as PullRequestReviewCommentModel)?.PullRequestId ?? 0;
Body = comment?.Body;
EditState = state;
Author = comment != null ? new ActorViewModel(comment.Author) : CurrentUser;
CreatedAt = comment?.CreatedAt ?? DateTimeOffset.MinValue;
WebUrl = comment?.Url != null ? new Uri(comment.Url) : null;
commitCaption = GetCommitCaptionObservable().ToProperty(this, x => x.CommitCaption);
return Task.CompletedTask;
}
protected void AddErrorHandler(ReactiveCommand command)
{
command.ThrownExceptions.Subscribe(x => ErrorMessage = x.Message);
}
protected virtual IObservable<string> GetCommitCaptionObservable()
{
return this.WhenAnyValue(x => x.Id)
.Select(x => x == null ? Resources.Comment : Resources.UpdateComment);
}
async Task DoDelete()
{
if (commentService.ConfirmCommentDelete())
{
try
{
ErrorMessage = null;
IsSubmitting = true;
await Thread.DeleteComment(this).ConfigureAwait(true);
}
catch (Exception e)
{
var message = e.Message;
ErrorMessage = message;
log.Error(e, "Error Deleting comment");
}
finally
{
IsSubmitting = false;
}
}
}
void DoBeginEdit()
{
if (state != CommentEditState.Editing)
{
ErrorMessage = null;
undoBody = Body;
EditState = CommentEditState.Editing;
}
}
void DoCancelEdit()
{
if (EditState == CommentEditState.Editing)
{
EditState = string.IsNullOrWhiteSpace(undoBody) ? CommentEditState.Placeholder : CommentEditState.None;
Body = undoBody;
ErrorMessage = null;
undoBody = null;
}
}
async Task DoCommitEdit()
{
try
{
ErrorMessage = null;
IsSubmitting = true;
if (Id == null)
{
await Thread.PostComment(this).ConfigureAwait(true);
}
else
{
await Thread.EditComment(this).ConfigureAwait(true);
}
EditState = CommentEditState.None;
}
catch (Exception e)
{
var message = e.Message;
ErrorMessage = message;
log.Error(e, "Error posting comment");
}
finally
{
IsSubmitting = false;
}
}
}
}