bovender framework
C# framework that implements MVVM and more
ReleaseInfoViewModel.cs
1 /* ReleaseInfoViewModel.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;
23 using Bovender.Mvvm.Messaging;
24 using Bovender.Mvvm;
25 
26 namespace Bovender.Versioning
27 {
32  {
33  #region Public properties
34 
35  public SemanticVersion ReleaseVersion { get { return ReleaseInfo.ReleaseVersion; } }
36 
37  public SemanticVersion CurrentVersion { get; set; }
38 
39  public string Summary { get { return ReleaseInfo.Summary; } }
40 
41  public Uri DownloadUri { get { return ReleaseInfo.DownloadUri; } }
42 
43  public string ExpectedHash { get { return ReleaseInfo.ExpectedHash; } }
44 
45  public string RawReleaseInfo { get { return ReleaseInfo.RawReleaseInfo; } }
46 
47  public ReleaseInfoStatus Status { get { return ReleaseInfo.Status; } }
48 
49  public override Exception Exception { get { return ReleaseInfo.Exception; } }
50 
51  #endregion
52 
53  #region MVVM commands
54 
55  public DelegatingCommand CheckForUpdateCommand
56  {
57  get
58  {
59  if (_checkForUpdateCommand == null)
60  {
61  _checkForUpdateCommand = new DelegatingCommand(
62  (param) => StartProcess());
63  }
64  return _checkForUpdateCommand;
65  }
66  }
67 
68  #endregion
69 
70  #region MVVM messages
71 
72  public Message<ProcessMessageContent> UpdateAvailableMessage
73  {
74  get
75  {
76  if (_updateAvailableMessage == null)
77  {
78  _updateAvailableMessage = new Message<ProcessMessageContent>();
79  }
80  return _updateAvailableMessage;
81  }
82  }
83 
84  public Message<ProcessMessageContent> NoUpdateAvailableMessage
85  {
86  get
87  {
88  if (_noUpdateAvailableMessage == null)
89  {
90  _noUpdateAvailableMessage = new Message<ProcessMessageContent>();
91  }
92  return _noUpdateAvailableMessage;
93  }
94  }
95 
96  public Message<ProcessMessageContent> ExceptionMessage
97  {
98  get
99  {
100  if (_exceptionMessage == null)
101  {
102  _exceptionMessage = new Message<ProcessMessageContent>();
103  }
104  return _exceptionMessage;
105  }
106  }
107 
108  #endregion
109 
110  #region Constructors
111 
112  public ReleaseInfoViewModel(SemanticVersion currentVersion)
113  : this(new ReleaseInfo(), currentVersion)
114  { }
115 
116  public ReleaseInfoViewModel(ReleaseInfo releaseInfo, SemanticVersion currentVersion)
117  : base(releaseInfo)
118  {
119  CurrentVersion = currentVersion;
120  }
121 
122  #endregion
123 
124  #region Implementation and overrides of ProcessViewModelBase
125 
126  protected override void UpdateProcessMessageContent(ProcessMessageContent processMessageContent)
127  {
128  processMessageContent.IsIndeterminate = true;
129  }
130 
131  protected override void SendProcessFinishedMessage()
132  {
133  switch (Status)
134  {
135  case ReleaseInfoStatus.InfoAvailable:
136  ProcessMessageContent.WasSuccessful = true;
137  if (ReleaseVersion > CurrentVersion)
138  {
139  Logger.Info("SendProcessFinishedMessage: update available, {0} > {1}",
140  ReleaseVersion.ToString(), CurrentVersion.ToString());
141  UpdateAvailableMessage.Send(ProcessMessageContent);
142  }
143  else
144  {
145  Logger.Info("SendProcessFinishedMessage: no update available, {0} <= {1}",
146  ReleaseVersion.ToString(), CurrentVersion.ToString());
147  NoUpdateAvailableMessage.Send(ProcessMessageContent);
148  }
149  break;
150  case ReleaseInfoStatus.FailureToFetch:
151  case ReleaseInfoStatus.FailureToParse:
152  Logger.Warn("SendProcessFinishedMessage: Exception occurred!");
153  Logger.Warn(ReleaseInfo.Exception);
155  ProcessMessageContent.WasSuccessful = false;
156  ExceptionMessage.Send(ProcessMessageContent);
157  break;
158  default:
159  break;
160  }
161  base.SendProcessFinishedMessage();
162  }
163 
164  #endregion
165 
166  #region Protected properties
167 
168  protected IReleaseInfo ReleaseInfo
169  {
170  get
171  {
172  return (IReleaseInfo)ProcessModel;
173  }
174  }
175 
176  #endregion
177 
178  #region Fields
179 
180  private DelegatingCommand _checkForUpdateCommand;
181  private Message<ProcessMessageContent> _updateAvailableMessage;
182  private Message<ProcessMessageContent> _noUpdateAvailableMessage;
183  private Message<ProcessMessageContent> _exceptionMessage;
184 
185  #endregion
186 
187  #region Class logger
188 
189  private static NLog.Logger Logger { get { return _logger.Value; } }
190 
191  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
192 
193  #endregion
194  }
195 }
Holds information about percent completion of a process and defines events that occur when the proces...
Command that implements ICommand and accepts delegates that contain the command implementation.
Abstract base class for view models that deal with processes.
A view model for the IReleaseInfo interface; i.e.
Interface for classes that fetch current release information.
Definition: IReleaseInfo.cs:30
Exception Exception
If something in the process went wrong, this will be the corresponding exception. ...
override void UpdateProcessMessageContent(ProcessMessageContent processMessageContent)
Updates the given ProcessMessageContent with the current process.
Class that handles semantic versioning.
Fetches and digests release information.
Definition: ReleaseInfo.cs:32
override void SendProcessFinishedMessage()
Sends the ProcessMessageContent.CompletedMessage to signal that the process has finished.