Skip to content

Commit

Permalink
add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Oct 13, 2024
1 parent 12be9a9 commit 9615fc3
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 55 deletions.
5 changes: 3 additions & 2 deletions SlimViews/Rename.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* PROGRAMER: Peter Geinitz (Wayfarer)
*/

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Windows;

Expand Down Expand Up @@ -34,7 +35,7 @@ public Rename()
public Rename(Dictionary<int, string> observer)
{
InitializeComponent();
View.Observer = observer;
View.Observer = new ConcurrentDictionary<int, string>(observer);
}

/// <summary>
Expand All @@ -43,6 +44,6 @@ public Rename(Dictionary<int, string> observer)
/// <value>
/// The observer.
/// </value>
internal Dictionary<int, string> Observer => View.Observer;
internal Dictionary<int, string> Observer => new Dictionary<int, string>(View.Observer);
}
}
241 changes: 188 additions & 53 deletions SlimViews/RenameView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// ReSharper disable EventNeverSubscribedTo.Global

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -116,15 +117,15 @@ public string Replacer
/// The explorer command.
/// </value>
public ICommand RemoveAppendageCommand => _removeAppendageCommand ??=
new DelegateCommand<object>(RemoveAppendageActionAsync, CanExecute);
new AsyncDelegateCommand<object>(RemoveAppendageActionAsync, CanExecute);

/// <summary>
/// Gets the add command.
/// </summary>
/// <value>
/// The add command.
/// </value>
public ICommand AddCommand => _addCommand ??= new DelegateCommand<object>(AddActionAsync, CanExecute);
public ICommand AddCommand => _addCommand ??= new AsyncDelegateCommand<object>(AddActionAsync, CanExecute);

/// <summary>
/// Gets the remove command.
Expand All @@ -133,7 +134,7 @@ public string Replacer
/// The remove command.
/// </value>
public ICommand RemoveCommand =>
_removeCommand ??= new DelegateCommand<object>(RemoveActionAsync, CanExecute);
_removeCommand ??= new AsyncDelegateCommand<object>(RemoveActionAsync, CanExecute);

/// <summary>
/// Gets the reorder command.
Expand All @@ -142,7 +143,7 @@ public string Replacer
/// The reorder command.
/// </value>
public ICommand ReorderCommand =>
_reorderCommand ??= new DelegateCommand<object>(ReorderCommandActionAsync, CanExecute);
_reorderCommand ??= new AsyncDelegateCommand<object>(ReorderCommandActionAsync, CanExecute);

/// <summary>
/// Gets the reorder command.
Expand All @@ -151,7 +152,7 @@ public string Replacer
/// The reorder command.
/// </value>
public ICommand ReplaceCommand =>
_replaceCommand ??= new DelegateCommand<object>(ReplaceCommandActionAsync, CanExecute);
_replaceCommand ??= new AsyncDelegateCommand<object>(ReplaceCommandActionAsync, CanExecute);

/// <summary>
/// Gets the appendages at command.
Expand All @@ -160,15 +161,15 @@ public string Replacer
/// The appendages at command.
/// </value>
public ICommand AppendagesAtCommand =>
_appendagesAtCommand ??= new DelegateCommand<object>(AppendagesAtActionAsync, CanExecute);
_appendagesAtCommand ??= new AsyncDelegateCommand<object>(AppendagesAtActionAsync, CanExecute);

/// <summary>
/// Gets or sets the observer.
/// </summary>
/// <value>
/// The observer.
/// </value>
internal Dictionary<int, string> Observer { get; set; }
internal ConcurrentDictionary<int, string> Observer { get; set; }

/// <summary>
/// Sets the property.
Expand Down Expand Up @@ -202,82 +203,214 @@ public bool CanExecute(object obj)
}

/// <summary>
/// Removes an appendage from the file names in the observer list.
/// Removes Appendage in File Name
/// </summary>
/// <param name="obj">The object, typically not used in this action.</param>
private async void RemoveAppendageActionAsync(object obj)
/// <param name="obj">The object.</param>
private async Task RemoveAppendageActionAsync(object obj)
{
var observer = new ConcurrentDictionary<int, string>(Observer);
if (Replacement == null) return;

await ProcessFileRenamingAsync((str) => str.RemoveAppendage(Replacement));
try
{
foreach (var (key, value) in Observer)
{
var str = Path.GetFileName(value);

var file = str.RemoveAppendage(Replacement);

if (string.IsNullOrEmpty(file) ||
string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue;

var directory = Path.GetDirectoryName(value);
if (string.IsNullOrEmpty(directory)) continue;

var target = Path.Combine(directory, file);

var check = await FileHandleRename.RenameFile(value, target);
if (check) observer[key] = target;
}

SlimViewerRegister.Changed = true;
Observer = observer;
}
catch (FileHandlerException ex)
{
Trace.WriteLine(ex);
_ = MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// Adds an appendage to the file names in the observer list.
/// Adds Elements in File Name
/// </summary>
/// <param name="obj">The object, typically not used in this action.</param>
private async void AddActionAsync(object obj)
private async Task AddActionAsync(object obj)
{
// Make a copy of the Observer dictionary to modify it
var observer = new Dictionary<int, string>(Observer);

// Ensure that the Replacement is not null
if (Replacement == null) return;

await ProcessFileRenamingAsync((str) => str.AddAppendage(Replacement));
foreach (var (key, value) in Observer)
{
// Get the filename without the directory path
var str = Path.GetFileName(value);

// Use the AddAppendage method to create a new filename
var file = str.AddAppendage(Replacement);

// If the new filename is the same as the original, skip it
if (string.IsNullOrEmpty(file) || string.Equals(str, file, StringComparison.OrdinalIgnoreCase))
continue;

// Get the directory path
var directory = Path.GetDirectoryName(value);
if (string.IsNullOrEmpty(directory))
continue;

// Combine the directory with the new filename
var target = Path.Combine(directory, file);

// Attempt to rename the file asynchronously
var check = await FileHandleRename.RenameFile(value, target);
if (check)
{
// Update the observer dictionary with the new filename
observer[key] = target;
}
}

// Update the Observer property with the modified dictionary
Observer = new ConcurrentDictionary<int, string>(observer);
}


/// <summary>
/// Removes a specified part from the file names in the observer list.
/// Remove Elements in File Name
/// </summary>
/// <param name="obj">The object, typically not used in this action.</param>
private async void RemoveActionAsync(object obj)
private async Task RemoveActionAsync(object obj)
{
var observer = new ConcurrentDictionary<int, string>(Observer);
if (Replacement == null) return;

await ProcessFileRenamingAsync((str) => str.ReplacePart(Replacement, string.Empty));
try
{
foreach (var (key, value) in Observer)
{
var str = Path.GetFileName(value);

var file = str.ReplacePart(Replacement, string.Empty);

if (string.IsNullOrEmpty(file) ||
string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue;

var directory = Path.GetDirectoryName(value);
if (string.IsNullOrEmpty(directory)) continue;

var target = Path.Combine(directory, file);

var check = await FileHandleRename.RenameFile(value, target);
if (check) observer[key] = value;
}

SlimViewerRegister.Changed = true;
Observer = new ConcurrentDictionary<int, string>(observer);
}
catch (FileHandlerException ex)
{
Trace.WriteLine(ex);
_ = MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// Reorders elements in the file names by numbers.
/// Reorder Elements in File Name
/// </summary>
/// <param name="obj">The object, typically not used in this action.</param>
private async void ReorderCommandActionAsync(object obj)
private async Task ReorderCommandActionAsync(object obj)
{
await ProcessFileRenamingAsync((str) =>
var observer = new ConcurrentDictionary<int, string>(Observer);
try
{
foreach (var (key, value) in Observer)
{
var str = Path.GetFileNameWithoutExtension(value);
var ext = Path.GetExtension(value);

var file = str.ReOrderNumbers();
if (string.IsNullOrEmpty(file) ||
string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue;

file = string.Concat(file, ext);

var directory = Path.GetDirectoryName(value);
if (string.IsNullOrEmpty(directory)) continue;

file = Path.Combine(directory, file);

var check = await FileHandleRename.RenameFile(value, file);
if (check) observer[key] = file;
}

SlimViewerRegister.Changed = true;
Observer = observer;
}
catch (FileHandlerException ex)
{
var file = str.ReOrderNumbers();
return string.IsNullOrEmpty(file) ? null : file;
}, true);
Trace.WriteLine(ex);
_ = MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// Replaces a specified part of the file names with a new value in the observer list.
/// Replaces part or the string command.
/// </summary>
/// <param name="obj">The object, typically not used in this action.</param>
private async void ReplaceCommandActionAsync(object obj)
/// <param name="obj">The object.</param>
private async Task ReplaceCommandActionAsync(object obj)
{
var observer = new ConcurrentDictionary<int, string>(Observer);
if (Replacer == null) return;

await ProcessFileRenamingAsync((str) => str.ReplacePart(Replacement, Replacer));
}
try
{
foreach (var (key, value) in Observer)
{
var str = Path.GetFileNameWithoutExtension(value);
var ext = Path.GetExtension(value);

/// <summary>
/// Removes a specified number of characters from the start of the file names in the observer list.
/// </summary>
/// <param name="obj">The object, typically not used in this action.</param>
private async void AppendagesAtActionAsync(object obj)
{
if (Numbers <= 0) return;
var file = str.ReplacePart(Replacement, Replacer);
if (string.IsNullOrEmpty(file) ||
string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue;

file = string.Concat(file, ext);

await ProcessFileRenamingAsync((str) => str.Length <= Numbers ? null : str.Remove(0, Numbers));
var directory = Path.GetDirectoryName(value);
if (string.IsNullOrEmpty(directory)) continue;

file = Path.Combine(directory, file);

var check = await FileHandleRename.RenameFile(value, file);
if (check) observer[key] = file;
}

SlimViewerRegister.Changed = true;
Observer = observer;
}
catch (FileHandlerException ex)
{
Trace.WriteLine(ex);
_ = MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// Processes file renaming for all entries in the observer list based on the provided modification function.
/// Remove Appendage at number count action.
/// </summary>
/// <param name="modifyFileName">A function that defines how to modify the file name.</param>
/// <param name="addExtension">Indicates whether to append the original file extension to the modified name.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
private async Task ProcessFileRenamingAsync(Func<string, string> modifyFileName, bool addExtension = false)
/// <param name="obj">The object.</param>
private async Task AppendagesAtActionAsync(object obj)
{
var observer = new Dictionary<int, string>(Observer);
var observer = new ConcurrentDictionary<int, string>(Observer);
if (Numbers <= 0) return;

try
{
Expand All @@ -286,19 +419,21 @@ private async Task ProcessFileRenamingAsync(Func<string, string> modifyFileName,
var str = Path.GetFileNameWithoutExtension(value);
var ext = Path.GetExtension(value);

var file = modifyFileName(str);
if (string.IsNullOrEmpty(file) || string.Equals(str, file, StringComparison.OrdinalIgnoreCase))
continue;
if (str.Length <= Numbers) continue;

var file = str.Remove(0, Numbers);

if (addExtension)
file = string.Concat(file, ext);
if (string.IsNullOrEmpty(file)) continue;

file = string.Concat(file, ext);

var directory = Path.GetDirectoryName(value);
if (string.IsNullOrEmpty(directory)) continue;

var target = Path.Combine(directory, file);
var check = await FileHandleRename.RenameFile(value, target);
if (check) observer[key] = target;
file = Path.Combine(directory, file);

var check = await FileHandleRename.RenameFile(value, file);
if (check) observer[key] = file;
}

SlimViewerRegister.Changed = true;
Expand Down

0 comments on commit 9615fc3

Please sign in to comment.