Filtration/Filtration.ThemeEditor/ViewModels/ThemeEditorViewModel.cs

223 lines
7.3 KiB
C#
Raw Normal View History

2015-06-26 12:42:20 -04:00
using System;
using System.IO;
2015-07-25 14:02:42 -04:00
using System.Threading.Tasks;
2015-06-26 12:42:20 -04:00
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
2015-06-26 12:42:20 -04:00
using System.Windows.Media.Imaging;
using Filtration.Common.Services;
2015-06-26 12:42:20 -04:00
using Filtration.Common.ViewModels;
using Filtration.Interface;
2015-07-06 08:41:46 -04:00
using Filtration.ObjectModel;
using Filtration.ObjectModel.Enums;
using Filtration.ObjectModel.ThemeEditor;
2015-07-06 08:41:46 -04:00
using Filtration.ThemeEditor.Messages;
2015-06-26 12:42:20 -04:00
using Filtration.ThemeEditor.Providers;
using GalaSoft.MvvmLight.CommandWpf;
2015-07-06 08:41:46 -04:00
using GalaSoft.MvvmLight.Messaging;
2015-07-03 17:16:17 -04:00
using NLog;
2015-06-26 12:42:20 -04:00
namespace Filtration.ThemeEditor.ViewModels
{
public interface IThemeEditorViewModel : IEditableDocument
2015-06-26 12:42:20 -04:00
{
RelayCommand<ThemeComponentType> AddThemeComponentCommand { get; }
RelayCommand<ThemeComponent> DeleteThemeComponentCommand { get; }
2015-07-06 08:41:46 -04:00
RelayCommand CloseCommand { get; }
2015-07-06 08:41:46 -04:00
void InitialiseForNewTheme(ThemeComponentCollection themeComponentCollection);
void InitialiseForMasterTheme(IItemFilterScript script);
2015-07-06 08:41:46 -04:00
bool IsMasterTheme { get; }
IItemFilterScript IsMasterThemeForScript { get; }
2015-06-26 12:42:20 -04:00
string Title { get; }
string FilePath { get; set; }
string Filename { get; }
string Name { get; set; }
ThemeComponentCollection Components { get; set; }
ThemeComponent SelectedThemeComponent { get; }
2015-06-26 12:42:20 -04:00
}
public class ThemeEditorViewModel : PaneViewModel, IThemeEditorViewModel
2015-06-26 12:42:20 -04:00
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2015-07-03 17:16:17 -04:00
2015-06-26 12:42:20 -04:00
private readonly IThemeProvider _themeProvider;
private readonly IMessageBoxService _messageBoxService;
2015-06-26 12:42:20 -04:00
private bool _filenameIsFake;
private string _filePath;
private ThemeComponent _selectedThemeComponent;
2015-06-26 12:42:20 -04:00
public ThemeEditorViewModel(IThemeProvider themeProvider,
IMessageBoxService messageBoxService)
2015-06-26 12:42:20 -04:00
{
_themeProvider = themeProvider;
_messageBoxService = messageBoxService;
2015-06-26 12:42:20 -04:00
2015-07-06 08:41:46 -04:00
AddThemeComponentCommand = new RelayCommand<ThemeComponentType>(OnAddThemeComponentCommand, t => IsMasterTheme);
DeleteThemeComponentCommand = new RelayCommand<ThemeComponent>(OnDeleteThemeComponentCommand,
2015-07-06 08:41:46 -04:00
t => IsMasterTheme && SelectedThemeComponent != null);
2015-07-25 14:02:42 -04:00
CloseCommand = new RelayCommand(async () => await OnCloseCommand());
2015-06-26 12:42:20 -04:00
var icon = new BitmapImage();
icon.BeginInit();
icon.UriSource = new Uri("pack://application:,,,/Filtration;component/Resources/Icons/Theme.ico");
2015-06-26 12:42:20 -04:00
icon.EndInit();
IconSource = icon;
2015-07-06 08:41:46 -04:00
2015-06-26 12:42:20 -04:00
}
public RelayCommand<ThemeComponentType> AddThemeComponentCommand { get; }
public RelayCommand<ThemeComponent> DeleteThemeComponentCommand { get; }
public RelayCommand CloseCommand { get; }
public bool IsMasterTheme => Components.IsMasterCollection;
public IItemFilterScript IsMasterThemeForScript { get; private set; }
2015-07-06 08:41:46 -04:00
public void InitialiseForNewTheme(ThemeComponentCollection themeComponentCollection)
2015-06-26 12:42:20 -04:00
{
Components = themeComponentCollection;
2015-07-06 08:41:46 -04:00
_filenameIsFake = true;
}
public void InitialiseForMasterTheme(IItemFilterScript script)
2015-07-06 08:41:46 -04:00
{
Components = script.ThemeComponents;
IsMasterThemeForScript = script;
_filenameIsFake = true;
2015-06-26 12:42:20 -04:00
}
public bool IsScript => false;
public bool IsTheme => true;
2015-06-26 12:42:20 -04:00
public bool IsDirty { get; private set; }
public string FilePath
{
get { return _filePath; }
set
{
_filePath = value;
Title = Filename;
}
}
public string Filename => _filenameIsFake ? FilePath : Path.GetFileName(FilePath);
2015-06-26 12:42:20 -04:00
public string Name { get; set; }
public ThemeComponentCollection Components { get; set; }
public ThemeComponent SelectedThemeComponent
{
get { return _selectedThemeComponent; }
set
{
_selectedThemeComponent = value;
RaisePropertyChanged();
}
}
2015-06-26 12:42:20 -04:00
2015-07-25 14:02:42 -04:00
public async Task SaveAsync()
2015-06-26 12:42:20 -04:00
{
2015-07-06 08:41:46 -04:00
if (IsMasterTheme) return;
2015-06-26 12:42:20 -04:00
if (_filenameIsFake)
{
2015-07-25 14:02:42 -04:00
await SaveAsAsync();
2015-06-26 12:42:20 -04:00
return;
}
try
{
2015-07-25 14:02:42 -04:00
await _themeProvider.SaveThemeAsync(this, FilePath);
2015-06-26 12:42:20 -04:00
//RemoveDirtyFlag();
}
catch (Exception e)
{
if (Logger.IsErrorEnabled)
2015-07-03 17:16:17 -04:00
{
Logger.Error(e);
2015-07-03 17:16:17 -04:00
}
_messageBoxService.Show("Save Error", "Error saving filter theme - " + e.Message, MessageBoxButton.OK, MessageBoxImage.Error);
2015-06-26 12:42:20 -04:00
}
}
2015-07-25 14:02:42 -04:00
public async Task SaveAsAsync()
2015-06-26 12:42:20 -04:00
{
2015-07-06 08:41:46 -04:00
if (IsMasterTheme) return;
2015-06-26 12:42:20 -04:00
var saveDialog = new SaveFileDialog
{
DefaultExt = ".filter",
Filter = @"Filter Theme Files (*.filtertheme)|*.filtertheme|All Files (*.*)|*.*"
};
var result = saveDialog.ShowDialog();
if (result != DialogResult.OK) return;
var previousFilePath = FilePath;
2015-07-03 17:16:17 -04:00
try
{
2015-06-26 12:42:20 -04:00
FilePath = saveDialog.FileName;
2015-07-25 14:02:42 -04:00
await _themeProvider.SaveThemeAsync(this, FilePath);
2015-06-26 12:42:20 -04:00
_filenameIsFake = false;
Title = Filename;
2015-06-26 12:42:20 -04:00
//RemoveDirtyFlag();
2015-07-03 17:16:17 -04:00
}
catch (Exception e)
{
if (Logger.IsErrorEnabled)
2015-07-03 17:16:17 -04:00
{
Logger.Error(e);
2015-07-03 17:16:17 -04:00
}
_messageBoxService.Show("Save Error", "Error saving theme file - " + e.Message, MessageBoxButton.OK,
2015-07-03 17:16:17 -04:00
MessageBoxImage.Error);
FilePath = previousFilePath;
}
2015-06-26 12:42:20 -04:00
}
2015-07-25 14:02:42 -04:00
public async Task OnCloseCommand()
2015-06-26 12:42:20 -04:00
{
2015-07-25 14:02:42 -04:00
await Close();
2015-06-26 12:42:20 -04:00
}
2015-07-25 14:02:42 -04:00
#pragma warning disable 1998
public async Task Close()
#pragma warning restore 1998
2015-07-06 08:41:46 -04:00
{
2015-07-25 14:02:42 -04:00
Messenger.Default.Send(new ThemeClosedMessage {ClosedViewModel = this});
2015-07-06 08:41:46 -04:00
}
private void OnAddThemeComponentCommand(ThemeComponentType themeComponentType)
{
switch (themeComponentType)
{
case ThemeComponentType.BackgroundColor:
case ThemeComponentType.BorderColor:
case ThemeComponentType.TextColor:
Components.Add(new ColorThemeComponent(themeComponentType, "Untitled Component",
new Color { A = 255, R = 255, G = 255, B = 255 }));
break;
case ThemeComponentType.FontSize:
Components.Add(new IntegerThemeComponent(themeComponentType, "Untitled Component", 35));
break;
2018-08-26 13:24:13 -04:00
case ThemeComponentType.AlertSound:
Components.Add(new StrIntThemeComponent(themeComponentType, "Untitled Component", "1", 100));
break;
}
}
private void OnDeleteThemeComponentCommand(ThemeComponent themeComponent)
{
if (themeComponent == null) return;
themeComponent.TerminateComponent();
Components.Remove(themeComponent);
}
2015-06-26 12:42:20 -04:00
}
}