Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 5199889

Browse files
Combine functionality to use BeginEdit
1 parent cc12bea commit 5199889

13 files changed

+69
-180
lines changed

src/GitHub.InlineReviews/SampleData/CommentViewModelDesigner.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ public CommentViewModelDesigner()
2424
public CommentEditState EditState { get; set; }
2525
public bool IsReadOnly { get; set; }
2626
public bool IsSubmitting { get; set; }
27-
public bool CanEditOrDelete { get; } = true;
27+
public bool CanDelete { get; } = true;
2828
public ICommentThreadViewModel Thread { get; }
2929
public DateTimeOffset UpdatedAt => DateTime.Now.Subtract(TimeSpan.FromDays(3));
3030
public IAccount User { get; set; }
3131

32-
public ReactiveCommand<object> BeginCreate { get; }
33-
public ReactiveCommand<object> CancelCreate { get; }
34-
public ReactiveCommand<Unit> CommitCreate { get; }
3532
public ReactiveCommand<object> BeginEdit { get; }
3633
public ReactiveCommand<object> CancelEdit { get; }
3734
public ReactiveCommand<Unit> CommitEdit { get; }

src/GitHub.InlineReviews/Services/InlineCommentPeekService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public ITrackingPoint Show(ITextView textView, AddInlineCommentTag tag)
111111
if (item != null)
112112
{
113113
var placeholder = item.ViewModel.Thread.Comments.Last();
114-
placeholder.CancelCreate.Take(1).Subscribe(_ => session.Dismiss());
114+
placeholder.CancelEdit.Take(1).Subscribe(_ => session.Dismiss());
115115
}
116116

117117
return trackingPoint;

src/GitHub.InlineReviews/ViewModels/CommentViewModel.cs

Lines changed: 24 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CommentViewModel : ReactiveObject, ICommentViewModel
2424
CommentEditState state;
2525
DateTimeOffset updatedAt;
2626
string undoBody;
27-
private bool canEditOrDelete;
27+
private bool canDelete;
2828

2929
/// <summary>
3030
/// Initializes a new instance of the <see cref="CommentViewModel"/> class.
@@ -61,49 +61,31 @@ protected CommentViewModel(
6161
User = user;
6262
UpdatedAt = updatedAt;
6363

64-
var canEditOrDeleteObservable = this.WhenAnyValue(
64+
var canDelete = this.WhenAnyValue(
6565
x => x.EditState,
6666
x => x == CommentEditState.None && user.Login.Equals(currentUser.Login));
6767

68-
canEditOrDeleteObservable.Subscribe(b => CanEditOrDelete = b);
68+
canDelete.Subscribe(b => CanDelete = b);
6969

70-
Delete = ReactiveCommand.CreateAsyncTask(canEditOrDeleteObservable, DoDelete);
70+
Delete = ReactiveCommand.CreateAsyncTask(canDelete, DoDelete);
7171

72-
BeginEdit = ReactiveCommand.Create(canEditOrDeleteObservable);
73-
BeginEdit.Subscribe(DoBeginEdit);
74-
AddErrorHandler(BeginEdit);
75-
76-
var canCreate = this.WhenAnyValue(
72+
var canEdit = this.WhenAnyValue(
7773
x => x.EditState,
78-
x => x == CommentEditState.Placeholder ||
79-
(x == CommentEditState.None && user.Login.Equals(currentUser.Login)));
80-
81-
BeginCreate = ReactiveCommand.Create(canCreate);
82-
BeginCreate.Subscribe(DoBeginCreate);
83-
AddErrorHandler(BeginCreate);
74+
x => x == CommentEditState.Placeholder || (x == CommentEditState.None && user.Equals(currentUser)));
8475

85-
CommitCreate = ReactiveCommand.CreateAsyncTask(
86-
Observable.CombineLatest(
87-
this.WhenAnyValue(x => x.IsReadOnly),
88-
this.WhenAnyValue(x => x.Body, x => !string.IsNullOrWhiteSpace(x)),
89-
this.WhenAnyObservable(x => x.Thread.PostComment.CanExecuteObservable),
90-
(readOnly, hasBody, canPost) => !readOnly && hasBody && canPost),
91-
DoCommitCreate);
92-
AddErrorHandler(CommitCreate);
76+
BeginEdit = ReactiveCommand.Create(canEdit);
77+
BeginEdit.Subscribe(DoBeginEdit);
78+
AddErrorHandler(BeginEdit);
9379

9480
CommitEdit = ReactiveCommand.CreateAsyncTask(
9581
Observable.CombineLatest(
9682
this.WhenAnyValue(x => x.IsReadOnly),
9783
this.WhenAnyValue(x => x.Body, x => !string.IsNullOrWhiteSpace(x)),
98-
this.WhenAnyObservable(x => x.Thread.EditComment.CanExecuteObservable),
84+
this.WhenAnyObservable(x => x.Thread.PostComment.CanExecuteObservable),
9985
(readOnly, hasBody, canPost) => !readOnly && hasBody && canPost),
10086
DoCommitEdit);
10187
AddErrorHandler(CommitEdit);
10288

103-
CancelCreate = ReactiveCommand.Create(CommitCreate.IsExecuting.Select(x => !x));
104-
CancelCreate.Subscribe(DoCancelCreate);
105-
AddErrorHandler(CancelCreate);
106-
10789
CancelEdit = ReactiveCommand.Create(CommitEdit.IsExecuting.Select(x => !x));
10890
CancelEdit.Subscribe(DoCancelEdit);
10991
AddErrorHandler(CancelEdit);
@@ -160,15 +142,6 @@ void DoBeginEdit(object unused)
160142
}
161143
}
162144

163-
void DoBeginCreate(object unused)
164-
{
165-
if (state != CommentEditState.Creating)
166-
{
167-
undoBody = Body;
168-
EditState = CommentEditState.Creating;
169-
}
170-
}
171-
172145
void DoCancelEdit(object unused)
173146
{
174147
if (EditState == CommentEditState.Editing)
@@ -180,50 +153,24 @@ void DoCancelEdit(object unused)
180153
}
181154
}
182155

183-
void DoCancelCreate(object unused)
184-
{
185-
if (EditState == CommentEditState.Creating)
186-
{
187-
EditState = string.IsNullOrWhiteSpace(undoBody) ? CommentEditState.Placeholder : CommentEditState.None;
188-
Body = undoBody;
189-
ErrorMessage = null;
190-
undoBody = null;
191-
}
192-
}
193-
194-
async Task DoCommitCreate(object unused)
156+
async Task DoCommitEdit(object unused)
195157
{
196158
try
197159
{
198160
ErrorMessage = null;
199161
IsSubmitting = true;
200162

201-
var model = await Thread.PostComment.ExecuteAsyncTask(Body);
202-
Id = model.Id;
203-
NodeId = model.NodeId;
204-
EditState = CommentEditState.None;
205-
UpdatedAt = DateTimeOffset.Now;
206-
}
207-
catch (Exception e)
208-
{
209-
var message = e.Message;
210-
ErrorMessage = message;
211-
log.Error(e, "Error posting comment");
212-
}
213-
finally
214-
{
215-
IsSubmitting = false;
216-
}
217-
}
163+
ICommentModel model;
218164

219-
async Task DoCommitEdit(object unused)
220-
{
221-
try
222-
{
223-
ErrorMessage = null;
224-
IsSubmitting = true;
165+
if (Id == 0)
166+
{
167+
model = await Thread.PostComment.ExecuteAsyncTask(Body);
168+
}
169+
else
170+
{
171+
model = await Thread.EditComment.ExecuteAsyncTask(new Tuple<int, string>(Id, Body));
172+
}
225173

226-
var model = await Thread.EditComment.ExecuteAsyncTask(new Tuple<int, string>(Id, Body));
227174
Id = model.Id;
228175
NodeId = model.NodeId;
229176
EditState = CommentEditState.None;
@@ -233,7 +180,7 @@ async Task DoCommitEdit(object unused)
233180
{
234181
var message = e.Message;
235182
ErrorMessage = message;
236-
log.Error(e, "Error editing comment");
183+
log.Error(e, "Error posting comment");
237184
}
238185
finally
239186
{
@@ -282,10 +229,10 @@ public bool IsSubmitting
282229
protected set { this.RaiseAndSetIfChanged(ref isSubmitting, value); }
283230
}
284231

285-
public bool CanEditOrDelete
232+
public bool CanDelete
286233
{
287-
get { return canEditOrDelete; }
288-
private set { this.RaiseAndSetIfChanged(ref canEditOrDelete, value); }
234+
get { return canDelete; }
235+
private set { this.RaiseAndSetIfChanged(ref canDelete, value); }
289236
}
290237

291238
/// <inheritdoc/>
@@ -304,15 +251,6 @@ public DateTimeOffset UpdatedAt
304251
/// <inheritdoc/>
305252
public IAccount User { get; }
306253

307-
/// <inheritdoc/>
308-
public ReactiveCommand<object> BeginCreate { get; }
309-
310-
/// <inheritdoc/>
311-
public ReactiveCommand<object> CancelCreate { get; }
312-
313-
/// <inheritdoc/>
314-
public ReactiveCommand<Unit> CommitCreate { get; }
315-
316254
/// <inheritdoc/>
317255
public ReactiveCommand<object> BeginEdit { get; }
318256

src/GitHub.InlineReviews/ViewModels/ICommentViewModel.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace GitHub.InlineReviews.ViewModels
99
public enum CommentEditState
1010
{
1111
None,
12-
Creating,
1312
Editing,
1413
Placeholder,
1514
}
@@ -58,7 +57,7 @@ public interface ICommentViewModel : IViewModel
5857
/// <summary>
5958
/// Gets a value indicating whether the comment can be edited or deleted by the current user
6059
/// </summary>
61-
bool CanEditOrDelete { get; }
60+
bool CanDelete { get; }
6261

6362
/// <summary>
6463
/// Gets the modified date of the comment.
@@ -75,21 +74,6 @@ public interface ICommentViewModel : IViewModel
7574
/// </summary>
7675
ICommentThreadViewModel Thread { get; }
7776

78-
/// <summary>
79-
/// Gets a command which will begin the creation of the comment.
80-
/// </summary>
81-
ReactiveCommand<object> BeginCreate { get; }
82-
83-
/// <summary>
84-
/// Gets a command which will cancel the creation of the comment.
85-
/// </summary>
86-
ReactiveCommand<object> CancelCreate { get; }
87-
88-
/// <summary>
89-
/// Gets a command which will commit the creation of the comment.
90-
/// </summary>
91-
ReactiveCommand<Unit> CommitCreate { get; }
92-
9377
/// <summary>
9478
/// Gets a command which will begin editing of the comment.
9579
/// </summary>

src/GitHub.InlineReviews/ViewModels/InlineCommentPeekViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async Task UpdateThread()
184184

185185
if (placeholder?.EditState == CommentEditState.Placeholder)
186186
{
187-
await placeholder.BeginCreate.ExecuteAsync(null);
187+
await placeholder.BeginEdit.ExecuteAsync(null);
188188
placeholder.Body = placeholderBody;
189189
}
190190
}
@@ -211,7 +211,7 @@ string GetPlaceholderBodyToPreserve()
211211
{
212212
var lastComment = Thread?.Comments.LastOrDefault();
213213

214-
if (lastComment?.EditState == CommentEditState.Creating)
214+
if (lastComment?.EditState == CommentEditState.Editing)
215215
{
216216
if (!lastComment.IsSubmitting) return lastComment.Body;
217217
}

src/GitHub.InlineReviews/ViewModels/NewInlineCommentThreadViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public NewInlineCommentThreadViewModel(
5454
o => null);
5555

5656
var placeholder = PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser);
57-
placeholder.BeginCreate.Execute(null);
57+
placeholder.BeginEdit.Execute(null);
5858
this.WhenAnyValue(x => x.NeedsPush).Subscribe(x => placeholder.IsReadOnly = x);
5959
Comments.Add(placeholder);
6060

src/GitHub.InlineReviews/ViewModels/PullRequestReviewCommentViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public PullRequestReviewCommentViewModel(
6161
.ToProperty(this, x => x.CommitCaption);
6262

6363
StartReview = ReactiveCommand.CreateAsyncTask(
64-
CommitCreate.CanExecuteObservable,
64+
CommitEdit.CanExecuteObservable,
6565
DoStartReview);
6666
AddErrorHandler(StartReview);
6767
}
@@ -125,7 +125,7 @@ async Task DoStartReview(object unused)
125125
try
126126
{
127127
await session.StartReview();
128-
await CommitCreate.ExecuteAsync(null);
128+
await CommitEdit.ExecuteAsync(null);
129129
}
130130
finally
131131
{

0 commit comments

Comments
 (0)