| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RecentFileViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.IO; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using System.Windows.Input; |
| | | 13 | | using MyNet.IO; |
| | | 14 | | using MyNet.IO.FileHistory; |
| | | 15 | | using MyNet.Primitives; |
| | | 16 | | using MyNet.UI.Commands; |
| | | 17 | | using MyNet.UI.Dialogs; |
| | | 18 | | using MyNet.UI.Dialogs.MessageBox; |
| | | 19 | | using MyNet.UI.Helpers; |
| | | 20 | | using MyNet.UI.Notifications; |
| | | 21 | | using MyNet.UI.Resources; |
| | | 22 | | using MyNet.UI.Services; |
| | | 23 | | using MyNet.UI.Services.FileHistory; |
| | | 24 | | |
| | | 25 | | namespace MyNet.UI.ViewModels.FileHistory; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// View model for a single recent file entry. |
| | | 29 | | /// </summary> |
| | | 30 | | public sealed class RecentFileViewModel : ViewModelBase |
| | | 31 | | { |
| | | 32 | | private readonly IRecentFilesOperations _recentFilesOperations; |
| | | 33 | | private readonly IRecentFileCommandsService _recentFileCommandsService; |
| | | 34 | | private readonly IDialogService _dialogService; |
| | | 35 | | private readonly INotificationPublisher _notificationPublisher; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="RecentFileViewModel"/> class. |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public RecentFileViewModel( |
| | 0 | 41 | | RecentFile recentFile, |
| | 0 | 42 | | IRecentFilesOperations recentFilesOperations, |
| | 0 | 43 | | IRecentFileCommandsService recentFileCommandsService, |
| | 0 | 44 | | IDialogService dialogService, |
| | 0 | 45 | | INotificationPublisher notificationPublisher, |
| | 0 | 46 | | ICommandFactory? commandFactory = null) |
| | | 47 | | { |
| | 0 | 48 | | ArgumentNullException.ThrowIfNull(recentFile); |
| | | 49 | | |
| | 0 | 50 | | _recentFilesOperations = recentFilesOperations ?? throw new ArgumentNullException(nameof(recentFilesOperations)) |
| | 0 | 51 | | _recentFileCommandsService = recentFileCommandsService ?? throw new ArgumentNullException(nameof(recentFileComma |
| | 0 | 52 | | _dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService)); |
| | 0 | 53 | | _notificationPublisher = notificationPublisher ?? throw new ArgumentNullException(nameof(notificationPublisher)) |
| | | 54 | | |
| | 0 | 55 | | Path = recentFile.Path; |
| | 0 | 56 | | Update(recentFile); |
| | | 57 | | |
| | 0 | 58 | | var commands = commandFactory.GetOrDefault(); |
| | 0 | 59 | | OpenCommand = commands.Create(OpenAsync, FileExists); |
| | | 60 | | OpenCopyCommand = commands.Create(OpenCopyAsync, () => !IsRecoveredFile && FileExists()); |
| | | 61 | | OpenFolderLocationCommand = commands.Create(() => IoHelper.OpenFolderLocation(Path, _notificationPublisher), () |
| | | 62 | | RemoveFromListCommand = commands.Create(RemoveFromListAsync, () => !IsRecoveredFile); |
| | 0 | 63 | | RemoveFileCommand = commands.Create(RemoveFileAsync, FileExists); |
| | | 64 | | PinCommand = commands.Create(() => SetPinnedAsync(true), () => !IsPinned && !IsRecoveredFile); |
| | | 65 | | PinOffCommand = commands.Create(() => SetPinnedAsync(false), () => IsPinned && !IsRecoveredFile); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the file path. |
| | | 70 | | /// </summary> |
| | | 71 | | public string Path { get; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the display name. |
| | | 75 | | /// </summary> |
| | 0 | 76 | | public string? DisplayName { get; private set => SetProperty(ref field, value); } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the last access date. |
| | | 80 | | /// </summary> |
| | 0 | 81 | | public DateTime? LastAccessDate { get; private set => SetProperty(ref field, value); } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the last modification date. |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public DateTime? ModificationDate { get; private set => SetProperty(ref field, value); } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets a value indicating whether the file is pinned. |
| | | 90 | | /// </summary> |
| | 0 | 91 | | public bool IsPinned { get; private set => SetProperty(ref field, value); } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets a value indicating whether the file is a recovered document. |
| | | 95 | | /// </summary> |
| | 0 | 96 | | public bool IsRecoveredFile { get; private set => SetProperty(ref field, value); } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the preview image bytes. |
| | | 100 | | /// </summary> |
| | 0 | 101 | | public byte[]? Image { get; private set => SetProperty(ref field, value); } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the command that opens the file. |
| | | 105 | | /// </summary> |
| | | 106 | | public ICommand OpenCommand { get; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the command that opens a copy of the file. |
| | | 110 | | /// </summary> |
| | | 111 | | public ICommand OpenCopyCommand { get; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the command that opens the containing folder. |
| | | 115 | | /// </summary> |
| | | 116 | | public ICommand OpenFolderLocationCommand { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the command that removes the entry from the recent-files list. |
| | | 120 | | /// </summary> |
| | | 121 | | public ICommand RemoveFromListCommand { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the command that deletes the file from disk and removes it from the list. |
| | | 125 | | /// </summary> |
| | | 126 | | public ICommand RemoveFileCommand { get; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the command that pins the file. |
| | | 130 | | /// </summary> |
| | | 131 | | public ICommand PinCommand { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets the command that unpins the file. |
| | | 135 | | /// </summary> |
| | | 136 | | public ICommand PinOffCommand { get; } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets a value indicating whether the file exists on disk. |
| | | 140 | | /// </summary> |
| | 0 | 141 | | public bool FileExists() => File.Exists(Path); |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Loads the preview image asynchronously. |
| | | 145 | | /// </summary> |
| | | 146 | | public async Task LoadImageAsync(CancellationToken cancellationToken = default) |
| | | 147 | | { |
| | 0 | 148 | | if (!FileExists()) |
| | 0 | 149 | | return; |
| | | 150 | | |
| | 0 | 151 | | await ExecuteSafeAsync( |
| | 0 | 152 | | async _ => Image = await _recentFileCommandsService.GetImageAsync(Path).ConfigureAwait(false), |
| | 0 | 153 | | cancellationToken).ConfigureAwait(false); |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Opens the file. |
| | | 158 | | /// </summary> |
| | | 159 | | public async Task OpenAsync() |
| | | 160 | | { |
| | 0 | 161 | | if (IsRecoveredFile) |
| | 0 | 162 | | await _recentFileCommandsService.OpenCopyAsync(Path).ConfigureAwait(false); |
| | | 163 | | else |
| | 0 | 164 | | await _recentFileCommandsService.OpenAsync(Path).ConfigureAwait(false); |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Opens a copy of the file. |
| | | 169 | | /// </summary> |
| | 0 | 170 | | public async Task OpenCopyAsync() => await _recentFileCommandsService.OpenCopyAsync(Path).ConfigureAwait(false); |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Updates the view model from the domain model. |
| | | 174 | | /// </summary> |
| | | 175 | | public void Update(RecentFile recentFile) |
| | | 176 | | { |
| | 0 | 177 | | ArgumentNullException.ThrowIfNull(recentFile); |
| | | 178 | | |
| | 0 | 179 | | DisplayName = recentFile.Name; |
| | 0 | 180 | | LastAccessDate = recentFile.LastAccessedAt?.LocalDateTime; |
| | 0 | 181 | | ModificationDate = recentFile.LastModifiedAt?.LocalDateTime; |
| | 0 | 182 | | IsPinned = recentFile.IsPinned; |
| | 0 | 183 | | IsRecoveredFile = recentFile.IsRecovered; |
| | 0 | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | private async Task RemoveFromListAsync() => await _recentFilesOperations.RemoveAsync(Path).ConfigureAwait(false); |
| | | 187 | | |
| | | 188 | | private async Task RemoveFileAsync() |
| | | 189 | | { |
| | 0 | 190 | | var result = await _dialogService |
| | 0 | 191 | | .ShowQuestionAsync(MessageResources.FileRemovingQuestion, UiResources.Removing) |
| | 0 | 192 | | .ConfigureAwait(false); |
| | | 193 | | |
| | 0 | 194 | | if (result != MessageBoxResult.Yes) |
| | 0 | 195 | | return; |
| | | 196 | | |
| | 0 | 197 | | if (FileHelper.TryDeleteFile(Path)) |
| | | 198 | | { |
| | 0 | 199 | | await _recentFilesOperations.RemoveAsync(Path).ConfigureAwait(false); |
| | 0 | 200 | | _notificationPublisher.PublishSuccess( |
| | 0 | 201 | | MessageResources.FileXRemovedSuccess.FormatWith(CultureInfo.CurrentCulture, Path)); |
| | | 202 | | } |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | private async Task SetPinnedAsync(bool isPinned) |
| | | 206 | | { |
| | 0 | 207 | | var updated = await _recentFilesOperations.SetPinnedAsync(Path, isPinned).ConfigureAwait(false); |
| | | 208 | | |
| | 0 | 209 | | if (updated is not null) |
| | 0 | 210 | | Update(updated); |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <inheritdoc /> |
| | | 214 | | protected override void DisposeManagedResources() |
| | | 215 | | { |
| | 0 | 216 | | Image = null; |
| | 0 | 217 | | base.DisposeManagedResources(); |
| | 0 | 218 | | } |
| | | 219 | | } |
| | | 220 | | |