-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShellViewModel.cs
376 lines (325 loc) · 11.3 KB
/
ShellViewModel.cs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Bootstrapper.Models;
using Bootstrapper.Models.Util;
using Bootstrapper.ViewModels.Util;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using WixToolset.Mba.Core;
namespace Bootstrapper.ViewModels
{
internal class ShellViewModel : ViewModelBase
{
private readonly Model _model;
private readonly CancelViewModel _cancelVm;
private readonly IDelegateCommand _installCommand;
private readonly IDelegateCommand _updateCommand;
private readonly IDelegateCommand _uninstallCommand;
private bool _isRepairAvailable;
private IDelegateCommand _executeCommand;
private string _executeDescription;
private string _message;
public ShellViewModel(Model model)
{
_model = model;
_cancelVm = new CancelViewModel(model);
_installCommand = new DelegateCommand(Install, CanInstall);
_uninstallCommand = new DelegateCommand(Uninstall, CanUninstall);
_updateCommand = new DelegateCommand(Update, CanUpdate);
RepairCommand = new DelegateCommand(Repair, CanRepair);
ExitCommand = new DelegateCommand(Exit, CanExit);
ShowLogCommand = new DelegateCommand(ShowLog, CanShowLog);
ConfigVm = new ConfigViewModel(model);
ConfigVm.PropertyChanged += ConfigVm_PropertyChanged;
ProgressVm = new ProgressViewModel();
}
public ConfigViewModel ConfigVm { get; }
public ProgressViewModel ProgressVm { get; }
public IDelegateCommand ShowLogCommand { get; }
public IDelegateCommand ExitCommand { get; }
public IDelegateCommand RepairCommand { get; }
public IDelegateCommand CancelCommand => _cancelVm.CancelCommand;
/// <summary>
/// Is installer waiting for user input?
/// </summary>
public bool IsWaiting => _model.State.BaStatus == BaStatus.Waiting;
/// <summary>
/// Is the UI running in passive mode, only displaying a progress bar?
/// </summary>
public bool IsPassive => _model.State.Display == Display.Passive;
/// <summary>
/// The command that will install or uninstall the software
/// </summary>
public IDelegateCommand ExecuteCommand
{
get => _executeCommand;
set
{
if (_executeCommand == value)
return;
_executeCommand = value;
OnPropertyChanged();
}
}
/// <summary>
/// A brief, one-word description of what will happen when the <see cref="ExecuteCommand" /> is run.
/// Should be appropriate for button text.
/// </summary>
public string ExecuteDescription
{
get => _executeDescription;
set
{
if (_executeDescription == value)
return;
_executeDescription = value;
OnPropertyChanged();
}
}
/// <summary>
/// Display the Repair button?
/// </summary>
public bool IsRepairAvailable
{
get => _isRepairAvailable;
set
{
if (_isRepairAvailable == value)
return;
_isRepairAvailable = value;
OnPropertyChanged();
}
}
/// <summary>
/// A message to display to the user.
/// </summary>
public string Message
{
get => _message;
set
{
if (_message == value)
return;
_message = value;
OnPropertyChanged();
}
}
/// <summary>
/// Call after the detect phase completes to refresh the UI.
/// </summary>
/// <param name="followupAction">
/// Indicates which action will be planned when the BA is running silently or in passive mode.
/// Pass <see cref="LaunchAction.Unknown" /> for full UI mode.
/// </param>
public void AfterDetect(LaunchAction followupAction)
{
try
{
OnPropertyChanged(nameof(IsWaiting));
if (_model.State.RelatedBundleStatus == BundleStatus.OlderInstalled || followupAction == LaunchAction.UpdateReplace || followupAction == LaunchAction.UpdateReplaceEmbedded)
{
ExecuteCommand = _updateCommand;
ExecuteDescription = "Update";
}
else if (_model.State.RelatedBundleStatus == BundleStatus.Current || followupAction == LaunchAction.Uninstall || followupAction == LaunchAction.UnsafeUninstall)
{
ExecuteCommand = _uninstallCommand;
ExecuteDescription = "Uninstall";
}
else
{
ExecuteCommand = _installCommand;
ExecuteDescription = "Install";
}
IsRepairAvailable = _model.State.RelatedBundleStatus == BundleStatus.Current;
AssignMessage();
ConfigVm.AfterDetect();
}
catch (Exception ex)
{
_model.Log.Write(ex);
Message = $"Error: {ex.Message}";
_model.UiFacade.ShowMessageBox($"Error: {ex.Message}", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
}
finally
{
CommandManager.InvalidateRequerySuggested();
}
}
/// <summary>
/// Call after the apply phase completes to refresh the UI.
/// </summary>
public void AfterApply()
{
try
{
ProgressVm.Reset();
AssignMessage();
}
catch (Exception ex)
{
_model.Log.Write(ex);
Message = $"Error: {ex.Message}";
_model.UiFacade.ShowMessageBox($"Error: {ex.Message}", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
}
finally
{
CommandManager.InvalidateRequerySuggested();
}
}
private void Install()
{
_model.PlanAndApply(LaunchAction.Install);
OnPropertyChanged(nameof(IsWaiting));
CommandManager.InvalidateRequerySuggested();
}
private bool CanInstall()
{
return _model.State.RelatedBundleStatus == BundleStatus.NotInstalled && CanPlanAndApply();
}
private void Update()
{
// Any older bundles that were discovered have already been scheduled for uninstall, so an "upgrade" will be a fresh installation.
_model.PlanAndApply(LaunchAction.Install);
OnPropertyChanged(nameof(IsWaiting));
CommandManager.InvalidateRequerySuggested();
}
private bool CanUpdate()
{
return _model.State.RelatedBundleStatus == BundleStatus.OlderInstalled && CanPlanAndApply();
}
private void Uninstall()
{
_model.PlanAndApply(LaunchAction.Uninstall);
OnPropertyChanged(nameof(IsWaiting));
CommandManager.InvalidateRequerySuggested();
}
private bool CanUninstall()
{
return _model.State.RelatedBundleStatus == BundleStatus.Current && CanPlanAndApply();
}
private void Repair()
{
_model.PlanAndApply(LaunchAction.Repair);
OnPropertyChanged(nameof(IsWaiting));
CommandManager.InvalidateRequerySuggested();
}
private bool CanRepair()
{
return _model.State.RelatedBundleStatus == BundleStatus.Current && CanPlanAndApply();
}
private void Exit()
{
_model.UiFacade.ShutDown();
}
private bool CanExit()
{
return _model.State.BaStatus == BaStatus.Failed || _model.State.BaStatus == BaStatus.Cancelled || _model.State.BaStatus == BaStatus.Applied || _model.State.BaStatus == BaStatus.Waiting;
;
}
private void ShowLog()
{
_model.ShowLog();
}
private bool CanShowLog()
{
return _model.State.BaStatus == BaStatus.Failed || _model.State.BaStatus == BaStatus.Cancelled || _model.State.BaStatus == BaStatus.Applied || _model.State.BaStatus == BaStatus.Waiting;
;
}
private void ConfigVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
RepairCommand.RaiseCanExecuteChanged();
ExecuteCommand?.RaiseCanExecuteChanged();
}
private bool CanPlanAndApply()
{
// Ensure ConfigVm is not displaying any data validation errors.
return _model.State.BaStatus == BaStatus.Waiting && string.IsNullOrEmpty(ConfigVm.Error);
}
/// <summary>
/// Will assign a value to <see cref="Message" /> based on the current state.
/// This should be called after Detect, and again after Apply.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private void AssignMessage()
{
switch (_model.State.BaStatus)
{
case BaStatus.Cancelled:
Message = "User cancelled";
break;
case BaStatus.Failed:
if (!string.IsNullOrWhiteSpace(_model.State.ErrorMessage))
Message = $"Failed: {_model.State.ErrorMessage}";
else if (_model.State.CancelRequested)
Message = "User cancelled";
else
Message = "An error occurred. See log for details.";
break;
case BaStatus.Planning:
case BaStatus.Applying:
case BaStatus.Waiting:
// BA will be in one of these states after successfully completing the detect phase.
if (string.IsNullOrEmpty(_model.State.RelatedBundleVersion))
Message = $"Installing v{_model.State.BundleVersion}";
else
{
switch (_model.State.RelatedBundleStatus)
{
case BundleStatus.Unknown:
case BundleStatus.NotInstalled:
Message = $"Installing v{_model.State.BundleVersion}";
break;
case BundleStatus.OlderInstalled:
Message = $"Updating v{_model.State.RelatedBundleVersion} to {_model.State.BundleVersion}";
break;
case BundleStatus.Current:
Message = $"v{_model.State.BundleVersion} is currently installed";
break;
case BundleStatus.NewerInstalled:
Message = $"There is already a newer version (v{_model.State.RelatedBundleVersion}) installed on this machine.";
break;
default:
throw new ArgumentOutOfRangeException(nameof(_model.State.RelatedBundleStatus));
}
}
break;
case BaStatus.Applied:
switch (_model.State.PlannedAction)
{
case LaunchAction.Layout:
Message = $"v{_model.State.BundleVersion} successfully laid out";
break;
case LaunchAction.UnsafeUninstall:
case LaunchAction.Uninstall:
Message = $"v{_model.State.BundleVersion} successfully removed";
break;
case LaunchAction.Modify:
Message = $"v{_model.State.BundleVersion} successfully modified";
break;
case LaunchAction.Repair:
Message = $"v{_model.State.BundleVersion} successfully repaired";
break;
case LaunchAction.UpdateReplace:
case LaunchAction.UpdateReplaceEmbedded:
Message = $"v{_model.State.RelatedBundleVersion} successfully updated to {_model.State.BundleVersion}";
break;
case LaunchAction.Unknown:
case LaunchAction.Help:
case LaunchAction.Cache:
case LaunchAction.Install:
default:
Message = $"v{_model.State.BundleVersion} successfully installed";
break;
}
break;
case BaStatus.Initializing:
case BaStatus.Detecting:
// No reason to display a message
break;
default:
throw new ArgumentOutOfRangeException(nameof(_model.State.BaStatus));
}
}
}
}