This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTeamExplorerItemBase.cs
More file actions
217 lines (184 loc) · 6.42 KB
/
Copy pathTeamExplorerItemBase.cs
File metadata and controls
217 lines (184 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using GitHub.Api;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.VisualStudio.Helpers;
using GitHub.ViewModels;
using GitHub.VisualStudio.UI;
using GitHub.Extensions;
using System.ComponentModel;
namespace GitHub.VisualStudio.Base
{
public class TeamExplorerItemBase : TeamExplorerGitRepoInfo, IServiceProviderAware
{
readonly ITeamExplorerServiceHolder holder;
readonly ISimpleApiClientFactory apiFactory;
ISimpleApiClient simpleApiClient;
public ISimpleApiClient SimpleApiClient
{
get { return simpleApiClient; }
set
{
if (simpleApiClient != value && value == null)
apiFactory.ClearFromCache(simpleApiClient);
simpleApiClient = value;
}
}
public TeamExplorerItemBase(IGitHubServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory,
ITeamExplorerServiceHolder holder) : this(serviceProvider, holder)
{
Guard.ArgumentNotNull(apiFactory, nameof(apiFactory));
this.apiFactory = apiFactory;
}
public TeamExplorerItemBase(IGitHubServiceProvider serviceProvider, ITeamExplorerServiceHolder holder)
: base(serviceProvider)
{
Guard.ArgumentNotNull(holder, nameof(holder));
this.holder = holder;
}
public virtual void Initialize(IServiceProvider serviceProvider)
{
Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider));
TEServiceProvider = serviceProvider;
Debug.Assert(holder != null, "Could not get an instance of TeamExplorerServiceHolder");
if (holder == null)
return;
holder.ServiceProvider = TEServiceProvider;
SubscribeToRepoChanges();
}
public virtual void Execute()
{
}
public virtual void Invalidate()
{
}
bool subscribedToRepoChanges = false;
protected void SubscribeToRepoChanges()
{
if (!subscribedToRepoChanges)
{
subscribedToRepoChanges = true;
UpdateRepoOnMainThread(holder.TeamExplorerContext.ActiveRepository);
holder.TeamExplorerContext.PropertyChanged += TeamExplorerContext_PropertyChanged;
}
}
void TeamExplorerContext_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(holder.TeamExplorerContext.ActiveRepository))
{
UpdateRepoOnMainThread(holder.TeamExplorerContext.ActiveRepository);
}
}
void UpdateRepoOnMainThread(LocalRepositoryModel repo)
{
holder.JoinableTaskContext.Factory.RunAsync(async () =>
{
await holder.JoinableTaskContext.Factory.SwitchToMainThreadAsync();
UpdateRepo(repo);
}).Task.Forget();
}
void Unsubscribe()
{
holder.TeamExplorerContext.PropertyChanged -= TeamExplorerContext_PropertyChanged;
if (TEServiceProvider != null)
holder.ClearServiceProvider(TEServiceProvider);
}
void UpdateRepo(LocalRepositoryModel repo)
{
ActiveRepo = repo;
RepoChanged();
Invalidate();
}
protected virtual void RepoChanged()
{
var repo = ActiveRepo;
if (repo != null)
{
var uri = repo.CloneUrl;
if (uri?.RepositoryName != null)
{
ActiveRepoUri = uri;
ActiveRepoName = uri.NameWithOwner;
}
}
}
protected async Task<RepositoryOrigin> GetRepositoryOrigin(UriString uri)
{
if (uri == null)
return RepositoryOrigin.Other;
Debug.Assert(apiFactory != null, "apiFactory cannot be null. Did you call the right constructor?");
SimpleApiClient = await apiFactory.Create(uri);
var isdotcom = HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl());
if (isdotcom)
{
return RepositoryOrigin.DotCom;
}
else
{
var repo = await SimpleApiClient.GetRepository();
if ((repo.FullName == ActiveRepoName || repo.Id == 0) && await SimpleApiClient.IsEnterprise())
{
return RepositoryOrigin.Enterprise;
}
}
return RepositoryOrigin.Other;
}
protected async Task<bool> IsAGitHubRepo(UriString uri)
{
var origin = await GetRepositoryOrigin(uri);
return origin == RepositoryOrigin.DotCom || origin == RepositoryOrigin.Enterprise;
}
protected async Task<bool> IsAGitHubDotComRepo(UriString uri)
{
var origin = await GetRepositoryOrigin(uri);
return origin == RepositoryOrigin.DotCom;
}
protected async Task<bool> IsUserAuthenticated()
{
if (SimpleApiClient == null)
{
if (ActiveRepo == null)
return false;
var uri = ActiveRepoUri;
if (uri == null)
return false;
SimpleApiClient = await apiFactory.Create(uri);
}
return SimpleApiClient?.IsAuthenticated() ?? false;
}
bool disposed;
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!disposed)
{
Unsubscribe();
disposed = true;
}
}
base.Dispose(disposing);
}
bool isEnabled;
public bool IsEnabled
{
get { return isEnabled; }
set { isEnabled = value; this.RaisePropertyChange(); }
}
bool isVisible;
public bool IsVisible
{
get { return isVisible; }
set { isVisible = value; this.RaisePropertyChange(); }
}
string text;
public string Text
{
get { return text; }
set { text = value; this.RaisePropertyChange(); }
}
}
}