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

Commit 7aece67

Browse files
Moving message box functionality to a CommentService and injecting it
1 parent e7ff5a4 commit 7aece67

14 files changed

+123
-43
lines changed

src/GitHub.InlineReviews/GitHub.InlineReviews.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
<Compile Include="Margins\InlineCommentMargin.cs" />
8585
<Compile Include="Margins\InlineCommentMarginVisible.cs" />
8686
<Compile Include="Margins\InlineCommentMarginEnabled.cs" />
87+
<Compile Include="Services\CommentService.cs" />
88+
<Compile Include="Services\ICommentService.cs" />
8789
<Compile Include="PullRequestStatusBarPackage.cs" />
8890
<Compile Include="InlineReviewsPackage.cs" />
8991
<Compile Include="Models\InlineCommentThreadModel.cs" />

src/GitHub.InlineReviews/Peek/InlineCommentPeekableItemSource.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ class InlineCommentPeekableItemSource : IPeekableItemSource
1616
readonly IPullRequestSessionManager sessionManager;
1717
readonly INextInlineCommentCommand nextCommentCommand;
1818
readonly IPreviousInlineCommentCommand previousCommentCommand;
19+
readonly ICommentService commentService;
1920

20-
public InlineCommentPeekableItemSource(
21-
IInlineCommentPeekService peekService,
21+
public InlineCommentPeekableItemSource(IInlineCommentPeekService peekService,
2222
IPullRequestSessionManager sessionManager,
2323
INextInlineCommentCommand nextCommentCommand,
24-
IPreviousInlineCommentCommand previousCommentCommand)
24+
IPreviousInlineCommentCommand previousCommentCommand,
25+
ICommentService commentService)
2526
{
2627
this.peekService = peekService;
2728
this.sessionManager = sessionManager;
2829
this.nextCommentCommand = nextCommentCommand;
2930
this.previousCommentCommand = previousCommentCommand;
31+
this.commentService = commentService;
3032
}
3133

3234
public void AugmentPeekSession(IPeekSession session, IList<IPeekableItem> peekableItems)
@@ -38,7 +40,8 @@ public void AugmentPeekSession(IPeekSession session, IList<IPeekableItem> peekab
3840
session,
3941
sessionManager,
4042
nextCommentCommand,
41-
previousCommentCommand);
43+
previousCommentCommand,
44+
commentService);
4245
viewModel.Initialize().Forget();
4346
peekableItems.Add(new InlineCommentPeekableItem(viewModel));
4447
}

src/GitHub.InlineReviews/Peek/InlineCommentPeekableItemSourceProvider.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ class InlineCommentPeekableItemSourceProvider : IPeekableItemSourceProvider
2020
readonly IPullRequestSessionManager sessionManager;
2121
readonly INextInlineCommentCommand nextCommentCommand;
2222
readonly IPreviousInlineCommentCommand previousCommentCommand;
23+
readonly ICommentService commentService;
2324

2425
[ImportingConstructor]
2526
public InlineCommentPeekableItemSourceProvider(
2627
IInlineCommentPeekService peekService,
2728
IPullRequestSessionManager sessionManager,
2829
INextInlineCommentCommand nextCommentCommand,
29-
IPreviousInlineCommentCommand previousCommentCommand)
30+
IPreviousInlineCommentCommand previousCommentCommand,
31+
ICommentService commentService)
3032
{
3133
this.peekService = peekService;
3234
this.sessionManager = sessionManager;
3335
this.nextCommentCommand = nextCommentCommand;
3436
this.previousCommentCommand = previousCommentCommand;
37+
this.commentService = commentService;
3538
}
3639

3740
public IPeekableItemSource TryCreatePeekableItemSource(ITextBuffer textBuffer)
@@ -40,7 +43,8 @@ public IPeekableItemSource TryCreatePeekableItemSource(ITextBuffer textBuffer)
4043
peekService,
4144
sessionManager,
4245
nextCommentCommand,
43-
previousCommentCommand);
46+
previousCommentCommand,
47+
commentService);
4448
}
4549
}
4650
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.ComponentModel.Composition;
2+
using System.Windows.Forms;
3+
4+
namespace GitHub.InlineReviews.Services
5+
{
6+
[Export(typeof(ICommentService))]
7+
[PartCreationPolicy(CreationPolicy.NonShared)]
8+
public class CommentService:ICommentService
9+
{
10+
public bool ConfirmCommentDelete()
11+
{
12+
return MessageBox.Show(
13+
VisualStudio.UI.Resources.DeleteCommentConfirmation,
14+
VisualStudio.UI.Resources.DeleteCommentConfirmationCaption,
15+
MessageBoxButtons.YesNo,
16+
MessageBoxIcon.Question) == DialogResult.Yes;
17+
}
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GitHub.InlineReviews.Services
2+
{
3+
public interface ICommentService
4+
{
5+
bool ConfirmCommentDelete();
6+
}
7+
}

src/GitHub.InlineReviews/ViewModels/CommentViewModel.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using System.Windows;
77
using GitHub.Extensions;
8+
using GitHub.InlineReviews.Services;
89
using GitHub.Logging;
910
using GitHub.Models;
1011
using GitHub.ViewModels;
@@ -19,6 +20,7 @@ namespace GitHub.InlineReviews.ViewModels
1920
public class CommentViewModel : ReactiveObject, ICommentViewModel
2021
{
2122
static readonly ILogger log = LogManager.ForContext<CommentViewModel>();
23+
ICommentService commentService;
2224
string body;
2325
string errorMessage;
2426
bool isReadOnly;
@@ -31,6 +33,7 @@ public class CommentViewModel : ReactiveObject, ICommentViewModel
3133
/// <summary>
3234
/// Initializes a new instance of the <see cref="CommentViewModel"/> class.
3335
/// </summary>
36+
/// <param name="commentService">The comment service</param>
3437
/// <param name="thread">The thread that the comment is a part of.</param>
3538
/// <param name="currentUser">The current user.</param>
3639
/// <param name="pullRequestId">The pull request id of the comment.</param>
@@ -42,6 +45,7 @@ public class CommentViewModel : ReactiveObject, ICommentViewModel
4245
/// <param name="updatedAt">The modified date of the comment.</param>
4346
/// <param name="webUrl"></param>
4447
protected CommentViewModel(
48+
ICommentService commentService,
4549
ICommentThreadViewModel thread,
4650
IActorViewModel currentUser,
4751
int pullRequestId,
@@ -53,6 +57,7 @@ protected CommentViewModel(
5357
DateTimeOffset updatedAt,
5458
Uri webUrl)
5559
{
60+
this.commentService = commentService;
5661
Guard.ArgumentNotNull(thread, nameof(thread));
5762
Guard.ArgumentNotNull(currentUser, nameof(currentUser));
5863
Guard.ArgumentNotNull(author, nameof(author));
@@ -103,14 +108,17 @@ protected CommentViewModel(
103108
/// <summary>
104109
/// Initializes a new instance of the <see cref="CommentViewModel"/> class.
105110
/// </summary>
111+
/// <param name="commentService">Comment Service</param>
106112
/// <param name="thread">The thread that the comment is a part of.</param>
107113
/// <param name="currentUser">The current user.</param>
108114
/// <param name="model">The comment model.</param>
109115
protected CommentViewModel(
116+
ICommentService commentService,
110117
ICommentThreadViewModel thread,
111118
ActorModel currentUser,
112119
CommentModel model)
113120
: this(
121+
commentService,
114122
thread,
115123
new ActorViewModel(currentUser),
116124
model.PullRequestId,
@@ -131,13 +139,7 @@ protected void AddErrorHandler<T>(ReactiveCommand<T> command)
131139

132140
async Task DoDelete(object unused)
133141
{
134-
var result = MessageBox.Show(
135-
VisualStudio.UI.Resources.DeleteCommentConfirmation,
136-
VisualStudio.UI.Resources.DeleteCommentConfirmationCaption,
137-
MessageBoxButton.YesNo,
138-
MessageBoxImage.Question);
139-
140-
if (result == MessageBoxResult.Yes)
142+
if (commentService.ConfirmCommentDelete())
141143
{
142144
try
143145
{

src/GitHub.InlineReviews/ViewModels/InlineCommentPeekViewModel.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using GitHub.Extensions.Reactive;
1111
using GitHub.Factories;
1212
using GitHub.InlineReviews.Commands;
13+
using GitHub.InlineReviews.Peek;
1314
using GitHub.InlineReviews.Services;
1415
using GitHub.Logging;
1516
using GitHub.Models;
@@ -31,6 +32,7 @@ public sealed class InlineCommentPeekViewModel : ReactiveObject, IDisposable
3132
readonly IInlineCommentPeekService peekService;
3233
readonly IPeekSession peekSession;
3334
readonly IPullRequestSessionManager sessionManager;
35+
readonly ICommentService commentService;
3436
IPullRequestSession session;
3537
IPullRequestSessionFile file;
3638
ICommentThreadViewModel thread;
@@ -44,12 +46,12 @@ public sealed class InlineCommentPeekViewModel : ReactiveObject, IDisposable
4446
/// <summary>
4547
/// Initializes a new instance of the <see cref="InlineCommentPeekViewModel"/> class.
4648
/// </summary>
47-
public InlineCommentPeekViewModel(
48-
IInlineCommentPeekService peekService,
49+
public InlineCommentPeekViewModel(IInlineCommentPeekService peekService,
4950
IPeekSession peekSession,
5051
IPullRequestSessionManager sessionManager,
5152
INextInlineCommentCommand nextCommentCommand,
52-
IPreviousInlineCommentCommand previousCommentCommand)
53+
IPreviousInlineCommentCommand previousCommentCommand,
54+
ICommentService commentService)
5355
{
5456
Guard.ArgumentNotNull(peekService, nameof(peekService));
5557
Guard.ArgumentNotNull(peekSession, nameof(peekSession));
@@ -60,6 +62,7 @@ public InlineCommentPeekViewModel(
6062
this.peekService = peekService;
6163
this.peekSession = peekSession;
6264
this.sessionManager = sessionManager;
65+
this.commentService = commentService;
6366
triggerPoint = peekSession.GetTriggerPoint(peekSession.TextView.TextBuffer);
6467

6568
peekSession.Dismissed += (s, e) => Dispose();
@@ -180,11 +183,11 @@ async Task UpdateThread()
180183

181184
if (thread != null)
182185
{
183-
Thread = new InlineCommentThreadViewModel(session, thread.Comments);
186+
Thread = new InlineCommentThreadViewModel(commentService, session, thread.Comments);
184187
}
185188
else
186189
{
187-
Thread = new NewInlineCommentThreadViewModel(session, file, lineNumber, leftBuffer);
190+
Thread = new NewInlineCommentThreadViewModel(commentService, session, file, lineNumber, leftBuffer);
188191
}
189192

190193
if (!string.IsNullOrWhiteSpace(placeholderBody))

src/GitHub.InlineReviews/ViewModels/InlineCommentThreadViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reactive.Linq;
55
using System.Threading.Tasks;
66
using GitHub.Extensions;
7+
using GitHub.InlineReviews.Services;
78
using GitHub.Models;
89
using GitHub.Services;
910
using ReactiveUI;
@@ -18,10 +19,10 @@ public class InlineCommentThreadViewModel : CommentThreadViewModel
1819
/// <summary>
1920
/// Initializes a new instance of the <see cref="InlineCommentThreadViewModel"/> class.
2021
/// </summary>
22+
/// <param name="commentService">The comment service</param>
2123
/// <param name="session">The current PR review session.</param>
2224
/// <param name="comments">The comments to display in this inline review.</param>
23-
public InlineCommentThreadViewModel(
24-
IPullRequestSession session,
25+
public InlineCommentThreadViewModel(ICommentService commentService, IPullRequestSession session,
2526
IEnumerable<InlineCommentModel> comments)
2627
: base(session.User)
2728
{
@@ -45,13 +46,14 @@ public InlineCommentThreadViewModel(
4546
{
4647
Comments.Add(new PullRequestReviewCommentViewModel(
4748
session,
49+
commentService,
4850
this,
4951
CurrentUser,
5052
comment.Review,
5153
comment.Comment));
5254
}
5355

54-
Comments.Add(PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser));
56+
Comments.Add(PullRequestReviewCommentViewModel.CreatePlaceholder(session, commentService, this, CurrentUser));
5557
}
5658

5759
/// <summary>

src/GitHub.InlineReviews/ViewModels/NewInlineCommentThreadViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reactive.Linq;
55
using System.Threading.Tasks;
66
using GitHub.Extensions;
7+
using GitHub.InlineReviews.Services;
78
using GitHub.Models;
89
using GitHub.Services;
910
using ReactiveUI;
@@ -20,13 +21,14 @@ public class NewInlineCommentThreadViewModel : CommentThreadViewModel
2021
/// <summary>
2122
/// Initializes a new instance of the <see cref="InlineCommentThreadViewModel"/> class.
2223
/// </summary>
24+
/// <param name="commentService">The comment service</param>
2325
/// <param name="session">The current PR review session.</param>
2426
/// <param name="file">The file being commented on.</param>
2527
/// <param name="lineNumber">The 0-based line number in the file.</param>
2628
/// <param name="leftComparisonBuffer">
27-
/// True if the comment is being left on the left-hand-side of a diff; otherwise false.
29+
/// True if the comment is being left on the left-hand-side of a diff; otherwise false.
2830
/// </param>
29-
public NewInlineCommentThreadViewModel(
31+
public NewInlineCommentThreadViewModel(ICommentService commentService,
3032
IPullRequestSession session,
3133
IPullRequestSessionFile file,
3234
int lineNumber,
@@ -53,7 +55,7 @@ public NewInlineCommentThreadViewModel(
5355
Observable.Return(false),
5456
o => null);
5557

56-
var placeholder = PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser);
58+
var placeholder = PullRequestReviewCommentViewModel.CreatePlaceholder(session, commentService, this, CurrentUser);
5759
placeholder.BeginEdit.Execute(null);
5860
this.WhenAnyValue(x => x.NeedsPush).Subscribe(x => placeholder.IsReadOnly = x);
5961
Comments.Add(placeholder);

src/GitHub.InlineReviews/ViewModels/PullRequestReviewCommentViewModel.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reactive.Linq;
66
using System.Threading.Tasks;
77
using GitHub.Extensions;
8+
using GitHub.InlineReviews.Services;
89
using GitHub.Logging;
910
using GitHub.Models;
1011
using GitHub.Services;
@@ -28,6 +29,7 @@ public class PullRequestReviewCommentViewModel : CommentViewModel, IPullRequestR
2829
/// Initializes a new instance of the <see cref="PullRequestReviewCommentViewModel"/> class.
2930
/// </summary>
3031
/// <param name="session">The pull request session.</param>
32+
/// <param name="commentService">The comment service</param>
3133
/// <param name="thread">The thread that the comment is a part of.</param>
3234
/// <param name="currentUser">The current user.</param>
3335
/// <param name="pullRequestId">The pull request id of the comment.</param>
@@ -39,7 +41,9 @@ public class PullRequestReviewCommentViewModel : CommentViewModel, IPullRequestR
3941
/// <param name="updatedAt">The modified date of the comment.</param>
4042
/// <param name="isPending">Whether this is a pending comment.</param>
4143
/// <param name="webUrl"></param>
42-
public PullRequestReviewCommentViewModel(IPullRequestSession session,
44+
public PullRequestReviewCommentViewModel(
45+
IPullRequestSession session,
46+
ICommentService commentService,
4347
ICommentThreadViewModel thread,
4448
IActorViewModel currentUser,
4549
int pullRequestId,
@@ -51,7 +55,7 @@ public PullRequestReviewCommentViewModel(IPullRequestSession session,
5155
DateTimeOffset updatedAt,
5256
bool isPending,
5357
Uri webUrl)
54-
: base(thread, currentUser, pullRequestId, commentId, databaseId, body, state, author, updatedAt, webUrl)
58+
: base(commentService, thread, currentUser, pullRequestId, commentId, databaseId, body, state, author, updatedAt, webUrl)
5559
{
5660
Guard.ArgumentNotNull(session, nameof(session));
5761

@@ -81,17 +85,20 @@ public PullRequestReviewCommentViewModel(IPullRequestSession session,
8185
/// Initializes a new instance of the <see cref="PullRequestReviewCommentViewModel"/> class.
8286
/// </summary>
8387
/// <param name="session">The pull request session.</param>
88+
/// <param name="commentService">Comment Service</param>
8489
/// <param name="thread">The thread that the comment is a part of.</param>
8590
/// <param name="currentUser">The current user.</param>
8691
/// <param name="model">The comment model.</param>
8792
public PullRequestReviewCommentViewModel(
8893
IPullRequestSession session,
94+
ICommentService commentService,
8995
ICommentThreadViewModel thread,
9096
IActorViewModel currentUser,
9197
PullRequestReviewModel review,
9298
PullRequestReviewCommentModel model)
9399
: this(
94100
session,
101+
commentService,
95102
thread,
96103
currentUser,
97104
model.PullRequestId,
@@ -110,16 +117,19 @@ public PullRequestReviewCommentViewModel(
110117
/// Creates a placeholder comment which can be used to add a new comment to a thread.
111118
/// </summary>
112119
/// <param name="session">The pull request session.</param>
120+
/// <param name="commentService">Comment Service</param>
113121
/// <param name="thread">The comment thread.</param>
114122
/// <param name="currentUser">The current user.</param>
115123
/// <returns>THe placeholder comment.</returns>
116124
public static CommentViewModel CreatePlaceholder(
117125
IPullRequestSession session,
126+
ICommentService commentService,
118127
ICommentThreadViewModel thread,
119128
IActorViewModel currentUser)
120129
{
121130
return new PullRequestReviewCommentViewModel(
122131
session,
132+
commentService,
123133
thread,
124134
currentUser,
125135
0,

0 commit comments

Comments
 (0)