Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Close #122 - Automatically restart when update settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve committed Jun 10, 2020
1 parent ea2b280 commit a0889cc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/Probel.Lanceur.Infrastructure/IAppRestarter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;

namespace Probel.Lanceur.Infrastructure
{
public interface IAppRestarter
{
#region Methods

Task<bool> DoRestartAsync();

void Restart();

#endregion Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IAppRestarter.cs" />
<Compile Include="ILogService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Probel.Lanceur/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected override void Configure()
//UI
_container.RegisterType<IUserNotifyer, UserNotifyer>();
_container.RegisterSingleton<INotificationManager, NotificationManager>();
_container.RegisterSingleton<IAppRestarter, AppRestarter>();

//Settings
_container.RegisterType<IConnectionStringManager, ConnectionStringManager>();
Expand Down
1 change: 1 addition & 0 deletions src/Probel.Lanceur/Probel.Lanceur.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
<Compile Include="Helpers\MapperExtension.cs" />
<Compile Include="Models\AliasModel.cs" />
<Compile Include="Models\AliasSessionModel.cs" />
<Compile Include="Services\AppRestarter.cs" />
<Compile Include="Services\MainViewFinder.cs" />
<Compile Include="Services\ClipboardService.cs" />
<Compile Include="Actions\KeywordLoader.cs" />
Expand Down
42 changes: 42 additions & 0 deletions src/Probel.Lanceur/Services/AppRestarter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Probel.Lanceur.Infrastructure;
using Probel.Lanceur.Plugin;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;

namespace Probel.Lanceur.Services
{
public class AppRestarter : IAppRestarter
{
#region Fields

private readonly IUserNotifyer _notifyer;

#endregion Fields

#region Constructors

public AppRestarter(IUserNotifyer notifyer)
{
_notifyer = notifyer;
}

#endregion Constructors

#region Methods

public async Task<bool> DoRestartAsync()
{
var result = await _notifyer.AskAsync("Do you want to restart?");
return (result == NotificationResult.Affirmative);
}

public void Restart()
{
Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();
}

#endregion Methods
}
}
19 changes: 15 additions & 4 deletions src/Probel.Lanceur/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Caliburn.Micro;
using Probel.Lanceur.Core.Services;
using Probel.Lanceur.Helpers;
using Probel.Lanceur.Infrastructure;
using Probel.Lanceur.Models;
using Probel.Lanceur.Models.Settings;
using Probel.Lanceur.Plugin;
using Probel.Lanceur.Services;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace Probel.Lanceur.ViewModels
{
Expand All @@ -14,6 +16,7 @@ public class SettingsViewModel : Screen
#region Fields

private readonly IDataSourceService _databaseService;
private readonly IAppRestarter _restarter;
private readonly ISettingsService _settingsService;
private readonly IUserNotifyer _userNotifyer;
private AppSettingsModel _appSettings;
Expand All @@ -28,8 +31,12 @@ public class SettingsViewModel : Screen

#region Constructors

public SettingsViewModel(ISettingsService settingsService, IDataSourceService databaseService, IUserNotifyer userNotifyer)
public SettingsViewModel(ISettingsService settingsService,
IDataSourceService databaseService,
IUserNotifyer userNotifyer,
IAppRestarter restarter)
{
_restarter = restarter;
_userNotifyer = userNotifyer;
_databaseService = databaseService;
_settingsService = settingsService;
Expand Down Expand Up @@ -114,16 +121,20 @@ public void ResetSettings()
AppSettings.SessionId = CurrentSession?.Id ?? 1;
}

public void SaveSettings()
public async Task SaveSettings()
{
AppSettings.SessionId = CurrentSession?.Id ?? 1;

if (_settingsService.Get().DatabasePath != AppSettings.DatabasePath) { IsRebootNeeded = true; }

var e = AppSettings.AsEntity();
_settingsService.Save(e);

_userNotifyer.NotifyInfo("Settings has been saved.");

if (_settingsService.Get().DatabasePath != AppSettings.DatabasePath)
{
IsRebootNeeded = true;
if (await _restarter.DoRestartAsync()) { _restarter.Restart(); }
}
}

#endregion Methods
Expand Down

0 comments on commit a0889cc

Please sign in to comment.