1111using System . Linq ;
1212using System . Threading ;
1313using System . Globalization ;
14+ using GitHub . Models ;
1415
1516namespace GitHub . VisualStudio . Base
1617{
1718 [ Export ( typeof ( ITeamExplorerServiceHolder ) ) ]
1819 [ PartCreationPolicy ( CreationPolicy . Shared ) ]
1920 public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
2021 {
21- readonly Dictionary < object , Action < IGitRepositoryInfo > > activeRepoHandlers = new Dictionary < object , Action < IGitRepositoryInfo > > ( ) ;
22- IGitRepositoryInfo activeRepo ;
22+ readonly Dictionary < object , Action < ISimpleRepositoryModel > > activeRepoHandlers = new Dictionary < object , Action < ISimpleRepositoryModel > > ( ) ;
23+ ISimpleRepositoryModel activeRepo ;
2324 bool activeRepoNotified = false ;
2425
2526 IServiceProvider serviceProvider ;
@@ -48,35 +49,50 @@ public IServiceProvider ServiceProvider
4849 if ( serviceProvider == null )
4950 return ;
5051 GitUIContext = GitUIContext ?? UIContext . FromUIContextGuid ( new Guid ( "11B8E6D7-C08B-4385-B321-321078CDD1F8" ) ) ;
51- UIContextChanged ( GitUIContext ? . IsActive ?? false ) ;
52+ UIContextChanged ( GitUIContext ? . IsActive ?? false , false ) ;
5253 }
5354 }
5455
5556 [ AllowNull ]
56- public IGitRepositoryInfo ActiveRepo
57+ public ISimpleRepositoryModel ActiveRepo
5758 {
5859 [ return : AllowNull ] get { return activeRepo ; }
5960 private set
6061 {
61- if ( activeRepo . Compare ( value ) )
62+ if ( activeRepo == value )
6263 return ;
64+ if ( activeRepo != null )
65+ activeRepo . PropertyChanged -= ActiveRepoPropertyChanged ;
6366 activeRepo = value ;
67+ if ( activeRepo != null )
68+ activeRepo . PropertyChanged += ActiveRepoPropertyChanged ;
6469 NotifyActiveRepo ( ) ;
6570 }
6671 }
6772
68- public void Subscribe ( object who , Action < IGitRepositoryInfo > handler )
73+ public void Subscribe ( object who , Action < ISimpleRepositoryModel > handler )
6974 {
75+ bool notificationsExist ;
76+ ISimpleRepositoryModel repo ;
7077 lock ( activeRepoHandlers )
7178 {
72- var repo = ActiveRepo ;
79+ repo = ActiveRepo ;
80+ notificationsExist = activeRepoNotified ;
7381 if ( ! activeRepoHandlers . ContainsKey ( who ) )
7482 activeRepoHandlers . Add ( who , handler ) ;
7583 else
7684 activeRepoHandlers [ who ] = handler ;
77- if ( activeRepoNotified )
78- handler ( repo ) ;
7985 }
86+
87+ // the repo url might have changed and we don't get notifications
88+ // for that, so this is a good place to refresh it in case that happened
89+ repo ? . Refresh ( ) ;
90+
91+ // if the active repo hasn't changed and there's notifications queued up,
92+ // notify the subscriber. If the repo has changed, the set above will trigger
93+ // notifications so we don't have to do it here.
94+ if ( repo == ActiveRepo && notificationsExist )
95+ handler ( repo ) ;
8096 }
8197
8298 public void Unsubscribe ( object who )
@@ -100,6 +116,12 @@ public void ClearServiceProvider(IServiceProvider provider)
100116 ServiceProvider = null ;
101117 }
102118
119+ public void Refresh ( )
120+ {
121+ GitUIContext = GitUIContext ?? UIContext . FromUIContextGuid ( new Guid ( "11B8E6D7-C08B-4385-B321-321078CDD1F8" ) ) ;
122+ UIContextChanged ( GitUIContext ? . IsActive ?? false , true ) ;
123+ }
124+
103125 void NotifyActiveRepo ( )
104126 {
105127 lock ( activeRepoHandlers )
@@ -113,10 +135,10 @@ void NotifyActiveRepo()
113135 void UIContextChanged ( object sender , UIContextChangedEventArgs e )
114136 {
115137 ActiveRepo = null ;
116- UIContextChanged ( e . Activated ) ;
138+ UIContextChanged ( e . Activated , false ) ;
117139 }
118140
119- async void UIContextChanged ( bool active )
141+ async void UIContextChanged ( bool active , bool refresh )
120142 {
121143 Debug . Assert ( ServiceProvider != null , "UIContextChanged called before service provider is set" ) ;
122144 if ( ServiceProvider == null )
@@ -125,7 +147,7 @@ async void UIContextChanged(bool active)
125147 if ( active )
126148 {
127149 GitService = GitService ?? ServiceProvider . GetService < IGitExt > ( ) ;
128- if ( ActiveRepo == null )
150+ if ( ActiveRepo == null || refresh )
129151 ActiveRepo = await System . Threading . Tasks . Task . Run ( ( ) =>
130152 {
131153 var repos = GitService ? . ActiveRepositories ;
@@ -140,7 +162,7 @@ async void UIContextChanged(bool active)
140162 if ( repos == null )
141163 VsOutputLogger . WriteLine ( string . Format ( CultureInfo . CurrentCulture , "Error 2002: ActiveRepositories is null. GitService: '{0}'" , GitService ) ) ;
142164 }
143- return repos ? . FirstOrDefault ( ) ;
165+ return repos ? . FirstOrDefault ( ) ? . ToModel ( ) ;
144166 } ) ;
145167 }
146168 else
@@ -156,11 +178,16 @@ void CheckAndUpdate(object sender, System.ComponentModel.PropertyChangedEventArg
156178 if ( service == null )
157179 return ;
158180
159- var repo = service . ActiveRepositories . FirstOrDefault ( ) ;
160- // this comparison is safe, the extension method supports null instances
161- if ( ! repo . Compare ( ActiveRepo ) )
181+ var repo = service . ActiveRepositories . FirstOrDefault ( ) ? . ToModel ( ) ;
182+ if ( repo != ActiveRepo )
162183 // so annoying that this is on the wrong thread
163- syncContext . Post ( r => ActiveRepo = r as IGitRepositoryInfo , repo ) ;
184+ syncContext . Post ( r => ActiveRepo = r as ISimpleRepositoryModel , repo ) ;
185+ }
186+
187+ void ActiveRepoPropertyChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
188+ {
189+ if ( e . PropertyName == "CloneUrl" )
190+ ActiveRepo = sender as ISimpleRepositoryModel ;
164191 }
165192
166193 public IGitAwareItem HomeSection
0 commit comments