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; + } + } +}