Skip to content

Commit

Permalink
- improved update check and added restart option when clicking notifi…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
Luka Grabarevic committed May 22, 2017
1 parent 49e4f49 commit 5cdb281
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using System.ComponentModel.Composition;
using System.Text;
using System.Windows;

using BuildsAppReborn.Contracts.Composition;
using BuildsAppReborn.Contracts.Models;
using BuildsAppReborn.Contracts.UI.Notifications;

using ToastNotifications;
using ToastNotifications.Lifetime;
using ToastNotifications.Messages;
Expand All @@ -24,11 +22,11 @@ internal class DefaultNotificationProvider : INotificationProvider
public DefaultNotificationProvider()
{
this.notifier = new Notifier(cfg =>
{
cfg.PositionProvider = new PrimaryScreenPositionProvider(Corner.BottomRight, 10, 50);
cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(TimeSpan.FromSeconds(10), MaximumNotificationCount.FromCount(3));
cfg.Dispatcher = Application.Current.Dispatcher;
});
{
cfg.PositionProvider = new PrimaryScreenPositionProvider(Corner.BottomRight, 10, 50);
cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(TimeSpan.FromSeconds(10), MaximumNotificationCount.FromCount(3));
cfg.Dispatcher = Application.Current.Dispatcher;
});
}

#endregion
Expand All @@ -43,11 +41,14 @@ public void ShowBuild(IBuild build, Func<IBuild, String> iconProvider, Action<IB
sb.AppendLine($"{build.Definition.Project.Name} - {build.Definition.Name}");
sb.AppendLine(build.Status.ToString());
sb.AppendLine(build.Requester.DisplayName);
var displayOptions = new MessageOptions { NotificationClickAction = n =>
{
notificationClickAction?.Invoke(build);
n.Close();
} };
var displayOptions = new MessageOptions
{
NotificationClickAction = n =>
{
notificationClickAction?.Invoke(build);
n.Close();
}
};

switch (build.Status)
{
Expand All @@ -73,11 +74,32 @@ public void ShowBuild(IBuild build, Func<IBuild, String> iconProvider, Action<IB
}

public void ShowMessage(String title, String message)
{
ShowMessage(title, message, null);
}

public void ShowMessage(String title, String message, Action clickAction)
{
var sb = new StringBuilder();
sb.AppendLine(title);
sb.AppendLine(message);
this.notifier.ShowInformation(sb.ToString());

if (clickAction == null)
{
this.notifier.ShowInformation(sb.ToString());
}
else
{
var displayOptions = new MessageOptions
{
NotificationClickAction = n =>
{
clickAction.Invoke();
n.Close();
}
};
this.notifier.ShowInformation(sb.ToString(), displayOptions);
}
}

public Boolean IsSupported => true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.ComponentModel.Composition;
using System.IO;

using Windows.UI.Notifications;

using BuildsAppReborn.Contracts.Composition;
using BuildsAppReborn.Contracts.Models;
using BuildsAppReborn.Contracts.UI.Notifications;
Expand Down Expand Up @@ -37,13 +35,18 @@ public void ShowBuild(IBuild build, Func<IBuild, String> iconProvider, Action<IB

// Create the toast and attach event listeners
var toast = new ToastNotification(toastXml);
toast.Activated += (sender, args) => { notificationClickAction?.Invoke(build); };
if (notificationClickAction != null) toast.Activated += (sender, args) => { notificationClickAction.Invoke(build); };

// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(AppId).Show(toast);
}

public void ShowMessage(String title, String message)
{
ShowMessage(title, message, null);
}

public void ShowMessage(String title, String message, Action clickAction)
{
// Get a toast XML template
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
Expand All @@ -57,6 +60,7 @@ public void ShowMessage(String title, String message)

// Create the toast and attach event listeners
var toast = new ToastNotification(toastXml);
if (clickAction != null) toast.Activated += (sender, args) => { clickAction.Invoke(); };

// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(AppId).Show(toast);
Expand Down
4 changes: 2 additions & 2 deletions BuildsAppReborn.Client/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void OnStartup(StartupEventArgs e)

this.updateChecker = compositionContainer.GetExportedValue<UpdateChecker>();
this.updateChecker.Start();
this.updateChecker.UpdateCheck();
this.updateChecker.UpdateCheck(false);
this.globalSettingsContainer = compositionContainer.GetExportedValue<GlobalSettingsContainer>();

this.buildMonitor = compositionContainer.GetExportedValue<IBuildMonitorAdvanced>();
Expand Down Expand Up @@ -106,7 +106,7 @@ private void RegisterToWindowsStartUp()
var key =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
var fileName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
var fileName = Path.GetFileName(Assembly.GetEntryAssembly().Location);
key?.SetValue(Consts.ApplicationName, $"\"{Path.Combine(Consts.InstallationFolder, fileName)}\"");
}
}
Expand Down
1 change: 1 addition & 0 deletions BuildsAppReborn.Client/Resources/NotifyIconResources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ContextMenu x:Key="SysTrayMenu" x:Shared="false">
<MenuItem Command="{Binding ShowWindowCommand}" Header="Show Window" />
<MenuItem Command="{Binding ShowSettingsWindowCommand}" Header="Show Settings" />
<MenuItem Command="{Binding CheckUpdateCommand}" Header="Check for updates" />
<Separator />
<MenuItem Command="{Binding ExitApplicationCommand}" Header="Exit" />
</ContextMenu>
Expand Down
58 changes: 36 additions & 22 deletions BuildsAppReborn.Client/UpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand All @@ -11,7 +11,6 @@
using BuildsAppReborn.Contracts.Models;
using BuildsAppReborn.Contracts.UI.Notifications;
using BuildsAppReborn.Infrastructure;

using log4net;
using Squirrel;

Expand Down Expand Up @@ -52,9 +51,9 @@ public void Stop()
this.timer.Stop();
}

public async void UpdateCheck()
public async void UpdateCheck(Boolean manualCheck)
{
await UpdateCheckInternal();
await UpdateCheckInternal(manualCheck);
}

#endregion
Expand All @@ -69,10 +68,10 @@ public async void UpdateCheck()

private async void TimerOnElapsed(Object sender, ElapsedEventArgs elapsedEventArgs)
{
await UpdateCheckInternal();
await UpdateCheckInternal(false);
}

private async Task UpdateCheckInternal()
private async Task UpdateCheckInternal(Boolean manualCheck)
{
#if DEBUG
return;
Expand All @@ -88,29 +87,42 @@ private async Task UpdateCheckInternal()
{
using (var updateManager = await updateMgrTask)
{
if (!GeneralSettings.AutoInstall)
var updateInfo = await updateManager.CheckForUpdate();
if (updateInfo.ReleasesToApply.Any())
{
var updateInfo = await updateManager.CheckForUpdate();
if (GeneralSettings.NotifyOnNewUpdate)
this.logger.Info("New Update found!");
if (!GeneralSettings.AutoInstall)
{
// ToDo: show if new update available
}
// ToDo: implement update when user accepts
}
else
{
var result = await updateManager.UpdateApp();
if (result != null)
{
this.logger.Info("New Update found!");
if (GeneralSettings.NotifyOnNewUpdate)
{
this.notificationProvider?.ShowMessage($"{this.version.ProductName} New update found!", "Update will be installed automatically on next start.");
// ToDo: show if new update available
}
// ToDo: implement update when user accepts
}
else
{
this.logger.Info("No Update found!");
this.logger.Debug("Auto installing update...");
await updateManager.DownloadReleases(updateInfo.ReleasesToApply);
var path = await updateManager.ApplyReleases(updateInfo);
if (path != null)
{
this.logger.Debug("Update install finished.");
if (GeneralSettings.NotifyOnNewUpdate)
{
this.notificationProvider?.ShowMessage($"{this.version.ProductName} - Update check finished!",
"New Updates installed. Click here to restart.",
() => UpdateManager.RestartApp(Path.Combine(path, Path.GetFileName(entryAssembly.Location))));
}
}
else this.logger.Debug("Update install failed.");
}
}
else
{
this.logger.Info("No Update found!");
if (manualCheck)
{
this.notificationProvider?.ShowMessage($"{this.version.ProductName} - Update check finished!", "No updates found!");
}
}
}
Expand All @@ -134,8 +146,10 @@ private async Task UpdateCheckInternal()
private ILog logger = LogManager.GetLogger(typeof(UpdateChecker));
private INotificationProvider notificationProvider;
private Timer timer = new Timer();
private FileVersionInfo version = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);
private FileVersionInfo version = FileVersionInfo.GetVersionInfo(entryAssembly.Location);

#endregion

private static Assembly entryAssembly = Assembly.GetEntryAssembly();
}
}
8 changes: 7 additions & 1 deletion BuildsAppReborn.Client/ViewModels/NotifyIconViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public class NotifyIconViewModel : ViewModelBase
#region Constructors

[ImportingConstructor]
public NotifyIconViewModel(ExportFactory<IBuildsStatusView> buildsExportFactory, ExportFactory<ISettingsView> settingsExportFactory, BuildCache buildCache, NotificationManager notificationManager)
internal NotifyIconViewModel(ExportFactory<IBuildsStatusView> buildsExportFactory, ExportFactory<ISettingsView> settingsExportFactory, BuildCache buildCache, NotificationManager notificationManager, UpdateChecker updateChecker)
{
if (buildCache.CacheStatus == BuildCacheStatus.NotConfigured) TrayIcon = IconProvider.SettingsIcon;
else TrayIcon = IconProvider.LoadingIcon;

this.buildsExportFactory = buildsExportFactory;
this.settingsExportFactory = settingsExportFactory;
this.updateChecker = updateChecker;
buildCache.CacheUpdated += (sender, args) => { TrayIcon = buildCache.CurrentIcon; };

Initialize();
Expand All @@ -35,6 +36,8 @@ public NotifyIconViewModel(ExportFactory<IBuildsStatusView> buildsExportFactory,

#region Public Properties

public ICommand CheckUpdateCommand { get; private set; }

public ICommand ExitApplicationCommand { get; private set; }

public ICommand ShowSettingsWindowCommand { get; private set; }
Expand Down Expand Up @@ -112,6 +115,8 @@ private void Initialize()
ShowSettingsWindowCommand = new DelegateCommand(() => { OpenWindow<ISettingsView>(this.settingsExportFactory); });

ExitApplicationCommand = new DelegateCommand(() => Application.Current.Shutdown());

CheckUpdateCommand = new DelegateCommand(() => this.updateChecker.UpdateCheck(true));
}

#endregion
Expand All @@ -122,6 +127,7 @@ private void Initialize()

private readonly ExportFactory<ISettingsView> settingsExportFactory;
private String trayIcon;
private readonly UpdateChecker updateChecker;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;

using BuildsAppReborn.Contracts.Models;

namespace BuildsAppReborn.Contracts.UI.Notifications
Expand All @@ -18,6 +17,8 @@ public interface INotificationProvider

void ShowMessage(String title, String message);

void ShowMessage(String title, String message, Action clickAction);

#endregion
}
}

0 comments on commit 5cdb281

Please sign in to comment.