Skip to content

Commit

Permalink
Download button
Browse files Browse the repository at this point in the history
  • Loading branch information
Nice3point committed Jun 12, 2022
1 parent 7e62b50 commit fd19ed3
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 51 deletions.
80 changes: 80 additions & 0 deletions RevitLookup.UI.Tests/ViewModels/Converters/UpdateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,86 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

[ValueConversion(typeof(UpdatingState), typeof(Visibility))]
public class UpToDateVisibilityConverter : MarkupExtension, IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var state = (UpdatingState) values[0];
var isCheckedUpdates = (bool) values[1];
return state == UpdatingState.UpToDate && isCheckedUpdates ? Visibility.Visible : Visibility.Collapsed;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

[ValueConversion(typeof(UpdatingState), typeof(Visibility))]
public class UpdateAvailableCardVisibilityConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var state = (UpdatingState) value!;
return state is UpdatingState.ReadyToDownload or UpdatingState.ErrorDownloading ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

[ValueConversion(typeof(UpdatingState), typeof(Visibility))]
public class ErrorCardVisibilityConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var state = (UpdatingState) value!;
return state is UpdatingState.ErrorChecking or UpdatingState.ErrorDownloading ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
[ValueConversion(typeof(UpdatingState), typeof(Visibility))]
public class DownloadProgressVisibilityConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isDownloading = (bool) value!;
return isDownloading ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
Expand Down
19 changes: 19 additions & 0 deletions RevitLookup.UI.Tests/ViewModels/Converters/ValueConterters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is not null && !(bool) value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
Expand Down
158 changes: 115 additions & 43 deletions RevitLookup.UI.Tests/ViewModels/Pages/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Newtonsoft.Json;
Expand All @@ -32,53 +33,20 @@ namespace RevitLookup.UI.Tests.ViewModels.Pages;

public sealed class AboutViewModel : INotifyPropertyChanged
{
private string _latestCheckDate;
private string _version;
private UpdatingState _state;
private string _errorMessage;
private bool _isCheckedUpdates;
private string _latestCheckDate = "never";
private string _newVersion;
private string _releaseNotesUrl;
private UpdatingState _state;
private string _version;
private bool _isDownloading;

public AboutViewModel()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var info = FileVersionInfo.GetVersionInfo(assembly.Location);
Version = info.ProductVersion;
LatestCheckDate = $"Latest check: {DateTime.Now:yyyy.MM.dd HH:mm:ss}";
}

public RelayCommand CheckUpdatesCommand => new(CheckUpdates);

private async void CheckUpdates()
{
string releasesJson;
using (var gitHubClient = new HttpClient())
{
gitHubClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "RevitLookup");
releasesJson = await gitHubClient.GetStringAsync("https://api.github.com/repos/jeremytammik/RevitLookup/releases");
}

var releases = JsonConvert.DeserializeObject<List<GutHubApiDto>>(releasesJson);
if (releases is null)
{
State = UpdatingState.ErrorChecking;
return;
}

var latestRelease = releases.OrderByDescending(release => release.PublishedDate).First();
var newVersion = new Version(latestRelease.TagName);
if (newVersion > new Version(Version))
{
State = UpdatingState.ReadyToDownload;
NewVersion = newVersion.ToString(3);
DownloadUrl = latestRelease.Assets[0].DownloadUrl;
}
else if (newVersion == new Version(Version))
{
State = UpdatingState.UpToDate;
}

LatestCheckDate = $"Latest check: {DateTime.Now:yyyy.MM.dd HH:mm:ss}";
ReleaseNotesUrl = latestRelease.Url;
}

public UpdatingState State
Expand All @@ -105,7 +73,7 @@ public string Version

public string LatestCheckDate
{
get => _latestCheckDate;
get => $"Latest check: {_latestCheckDate}";
set
{
if (value == _latestCheckDate) return;
Expand All @@ -125,9 +93,6 @@ public string ReleaseNotesUrl
}
}

public string DownloadUrl { get; set; }
public string DownloadedInstallerFilename { get; set; }

public string NewVersion
{
get => _newVersion;
Expand All @@ -139,8 +104,115 @@ public string NewVersion
}
}

public string ErrorMessage
{
get => _errorMessage;
set
{
if (value == _errorMessage) return;
_errorMessage = value;
OnPropertyChanged();
}
}

[JsonIgnore]
public bool IsCheckedUpdates
{
get => _isCheckedUpdates;
set
{
if (value == _isCheckedUpdates) return;
_isCheckedUpdates = value;
OnPropertyChanged();
}
}

[JsonIgnore]
public bool IsDownloading
{
get => _isDownloading;
set
{
if (value == _isDownloading) return;
_isDownloading = value;
OnPropertyChanged();
}
}

public string DownloadUrl { get; set; }
public string DownloadedInstallerFilename { get; set; }

public RelayCommand CheckUpdatesCommand => new(CheckUpdates);
public RelayCommand DownloadCommand => new(DownloadUpdate);

public event PropertyChangedEventHandler PropertyChanged;

private async void CheckUpdates()
{
try
{
string releasesJson;
using (var gitHubClient = new HttpClient())
{
gitHubClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "RevitLookup");
releasesJson = await gitHubClient.GetStringAsync("https://api.github.com/repos/jeremytammik/RevitLookup/releases");
}

var releases = JsonConvert.DeserializeObject<List<GutHubApiDto>>(releasesJson);
if (releases is null)
{
State = UpdatingState.ErrorChecking;
ErrorMessage = "GitHub server unavailable to check for updates";
}
else
{
var latestRelease = releases.OrderByDescending(release => release.PublishedDate).First();
var newVersion = new Version(latestRelease.TagName);
if (newVersion > new Version(Version))
{
State = UpdatingState.ReadyToDownload;
NewVersion = newVersion.ToString(3);
DownloadUrl = latestRelease.Assets[0].DownloadUrl;
ReleaseNotesUrl = latestRelease.Url;
}
else
{
State = UpdatingState.UpToDate;
}
}
}
catch
{
State = UpdatingState.ErrorChecking;
ErrorMessage = "An error occurred while checking for updates";
}
finally
{
IsCheckedUpdates = true;
LatestCheckDate = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
}
}

private async void DownloadUpdate()
{
IsDownloading = true;
State = UpdatingState.ReadyToDownload;
try
{
await Task.Run(() => Thread.Sleep(TimeSpan.FromSeconds(3)));
throw new NetworkInformationException();
}
catch
{
State = UpdatingState.ErrorDownloading;
ErrorMessage = "An error occurred while downloading the update";
}
finally
{
IsDownloading = false;
}
}

[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
Expand Down
Loading

0 comments on commit fd19ed3

Please sign in to comment.