* Removed incorrect use of async method in MainWindowViewModel constructor

* Moved script/theme loading code to new ScriptLoadingService class
This commit is contained in:
Ben Wallis 2018-11-27 21:48:36 +00:00
parent e5386132c3
commit 25c25c2a1d
5 changed files with 122 additions and 65 deletions

View File

@ -217,6 +217,7 @@
<Compile Include="Services\HTTPService.cs" />
<Compile Include="Services\ItemFilterPersistenceService.cs" />
<Compile Include="Services\ItemFilterScriptDirectoryService.cs" />
<Compile Include="Services\ScriptLoadingService.cs" />
<Compile Include="Services\SettingsService.cs" />
<Compile Include="Services\StaticDataService.cs" />
<Compile Include="Services\UpdateService.cs" />

View File

@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using Filtration.Properties;
using Filtration.Views;
using NLog;
@ -17,16 +18,19 @@ namespace Filtration.Services
private readonly IItemFilterScriptDirectoryService _itemFilterScriptDirectoryService;
private readonly IMainWindow _mainWindow;
private readonly IScriptLoadingService _scriptLoadingService;
private readonly ISettingsService _settingsService;
private readonly IUpdateService _updateService;
public Bootstrapper(IItemFilterScriptDirectoryService itemFilterScriptDirectoryService,
IMainWindow mainWindow,
IScriptLoadingService scriptLoadingService,
ISettingsService settingsService,
IUpdateService updateService)
{
_itemFilterScriptDirectoryService = itemFilterScriptDirectoryService;
_mainWindow = mainWindow;
_scriptLoadingService = scriptLoadingService;
_settingsService = settingsService;
_updateService = updateService;
}
@ -44,6 +48,12 @@ namespace Filtration.Services
_mainWindow.Show();
// If there were scripts open the last time the application was closed, reopen them
if (!string.IsNullOrWhiteSpace(Settings.Default.LastOpenScripts))
{
await _scriptLoadingService.LoadScriptsAsync(Settings.Default.LastOpenScripts.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
}
await _updateService.CheckForUpdatesAsync();
}

View File

@ -0,0 +1,98 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using Filtration.Common.Services;
using Filtration.Repositories;
using Filtration.ThemeEditor.Providers;
using Filtration.ThemeEditor.ViewModels;
using Filtration.ViewModels;
using GalaSoft.MvvmLight.Messaging;
using NLog;
namespace Filtration.Services
{
internal interface IScriptLoadingService
{
Task LoadScriptAsync(string scriptFilename);
Task LoadScriptsAsync(string[] files);
Task LoadThemeAsync(string themeFilename);
}
internal sealed class ScriptLoadingService : IScriptLoadingService
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IAvalonDockWorkspaceViewModel _avalonDockWorkspaceViewModel;
private readonly IMessageBoxService _messageBoxService;
private readonly IThemeProvider _themeProvider;
private readonly IItemFilterScriptRepository _itemFilterScriptRepository;
public ScriptLoadingService(IAvalonDockWorkspaceViewModel avalonDockWorkspaceViewModel,
IItemFilterScriptRepository itemFilterScriptRepository,
IMessageBoxService messageBoxService,
IThemeProvider themeProvider)
{
_avalonDockWorkspaceViewModel = avalonDockWorkspaceViewModel;
_messageBoxService = messageBoxService;
_themeProvider = themeProvider;
_itemFilterScriptRepository = itemFilterScriptRepository;
}
public async Task LoadScriptsAsync(string[] files)
{
foreach (var file in files)
{
if (File.Exists(file))
{
await LoadScriptAsync(file);
}
}
}
public async Task LoadScriptAsync(string scriptFilename)
{
IItemFilterScriptViewModel loadedViewModel;
Messenger.Default.Send(new NotificationMessage("ShowLoadingBanner"));
try
{
loadedViewModel = await _itemFilterScriptRepository.LoadScriptFromFileAsync(scriptFilename);
}
catch (Exception e)
{
Logger.Error(e);
_messageBoxService.Show("Script Load Error", "Error loading filter script - " + e.Message,
MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
finally
{
Messenger.Default.Send(new NotificationMessage("HideLoadingBanner"));
}
_avalonDockWorkspaceViewModel.AddDocument(loadedViewModel);
}
public async Task LoadThemeAsync(string themeFilename)
{
IThemeEditorViewModel loadedViewModel;
try
{
loadedViewModel = await _themeProvider.LoadThemeFromFile(themeFilename);
}
catch (Exception e)
{
Logger.Error(e);
_messageBoxService.Show("Theme Load Error", "Error loading filter theme - " + e.Message,
MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
_avalonDockWorkspaceViewModel.AddDocument(loadedViewModel);
}
}
}

View File

@ -49,6 +49,7 @@ namespace Filtration.ViewModels
private readonly IItemFilterScriptTranslator _itemFilterScriptTranslator;
private readonly IReplaceColorsViewModel _replaceColorsViewModel;
private readonly IAvalonDockWorkspaceViewModel _avalonDockWorkspaceViewModel;
private readonly IScriptLoadingService _scriptLoadingService;
private readonly IThemeProvider _themeProvider;
private readonly IThemeService _themeService;
private readonly IMessageBoxService _messageBoxService;
@ -63,6 +64,7 @@ namespace Filtration.ViewModels
IItemFilterScriptTranslator itemFilterScriptTranslator,
IReplaceColorsViewModel replaceColorsViewModel,
IAvalonDockWorkspaceViewModel avalonDockWorkspaceViewModel,
IScriptLoadingService scriptLoadingService,
ISettingsPageViewModel settingsPageViewModel,
IThemeProvider themeProvider,
IThemeService themeService,
@ -75,6 +77,7 @@ namespace Filtration.ViewModels
_itemFilterScriptTranslator = itemFilterScriptTranslator;
_replaceColorsViewModel = replaceColorsViewModel;
_avalonDockWorkspaceViewModel = avalonDockWorkspaceViewModel;
_scriptLoadingService = scriptLoadingService;
SettingsPageViewModel = settingsPageViewModel;
_themeProvider = themeProvider;
_themeService = themeService;
@ -204,10 +207,6 @@ namespace Filtration.ViewModels
}
});
if (!string.IsNullOrWhiteSpace(Settings.Default.LastOpenScripts))
{
LoadScriptsAsync(Settings.Default.LastOpenScripts.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
}
}
public RelayCommand OpenScriptCommand { get; }
@ -369,12 +368,12 @@ namespace Filtration.ViewModels
{
case ".FILTER":
{
await LoadScriptAsync(filename);
await _scriptLoadingService.LoadScriptAsync(filename);
break;
}
case ".FILTERTHEME":
{
await LoadThemeAsync(filename);
await _scriptLoadingService.LoadThemeAsync(filename);
break;
}
}
@ -431,43 +430,7 @@ namespace Filtration.ViewModels
return;
}
await LoadScriptAsync(filePath); // TODO: fix crash
}
private async Task LoadScriptsAsync(string[] files)
{
foreach (var file in files)
{
if (File.Exists(file))
{
await LoadScriptAsync(file);
}
}
}
private async Task LoadScriptAsync(string scriptFilename)
{
IItemFilterScriptViewModel loadedViewModel;
Messenger.Default.Send(new NotificationMessage("ShowLoadingBanner"));
try
{
loadedViewModel = await _itemFilterScriptRepository.LoadScriptFromFileAsync(scriptFilename);
}
catch (Exception e)
{
Logger.Error(e);
_messageBoxService.Show("Script Load Error", "Error loading filter script - " + e.Message,
MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
finally
{
Messenger.Default.Send(new NotificationMessage("HideLoadingBanner"));
}
_avalonDockWorkspaceViewModel.AddDocument(loadedViewModel);
await _scriptLoadingService.LoadScriptAsync(filePath);
}
private async Task OnOpenThemeCommandAsync()
@ -478,27 +441,7 @@ namespace Filtration.ViewModels
return;
}
await LoadThemeAsync(filePath);
}
private async Task LoadThemeAsync(string themeFilename)
{
IThemeEditorViewModel loadedViewModel;
try
{
loadedViewModel = await _themeProvider.LoadThemeFromFile(themeFilename);
}
catch (Exception e)
{
Logger.Error(e);
_messageBoxService.Show("Theme Load Error", "Error loading filter theme - " + e.Message,
MessageBoxButton.OK,
MessageBoxImage.Error);
return;
}
_avalonDockWorkspaceViewModel.AddDocument(loadedViewModel);
await _scriptLoadingService.LoadThemeAsync(filePath);
}
private async Task OnApplyThemeToScriptCommandAsync()

View File

@ -5,7 +5,7 @@ using Filtration.Services;
namespace Filtration.WindsorInstallers
{
public class ServicesInstaller :IWindsorInstaller
public class ServicesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
@ -19,6 +19,11 @@ namespace Filtration.WindsorInstallers
.ImplementedBy<ItemFilterScriptDirectoryService>()
.LifeStyle.Singleton);
container.Register(
Component.For<IScriptLoadingService>()
.ImplementedBy<ScriptLoadingService>()
.LifeStyle.Singleton);
container.Register(
Component.For<IStaticDataService>()
.ImplementedBy<StaticDataService>()