5d8327f071
- You can now add, remove, modify and move operations - FilterChangedAny was renamed to FilterChanged, the new event for the preview follows the same naming scheme with PreviewChanged - Operations now actually verify that they're enabled (This was originally supposed to be done in the loop in Preview(), but this way seems nicer since it also removes the error label when the operation is disabled) - The foundation has been laid to make it very easy to add more operations
43 lines
1.7 KiB
C#
43 lines
1.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
|
|
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.
|
|
public AvaloniaProperty<string> errorProperty = AvaloniaProperty.Register<Operation, string>(nameof(Error), defaultValue: "");
|
|
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];
|
|
}
|
|
}
|
|
}
|
|
}
|