Skip to content

Commit

Permalink
Added an update check feature that will prompt and take the user to t…
Browse files Browse the repository at this point in the history
…he github tag if a newer version is found.
  • Loading branch information
laurencee committed Dec 1, 2015
1 parent 023c59a commit 56b15c7
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Livestream.Monitor/Livestream.Monitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Octokit, Version=0.16.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Octokit.0.16.0\lib\net45\Octokit.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
Expand Down
4 changes: 2 additions & 2 deletions Livestream.Monitor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
75 changes: 71 additions & 4 deletions Livestream.Monitor/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using Caliburn.Micro;
using Hardcodet.Wpf.TaskbarNotification;
using Livestream.Monitor.Core;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Octokit;
using Application = System.Windows.Application;

namespace Livestream.Monitor.ViewModels
{
public class ShellViewModel : Conductor<Screen>.Collection.OneActive
{
public const string TrayIconControlName = "TrayIcon";

private readonly Version currentAppVersion;
private WindowState windowState = WindowState.Normal;
private TaskbarIcon taskbarIcon;
private bool firstMinimize = true;
Expand Down Expand Up @@ -40,8 +50,8 @@ public ShellViewModel(
Settings.ActivateWith(this);
ThemeSelector.ActivateWith(this);

var assemblyVersion = GetType().Assembly.GetName().Version;
DisplayName = $"LIVESTREAM MONITOR V{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}";
currentAppVersion = GetType().Assembly.GetName().Version;
DisplayName = $"LIVESTREAM MONITOR V{currentAppVersion.Major}.{currentAppVersion.Minor}.{currentAppVersion.Build}";
}

public override string DisplayName { get; set; }
Expand Down Expand Up @@ -96,16 +106,73 @@ private void WindowMinimized()
}
}

protected override void OnViewLoaded(object view)
protected override async void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
taskbarIcon = Application.Current.MainWindow.FindChild<TaskbarIcon>(TrayIconControlName);
await CheckForNewVersion();
}

protected override void OnDeactivate(bool close)
{
taskbarIcon.Dispose(); // this will be cleaned up on app close anyway but this is a bit cleaner
base.OnDeactivate(close);
}

private async Task CheckForNewVersion()
{
var githubClient =
new GitHubClient(new ProductHeaderValue("Livestream.Monitor",
$"{currentAppVersion.Major}.{currentAppVersion.Minor}.{currentAppVersion.Build}"));

const string githubRepository = "Livestream.Monitor";
const string githubUsername = "laurencee";

var dialogController = await this.ShowProgressAsync("Update Check", "Checking for newer version...");
try
{
var releases = await githubClient.Release.GetAll(githubUsername, githubRepository);
var latestRelease = releases.FirstOrDefault();
if (latestRelease != null)
{
if (IsNewerVersion(latestRelease))
{
await dialogController.CloseAsync();
var dialogResult = await this.ShowMessageAsync("New version available",
"There is a newer version available. Go to download page?",
MessageDialogStyle.AffirmativeAndNegative);

if (dialogResult == MessageDialogResult.Affirmative)
{
System.Diagnostics.Process.Start(latestRelease.HtmlUrl);
}
}
}
}
catch (Exception ex)
{
if (dialogController.IsOpen) await dialogController.CloseAsync();
await this.ShowMessageAsync("Error",
$"An error occured while checking for a newer version.{Environment.NewLine}{ex.Message}");
}

if (dialogController.IsOpen) await dialogController.CloseAsync();
}

private bool IsNewerVersion(Release latestRelease)
{
if (string.IsNullOrWhiteSpace(latestRelease?.TagName)) return false;

try
{
var releaseVersion = new Version(latestRelease.TagName);
return releaseVersion > currentAppVersion;
}
catch
{
// failed to convert the tagname to a version for some reason
return false;
}
}
}
}
1 change: 1 addition & 0 deletions Livestream.Monitor/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.5" targetFramework="net46" />
<package id="MahApps.Metro" version="1.1.2.0" targetFramework="net46" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net46" />
<package id="Octokit" version="0.16.0" targetFramework="net46" />
</packages>
4 changes: 2 additions & 2 deletions TwitchTv/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.2.0")]
[assembly: AssemblyFileVersion("1.1.2.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]

0 comments on commit 56b15c7

Please sign in to comment.