bfr/BFR/MainWindow.xaml.cs
adroslice 0eb940012d Implemented preview TODO, modified exception handling
- Better handles unexpected exceptions
- Expected exceptions should be OperationException
- Leaves files unchanged even if the exception occurs at *any* point of the operation
- TODO: Probably have to optimize this.
- Operations now operate on IList<T> instead of List<T> to maintain compatibility with AvaloniaList<T>
- Slightly better comments
2019-11-16 02:47:07 +01:00

62 lines
2.1 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Avalonia.Input;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Interactivity;
using BFR.Helpers;
using BFR.DataModels;
namespace BFR
{
public partial class MainWindow : Window
{
public async Task OpenDirectoryButtonClick() => OpenDirectory(await new OpenFolderDialog() { Directory = WorkingDirectory }.ShowAsync(this));
public void OpenDirectory(string directory)
{
WorkingDirectory = directory;
AllFiles.ReplaceAll(Directory.GetFiles(WorkingDirectory).Select(x => new FileModel(x)));
Filter();
}
public void FilterChangedAny(object sender, KeyEventArgs e) => Filter();
public void FilterChangedAny(object sender, RoutedEventArgs e) => Filter();
public void Filter()
{
// 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))));
Preview();
}
public void Preview()
{
// Reset all files to how they currently exist
foreach (var file in Files)
file.Path = file.OldPath;
// Apply operations
foreach (var operation in Operations)
operation.ApplyTo(Files);
// 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 MainWindow() => InitializeComponent();
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
DataContext = this;
OpenDirectory(Environment.OSVersion.Platform == PlatformID.Win32NT ? @"C:\Users" : @"\home");
}
}
}