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

Refactor comment view models. #1993

Merged
merged 4 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/GitHub.App/SampleData/CommentThreadViewModelDesigner.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using GitHub.ViewModels;
using ReactiveUI;

namespace GitHub.SampleData
{
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
public class CommentThreadViewModelDesigner : ICommentThreadViewModel
public class CommentThreadViewModelDesigner : ViewModelBase, ICommentThreadViewModel
{
public ObservableCollection<ICommentViewModel> Comments { get; }
= new ObservableCollection<ICommentViewModel>();
public IReadOnlyReactiveList<ICommentViewModel> Comments { get; }
= new ReactiveList<ICommentViewModel>();

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

public ReactiveCommand<string, Unit> PostComment { get; }
public ReactiveCommand<Tuple<string, string>, Unit> EditComment { get; }
public ReactiveCommand<Tuple<int, int>, Unit> DeleteComment { get; }
public Task DeleteComment(int pullRequestId, int commentId) => Task.CompletedTask;
public Task EditComment(string id, string body) => Task.CompletedTask;
public Task PostComment(string body) => Task.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion src/GitHub.App/SampleData/CommentViewModelDesigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public CommentViewModelDesigner()
public bool IsSubmitting { get; set; }
public bool CanDelete { get; } = true;
public ICommentThreadViewModel Thread { get; }
public DateTimeOffset UpdatedAt => DateTime.Now.Subtract(TimeSpan.FromDays(3));
public DateTimeOffset CreatedAt => DateTime.Now.Subtract(TimeSpan.FromDays(3));
public IActorViewModel Author { get; set; }
public Uri WebUrl { get; }

Expand Down
70 changes: 23 additions & 47 deletions src/GitHub.App/ViewModels/CommentThreadViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,53 @@
using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.ViewModels;
using ReactiveUI;

namespace GitHub.InlineReviews.ViewModels
namespace GitHub.ViewModels
{
/// <summary>
/// Base view model for a thread of comments.
/// </summary>
public abstract class CommentThreadViewModel : ReactiveObject, ICommentThreadViewModel
{
ReactiveCommand<string, Unit> postComment;
ReactiveCommand<Tuple<string, string>, Unit> editComment;
ReactiveCommand<Tuple<int, int>, Unit> deleteComment;
readonly ReactiveList<ICommentViewModel> comments = new ReactiveList<ICommentViewModel>();

/// <summary>
/// Initializes a new instance of the <see cref="CommentThreadViewModel"/> class.
/// </summary>
[ImportingConstructor]
public CommentThreadViewModel()
{
}

/// <summary>
/// Intializes a new instance of the <see cref="CommentThreadViewModel"/> class.
/// </summary>
/// <param name="currentUser">The current user.</param>
protected CommentThreadViewModel(ActorModel currentUser)
protected Task InitializeAsync(ActorModel currentUser)
{
Guard.ArgumentNotNull(currentUser, nameof(currentUser));

Comments = new ObservableCollection<ICommentViewModel>();
CurrentUser = new ActorViewModel(currentUser);
return Task.CompletedTask;
}

/// <inheritdoc/>
public ObservableCollection<ICommentViewModel> Comments { get; }
public IReactiveList<ICommentViewModel> Comments => comments;

/// <inheritdoc/>
public ReactiveCommand<string, Unit> PostComment
{
get { return postComment; }
protected set
{
Guard.ArgumentNotNull(value, nameof(value));
postComment = value;

// We want to ignore thrown exceptions from PostComment - the error should be handled
// by the CommentViewModel that trigged PostComment.Execute();
value.ThrownExceptions.Subscribe(_ => { });
}
}
public IActorViewModel CurrentUser { get; private set; }

public ReactiveCommand<Tuple<string, string>, Unit> EditComment
{
get { return editComment; }
protected set
{
Guard.ArgumentNotNull(value, nameof(value));
editComment = value;

value.ThrownExceptions.Subscribe(_ => { });
}
}
/// <inheritdoc/>
IReadOnlyReactiveList<ICommentViewModel> ICommentThreadViewModel.Comments => comments;

public ReactiveCommand<Tuple<int, int>, Unit> DeleteComment
{
get { return deleteComment; }
protected set
{
Guard.ArgumentNotNull(value, nameof(value));
deleteComment = value;
/// <inheritdoc/>
public abstract Task PostComment(string body);

value.ThrownExceptions.Subscribe(_ => { });
}
}
/// <inheritdoc/>
public abstract Task EditComment(string id, string body);

/// <inheritdoc/>
public IActorViewModel CurrentUser { get; }
public abstract Task DeleteComment(int pullRequestId, int commentId);
}
}
Loading