bovender framework
C# framework that implements MVVM and more
UpdaterViewModel.cs
1 /* UpdaterViewModel.cs
2  * part of Bovender framework
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using Bovender.Mvvm;
24 using Bovender.Mvvm.Messaging;
25 using System.Diagnostics;
26 
27 namespace Bovender.Versioning
28 {
30  {
31  #region Public properties
32 
33  public double DownloadMegaBytesReceived
34  {
35  get
36  {
37  return Updater.DownloadBytesReceived / 1000000;
38  }
39  }
40 
41  public double DownloadMegaBytesTotal
42  {
43  get
44  {
45  return Updater.DownloadBytesTotal / 1000000;
46  }
47  }
48 
49  public SemanticVersion CurrentVersion { get { return Updater.CurrentVersion; } }
50 
51  public SemanticVersion NewVersion { get { return Updater.ReleaseInfo.ReleaseVersion; } }
52 
53  public string DownloadFolder { get; set; }
54 
55  public string Summary { get { return Updater.ReleaseInfo.Summary; } }
56 
57  #endregion
58 
59  #region MVVM commands
60 
61  public DelegatingCommand DownloadCommand
62  {
63  get
64  {
65  if (_downloadCommand == null)
66  {
67  _downloadCommand = new DelegatingCommand(
68  param => StartProcess(),
69  param => CanStartProcess());
70  }
71  return _downloadCommand;
72  }
73  }
74 
75  public DelegatingCommand ChooseFolderCommand
76  {
77  get
78  {
79  if (_chooseFolderCommand == null)
80  {
81  _chooseFolderCommand = new DelegatingCommand(ChooseFolder);
82  }
83  return _chooseFolderCommand;
84  }
85  }
86 
87  public DelegatingCommand InstallCommand
88  {
89  get
90  {
91  if (_installCommand == null)
92  {
93  _installCommand = new DelegatingCommand(Install);
94  }
95  return _installCommand;
96  }
97  }
98 
99  #endregion
100 
101  #region MVVM messages
102 
103  public Message<FileNameMessageContent> ChooseFolderMessage
104  {
105  get
106  {
107  if (_chooseFolderMessage == null)
108  {
109  _chooseFolderMessage = new Message<FileNameMessageContent>();
110  }
111  return _chooseFolderMessage;
112  }
113  }
114 
115  public Message<ViewModelMessageContent> DownloadFinishedMessage
116  {
117  get
118  {
119  if (_downloadFinishedMessage == null)
120  {
121  _downloadFinishedMessage = new Message<ViewModelMessageContent>();
122  }
123  return _downloadFinishedMessage;
124  }
125  }
126 
127  public Message<ViewModelMessageContent> DownloadFailedMessage
128  {
129  get
130  {
131  if (_downloadFailedMessage == null)
132  {
133  _downloadFailedMessage = new Message<ViewModelMessageContent>();
134  }
135  return _downloadFailedMessage;
136  }
137  }
138 
139  #endregion
140 
141  #region Constructors
142 
143  public UpdaterViewModel(Updater updater)
144  : base(updater)
145  { }
146 
147  #endregion
148 
149  #region Implementation of ProcessViewModelBase
150 
151  protected override void UpdateProcessMessageContent(ProcessMessageContent processMessageContent)
152  {
154  processMessageContent.PercentCompleted = Updater.PercentDownloaded;
155  if (d != null)
156  {
157  d.DownloadMegaBytesReceived = (double)Updater.DownloadBytesReceived / 1000000;
158  d.DownloadMegaBytesTotal = (double)Updater.DownloadBytesTotal / 1000000;
159  }
160  else
161  {
162  Logger.Warn("UpdateProcessMessageContent: processMessageContent is not a DownloadProcessMessageContent!");
163  }
164  }
165 
166  #endregion
167 
168  #region Protected methods
169 
170  protected virtual void SendDownloadFinishedMessage()
171  {
172  Logger.Info("SendDownloadFinishedMessage");
173  DownloadFinishedMessage.Send(new ViewModelMessageContent(this));
174  }
175 
176  protected virtual void SendDownloadFailedMessage()
177  {
178  Logger.Info("SendDownloadFailedMessage");
179  DownloadFailedMessage.Send(new ViewModelMessageContent(this));
180  }
181 
182  protected virtual bool CanStartProcess()
183  {
184  return !IsProcessing;
185  }
186 
187  protected virtual void ChooseFolder(object param)
188  {
189  ChooseFolderMessage.Send(new FileNameMessageContent(DownloadFolder), ConfirmFolder);
190  }
191 
192  protected virtual void ConfirmFolder(FileNameMessageContent fileNameMessageContent)
193  {
194  if (fileNameMessageContent.Confirmed)
195  {
196  Logger.Info("ConfirmFolder: Confirmed, proceeding to start download");
197  Updater.DestinationFolder = fileNameMessageContent.Value;
198  StartProcess();
199  }
200  else
201  {
202  Logger.Info("ConfirmFolder: Not confirmed!");
203  }
204  }
205 
206  protected virtual void Install(object param)
207  {
208  DoCloseView();
209  Updater.Install();
210  }
211 
212  #endregion
213 
214  #region Protected properties
215 
216  protected Updater Updater
217  {
218  [DebuggerStepThrough]
219  get
220  {
221  return (Updater)ProcessModel;
222  }
223  }
224 
225  #endregion
226 
227  #region Overrides
228 
229  protected override void SendProcessFinishedMessage()
230  {
231  base.SendProcessFinishedMessage();
232  switch (Updater.Status)
233  {
234  case UpdaterStatus.Downloaded:
235  DownloadFinishedMessage.Send(ProcessMessageContent);
236  break;
237  case UpdaterStatus.DownloadFailed:
238  DownloadFailedMessage.Send(ProcessMessageContent);
239  break;
240  default:
241  break;
242  }
243  }
244 
246  {
247  get
248  {
249  if (_downloadProcessMessageContent == null)
250  {
251  _downloadProcessMessageContent = new DownloadProcessMessageContent(this, CancelProcess);
252  }
253  return _downloadProcessMessageContent;
254  }
255  }
256 
257  #endregion
258 
259  #region Fields
260 
261  private DownloadProcessMessageContent _downloadProcessMessageContent;
262  private DelegatingCommand _downloadCommand;
263  private DelegatingCommand _chooseFolderCommand;
264  private DelegatingCommand _installCommand;
265  private Message<ViewModelMessageContent> _downloadFinishedMessage;
266  private Message<ViewModelMessageContent> _downloadFailedMessage;
267  private Message<FileNameMessageContent> _chooseFolderMessage;
268 
269  #endregion
270 
271  #region Class logger
272 
273  private static NLog.Logger Logger { get { return _logger.Value; } }
274 
275  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
276 
277  #endregion
278  }
279 }
Holds information about percent completion of a process and defines events that occur when the proces...
override void UpdateProcessMessageContent(ProcessMessageContent processMessageContent)
Updates the given ProcessMessageContent with the current process.
Message content that holds a reference to a view model.
Command that implements ICommand and accepts delegates that contain the command implementation.
Abstract base class for view models that deal with processes.
string Summary
Gets or sets the release summary.
Definition: IReleaseInfo.cs:42
SemanticVersion ReleaseVersion
Gets or sets the version of the release.
Definition: IReleaseInfo.cs:37
Class that handles semantic versioning.
Fetches version information from the internet and raises an UpdateAvailable event if a new version is...
Definition: Updater.cs:41
override void SendProcessFinishedMessage()
Sends the ProcessMessageContent.CompletedMessage to signal that the process has finished.