* Removed incorrect use of async method in MainWindowViewModel constructor
* Moved script/theme loading code to new ScriptLoadingService class
This commit is contained in:
parent
e5386132c3
commit
25c25c2a1d
|
@ -217,6 +217,7 @@
|
||||||
<Compile Include="Services\HTTPService.cs" />
|
<Compile Include="Services\HTTPService.cs" />
|
||||||
<Compile Include="Services\ItemFilterPersistenceService.cs" />
|
<Compile Include="Services\ItemFilterPersistenceService.cs" />
|
||||||
<Compile Include="Services\ItemFilterScriptDirectoryService.cs" />
|
<Compile Include="Services\ItemFilterScriptDirectoryService.cs" />
|
||||||
|
<Compile Include="Services\ScriptLoadingService.cs" />
|
||||||
<Compile Include="Services\SettingsService.cs" />
|
<Compile Include="Services\SettingsService.cs" />
|
||||||
<Compile Include="Services\StaticDataService.cs" />
|
<Compile Include="Services\StaticDataService.cs" />
|
||||||
<Compile Include="Services\UpdateService.cs" />
|
<Compile Include="Services\UpdateService.cs" />
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using Filtration.Properties;
|
||||||
using Filtration.Views;
|
using Filtration.Views;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
|
@ -17,16 +18,19 @@ namespace Filtration.Services
|
||||||
|
|
||||||
private readonly IItemFilterScriptDirectoryService _itemFilterScriptDirectoryService;
|
private readonly IItemFilterScriptDirectoryService _itemFilterScriptDirectoryService;
|
||||||
private readonly IMainWindow _mainWindow;
|
private readonly IMainWindow _mainWindow;
|
||||||
|
private readonly IScriptLoadingService _scriptLoadingService;
|
||||||
private readonly ISettingsService _settingsService;
|
private readonly ISettingsService _settingsService;
|
||||||
private readonly IUpdateService _updateService;
|
private readonly IUpdateService _updateService;
|
||||||
|
|
||||||
public Bootstrapper(IItemFilterScriptDirectoryService itemFilterScriptDirectoryService,
|
public Bootstrapper(IItemFilterScriptDirectoryService itemFilterScriptDirectoryService,
|
||||||
IMainWindow mainWindow,
|
IMainWindow mainWindow,
|
||||||
|
IScriptLoadingService scriptLoadingService,
|
||||||
ISettingsService settingsService,
|
ISettingsService settingsService,
|
||||||
IUpdateService updateService)
|
IUpdateService updateService)
|
||||||
{
|
{
|
||||||
_itemFilterScriptDirectoryService = itemFilterScriptDirectoryService;
|
_itemFilterScriptDirectoryService = itemFilterScriptDirectoryService;
|
||||||
_mainWindow = mainWindow;
|
_mainWindow = mainWindow;
|
||||||
|
_scriptLoadingService = scriptLoadingService;
|
||||||
_settingsService = settingsService;
|
_settingsService = settingsService;
|
||||||
_updateService = updateService;
|
_updateService = updateService;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +48,12 @@ namespace Filtration.Services
|
||||||
|
|
||||||
_mainWindow.Show();
|
_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();
|
await _updateService.CheckForUpdatesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -49,6 +49,7 @@ namespace Filtration.ViewModels
|
||||||
private readonly IItemFilterScriptTranslator _itemFilterScriptTranslator;
|
private readonly IItemFilterScriptTranslator _itemFilterScriptTranslator;
|
||||||
private readonly IReplaceColorsViewModel _replaceColorsViewModel;
|
private readonly IReplaceColorsViewModel _replaceColorsViewModel;
|
||||||
private readonly IAvalonDockWorkspaceViewModel _avalonDockWorkspaceViewModel;
|
private readonly IAvalonDockWorkspaceViewModel _avalonDockWorkspaceViewModel;
|
||||||
|
private readonly IScriptLoadingService _scriptLoadingService;
|
||||||
private readonly IThemeProvider _themeProvider;
|
private readonly IThemeProvider _themeProvider;
|
||||||
private readonly IThemeService _themeService;
|
private readonly IThemeService _themeService;
|
||||||
private readonly IMessageBoxService _messageBoxService;
|
private readonly IMessageBoxService _messageBoxService;
|
||||||
|
@ -63,6 +64,7 @@ namespace Filtration.ViewModels
|
||||||
IItemFilterScriptTranslator itemFilterScriptTranslator,
|
IItemFilterScriptTranslator itemFilterScriptTranslator,
|
||||||
IReplaceColorsViewModel replaceColorsViewModel,
|
IReplaceColorsViewModel replaceColorsViewModel,
|
||||||
IAvalonDockWorkspaceViewModel avalonDockWorkspaceViewModel,
|
IAvalonDockWorkspaceViewModel avalonDockWorkspaceViewModel,
|
||||||
|
IScriptLoadingService scriptLoadingService,
|
||||||
ISettingsPageViewModel settingsPageViewModel,
|
ISettingsPageViewModel settingsPageViewModel,
|
||||||
IThemeProvider themeProvider,
|
IThemeProvider themeProvider,
|
||||||
IThemeService themeService,
|
IThemeService themeService,
|
||||||
|
@ -75,6 +77,7 @@ namespace Filtration.ViewModels
|
||||||
_itemFilterScriptTranslator = itemFilterScriptTranslator;
|
_itemFilterScriptTranslator = itemFilterScriptTranslator;
|
||||||
_replaceColorsViewModel = replaceColorsViewModel;
|
_replaceColorsViewModel = replaceColorsViewModel;
|
||||||
_avalonDockWorkspaceViewModel = avalonDockWorkspaceViewModel;
|
_avalonDockWorkspaceViewModel = avalonDockWorkspaceViewModel;
|
||||||
|
_scriptLoadingService = scriptLoadingService;
|
||||||
SettingsPageViewModel = settingsPageViewModel;
|
SettingsPageViewModel = settingsPageViewModel;
|
||||||
_themeProvider = themeProvider;
|
_themeProvider = themeProvider;
|
||||||
_themeService = themeService;
|
_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; }
|
public RelayCommand OpenScriptCommand { get; }
|
||||||
|
@ -369,12 +368,12 @@ namespace Filtration.ViewModels
|
||||||
{
|
{
|
||||||
case ".FILTER":
|
case ".FILTER":
|
||||||
{
|
{
|
||||||
await LoadScriptAsync(filename);
|
await _scriptLoadingService.LoadScriptAsync(filename);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ".FILTERTHEME":
|
case ".FILTERTHEME":
|
||||||
{
|
{
|
||||||
await LoadThemeAsync(filename);
|
await _scriptLoadingService.LoadThemeAsync(filename);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -431,43 +430,7 @@ namespace Filtration.ViewModels
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await LoadScriptAsync(filePath); // TODO: fix crash
|
await _scriptLoadingService.LoadScriptAsync(filePath);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task OnOpenThemeCommandAsync()
|
private async Task OnOpenThemeCommandAsync()
|
||||||
|
@ -478,27 +441,7 @@ namespace Filtration.ViewModels
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await LoadThemeAsync(filePath);
|
await _scriptLoadingService.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task OnApplyThemeToScriptCommandAsync()
|
private async Task OnApplyThemeToScriptCommandAsync()
|
||||||
|
|
|
@ -19,6 +19,11 @@ namespace Filtration.WindsorInstallers
|
||||||
.ImplementedBy<ItemFilterScriptDirectoryService>()
|
.ImplementedBy<ItemFilterScriptDirectoryService>()
|
||||||
.LifeStyle.Singleton);
|
.LifeStyle.Singleton);
|
||||||
|
|
||||||
|
container.Register(
|
||||||
|
Component.For<IScriptLoadingService>()
|
||||||
|
.ImplementedBy<ScriptLoadingService>()
|
||||||
|
.LifeStyle.Singleton);
|
||||||
|
|
||||||
container.Register(
|
container.Register(
|
||||||
Component.For<IStaticDataService>()
|
Component.For<IStaticDataService>()
|
||||||
.ImplementedBy<StaticDataService>()
|
.ImplementedBy<StaticDataService>()
|
||||||
|
|
Loading…
Reference in New Issue