diff --git a/FileSystemItem.cs b/FileSystemItem.cs index 047e305..311955a 100644 --- a/FileSystemItem.cs +++ b/FileSystemItem.cs @@ -1,33 +1,43 @@ using System.ComponentModel; using System.Runtime.CompilerServices; -namespace CommanderApp +namespace CommanderApp; + +public class FileSystemItem : INotifyPropertyChanged { - public class FileSystemItem : INotifyPropertyChanged - { - public string Name { get; set; } - public string FullName { get; set; } - public bool IsDirectory { get; set; } - public string Icon => IsDirectory ? "folder.png" : "file.png"; + public string Name { get; set; } = string.Empty; + public string FullName { get; set; } = string.Empty; + public bool IsDirectory { get; set; } - private bool _isSelected; - public bool IsSelected + public string DisplayText + { + get { - get => _isSelected; - set - { - _isSelected = value; - OnPropertyChanged(); - } - } + if (string.IsNullOrWhiteSpace(Name)) + return "[No Name]"; + + if (Name == "..") + return "⬆️ .."; - // Событие из INotifyPropertyChanged - public event PropertyChangedEventHandler? PropertyChanged; + return IsDirectory ? $"📁 {Name}" : $"📄 {Name}"; + } + } - // Защищённый метод для вызова события - protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) + private bool _isSelected; + public bool IsSelected + { + get => _isSelected; + set { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + _isSelected = value; + OnPropertyChanged(); } } -} + + public event PropertyChangedEventHandler? PropertyChanged; + + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } +} \ No newline at end of file diff --git a/FileSystemService.cs b/FileSystemService.cs index 3055c3b..80838bd 100644 --- a/FileSystemService.cs +++ b/FileSystemService.cs @@ -1,35 +1,48 @@ -namespace CommanderApp +using System.IO; +using System.Linq; +using Microsoft.Maui.Storage; + +namespace CommanderApp; + +public class FileSystemService : IFileSystemService { - public class FileSystemService : IFileSystemService + public string GetRootPath() { - public string GetRootPath() - { #if ANDROID || IOS || MACCATALYST - return FileSystem.Current.AppDataDirectory; + return FileSystem.AppDataDirectory; #else - return Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + // Убедимся, что путь корректен + return Path.GetFullPath(path); #endif - } + } + + public IEnumerable GetDirectoryContents(string path) + { + // Нормализуем путь + path = Path.GetFullPath(path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)); - public IEnumerable GetDirectoryContents(string path) + if (!Directory.Exists(path)) { - if (!Directory.Exists(path)) - return Enumerable.Empty(); + System.Diagnostics.Debug.WriteLine($"Directory does not exist: {path}"); + return Enumerable.Empty(); + } - var items = new List(); + var items = new List(); - // Добавляем ".." если не корень - var parent = Directory.GetParent(path); - if (parent != null) + var parent = Directory.GetParent(path); + if (parent != null) + { + items.Add(new FileSystemItem { - items.Add(new FileSystemItem - { - Name = "..", - FullName = parent.FullName, - IsDirectory = true - }); - } + Name = "..", + FullName = parent.FullName, + IsDirectory = true + }); + } + try + { var dirs = Directory.GetDirectories(path) .Select(d => new FileSystemItem { @@ -48,8 +61,22 @@ items.AddRange(dirs.OrderBy(d => d.Name)); items.AddRange(files.OrderBy(f => f.Name)); - - return items; } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Error reading directory {path}: {ex.Message}"); + // Возвращаем хотя бы ".." если возможно + if (items.Count == 0 && Directory.GetParent(path) != null) + { + items.Add(new FileSystemItem + { + Name = "..", + FullName = Directory.GetParent(path)!.FullName, + IsDirectory = true + }); + } + } + + return items; } -} +} \ No newline at end of file diff --git a/MainPage.xaml b/MainPage.xaml index 517dfbf..f38dd28 100644 --- a/MainPage.xaml +++ b/MainPage.xaml @@ -4,9 +4,8 @@ x:Class="CommanderApp.MainPage" Title="MAUI Commander"> - - +