-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathShellViewModel.cs
More file actions
197 lines (159 loc) · 6.93 KB
/
Copy pathShellViewModel.cs
File metadata and controls
197 lines (159 loc) · 6.93 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
namespace ServiceControl.Config.UI.Shell
{
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Caliburn.Micro;
using Commands;
using Events;
using Framework;
using Framework.Rx;
using ListInstances;
using NoInstances;
using NuGet.Versioning;
using ServiceControlInstaller.Engine.Instances;
class ShellViewModel : RxConductor<RxScreen>.OneActive, IHandle<PostRefreshInstances>, IHandle<ResetInstances>
{
public ShellViewModel(
NoInstancesViewModel noInstances,
ListInstancesViewModel listInstances,
AddServiceControlInstanceCommand addInstance,
AddMonitoringInstanceCommand addMonitoringInstance,
LicenseStatusManager licenseStatusManager,
IEventAggregator eventAggregator
)
{
this.listInstances = listInstances;
this.noInstances = noInstances;
this.addInstance = addInstance;
this.addMonitoringInstance = addMonitoringInstance;
OpenUrl = new OpenURLCommand();
LicenseStatusManager = licenseStatusManager;
DisplayName = "ServiceControl Config";
IsModal = false;
LoadAppVersion();
#pragma warning disable RS0030 // Do not use banned apis: UtcNow for copyright is acceptable
CopyrightInfo = $"{DateTime.UtcNow.Year} © Particular Software";
#pragma warning restore RS0030
RefreshInstancesCmd = Command.Create(async () =>
{
await eventAggregator.PublishOnUIThreadAsync(new RefreshInstances());
// Used to "blink" the refresh button to indicate the refresh actually ran.
await Task.Delay(500);
});
}
public async Task LaunchServiceControlAdd(bool installError, bool installAudit, bool installServicePulse, CancellationToken cancellationToken = default)
{
ShowingMenuOverlay = false;
await addInstance.ExecuteWithOptions(installError, installAudit, installServicePulse, cancellationToken);
}
public void LaunchMonitoringAdd()
{
ShowingMenuOverlay = false;
addMonitoringInstance.Execute(null);
}
public object ActiveContext { get; set; }
public bool IsModal { get; set; }
public bool ShowOverlay => Overlay != null;
public bool ShowRefresh => !ShowOverlay && !IsModal;
public bool ShowingMenuOverlay { get; set; }
public RxScreen Overlay { get; set; }
public SemanticVersion AppVersion { get; private set; }
public string CopyrightInfo { get; }
public bool HasInstances { get; private set; }
public ICommand OpenUrl { get; private set; }
public ICommand OpenFeedBack { get; set; }
public ICommand RefreshInstancesCmd { get; }
public LicenseStatusManager LicenseStatusManager { get; private set; }
public bool UpdateAvailable { get; set; }
public string UpdateAvailableText { get; set; }
public string AvailableUpgradeReleaseLink { get; set; }
public Task HandleAsync(PostRefreshInstances message, CancellationToken cancellationToken = default) => RefreshInstances(cancellationToken);
public Task HandleAsync(ResetInstances message, CancellationToken cancellationToken = default) => RefreshInstances(cancellationToken);
protected override Task OnInitialize(CancellationToken cancellationToken = default) => RefreshInstances(cancellationToken);
protected override async Task OnActivate(CancellationToken cancellationToken = default)
{
await base.OnActivate(cancellationToken);
BeginCheckForUpdates();
}
protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
// First run: nothing is installed yet, so open the "choose a setup scenario"
// popup immediately instead of leaving the user on the empty instance list.
// OnViewLoaded fires once, so alt-tabbing back or hitting refresh (both of
// which re-run RefreshInstances) won't re-trigger it, and dismissing the
// popup leaves the user on the NoInstances view.
if (!HasInstances)
{
ShowingMenuOverlay = true;
}
}
public async Task RefreshInstances(CancellationToken cancellationToken = default)
{
HasInstances = InstanceFinder.AllInstances().Any();
if (ActiveItem == null || ActiveItem == listInstances || ActiveItem == noInstances)
{
if (HasInstances)
{
await ActivateItem(listInstances, cancellationToken);
}
else
{
await ActivateItem(noInstances, cancellationToken);
}
}
}
void LoadAppVersion() => AppVersion = Constants.CurrentVersion;
void BeginCheckForUpdates()
{
if (updateCheckTask is not null)
{
return;
}
updateCheckTask = CheckForUpdates(CancellationToken.None);
NotifyOfPropertyChange(nameof(IsCheckingForUpdate));
}
async Task CheckForUpdates(CancellationToken cancellationToken)
{
try
{
var availableUpgradeRelease = await VersionCheckerHelper.GetLatestRelease(AppVersion, cancellationToken);
if (availableUpgradeRelease.Version == AppVersion)
{
UpdateAvailable = false;
}
else
{
AvailableUpgradeReleaseLink = availableUpgradeRelease.Assets.FirstOrDefault()?.Download.ToString();
UpdateAvailableText = $"v{availableUpgradeRelease.Version} - Update Available";
UpdateAvailable = true;
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch
{
UpdateAvailable = false;
}
finally
{
updateCheckTask = null;
NotifyOfPropertyChange(nameof(UpdateAvailable));
NotifyOfPropertyChange(nameof(UpdateAvailableText));
NotifyOfPropertyChange(nameof(AvailableUpgradeReleaseLink));
NotifyOfPropertyChange(nameof(IsCheckingForUpdate));
}
}
public bool IsCheckingForUpdate => updateCheckTask is not null;
Task updateCheckTask;
readonly ListInstancesViewModel listInstances;
readonly NoInstancesViewModel noInstances;
readonly AddServiceControlInstanceCommand addInstance;
readonly ICommand addMonitoringInstance;
}
}