11using System ;
22using System . Windows ;
3+ using System . Windows . Input ;
34using System . Windows . Controls ;
45using System . Windows . Controls . Primitives ;
56using System . ComponentModel . Composition ;
7+ using System . Reactive . Linq ;
8+ using System . Linq . Expressions ;
69using GitHub . Commands ;
710using GitHub . InlineReviews . Views ;
811using GitHub . InlineReviews . ViewModels ;
912using GitHub . Services ;
1013using GitHub . Models ;
1114using GitHub . Logging ;
15+ using GitHub . Extensions ;
1216using Serilog ;
1317using ReactiveUI ;
1418
@@ -23,19 +27,31 @@ public class PullRequestStatusBarManager
2327 static readonly ILogger log = LogManager . ForContext < PullRequestStatusBarManager > ( ) ;
2428 const string StatusBarPartName = "PART_SccStatusBarHost" ;
2529
26- readonly IShowCurrentPullRequestCommand showCurrentPullRequestCommand ;
30+ readonly ICommand openPullRequestsCommand ;
31+ readonly ICommand showCurrentPullRequestCommand ;
2732
28- // At the moment this must be constructed on the main thread.
33+ // At the moment these must be constructed on the main thread.
2934 // TeamExplorerContext needs to retrieve DTE using GetService.
3035 readonly Lazy < IPullRequestSessionManager > pullRequestSessionManager ;
36+ readonly Lazy < ITeamExplorerContext > teamExplorerContext ;
37+
38+ IDisposable currentSessionSubscription ;
3139
3240 [ ImportingConstructor ]
3341 public PullRequestStatusBarManager (
42+ IUsageTracker usageTracker ,
43+ IOpenPullRequestsCommand openPullRequestsCommand ,
3444 IShowCurrentPullRequestCommand showCurrentPullRequestCommand ,
35- Lazy < IPullRequestSessionManager > pullRequestSessionManager )
45+ Lazy < IPullRequestSessionManager > pullRequestSessionManager ,
46+ Lazy < ITeamExplorerContext > teamExplorerContext )
3647 {
37- this . showCurrentPullRequestCommand = showCurrentPullRequestCommand ;
48+ this . openPullRequestsCommand = new UsageTrackingCommand ( openPullRequestsCommand ,
49+ usageTracker , x => x . NumberOfStatusBarOpenPullRequestList ) ;
50+ this . showCurrentPullRequestCommand = new UsageTrackingCommand ( showCurrentPullRequestCommand ,
51+ usageTracker , x => x . NumberOfShowCurrentPullRequest ) ;
52+
3853 this . pullRequestSessionManager = pullRequestSessionManager ;
54+ this . teamExplorerContext = teamExplorerContext ;
3955 }
4056
4157 /// <summary>
@@ -48,27 +64,44 @@ public void StartShowingStatus()
4864 {
4965 try
5066 {
51- pullRequestSessionManager . Value . WhenAnyValue ( x => x . CurrentSession )
52- . Subscribe ( x => RefreshCurrentSession ( ) ) ;
67+ teamExplorerContext . Value . WhenAnyValue ( x => x . ActiveRepository )
68+ . ObserveOn ( RxApp . MainThreadScheduler )
69+ . Subscribe ( x => RefreshActiveRepository ( x ) ) ;
5370 }
5471 catch ( Exception e )
5572 {
5673 log . Error ( e , "Error initializing" ) ;
5774 }
5875 }
5976
60- void RefreshCurrentSession ( )
77+ void RefreshActiveRepository ( ILocalRepositoryModel repository )
78+ {
79+ currentSessionSubscription ? . Dispose ( ) ;
80+ currentSessionSubscription = pullRequestSessionManager . Value . WhenAnyValue ( x => x . CurrentSession )
81+ . Subscribe ( x => RefreshCurrentSession ( repository , x ) ) ;
82+ }
83+
84+ void RefreshCurrentSession ( ILocalRepositoryModel repository , IPullRequestSession session )
6185 {
62- var pullRequest = pullRequestSessionManager . Value . CurrentSession ? . PullRequest ;
63- var viewModel = pullRequest != null ? CreatePullRequestStatusViewModel ( pullRequest ) : null ;
64- ShowStatus ( viewModel ) ;
86+ var cloneUrl = repository ? . CloneUrl ;
87+ if ( cloneUrl != null )
88+ {
89+ // Only show PR status bar if repo has remote
90+ var viewModel = CreatePullRequestStatusViewModel ( session ) ;
91+ ShowStatus ( viewModel ) ;
92+ }
93+ else
94+ {
95+ ShowStatus ( null ) ;
96+ }
6597 }
6698
67- PullRequestStatusViewModel CreatePullRequestStatusViewModel ( IPullRequestModel pullRequest )
99+ PullRequestStatusViewModel CreatePullRequestStatusViewModel ( IPullRequestSession session )
68100 {
69- var pullRequestStatusViewModel = new PullRequestStatusViewModel ( showCurrentPullRequestCommand ) ;
70- pullRequestStatusViewModel . Number = pullRequest . Number ;
71- pullRequestStatusViewModel . Title = pullRequest . Title ;
101+ var pullRequestStatusViewModel = new PullRequestStatusViewModel ( openPullRequestsCommand , showCurrentPullRequestCommand ) ;
102+ var pullRequest = session ? . PullRequest ;
103+ pullRequestStatusViewModel . Number = pullRequest ? . Number ;
104+ pullRequestStatusViewModel . Title = pullRequest ? . Title ;
72105 return pullRequestStatusViewModel ;
73106 }
74107
@@ -110,5 +143,37 @@ StatusBar FindSccStatusBar(Window mainWindow)
110143 var contentControl = mainWindow ? . Template ? . FindName ( StatusBarPartName , mainWindow ) as ContentControl ;
111144 return contentControl ? . Content as StatusBar ;
112145 }
146+
147+ class UsageTrackingCommand : ICommand
148+ {
149+ readonly ICommand command ;
150+ readonly IUsageTracker usageTracker ;
151+ readonly Expression < Func < UsageModel . MeasuresModel , int > > counter ;
152+
153+ internal UsageTrackingCommand ( ICommand command , 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 . IncrementCounter ( counter ) . Forget ( ) ;
176+ }
177+ }
113178 }
114179}
0 commit comments