|
| 1 | +using System; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 7 | +using CommunityToolkit.Mvvm.Input; |
| 8 | +using MiniSoftware; |
| 9 | + |
| 10 | +namespace MiniPdf.Gui.ViewModels; |
| 11 | + |
| 12 | +public partial class MainWindowViewModel : ObservableObject |
| 13 | +{ |
| 14 | + [ObservableProperty] |
| 15 | + private string _statusText = "Ready"; |
| 16 | + |
| 17 | + [ObservableProperty] |
| 18 | + private bool _isConverting; |
| 19 | + |
| 20 | + [ObservableProperty] |
| 21 | + private double _progress; |
| 22 | + |
| 23 | + [ObservableProperty] |
| 24 | + private string _fontDirectory = string.Empty; |
| 25 | + |
| 26 | + public ObservableCollection<FileItem> Files { get; } = new(); |
| 27 | + |
| 28 | + public MainWindowViewModel() |
| 29 | + { |
| 30 | + Files.CollectionChanged += (_, _) => ConvertAllCommand.NotifyCanExecuteChanged(); |
| 31 | + } |
| 32 | + |
| 33 | + public void AddFiles(params string[] paths) |
| 34 | + { |
| 35 | + foreach (var path in paths) |
| 36 | + { |
| 37 | + if (Directory.Exists(path)) |
| 38 | + { |
| 39 | + var files = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly) |
| 40 | + .Where(f => IsSupportedFile(f)); |
| 41 | + foreach (var file in files) |
| 42 | + AddFileIfNotExists(file); |
| 43 | + } |
| 44 | + else if (File.Exists(path) && IsSupportedFile(path)) |
| 45 | + { |
| 46 | + AddFileIfNotExists(path); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void AddFileIfNotExists(string filePath) |
| 52 | + { |
| 53 | + if (Files.All(f => !string.Equals(f.InputPath, filePath, StringComparison.OrdinalIgnoreCase))) |
| 54 | + { |
| 55 | + Files.Add(new FileItem(filePath)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static bool IsSupportedFile(string path) |
| 60 | + { |
| 61 | + var ext = Path.GetExtension(path); |
| 62 | + return ext.Equals(".docx", StringComparison.OrdinalIgnoreCase) |
| 63 | + || ext.Equals(".xlsx", StringComparison.OrdinalIgnoreCase); |
| 64 | + } |
| 65 | + |
| 66 | + [RelayCommand] |
| 67 | + private void RemoveFile(FileItem item) |
| 68 | + { |
| 69 | + Files.Remove(item); |
| 70 | + } |
| 71 | + |
| 72 | + [RelayCommand] |
| 73 | + private void ClearFiles() |
| 74 | + { |
| 75 | + Files.Clear(); |
| 76 | + StatusText = "Ready"; |
| 77 | + Progress = 0; |
| 78 | + } |
| 79 | + |
| 80 | + [RelayCommand(CanExecute = nameof(CanConvert))] |
| 81 | + private async Task ConvertAllAsync() |
| 82 | + { |
| 83 | + IsConverting = true; |
| 84 | + Progress = 0; |
| 85 | + |
| 86 | + RegisterFonts(); |
| 87 | + |
| 88 | + var total = Files.Count; |
| 89 | + var completed = 0; |
| 90 | + |
| 91 | + foreach (var file in Files) |
| 92 | + { |
| 93 | + if (file.Status == ConvertStatus.Done) |
| 94 | + { |
| 95 | + completed++; |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + file.Status = ConvertStatus.Converting; |
| 100 | + StatusText = $"Converting {file.FileName}..."; |
| 101 | + |
| 102 | + try |
| 103 | + { |
| 104 | + var outputPath = Path.ChangeExtension(file.InputPath, ".pdf"); |
| 105 | + await Task.Run(() => MiniSoftware.MiniPdf.ConvertToPdf(file.InputPath, outputPath)); |
| 106 | + file.OutputPath = outputPath; |
| 107 | + file.Status = ConvertStatus.Done; |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + file.Status = ConvertStatus.Error; |
| 112 | + file.ErrorMessage = ex.Message; |
| 113 | + } |
| 114 | + |
| 115 | + completed++; |
| 116 | + Progress = (double)completed / total * 100; |
| 117 | + } |
| 118 | + |
| 119 | + IsConverting = false; |
| 120 | + var errors = Files.Count(f => f.Status == ConvertStatus.Error); |
| 121 | + StatusText = errors > 0 |
| 122 | + ? $"Completed with {errors} error(s)" |
| 123 | + : $"All {total} file(s) converted successfully"; |
| 124 | + } |
| 125 | + |
| 126 | + private bool CanConvert() => Files.Count > 0 && !IsConverting; |
| 127 | + |
| 128 | + partial void OnIsConvertingChanged(bool value) => ConvertAllCommand.NotifyCanExecuteChanged(); |
| 129 | + |
| 130 | + private void RegisterFonts() |
| 131 | + { |
| 132 | + if (string.IsNullOrWhiteSpace(FontDirectory) || !Directory.Exists(FontDirectory)) |
| 133 | + return; |
| 134 | + |
| 135 | + foreach (var fontFile in Directory.EnumerateFiles(FontDirectory, "*.*") |
| 136 | + .Where(f => f.EndsWith(".ttf", StringComparison.OrdinalIgnoreCase) |
| 137 | + || f.EndsWith(".ttc", StringComparison.OrdinalIgnoreCase))) |
| 138 | + { |
| 139 | + var name = Path.GetFileNameWithoutExtension(fontFile); |
| 140 | + MiniSoftware.MiniPdf.RegisterFont(name, File.ReadAllBytes(fontFile)); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +public partial class FileItem : ObservableObject |
| 146 | +{ |
| 147 | + public string InputPath { get; } |
| 148 | + public string FileName => Path.GetFileName(InputPath); |
| 149 | + public string FileType => Path.GetExtension(InputPath).TrimStart('.').ToUpperInvariant(); |
| 150 | + |
| 151 | + [ObservableProperty] |
| 152 | + private ConvertStatus _status = ConvertStatus.Pending; |
| 153 | + |
| 154 | + [ObservableProperty] |
| 155 | + private string? _outputPath; |
| 156 | + |
| 157 | + [ObservableProperty] |
| 158 | + private string? _errorMessage; |
| 159 | + |
| 160 | + public FileItem(string inputPath) => InputPath = inputPath; |
| 161 | +} |
| 162 | + |
| 163 | +public enum ConvertStatus |
| 164 | +{ |
| 165 | + Pending, |
| 166 | + Converting, |
| 167 | + Done, |
| 168 | + Error |
| 169 | +} |
0 commit comments