Added commit and undo + Bugfix

- Bugfix: The Name-filter was based on Name and FullName, instead of how it is now, OldName and OldFullName. This would previously cause the filter to apply to the already previewed changes.
This commit is contained in:
adroslice 2019-11-17 02:41:05 +01:00
parent 5d8327f071
commit e0fe30b380
3 changed files with 44 additions and 6 deletions

View File

@ -68,8 +68,8 @@
<!-- Commit and Undo Buttons -->
<Border Grid.Row="2" Classes="ConnectUp">
<Grid ColumnDefinitions="*,*">
<Button Grid.Column="0" Content="Undo"/>
<Button Grid.Column="1" Content="Rename All"/>
<Button Grid.Column="0" Content="Undo" Command="{Binding Undo}" IsEnabled="{Binding !!UndoCount}"/>
<Button Grid.Column="1" Content="Rename All" Command="{Binding Commit}" IsEnabled="{Binding IsCommitButtonEnabled}"/>
</Grid>
</Border>
</Grid>

View File

@ -32,7 +32,7 @@ namespace BFR
// Filter all files in the directory for those satisfying the given filters
Files.ReplaceAll(AllFiles.Where(x =>
(FilterExtension == "" || x.Extension == FilterExtension)
&& (FilterFullName ? x.FullName : x.Name).RegexContains(FilterRegex ? FilterPattern : Regex.Escape(FilterPattern))));
&& (FilterFullName ? x.OldFullName : x.OldName).RegexContains(FilterRegex ? FilterPattern : Regex.Escape(FilterPattern))));
Preview();
}
@ -51,15 +51,23 @@ namespace BFR
foreach (var operation in Operations)
operation.ApplyTo(Files);
// Validate that there are any changes, and that the new file names are all unique.
IsCommitButtonEnabled =
Files.Any(x => x.Path != x.OldPath) // Check for changes
&& Files.GroupBy(x => x.Path).All(g => g.Count() == 1) // Check for duplicates
&& !Files.Any(x => x.FullName == ""); // Check for invalid file names; TODO: Further validation
// Refresh the file list to guarantee that changes are displayed. TODO: Find a better way to do this.
Files.ReplaceAll(new List<FileModel>(Files));
}
public void AddOperation()
{
if (Operations.Count >= 1)
Operations.Insert(
SelectedOperation >= 0 ? SelectedOperation + 1 : Operations.Count,
OperationTypes[SelectedOperationType].Create());
else Operations.Add(OperationTypes[SelectedOperationType].Create());
Preview();
}
@ -81,6 +89,27 @@ namespace BFR
Preview();
}
public void Commit()
{
foreach (var file in Files)
if (file.OldPath != file.Path)
File.Move(file.OldPath, file.Path);
UndoStack.Push(new List<FileModel>(Files));
UndoCount = UndoStack.Count;
OpenDirectory(WorkingDirectory);
}
public void Undo()
{
foreach (var file in UndoStack.Pop())
File.Move(file.Path, file.OldPath);
UndoCount = UndoStack.Count;
OpenDirectory(WorkingDirectory);
}
public MainWindow() => InitializeComponent();
private void InitializeComponent()
{

View File

@ -4,6 +4,7 @@ using Avalonia.Controls;
using BFR.DataModels;
using BFR.Operations;
using System.Collections.Generic;
namespace BFR
{
@ -24,6 +25,14 @@ namespace BFR
OperationType.Make<Overwrite>()
};
public readonly AvaloniaProperty<bool> isCommitButtonEnabled =
AvaloniaProperty.Register<MainWindow, bool>("IsCommitButtonEnabled", defaultValue: false);
public bool IsCommitButtonEnabled { get => GetValue(isCommitButtonEnabled); set => SetValue(isCommitButtonEnabled, value); }
public readonly AvaloniaProperty<int> undoCount =
AvaloniaProperty.Register<MainWindow, int>("UndoCount", defaultValue: 0);
public int UndoCount { get => GetValue(undoCount); set => SetValue(undoCount, value); }
private readonly Stack<List<FileModel>> UndoStack = new Stack<List<FileModel>>();
// Filters
public string FilterExtension { get; set; } = "";
public string FilterPattern { get; set; } = "";