You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
8.2 KiB
C#

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls;
namespace CommanderApp;
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
private readonly IFileSystemService _fileService;
// Коллекции больше не нужны для привязки, но оставим для логики
public ObservableCollection<FileSystemItem> LeftItems { get; } = new();
public ObservableCollection<FileSystemItem> RightItems { get; } = new();
private string _leftPath = string.Empty;
private string _rightPath = string.Empty;
public string LeftPath
{
get => _leftPath;
set
{
if (_leftPath != value)
{
_leftPath = value;
NotifyPropertyChanged();
}
}
}
public string RightPath
{
get => _rightPath;
set
{
if (_rightPath != value)
{
_rightPath = value;
NotifyPropertyChanged();
}
}
}
private string _currentLeftPath = string.Empty;
private string _currentRightPath = string.Empty;
private FileSystemItem? _selectedLeftItem;
private FileSystemItem? _selectedRightItem;
public MainPage(IFileSystemService fileService)
{
InitializeComponent();
_fileService = fileService;
BindingContext = this;
var root = _fileService.GetRootPath();
LoadDirectory(root, true);
LoadDirectory(root, false);
}
private void LoadDirectory(string path, bool isLeft)
{
var items = _fileService.GetDirectoryContents(path).ToList();
var panel = isLeft ? LeftPanel : RightPanel;
var targetItems = isLeft ? LeftItems : RightItems;
var currentPathProperty = isLeft ? nameof(_currentLeftPath) : nameof(_currentRightPath);
// Очистка панели
panel.Clear();
targetItems.Clear();
// Заполнение
foreach (var item in items)
{
targetItems.Add(item);
var button = new Button
{
Text = item.DisplayText,
TextColor = Colors.Black,
Padding = new Thickness(10),
HeightRequest = 40,
BackgroundColor = Colors.Transparent,
BorderWidth = 0,
CommandParameter = item
};
button.Clicked += isLeft ? OnLeftItemActivated : OnRightItemActivated;
// Добавляем визуальные состояния
var normal = new VisualState { Name = "Normal" };
var focused = new VisualState { Name = "Focused" };
focused.Setters.Add(new Setter { Property = Button.BorderColorProperty, Value = Colors.Black });
focused.Setters.Add(new Setter { Property = Button.BorderWidthProperty, Value = 2 });
var pressed = new VisualState { Name = "Pressed" };
pressed.Setters.Add(new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromArgb("#e0e0e0") });
var commonStates = new VisualStateGroup { Name = "CommonStates" };
commonStates.States.Add(normal);
commonStates.States.Add(focused);
commonStates.States.Add(pressed);
var groups = new VisualStateGroupList();
groups.Add(commonStates);
VisualStateManager.SetVisualStateGroups(button, groups);
panel.Add(button);
}
// Обновление пути
if (isLeft)
{
_currentLeftPath = path;
LeftPath = path;
}
else
{
_currentRightPath = path;
RightPath = path;
}
}
private void OnLeftItemActivated(object sender, EventArgs e)
{
var button = (Button)sender;
var item = (FileSystemItem)button.CommandParameter;
_selectedLeftItem = item;
_selectedRightItem = null;
if (item.IsDirectory)
{
LoadDirectory(item.FullName, true);
_selectedLeftItem = null;
}
}
private void OnRightItemActivated(object sender, EventArgs e)
{
var button = (Button)sender;
var item = (FileSystemItem)button.CommandParameter;
_selectedRightItem = item;
_selectedLeftItem = null;
if (item.IsDirectory)
{
LoadDirectory(item.FullName, false);
_selectedRightItem = null;
}
}
private async void OnCopyClicked(object sender, EventArgs e)
{
await ProcessFileOperation(async (src, destDir) =>
{
if (Directory.Exists(src))
await CopyDirectory(src, Path.Combine(destDir, Path.GetFileName(src)));
else
File.Copy(src, Path.Combine(destDir, Path.GetFileName(src)), overwrite: true);
}, "Copy");
}
private async void OnMoveClicked(object sender, EventArgs e)
{
await ProcessFileOperation((src, destDir) =>
{
var dest = Path.Combine(destDir, Path.GetFileName(src));
if (Directory.Exists(src))
Directory.Move(src, dest);
else
File.Move(src, dest, overwrite: true);
return Task.CompletedTask;
}, "Move");
}
private async void OnDeleteClicked(object sender, EventArgs e)
{
var item = _selectedLeftItem ?? _selectedRightItem;
if (item == null) return;
var confirm = await DisplayAlert("Delete", $"Delete '{item.Name}'?", "Yes", "No");
if (confirm)
{
if (item.IsDirectory)
Directory.Delete(item.FullName, recursive: true);
else
File.Delete(item.FullName);
LoadDirectory(_currentLeftPath, true);
LoadDirectory(_currentRightPath, false);
}
}
private async void OnMkdirClicked(object sender, EventArgs e)
{
var result = await DisplayPromptAsync("New Folder", "Folder name:", "Create", "Cancel");
if (string.IsNullOrWhiteSpace(result)) return;
string targetPath = (_selectedLeftItem != null || LeftItems.Count > 0) ? _currentLeftPath : _currentRightPath;
string newPath = Path.Combine(targetPath, result.Trim());
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
LoadDirectory(_currentLeftPath, true);
LoadDirectory(_currentRightPath, false);
}
else
{
await DisplayAlert("Error", "Folder already exists.", "OK");
}
}
private void OnExitClicked(object sender, EventArgs e)
{
Application.Current?.Quit();
}
private async Task CopyDirectory(string sourceDir, string targetDir)
{
Directory.CreateDirectory(targetDir);
foreach (var file in Directory.GetFiles(sourceDir))
{
await Task.Run(() => File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), overwrite: true));
}
foreach (var dir in Directory.GetDirectories(sourceDir))
{
await CopyDirectory(dir, Path.Combine(targetDir, Path.GetFileName(dir)));
}
}
private async Task ProcessFileOperation(Func<string, string, Task> operation, string actionName)
{
var srcItem = _selectedLeftItem ?? _selectedRightItem;
if (srcItem == null)
{
await DisplayAlert("Error", "Select a file or folder first.", "OK");
return;
}
string destDir = (srcItem == _selectedLeftItem) ? _currentRightPath : _currentLeftPath;
try
{
await operation(srcItem.FullName, destDir);
LoadDirectory(_currentLeftPath, true);
LoadDirectory(_currentRightPath, false);
}
catch (Exception ex)
{
await DisplayAlert("Error", $"{actionName} failed: {ex.Message}", "OK");
}
}
public new event PropertyChangedEventHandler? PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}