11using System ;
22using System . Windows ;
3- using System . Windows . Input ;
43using System . Windows . Controls ;
54using System . Windows . Controls . Primitives ;
65using System . ComponentModel . Composition ;
6+ using GitHub . Commands ;
77using GitHub . InlineReviews . Views ;
88using GitHub . InlineReviews . ViewModels ;
99using GitHub . Services ;
10- using GitHub . VisualStudio ;
1110using GitHub . Models ;
1211using GitHub . Logging ;
13- using GitHub . Extensions ;
1412using Serilog ;
1513using ReactiveUI ;
1614
@@ -19,21 +17,25 @@ namespace GitHub.InlineReviews.Services
1917 /// <summary>
2018 /// Manage the UI that shows the PR for the current branch.
2119 /// </summary>
20+ [ Export ( typeof ( PullRequestStatusBarManager ) ) ]
2221 public class PullRequestStatusBarManager
2322 {
2423 static readonly ILogger log = LogManager . ForContext < PullRequestStatusBarManager > ( ) ;
2524 const string StatusBarPartName = "PART_SccStatusBarHost" ;
2625
27- readonly IUsageTracker usageTracker ;
28- readonly IGitHubServiceProvider serviceProvider ;
26+ readonly IShowCurrentPullRequestCommand showCurrentPullRequestCommand ;
2927
30- IPullRequestSessionManager pullRequestSessionManager ;
28+ // At the moment this must be constructed on the main thread.
29+ // TeamExplorerContext needs to retrieve DTE using GetService.
30+ readonly Lazy < IPullRequestSessionManager > pullRequestSessionManager ;
3131
3232 [ ImportingConstructor ]
33- public PullRequestStatusBarManager ( IUsageTracker usageTracker , IGitHubServiceProvider serviceProvider )
33+ public PullRequestStatusBarManager (
34+ IShowCurrentPullRequestCommand showCurrentPullRequestCommand ,
35+ Lazy < IPullRequestSessionManager > pullRequestSessionManager )
3436 {
35- this . usageTracker = usageTracker ;
36- this . serviceProvider = serviceProvider ;
37+ this . showCurrentPullRequestCommand = showCurrentPullRequestCommand ;
38+ this . pullRequestSessionManager = pullRequestSessionManager ;
3739 }
3840
3941 /// <summary>
@@ -46,8 +48,7 @@ public void StartShowingStatus()
4648 {
4749 try
4850 {
49- pullRequestSessionManager = serviceProvider . GetService < IPullRequestSessionManager > ( ) ;
50- pullRequestSessionManager . WhenAnyValue ( x => x . CurrentSession )
51+ pullRequestSessionManager . Value . WhenAnyValue ( x => x . CurrentSession )
5152 . Subscribe ( x => RefreshCurrentSession ( ) ) ;
5253 }
5354 catch ( Exception e )
@@ -58,16 +59,14 @@ public void StartShowingStatus()
5859
5960 void RefreshCurrentSession ( )
6061 {
61- var pullRequest = pullRequestSessionManager . CurrentSession ? . PullRequest ;
62+ var pullRequest = pullRequestSessionManager . Value . CurrentSession ? . PullRequest ;
6263 var viewModel = pullRequest != null ? CreatePullRequestStatusViewModel ( pullRequest ) : null ;
6364 ShowStatus ( viewModel ) ;
6465 }
6566
6667 PullRequestStatusViewModel CreatePullRequestStatusViewModel ( IPullRequestModel pullRequest )
6768 {
68- var dte = serviceProvider . TryGetService < EnvDTE . DTE > ( ) ;
69- var command = new RaisePullRequestCommand ( dte , usageTracker ) ;
70- var pullRequestStatusViewModel = new PullRequestStatusViewModel ( command ) ;
69+ var pullRequestStatusViewModel = new PullRequestStatusViewModel ( showCurrentPullRequestCommand ) ;
7170 pullRequestStatusViewModel . Number = pullRequest . Number ;
7271 pullRequestStatusViewModel . Title = pullRequest . Title ;
7372 return pullRequestStatusViewModel ;
@@ -111,44 +110,5 @@ StatusBar FindSccStatusBar(Window mainWindow)
111110 var contentControl = mainWindow ? . Template ? . FindName ( StatusBarPartName , mainWindow ) as ContentControl ;
112111 return contentControl ? . Content as StatusBar ;
113112 }
114-
115- class RaisePullRequestCommand : ICommand
116- {
117- readonly string guid = Guids . guidGitHubCmdSetString ;
118- readonly int id = PkgCmdIDList . showCurrentPullRequestCommand ;
119-
120- readonly EnvDTE . DTE dte ;
121- readonly IUsageTracker usageTracker ;
122-
123- internal RaisePullRequestCommand ( EnvDTE . DTE dte , IUsageTracker usageTracker )
124- {
125- this . dte = dte ;
126- this . usageTracker = usageTracker ;
127- }
128-
129- public bool CanExecute ( object parameter ) => true ;
130-
131- public void Execute ( object parameter )
132- {
133- try
134- {
135- object customIn = null ;
136- object customOut = null ;
137- dte ? . Commands . Raise ( guid , id , ref customIn , ref customOut ) ;
138- }
139- catch ( Exception e )
140- {
141- log . Error ( e , "Couldn't raise {Guid}:{ID}" , guid , id ) ;
142- }
143-
144- usageTracker . IncrementCounter ( x => x . NumberOfShowCurrentPullRequest ) . Forget ( ) ;
145- }
146-
147- public event EventHandler CanExecuteChanged
148- {
149- add { }
150- remove { }
151- }
152- }
153113 }
154114}
0 commit comments