Implemented async saving and loading
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Filtration.ObjectModel;
|
||||
using Filtration.ObjectModel.ThemeEditor;
|
||||
@@ -13,9 +14,9 @@ namespace Filtration.ThemeEditor.Providers
|
||||
{
|
||||
IThemeEditorViewModel NewThemeForScript(ItemFilterScript script);
|
||||
IThemeEditorViewModel MasterThemeForScript(ItemFilterScript script);
|
||||
IThemeEditorViewModel LoadThemeFromFile(string filePath);
|
||||
Theme LoadThemeModelFromFile(string filePath);
|
||||
void SaveTheme(IThemeEditorViewModel themeEditorViewModel, string filePath);
|
||||
Task<IThemeEditorViewModel> LoadThemeFromFile(string filePath);
|
||||
Task<Theme> LoadThemeModelFromFile(string filePath);
|
||||
Task SaveThemeAsync(IThemeEditorViewModel themeEditorViewModel, string filePath);
|
||||
}
|
||||
|
||||
internal class ThemeProvider : IThemeProvider
|
||||
@@ -54,23 +55,26 @@ namespace Filtration.ThemeEditor.Providers
|
||||
return themeViewModel;
|
||||
}
|
||||
|
||||
public IThemeEditorViewModel LoadThemeFromFile(string filePath)
|
||||
public async Task<IThemeEditorViewModel> LoadThemeFromFile(string filePath)
|
||||
{
|
||||
var model = _themePersistenceService.LoadTheme(filePath);
|
||||
var model = await _themePersistenceService.LoadThemeAsync(filePath);
|
||||
var viewModel = Mapper.Map<IThemeEditorViewModel>(model);
|
||||
viewModel.FilePath = filePath;
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public Theme LoadThemeModelFromFile(string filePath)
|
||||
public async Task<Theme> LoadThemeModelFromFile(string filePath)
|
||||
{
|
||||
return _themePersistenceService.LoadTheme(filePath);
|
||||
return await _themePersistenceService.LoadThemeAsync(filePath);
|
||||
}
|
||||
|
||||
public void SaveTheme(IThemeEditorViewModel themeEditorViewModel, string filePath)
|
||||
public async Task SaveThemeAsync(IThemeEditorViewModel themeEditorViewModel, string filePath)
|
||||
{
|
||||
var theme = Mapper.Map<Theme>(themeEditorViewModel);
|
||||
_themePersistenceService.SaveTheme(theme, filePath);
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var theme = Mapper.Map<Theme>(themeEditorViewModel);
|
||||
_themePersistenceService.SaveThemeAsync(theme, filePath);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using Filtration.ObjectModel.ThemeEditor;
|
||||
|
||||
@@ -6,35 +7,42 @@ namespace Filtration.ThemeEditor.Services
|
||||
{
|
||||
internal interface IThemePersistenceService
|
||||
{
|
||||
Theme LoadTheme(string filePath);
|
||||
void SaveTheme(Theme theme, string filePath);
|
||||
Task<Theme> LoadThemeAsync(string filePath);
|
||||
Task SaveThemeAsync(Theme theme, string filePath);
|
||||
}
|
||||
|
||||
internal class ThemePersistenceService : IThemePersistenceService
|
||||
{
|
||||
public Theme LoadTheme(string filePath)
|
||||
public async Task<Theme> LoadThemeAsync(string filePath)
|
||||
{
|
||||
var xmlSerializer = new XmlSerializer(typeof(Theme));
|
||||
Theme loadedTheme = null;
|
||||
|
||||
Theme loadedTheme;
|
||||
|
||||
using (Stream reader = new FileStream(filePath, FileMode.Open))
|
||||
await Task.Run(() =>
|
||||
{
|
||||
loadedTheme = (Theme)xmlSerializer.Deserialize(reader);
|
||||
}
|
||||
|
||||
loadedTheme.FilePath = filePath;
|
||||
var xmlSerializer = new XmlSerializer(typeof (Theme));
|
||||
|
||||
using (Stream reader = new FileStream(filePath, FileMode.Open))
|
||||
{
|
||||
loadedTheme = (Theme) xmlSerializer.Deserialize(reader);
|
||||
}
|
||||
|
||||
loadedTheme.FilePath = filePath;
|
||||
});
|
||||
|
||||
return loadedTheme;
|
||||
}
|
||||
|
||||
public void SaveTheme(Theme theme, string filePath)
|
||||
public async Task SaveThemeAsync(Theme theme, string filePath)
|
||||
{
|
||||
var xmlSerializer = new XmlSerializer(typeof(Theme));
|
||||
|
||||
using (Stream writer = new FileStream(filePath, FileMode.Create))
|
||||
await Task.Run(() =>
|
||||
{
|
||||
xmlSerializer.Serialize(writer, theme);
|
||||
}
|
||||
var xmlSerializer = new XmlSerializer(typeof (Theme));
|
||||
|
||||
using (Stream writer = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
xmlSerializer.Serialize(writer, theme);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Media;
|
||||
@@ -55,7 +56,7 @@ namespace Filtration.ThemeEditor.ViewModels
|
||||
AddThemeComponentCommand = new RelayCommand<ThemeComponentType>(OnAddThemeComponentCommand, t => IsMasterTheme);
|
||||
DeleteThemeComponentCommand = new RelayCommand<ThemeComponent>(OnDeleteThemeComponentCommand,
|
||||
t => IsMasterTheme && SelectedThemeComponent != null);
|
||||
CloseCommand = new RelayCommand(OnCloseCommand);
|
||||
CloseCommand = new RelayCommand(async () => await OnCloseCommand());
|
||||
|
||||
var icon = new BitmapImage();
|
||||
icon.BeginInit();
|
||||
@@ -123,19 +124,19 @@ namespace Filtration.ThemeEditor.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
if (IsMasterTheme) return;
|
||||
|
||||
if (_filenameIsFake)
|
||||
{
|
||||
SaveAs();
|
||||
await SaveAsAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_themeProvider.SaveTheme(this, FilePath);
|
||||
await _themeProvider.SaveThemeAsync(this, FilePath);
|
||||
//RemoveDirtyFlag();
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -149,7 +150,7 @@ namespace Filtration.ThemeEditor.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveAs()
|
||||
public async Task SaveAsAsync()
|
||||
{
|
||||
if (IsMasterTheme) return;
|
||||
|
||||
@@ -167,7 +168,7 @@ namespace Filtration.ThemeEditor.ViewModels
|
||||
try
|
||||
{
|
||||
FilePath = saveDialog.FileName;
|
||||
_themeProvider.SaveTheme(this, FilePath);
|
||||
await _themeProvider.SaveThemeAsync(this, FilePath);
|
||||
_filenameIsFake = false;
|
||||
Title = Filename;
|
||||
//RemoveDirtyFlag();
|
||||
@@ -185,14 +186,16 @@ namespace Filtration.ThemeEditor.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCloseCommand()
|
||||
public async Task OnCloseCommand()
|
||||
{
|
||||
Close();
|
||||
await Close();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
#pragma warning disable 1998
|
||||
public async Task Close()
|
||||
#pragma warning restore 1998
|
||||
{
|
||||
Messenger.Default.Send(new ThemeClosedMessage { ClosedViewModel = this });
|
||||
Messenger.Default.Send(new ThemeClosedMessage {ClosedViewModel = this});
|
||||
}
|
||||
|
||||
private void OnAddThemeComponentCommand(ThemeComponentType themeComponentType)
|
||||
|
||||
Reference in New Issue
Block a user