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

Commit 6c4ff1b

Browse files
committed
Refresh PR detail view when active repo changes
This commit will automatically dispose of any preview PullRequestDetailViewModel object.
1 parent e13678c commit 6c4ff1b

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ namespace GitHub.ViewModels
2626
/// </summary>
2727
[ExportViewModel(ViewType = UIViewType.PRDetail)]
2828
[PartCreationPolicy(CreationPolicy.NonShared)]
29-
public class PullRequestDetailViewModel : PanePageViewModelBase, IPullRequestDetailViewModel
29+
public class PullRequestDetailViewModel : PanePageViewModelBase, IPullRequestDetailViewModel, IDisposable
3030
{
3131
static readonly ILogger log = LogManager.ForContext<PullRequestDetailViewModel>();
3232

3333
readonly IModelService modelService;
3434
readonly IPullRequestService pullRequestsService;
3535
readonly IPullRequestSessionManager sessionManager;
3636
readonly IUsageTracker usageTracker;
37+
readonly IVSGitExt vsGitExt;
3738
IPullRequestModel model;
3839
string sourceBranchDisplayName;
3940
string targetBranchDisplayName;
@@ -65,13 +66,35 @@ public class PullRequestDetailViewModel : PanePageViewModelBase, IPullRequestDet
6566
ITeamExplorerServiceHolder teservice,
6667
IPullRequestService pullRequestsService,
6768
IPullRequestSessionManager sessionManager,
68-
IUsageTracker usageTracker)
69+
IUsageTracker usageTracker,
70+
IVSGitExt vsGitExt)
6971
: this(teservice.ActiveRepo,
7072
modelServiceFactory.CreateBlocking(connection.Get()),
7173
pullRequestsService,
7274
sessionManager,
7375
usageTracker)
7476
{
77+
this.vsGitExt = vsGitExt;
78+
79+
AutoDispose.DisposePrevious(this);
80+
vsGitExt.ActiveRepositoriesChanged += Refresh;
81+
}
82+
83+
public void Dispose()
84+
{
85+
vsGitExt.ActiveRepositoriesChanged -= Refresh;
86+
}
87+
88+
// HACK: This is a workaround for models not being automatically disposed.
89+
class AutoDispose
90+
{
91+
static IDisposable previous;
92+
93+
internal static void DisposePrevious(IDisposable disposable)
94+
{
95+
previous?.Dispose();
96+
previous = disposable;
97+
}
7598
}
7699

77100
/// <summary>
@@ -502,6 +525,18 @@ public string GetLocalFilePath(IPullRequestFileNode file)
502525
return Path.Combine(LocalRepository.LocalPath, file.DirectoryPath, file.FileName);
503526
}
504527

528+
async void Refresh()
529+
{
530+
try
531+
{
532+
await Load(RemoteRepositoryOwner, Model);
533+
}
534+
catch (Exception e)
535+
{
536+
log.Error(e, "Error refreshing model");
537+
}
538+
}
539+
505540
void SubscribeOperationError(ReactiveCommand<Unit> command)
506541
{
507542
command.ThrownExceptions.Subscribe(x => OperationError = x.Message);

0 commit comments

Comments
 (0)