From 704b7ea1a967b21ac7f27720b013503a7a65056d Mon Sep 17 00:00:00 2001 From: adroslice Date: Sat, 16 Nov 2019 02:00:17 +0100 Subject: [PATCH] Added internal Operation Infrastructure + Example - Overwrite might be a bad example, because it has no fail conditions. The intended behaviour for those is to throw an exception for invalid parameters, with an error text describing to the user what went wrong. - Fail condition check may be moved to a seperate abstract method, forcing implementers to at least give fail conditions a thought. - Minor refactor - TODO: Better folder structure to group operations (Looking for suggestions) --- BFR/MainWindow.xaml.cs | 8 ++------ BFR/Operations/Operation.cs | 29 +++++++++++++++++++++++++++++ BFR/Operations/OperationType.cs | 20 ++++++++++++++++++++ BFR/Operations/Overwrite.cs | 27 +++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 BFR/Operations/Operation.cs create mode 100644 BFR/Operations/OperationType.cs create mode 100644 BFR/Operations/Overwrite.cs diff --git a/BFR/MainWindow.xaml.cs b/BFR/MainWindow.xaml.cs index d95b0b0..82bfbf1 100644 --- a/BFR/MainWindow.xaml.cs +++ b/BFR/MainWindow.xaml.cs @@ -17,9 +17,7 @@ namespace BFR { public partial class MainWindow : Window { - public async Task OpenDirectoryButtonClick() => - OpenDirectory(await new OpenFolderDialog() { Directory = WorkingDirectory }.ShowAsync(this)); - + public async Task OpenDirectoryButtonClick() => OpenDirectory(await new OpenFolderDialog() { Directory = WorkingDirectory }.ShowAsync(this)); public void OpenDirectory(string directory) { WorkingDirectory = directory; @@ -51,9 +49,7 @@ namespace BFR Files.ReplaceAll(new List(Files)); } - public MainWindow() => - InitializeComponent(); - + public MainWindow() => InitializeComponent(); private void InitializeComponent() { AvaloniaXamlLoader.Load(this); diff --git a/BFR/Operations/Operation.cs b/BFR/Operations/Operation.cs new file mode 100644 index 0000000..302ca93 --- /dev/null +++ b/BFR/Operations/Operation.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; + +using Avalonia; + +using BFR.DataModels; + +namespace BFR.Operations +{ + public abstract class Operation : AvaloniaObject + { + // Needs to be an avalonia property to update the UI with with any potential error. + public AvaloniaProperty errorProperty = AvaloniaProperty.Register(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; } + public abstract OperationType OperationType { get; } + + protected abstract void ApplyToInternal(List files); + + // Updates the UI with the appropriate error if the operation fails + public void ApplyTo(List files) + { + try { ApplyToInternal(files); Error = ""; } + catch (Exception e) { Error = e.Message; } + } + } +} diff --git a/BFR/Operations/OperationType.cs b/BFR/Operations/OperationType.cs new file mode 100644 index 0000000..20a353c --- /dev/null +++ b/BFR/Operations/OperationType.cs @@ -0,0 +1,20 @@ +using System; + +namespace BFR.Operations +{ + public class OperationType + { + public string Name { get; } + public string Description { get; } + public Func Create { get; } + + internal static OperationType Make() where T : Operation, new() + { + var pilot = new T(); + return new OperationType(pilot.Name, pilot.Description, () => new T()); + } + + private OperationType(string name, string description, Func create) => + (Name, Description, Create) = (name, description, create); + } +} diff --git a/BFR/Operations/Overwrite.cs b/BFR/Operations/Overwrite.cs new file mode 100644 index 0000000..96153bb --- /dev/null +++ b/BFR/Operations/Overwrite.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; + +using BFR.DataModels; + +namespace BFR.Operations +{ + public class Overwrite : Operation + { + // Operation Info + public override string Name => nameof(Overwrite); + public override string Description => "Overwrites all file names. To produce a valid result this has to be combined with numbering."; + public override OperationType OperationType { get; } = OperationType.Make(); + + // Operation Parameters + public string Replacement { get; set; } = ""; + public bool FullName { get; set; } = false; + + protected override void ApplyToInternal(List files) + { + // Fail conditions: No fail conditions + // Apply Operation + foreach (var file in files) + if (FullName) file.FullName = Replacement; + else file.Name = Replacement; + } + } +}