From a1d76d192049079782649c1597db5fdc9c30d2c7 Mon Sep 17 00:00:00 2001 From: adroslice Date: Fri, 22 Nov 2019 19:34:21 +0100 Subject: [PATCH] Added Add-Operation + UI --- BFR/MainWindow.xaml | 20 ++++++++++++++ BFR/MainWindowUIProperties.cs | 1 + BFR/Operations/Add.cs | 52 +++++++++++++++++++++++++++++++++++ BFR/Operations/AddMode.cs | 37 +++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 BFR/Operations/Add.cs create mode 100644 BFR/Operations/AddMode.cs diff --git a/BFR/MainWindow.xaml b/BFR/MainWindow.xaml index cd35980..e98ffcc 100644 --- a/BFR/MainWindow.xaml +++ b/BFR/MainWindow.xaml @@ -260,5 +260,25 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/BFR/MainWindowUIProperties.cs b/BFR/MainWindowUIProperties.cs index 7ae2a2a..d36b936 100644 --- a/BFR/MainWindowUIProperties.cs +++ b/BFR/MainWindowUIProperties.cs @@ -27,6 +27,7 @@ namespace BFR OperationType.Make(), OperationType.Make(), OperationType.Make(), + OperationType.Make(), }; public static readonly AvaloniaProperty IsCommitButtonEnabledProperty = diff --git a/BFR/Operations/Add.cs b/BFR/Operations/Add.cs new file mode 100644 index 0000000..ca1b4b6 --- /dev/null +++ b/BFR/Operations/Add.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Xml.Serialization; +using System.Linq; + +using Avalonia; + +using BFR.DataModels; + +namespace BFR.Operations +{ + public class Add : Operation + { + public override string Name => nameof(Add); + public override string Description => "Adds the specified text to the file name."; + + [XmlIgnore] + public static readonly AvaloniaProperty ModeProperty = + AvaloniaProperty.Register(nameof(Mode), AddMode.Prepend); + public AddMode Mode { get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); } + + public AddOperationMode[] Modes => AddOperationMode.Modes; + + public int InsertPosition { get; set; } = 0; + public string Text { get; set; } = ""; + public bool FullName { get; set; } = false; + + protected override void ApplyToInternal(IList files) + { + // Fail conditions: Out of bounds insert + var shortestLength = files.Min(x => FullName ? x.FullName.Length : x.Name.Length); + if (Mode == AddMode.Insert && InsertPosition > shortestLength) throw new OperationException($"Index can't be greater than the shortest file name length ({shortestLength})."); + + // Apply operation + foreach (var file in files) + { + var newName = FullName ? file.FullName : file.Name; + + newName = Mode switch + { + AddMode.Prepend => $"{Text}{newName}", + AddMode.Append => $"{newName}{Text}", + AddMode.Insert => newName.Insert(InsertPosition, Text), + _ => newName + }; + + if (FullName) file.FullName = newName; + else file.Name = newName; + } + } + } +} diff --git a/BFR/Operations/AddMode.cs b/BFR/Operations/AddMode.cs new file mode 100644 index 0000000..b1b6991 --- /dev/null +++ b/BFR/Operations/AddMode.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +using BFR.Helpers; + +namespace BFR.Operations +{ + public class AddOperationMode : OperationMode + { + public bool IsPrepend => Index == AddMode.Prepend; + public bool IsAppend => Index == AddMode.Append; + public bool IsInsert => Index == AddMode.Insert; + + public readonly static AddOperationMode[] Modes = All(); + + public AddOperationMode(AddMode index, string name, string description) : + base(index, name, description) { } + + protected static AddOperationMode[] All() => ((IEnumerable)Enum.GetValues(typeof(AddMode))).Select(x => + new AddOperationMode( + x, + x.GetAttribute().DisplayName, + x.GetAttribute().Description + )).ToArray(); + } + + public enum AddMode + { + [OperationMode(nameof(Prepend), "Inserts before the file name.")] + Prepend, + [OperationMode(nameof(Append), "Inserts after the file name.")] + Append, + [OperationMode(nameof(Insert), "Inserts at the specified index.")] + Insert + } +}