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

Commit a9f4336

Browse files
authored
Merge pull request #1993 from github/refactor/comment-viewmodels
Refactor comment view models.
2 parents c44db52 + e141b61 commit a9f4336

24 files changed

+1015
-1138
lines changed
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
using System;
2-
using System.Collections.ObjectModel;
3-
using System.Diagnostics.CodeAnalysis;
4-
using System.Reactive;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Threading.Tasks;
53
using GitHub.ViewModels;
64
using ReactiveUI;
75

86
namespace GitHub.SampleData
97
{
108
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
11-
public class CommentThreadViewModelDesigner : ICommentThreadViewModel
9+
public class CommentThreadViewModelDesigner : ViewModelBase, ICommentThreadViewModel
1210
{
13-
public ObservableCollection<ICommentViewModel> Comments { get; }
14-
= new ObservableCollection<ICommentViewModel>();
11+
public IReadOnlyReactiveList<ICommentViewModel> Comments { get; }
12+
= new ReactiveList<ICommentViewModel>();
1513

1614
public IActorViewModel CurrentUser { get; set; }
1715
= new ActorViewModel { Login = "shana" };
1816

19-
public ReactiveCommand<string, Unit> PostComment { get; }
20-
public ReactiveCommand<Tuple<string, string>, Unit> EditComment { get; }
21-
public ReactiveCommand<Tuple<int, int>, Unit> DeleteComment { get; }
17+
public Task DeleteComment(int pullRequestId, int commentId) => Task.CompletedTask;
18+
public Task EditComment(string id, string body) => Task.CompletedTask;
19+
public Task PostComment(string body) => Task.CompletedTask;
2220
}
2321
}

src/GitHub.App/SampleData/CommentViewModelDesigner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public CommentViewModelDesigner()
2424
public bool IsSubmitting { get; set; }
2525
public bool CanDelete { get; } = true;
2626
public ICommentThreadViewModel Thread { get; }
27-
public DateTimeOffset UpdatedAt => DateTime.Now.Subtract(TimeSpan.FromDays(3));
27+
public DateTimeOffset CreatedAt => DateTime.Now.Subtract(TimeSpan.FromDays(3));
2828
public IActorViewModel Author { get; set; }
2929
public Uri WebUrl { get; }
3030

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,53 @@
1-
using System;
2-
using System.Collections.ObjectModel;
3-
using System.Reactive;
1+
using System.ComponentModel.Composition;
2+
using System.Threading.Tasks;
43
using GitHub.Extensions;
54
using GitHub.Models;
6-
using GitHub.ViewModels;
75
using ReactiveUI;
86

9-
namespace GitHub.InlineReviews.ViewModels
7+
namespace GitHub.ViewModels
108
{
119
/// <summary>
1210
/// Base view model for a thread of comments.
1311
/// </summary>
1412
public abstract class CommentThreadViewModel : ReactiveObject, ICommentThreadViewModel
1513
{
16-
ReactiveCommand<string, Unit> postComment;
17-
ReactiveCommand<Tuple<string, string>, Unit> editComment;
18-
ReactiveCommand<Tuple<int, int>, Unit> deleteComment;
14+
readonly ReactiveList<ICommentViewModel> comments = new ReactiveList<ICommentViewModel>();
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="CommentThreadViewModel"/> class.
18+
/// </summary>
19+
[ImportingConstructor]
20+
public CommentThreadViewModel()
21+
{
22+
}
1923

2024
/// <summary>
2125
/// Intializes a new instance of the <see cref="CommentThreadViewModel"/> class.
2226
/// </summary>
2327
/// <param name="currentUser">The current user.</param>
24-
protected CommentThreadViewModel(ActorModel currentUser)
28+
protected Task InitializeAsync(ActorModel currentUser)
2529
{
2630
Guard.ArgumentNotNull(currentUser, nameof(currentUser));
27-
28-
Comments = new ObservableCollection<ICommentViewModel>();
2931
CurrentUser = new ActorViewModel(currentUser);
32+
return Task.CompletedTask;
3033
}
3134

3235
/// <inheritdoc/>
33-
public ObservableCollection<ICommentViewModel> Comments { get; }
36+
public IReactiveList<ICommentViewModel> Comments => comments;
3437

3538
/// <inheritdoc/>
36-
public ReactiveCommand<string, Unit> PostComment
37-
{
38-
get { return postComment; }
39-
protected set
40-
{
41-
Guard.ArgumentNotNull(value, nameof(value));
42-
postComment = value;
43-
44-
// We want to ignore thrown exceptions from PostComment - the error should be handled
45-
// by the CommentViewModel that trigged PostComment.Execute();
46-
value.ThrownExceptions.Subscribe(_ => { });
47-
}
48-
}
39+
public IActorViewModel CurrentUser { get; private set; }
4940

50-
public ReactiveCommand<Tuple<string, string>, Unit> EditComment
51-
{
52-
get { return editComment; }
53-
protected set
54-
{
55-
Guard.ArgumentNotNull(value, nameof(value));
56-
editComment = value;
57-
58-
value.ThrownExceptions.Subscribe(_ => { });
59-
}
60-
}
41+
/// <inheritdoc/>
42+
IReadOnlyReactiveList<ICommentViewModel> ICommentThreadViewModel.Comments => comments;
6143

62-
public ReactiveCommand<Tuple<int, int>, Unit> DeleteComment
63-
{
64-
get { return deleteComment; }
65-
protected set
66-
{
67-
Guard.ArgumentNotNull(value, nameof(value));
68-
deleteComment = value;
44+
/// <inheritdoc/>
45+
public abstract Task PostComment(string body);
6946

70-
value.ThrownExceptions.Subscribe(_ => { });
71-
}
72-
}
47+
/// <inheritdoc/>
48+
public abstract Task EditComment(string id, string body);
7349

7450
/// <inheritdoc/>
75-
public IActorViewModel CurrentUser { get; }
51+
public abstract Task DeleteComment(int pullRequestId, int commentId);
7652
}
7753
}

0 commit comments

Comments
 (0)