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

Commit 7b4c565

Browse files
committed
Factor out UsageTrackingCommand
1 parent f5687c0 commit 7b4c565

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Windows.Input;
3+
using System.Linq.Expressions;
4+
using GitHub.Models;
5+
using GitHub.Services;
6+
using GitHub.Extensions;
7+
8+
namespace GitHub.Commands
9+
{
10+
/// <summary>
11+
/// A proxy <see cref="ICommand"/> that increments a usage counter after executing the command.
12+
/// </summary>
13+
public class UsageTrackingCommand : ICommand
14+
{
15+
readonly ICommand command;
16+
readonly Lazy<IUsageTracker> usageTracker;
17+
readonly Expression<Func<UsageModel.MeasuresModel, int>> counter;
18+
19+
/// <summary>
20+
/// The usage tracker and counter to increment after the target command is executed.
21+
/// </summary>
22+
/// <param name="usageTracker">The usage tracker.</param>
23+
/// <param name="counter">The counter to increment.</param>
24+
/// <param name="command">The target command.</param>
25+
public UsageTrackingCommand(
26+
Lazy<IUsageTracker> usageTracker, Expression<Func<UsageModel.MeasuresModel, int>> counter,
27+
ICommand command)
28+
{
29+
this.command = command;
30+
this.usageTracker = usageTracker;
31+
this.counter = counter;
32+
}
33+
34+
public event EventHandler CanExecuteChanged
35+
{
36+
add { command.CanExecuteChanged += value; }
37+
remove { command.CanExecuteChanged -= value; }
38+
}
39+
40+
public bool CanExecute(object parameter)
41+
{
42+
return command.CanExecute(parameter);
43+
}
44+
45+
public void Execute(object parameter)
46+
{
47+
command.Execute(parameter);
48+
usageTracker.Value.IncrementCounter(counter).Forget();
49+
}
50+
}
51+
}

src/GitHub.App/GitHub.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
<Reference Include="WindowsBase" />
206206
</ItemGroup>
207207
<ItemGroup>
208+
<Compile Include="Commands\UsageTrackingCommand.cs" />
208209
<Compile Include="Factories\ViewViewModelFactory.cs" />
209210
<Compile Include="Factories\ModelServiceFactory.cs" />
210211
<Compile Include="Models\IssueCommentModel.cs" />

src/GitHub.InlineReviews/Services/PullRequestStatusBarManager.cs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
using System.Windows.Controls.Primitives;
66
using System.ComponentModel.Composition;
77
using System.Reactive.Linq;
8-
using System.Linq.Expressions;
98
using GitHub.Commands;
109
using GitHub.InlineReviews.Views;
1110
using GitHub.InlineReviews.ViewModels;
1211
using GitHub.Services;
1312
using GitHub.Models;
1413
using GitHub.Logging;
15-
using GitHub.Extensions;
1614
using Serilog;
1715
using ReactiveUI;
1816

@@ -45,10 +43,10 @@ public PullRequestStatusBarManager(
4543
Lazy<IPullRequestSessionManager> pullRequestSessionManager,
4644
Lazy<ITeamExplorerContext> teamExplorerContext)
4745
{
48-
this.openPullRequestsCommand = new UsageTrackingCommand(openPullRequestsCommand,
49-
usageTracker, x => x.NumberOfStatusBarOpenPullRequestList);
50-
this.showCurrentPullRequestCommand = new UsageTrackingCommand(showCurrentPullRequestCommand,
51-
usageTracker, x => x.NumberOfShowCurrentPullRequest);
46+
this.openPullRequestsCommand = new UsageTrackingCommand(usageTracker,
47+
x => x.NumberOfStatusBarOpenPullRequestList, openPullRequestsCommand);
48+
this.showCurrentPullRequestCommand = new UsageTrackingCommand(usageTracker,
49+
x => x.NumberOfShowCurrentPullRequest, showCurrentPullRequestCommand);
5250

5351
this.pullRequestSessionManager = pullRequestSessionManager;
5452
this.teamExplorerContext = teamExplorerContext;
@@ -143,37 +141,5 @@ StatusBar FindSccStatusBar(Window mainWindow)
143141
var contentControl = mainWindow?.Template?.FindName(StatusBarPartName, mainWindow) as ContentControl;
144142
return contentControl?.Content as StatusBar;
145143
}
146-
147-
class UsageTrackingCommand : ICommand
148-
{
149-
readonly ICommand command;
150-
readonly Lazy<IUsageTracker> usageTracker;
151-
readonly Expression<Func<UsageModel.MeasuresModel, int>> counter;
152-
153-
internal UsageTrackingCommand(ICommand command, Lazy<IUsageTracker> usageTracker,
154-
Expression<Func<UsageModel.MeasuresModel, int>> counter)
155-
{
156-
this.command = command;
157-
this.usageTracker = usageTracker;
158-
this.counter = counter;
159-
}
160-
161-
public event EventHandler CanExecuteChanged
162-
{
163-
add { command.CanExecuteChanged += value; }
164-
remove { command.CanExecuteChanged -= value; }
165-
}
166-
167-
public bool CanExecute(object parameter)
168-
{
169-
return command.CanExecute(parameter);
170-
}
171-
172-
public void Execute(object parameter)
173-
{
174-
command.Execute(parameter);
175-
usageTracker.Value.IncrementCounter(counter).Forget();
176-
}
177-
}
178144
}
179145
}

0 commit comments

Comments
 (0)