bfr/BFR/Operations/Operation.cs
adroslice ae7c3a3fff OperationMode overhaul, improved event handling, fixed properties
- OperationMode now designed and implemented in a way that allows serialization
- Modes are now defined using an enumeration with with attributes decorated values
- MainWindow.xaml.cs's event handlers now only listen for changes to certain properties
- Property naming conventions fixed
- AvaloniaProperties are now public + static + readonly
2019-11-21 19:07:46 +01:00

46 lines
1.7 KiB
C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Xml.Serialization;
using Avalonia;
using BFR.DataModels;
namespace BFR.Operations
{
public abstract class Operation : AvaloniaObject
{
// Needs to be avalonia property to update UI with any potential error.
[XmlIgnore]
public static readonly AvaloniaProperty<string> ErrorProperty = AvaloniaProperty.Register<MainWindow, string>(nameof(Error), defaultValue: "");
[XmlIgnore]
public string Error { get => GetValue(ErrorProperty); set => SetValue(ErrorProperty, value); }
public bool IsEnabled { get; set; } = true;
public abstract string Name { get; }
public abstract string Description { get; }
protected abstract void ApplyToInternal(IList<FileModel> files);
public void ApplyTo(IList<FileModel> files)
{
// Backup of all changes thus far, in case something goes wrong
var backup = files.Select(file => file.Path).ToList();
try
{
if(IsEnabled)
ApplyToInternal(files); // Try to apply operation
Error = ""; // If successul, remove any previous error
}
catch (OperationException e) { Error = e.Message; } // If expected, show user a concise and descriptive message
catch (Exception e) { Error = $"Unexpected {e}"; } // If not, hint with the exception thrown that this is unexpected behaviour
finally
{
if (Error != "") // If something went wrong, restore backup
for (int i = 0; i < files.Count; i++)
files[i].Path = backup[i];
}
}
}
}