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

Commit e0c7781

Browse files
committed
Cleanup Reload code
1 parent 13d0ac5 commit e0c7781

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/GitHub.VisualStudio/UI/Views/GitHubPaneViewModel.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace GitHub.VisualStudio.UI.Views
2727
[PartCreationPolicy(CreationPolicy.NonShared)]
2828
public class GitHubPaneViewModel : TeamExplorerItemBase, IGitHubPaneViewModel
2929
{
30+
const UIControllerFlow DefaultControllerFlow = UIControllerFlow.PullRequests;
31+
3032
bool initialized;
3133
readonly CompositeDisposable disposables = new CompositeDisposable();
3234
IUIController uiController;
@@ -53,8 +55,6 @@ public GitHubPaneViewModel(ISimpleApiClientFactory apiFactory, ITeamExplorerServ
5355
syncContext = SynchronizationContext.Current;
5456
CancelCommand = ReactiveCommand.Create();
5557
Title = "GitHub";
56-
57-
hosts.WhenAnyValue(x => x.IsLoggedInToAnyHost).Subscribe(_ => Reload().Forget());
5858
}
5959

6060
public override void Initialize(IServiceProvider serviceProvider)
@@ -89,6 +89,8 @@ public override void Initialize(IServiceProvider serviceProvider)
8989
initialized = true;
9090

9191
base.Initialize(serviceProvider);
92+
93+
hosts.WhenAnyValue(x => x.IsLoggedInToAnyHost).Subscribe(_ => Reload().Forget());
9294
}
9395

9496
public void Initialize([AllowNull] ViewWithData data)
@@ -155,28 +157,50 @@ async Task Reload([AllowNull] ViewWithData data = null, bool navigating = false)
155157
if (!IsGitHubRepo.Value)
156158
{
157159
//LoadView(UIViewType.NotAGitHubRepo);
158-
return;
159160
}
160161

161-
if (!IsLoggedIn)
162+
else if (!IsLoggedIn)
162163
{
163164
LoadView(UIViewType.LoggedOut);
164-
return;
165165
}
166166

167-
if (uiController == null || (data != null && data.ActiveFlow != uiController.SelectedFlow))
168-
StartFlow(data?.ActiveFlow ?? UIControllerFlow.PullRequests, connection, data);
169-
else if (data != null || currentNavItem >= 0)
167+
else
168+
{
169+
LoadView(data?.ActiveFlow ?? DefaultControllerFlow, connection, data);
170+
}
171+
}
172+
173+
void LoadView(UIControllerFlow flow, IConnection connection = null, ViewWithData data = null, UIViewType type = UIViewType.None)
174+
{
175+
// if we're loading a single view or a different flow, we need to stop the current controller
176+
var restart = flow == UIControllerFlow.None || uiController?.SelectedFlow != flow;
177+
178+
if (restart)
179+
Stop();
180+
181+
// if there's no selected flow, then just load a view directly
182+
if (flow == UIControllerFlow.None)
183+
{
184+
var factory = ServiceProvider.GetExportedValue<IUIFactory>();
185+
var c = factory.CreateViewAndViewModel(type);
186+
c.View.DataContext = c.ViewModel;
187+
Control = c.View;
188+
}
189+
// it's a new flow!
190+
else if (restart)
191+
{
192+
StartFlow(flow, connection, data);
193+
}
194+
// navigate to a requested view within the currently running uiController
195+
else
196+
{
170197
uiController.Jump(data ?? navStack[currentNavItem]);
198+
}
171199
}
172200

173201
void LoadView(UIViewType type)
174202
{
175-
Stop();
176-
var factory = ServiceProvider.GetExportedValue<IUIFactory>();
177-
var c = factory.CreateViewAndViewModel(type);
178-
c.View.DataContext = c.ViewModel;
179-
Control = c.View;
203+
LoadView(UIControllerFlow.None, type: type);
180204
}
181205

182206
void StartFlow(UIControllerFlow controllerFlow, [AllowNull]IConnection conn, ViewWithData data = null)

0 commit comments

Comments
 (0)