| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RecentFilesViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Reactive.Disposables; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | using System.Windows.Input; |
| | | 14 | | using MyNet.IO.FileHistory; |
| | | 15 | | using MyNet.Observable.Collections; |
| | | 16 | | using MyNet.UI.Commands; |
| | | 17 | | using MyNet.UI.Dialogs; |
| | | 18 | | using MyNet.UI.Notifications; |
| | | 19 | | using MyNet.UI.Services; |
| | | 20 | | using MyNet.UI.Services.FileHistory; |
| | | 21 | | using MyNet.UI.ViewModels.List; |
| | | 22 | | |
| | | 23 | | namespace MyNet.UI.ViewModels.FileHistory; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// View model for the recent-files list. |
| | | 27 | | /// </summary> |
| | | 28 | | public sealed class RecentFilesViewModel : ListViewModelBase<RecentFileViewModel, ExtendedCollection<RecentFileViewModel |
| | | 29 | | { |
| | | 30 | | private readonly IRecentFilesOperations _recentFilesOperations; |
| | | 31 | | private readonly IRecentFileCommandsService _recentFileCommandsService; |
| | | 32 | | private readonly IDialogService _dialogService; |
| | | 33 | | private readonly INotificationPublisher _notificationPublisher; |
| | | 34 | | private readonly RecentFilesListOptions _listOptions; |
| | 0 | 35 | | private readonly Dictionary<string, RecentFileViewModel> _entries = new(StringComparer.OrdinalIgnoreCase); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="RecentFilesViewModel"/> class. |
| | | 39 | | /// </summary> |
| | | 40 | | public RecentFilesViewModel( |
| | | 41 | | IRecentFilesOperations recentFilesOperations, |
| | | 42 | | IRecentFileCommandsService recentFileCommandsService, |
| | | 43 | | IDialogService dialogService, |
| | | 44 | | INotificationPublisher notificationPublisher, |
| | | 45 | | ICommandFactory? commandFactory = null) |
| | 0 | 46 | | : base(CreateCollection(), CreateListOptions(out var listOptions).Filters, listOptions.Sorting) |
| | | 47 | | { |
| | 0 | 48 | | _recentFilesOperations = recentFilesOperations ?? throw new ArgumentNullException(nameof(recentFilesOperations)) |
| | 0 | 49 | | _recentFileCommandsService = recentFileCommandsService ?? throw new ArgumentNullException(nameof(recentFileComma |
| | 0 | 50 | | _dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService)); |
| | 0 | 51 | | _notificationPublisher = notificationPublisher ?? throw new ArgumentNullException(nameof(notificationPublisher)) |
| | 0 | 52 | | _listOptions = listOptions; |
| | | 53 | | |
| | 0 | 54 | | var commands = commandFactory.GetOrDefault(); |
| | 0 | 55 | | OpenCommand = commands.Create<RecentFileViewModel?>(async item => |
| | 0 | 56 | | { |
| | 0 | 57 | | if (item is not null) |
| | 0 | 58 | | await item.OpenAsync().ConfigureAwait(false); |
| | 0 | 59 | | }, |
| | 0 | 60 | | item => item is not null); |
| | | 61 | | |
| | 0 | 62 | | RemoveItemsCommand = commands.Create<IEnumerable<RecentFileViewModel>>(async items => |
| | 0 | 63 | | { |
| | 0 | 64 | | foreach (var item in items ?? []) |
| | 0 | 65 | | await _recentFilesOperations.RemoveAsync(item.Path).ConfigureAwait(false); |
| | 0 | 66 | | }); |
| | | 67 | | |
| | | 68 | | ReloadCommand = commands.Create(() => ReloadAsync()); |
| | | 69 | | |
| | 0 | 70 | | _recentFilesOperations.Changed += OnRecentFilesChanged; |
| | | 71 | | Disposables.Add(Disposable.Create(() => _recentFilesOperations.Changed -= OnRecentFilesChanged)); |
| | | 72 | | |
| | 0 | 73 | | _ = ReloadAsync(); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the command that opens a recent file. |
| | | 78 | | /// </summary> |
| | | 79 | | public ICommand OpenCommand { get; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the command that removes items from the recent-files list. |
| | | 83 | | /// </summary> |
| | | 84 | | public ICommand RemoveItemsCommand { get; } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the command that reloads the list from storage. |
| | | 88 | | /// </summary> |
| | | 89 | | public ICommand ReloadCommand { get; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets or sets the search text applied to name and path. |
| | | 93 | | /// </summary> |
| | | 94 | | public string SearchText |
| | | 95 | | { |
| | 0 | 96 | | get => _listOptions.NameFilter.Value ?? string.Empty; |
| | 0 | 97 | | set => _listOptions.SetSearchText(value); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Reloads items from the recent-files service. |
| | | 102 | | /// </summary> |
| | 0 | 103 | | public async Task ReloadAsync(CancellationToken cancellationToken = default) => await ExecuteSafeAsync(async ct => |
| | 0 | 104 | | { |
| | 0 | 105 | | var files = await _recentFilesOperations.GetAllAsync(ct).ConfigureAwait(false); |
| | 0 | 106 | | var items = new List<RecentFileViewModel>(files.Count); |
| | 0 | 107 | | var activePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 108 | | |
| | 0 | 109 | | foreach (var file in files) |
| | 0 | 110 | | { |
| | 0 | 111 | | activePaths.Add(file.Path); |
| | 0 | 112 | | |
| | 0 | 113 | | if (!_entries.TryGetValue(file.Path, out var viewModel)) |
| | 0 | 114 | | { |
| | 0 | 115 | | viewModel = CreateItemViewModel(file); |
| | 0 | 116 | | _entries[file.Path] = viewModel; |
| | 0 | 117 | | } |
| | 0 | 118 | | else |
| | 0 | 119 | | { |
| | 0 | 120 | | viewModel.Update(file); |
| | 0 | 121 | | } |
| | 0 | 122 | | |
| | 0 | 123 | | items.Add(viewModel); |
| | 0 | 124 | | } |
| | 0 | 125 | | |
| | 0 | 126 | | foreach (var path in _entries.Keys.Where(path => !activePaths.Contains(path)).ToList()) |
| | 0 | 127 | | _entries.Remove(path); |
| | 0 | 128 | | |
| | 0 | 129 | | Collection.Set(items); |
| | 0 | 130 | | |
| | 0 | 131 | | foreach (var viewModel in items.Where(x => x.Image is null)) |
| | 0 | 132 | | await viewModel.LoadImageAsync(ct).ConfigureAwait(false); |
| | 0 | 133 | | }, |
| | 0 | 134 | | cancellationToken).ConfigureAwait(false); |
| | | 135 | | |
| | | 136 | | private static ExtendedCollection<RecentFileViewModel> CreateCollection() => |
| | 0 | 137 | | ExtendedCollection.Create<RecentFileViewModel>(); |
| | | 138 | | |
| | | 139 | | private static RecentFilesListOptions CreateListOptions(out RecentFilesListOptions options) |
| | | 140 | | { |
| | 0 | 141 | | options = RecentFilesListOptions.Create(); |
| | 0 | 142 | | return options; |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | private void OnRecentFilesChanged(object? sender, EventArgs e) => _ = ReloadAsync(); |
| | | 146 | | |
| | | 147 | | private RecentFileViewModel CreateItemViewModel(RecentFile file) => |
| | 0 | 148 | | new( |
| | 0 | 149 | | file, |
| | 0 | 150 | | _recentFilesOperations, |
| | 0 | 151 | | _recentFileCommandsService, |
| | 0 | 152 | | _dialogService, |
| | 0 | 153 | | _notificationPublisher); |
| | | 154 | | } |
| | | 155 | | |