11using System ;
2- using System . Collections . Generic ;
32using System . ComponentModel . Composition ;
4- using System . Linq ;
53using GitHub . Extensions ;
64using GitHub . Logging ;
7- using GitHub . Models ;
85using GitHub . Services ;
96using Serilog ;
107using Microsoft . TeamFoundation . Controls ;
118using Microsoft . VisualStudio . Shell ;
129using Microsoft . VisualStudio . Threading ;
13- using System . Windows ;
1410
1511namespace GitHub . VisualStudio . Base
1612{
@@ -20,20 +16,15 @@ public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
2016 {
2117 static readonly ILogger log = LogManager . ForContext < TeamExplorerServiceHolder > ( ) ;
2218
23- readonly Dictionary < object , Action < ILocalRepositoryModel > > activeRepoHandlers = new Dictionary < object , Action < ILocalRepositoryModel > > ( ) ;
24- ILocalRepositoryModel activeRepo ;
25- bool activeRepoNotified = false ;
26-
2719 IServiceProvider serviceProvider ;
28- readonly IVSGitExt gitExt ;
29- readonly IGitService gitService ;
3020
3121 /// <summary>
3222 /// This class relies on IVSGitExt that provides information when VS switches repositories.
3323 /// </summary>
3424 /// <param name="gitExt">Used for monitoring the active repository.</param>
3525 [ ImportingConstructor ]
36- TeamExplorerServiceHolder ( IVSGitExt gitExt , IGitService gitService ) : this ( gitExt , gitService , ThreadHelper . JoinableTaskContext )
26+ TeamExplorerServiceHolder ( ITeamExplorerContext teamExplorerContext ) :
27+ this ( teamExplorerContext , ThreadHelper . JoinableTaskContext )
3728 {
3829 }
3930
@@ -42,23 +33,13 @@ public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
4233 /// </summary>
4334 /// <param name="gitService">Used for monitoring the active repository.</param>
4435 /// <param name="joinableTaskContext">Used for switching to the Main thread.</param>
45- public TeamExplorerServiceHolder ( IVSGitExt gitExt , IGitService gitService , JoinableTaskContext joinableTaskContext )
36+ public TeamExplorerServiceHolder ( ITeamExplorerContext teamExplorerContext , JoinableTaskContext joinableTaskContext )
4637 {
4738 JoinableTaskCollection = joinableTaskContext . CreateCollection ( ) ;
4839 JoinableTaskCollection . DisplayName = nameof ( TeamExplorerServiceHolder ) ;
4940 JoinableTaskFactory = joinableTaskContext . CreateFactory ( JoinableTaskCollection ) ;
5041
51- // HACK: This might be null in SafeMode.
52- // We should be throwing rather than returning a null MEF service.
53- if ( gitExt == null )
54- {
55- return ;
56- }
57-
58- this . gitExt = gitExt ;
59- this . gitService = gitService ;
60- UpdateActiveRepo ( ) ;
61- gitExt . ActiveRepositoriesChanged += UpdateActiveRepo ;
42+ TeamExplorerContext = teamExplorerContext ;
6243 }
6344
6445 // set by the sections when they get initialized
@@ -76,61 +57,6 @@ public IServiceProvider ServiceProvider
7657 }
7758 }
7859
79- public ILocalRepositoryModel ActiveRepo
80- {
81- get { return activeRepo ; }
82- private set
83- {
84- if ( activeRepo == value )
85- return ;
86- if ( activeRepo != null )
87- activeRepo . PropertyChanged -= ActiveRepoPropertyChanged ;
88- activeRepo = value ;
89- if ( activeRepo != null )
90- activeRepo . PropertyChanged += ActiveRepoPropertyChanged ;
91- NotifyActiveRepo ( ) ;
92- }
93- }
94-
95- public void Subscribe ( object who , Action < ILocalRepositoryModel > handler )
96- {
97- Guard . ArgumentNotNull ( who , nameof ( who ) ) ;
98- Guard . ArgumentNotNull ( handler , nameof ( handler ) ) ;
99-
100- bool notificationsExist ;
101- ILocalRepositoryModel repo ;
102- lock ( activeRepoHandlers )
103- {
104- repo = ActiveRepo ;
105- notificationsExist = activeRepoNotified ;
106- if ( ! activeRepoHandlers . ContainsKey ( who ) )
107- activeRepoHandlers . Add ( who , handler ) ;
108- else
109- activeRepoHandlers [ who ] = handler ;
110- }
111-
112- // the repo url might have changed and we don't get notifications
113- // for that, so this is a good place to refresh it in case that happened
114- if ( repo != null )
115- {
116- gitService . RefreshCloneUrl ( repo ) ;
117- }
118-
119- // if the active repo hasn't changed and there's notifications queued up,
120- // notify the subscriber. If the repo has changed, the set above will trigger
121- // notifications so we don't have to do it here.
122- if ( repo == ActiveRepo && notificationsExist )
123- handler ( repo ) ;
124- }
125-
126- public void Unsubscribe ( object who )
127- {
128- Guard . ArgumentNotNull ( who , nameof ( who ) ) ;
129-
130- if ( activeRepoHandlers . ContainsKey ( who ) )
131- activeRepoHandlers . Remove ( who ) ;
132- }
133-
13460 /// <summary>
13561 /// Clears the current ServiceProvider if it matches the one that is passed in.
13662 /// This is usually called on Dispose, which might happen after another section
@@ -148,39 +74,6 @@ public void ClearServiceProvider(IServiceProvider provider)
14874 ServiceProvider = null ;
14975 }
15076
151- void NotifyActiveRepo ( )
152- {
153- lock ( activeRepoHandlers )
154- {
155- activeRepoNotified = true ;
156- foreach ( var handler in activeRepoHandlers . Values )
157- handler ( activeRepo ) ;
158- }
159- }
160-
161- void UpdateActiveRepo ( )
162- {
163- var repo = gitExt . ActiveRepositories . FirstOrDefault ( ) ;
164-
165- if ( ! Equals ( repo , ActiveRepo ) )
166- {
167- // Fire property change events on Main thread
168- JoinableTaskFactory . RunAsync ( async ( ) =>
169- {
170- await JoinableTaskFactory . SwitchToMainThreadAsync ( ) ;
171- ActiveRepo = repo ;
172- } ) . Task . Forget ( log ) ;
173- }
174- }
175-
176- void ActiveRepoPropertyChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
177- {
178- Guard . ArgumentNotNull ( e , nameof ( e ) ) ;
179-
180- if ( e . PropertyName == "CloneUrl" )
181- ActiveRepo = sender as ILocalRepositoryModel ;
182- }
183-
18477 public IGitAwareItem HomeSection
18578 {
18679 get
@@ -200,6 +93,7 @@ ITeamExplorerPage PageService
20093 }
20194
20295 public JoinableTaskCollection JoinableTaskCollection { get ; }
203- JoinableTaskFactory JoinableTaskFactory { get ; }
96+ public JoinableTaskFactory JoinableTaskFactory { get ; }
97+ public ITeamExplorerContext TeamExplorerContext { get ; }
20498 }
20599}
0 commit comments