|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Files.App.Services.SizeProvider; |
| 5 | +using Files.Core.Storage.Storables; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using System.IO; |
| 8 | + |
| 9 | +namespace Files.App.Data.Models |
| 10 | +{ |
| 11 | + public sealed class DrivesViewModel : ObservableObject, IDisposable |
| 12 | + { |
| 13 | + public ObservableCollection<ILocatableFolder> Drives |
| 14 | + { |
| 15 | + get => drives; |
| 16 | + private set => SetProperty(ref drives, value); |
| 17 | + } |
| 18 | + |
| 19 | + public bool ShowUserConsentOnInit |
| 20 | + { |
| 21 | + get => showUserConsentOnInit; |
| 22 | + set => SetProperty(ref showUserConsentOnInit, value); |
| 23 | + } |
| 24 | + |
| 25 | + private bool showUserConsentOnInit; |
| 26 | + private ObservableCollection<ILocatableFolder> drives; |
| 27 | + private readonly IRemovableDrivesService removableDrivesService; |
| 28 | + private readonly ISizeProvider folderSizeProvider; |
| 29 | + private readonly IStorageDeviceWatcher watcher; |
| 30 | + private readonly ILogger<App> logger; |
| 31 | + |
| 32 | + public DrivesViewModel(IRemovableDrivesService removableDrivesService, ISizeProvider folderSizeProvider, ILogger<App> logger) |
| 33 | + { |
| 34 | + this.removableDrivesService = removableDrivesService; |
| 35 | + this.folderSizeProvider = folderSizeProvider; |
| 36 | + this.logger = logger; |
| 37 | + |
| 38 | + drives = []; |
| 39 | + |
| 40 | + watcher = removableDrivesService.CreateWatcher(); |
| 41 | + watcher.DeviceAdded += Watcher_DeviceAdded; |
| 42 | + watcher.DeviceRemoved += Watcher_DeviceRemoved; |
| 43 | + watcher.DeviceModified += Watcher_DeviceModified; |
| 44 | + watcher.EnumerationCompleted += Watcher_EnumerationCompleted; |
| 45 | + } |
| 46 | + |
| 47 | + private async void Watcher_EnumerationCompleted(object? sender, System.EventArgs e) |
| 48 | + { |
| 49 | + logger.LogDebug("Watcher_EnumerationCompleted"); |
| 50 | + await folderSizeProvider.CleanAsync(); |
| 51 | + } |
| 52 | + |
| 53 | + private async void Watcher_DeviceModified(object? sender, string e) |
| 54 | + { |
| 55 | + var matchingDriveEjected = Drives.FirstOrDefault(x => Path.GetFullPath(x.Path) == Path.GetFullPath(e)); |
| 56 | + if (matchingDriveEjected != null) |
| 57 | + await removableDrivesService.UpdateDrivePropertiesAsync(matchingDriveEjected); |
| 58 | + } |
| 59 | + |
| 60 | + private void Watcher_DeviceRemoved(object? sender, string e) |
| 61 | + { |
| 62 | + logger.LogInformation($"Drive removed: {e}"); |
| 63 | + lock (Drives) |
| 64 | + { |
| 65 | + var drive = Drives.FirstOrDefault(x => x.Id == e); |
| 66 | + if (drive is not null) |
| 67 | + Drives.Remove(drive); |
| 68 | + } |
| 69 | + |
| 70 | + // Update the collection on the ui-thread. |
| 71 | + Watcher_EnumerationCompleted(null, EventArgs.Empty); |
| 72 | + } |
| 73 | + |
| 74 | + private void Watcher_DeviceAdded(object? sender, ILocatableFolder e) |
| 75 | + { |
| 76 | + lock (Drives) |
| 77 | + { |
| 78 | + // If drive already in list, remove it first. |
| 79 | + var matchingDrive = Drives.FirstOrDefault(x => |
| 80 | + x.Id == e.Id || |
| 81 | + string.IsNullOrEmpty(e.Path) |
| 82 | + ? x.Path.Contains(e.Name, StringComparison.OrdinalIgnoreCase) |
| 83 | + : Path.GetFullPath(x.Path) == Path.GetFullPath(e.Path) |
| 84 | + ); |
| 85 | + |
| 86 | + if (matchingDrive is not null) |
| 87 | + Drives.Remove(matchingDrive); |
| 88 | + |
| 89 | + logger.LogInformation($"Drive added: {e.Path}"); |
| 90 | + Drives.Add(e); |
| 91 | + } |
| 92 | + |
| 93 | + Watcher_EnumerationCompleted(null, EventArgs.Empty); |
| 94 | + } |
| 95 | + |
| 96 | + public async Task UpdateDrivesAsync() |
| 97 | + { |
| 98 | + Drives.Clear(); |
| 99 | + await foreach (ILocatableFolder item in removableDrivesService.GetDrivesAsync()) |
| 100 | + { |
| 101 | + Drives.AddIfNotPresent(item); |
| 102 | + } |
| 103 | + |
| 104 | + var osDrive = await removableDrivesService.GetPrimaryDriveAsync(); |
| 105 | + |
| 106 | + // Show consent dialog if the OS drive could not be accessed |
| 107 | + if (!Drives.Any(x => Path.GetFullPath(x.Path) == Path.GetFullPath(osDrive.Path))) |
| 108 | + ShowUserConsentOnInit = true; |
| 109 | + |
| 110 | + if (watcher.CanBeStarted) |
| 111 | + watcher.Start(); |
| 112 | + } |
| 113 | + |
| 114 | + public void Dispose() |
| 115 | + { |
| 116 | + watcher.Stop(); |
| 117 | + watcher.DeviceAdded -= Watcher_DeviceAdded; |
| 118 | + watcher.DeviceRemoved -= Watcher_DeviceRemoved; |
| 119 | + watcher.DeviceModified -= Watcher_DeviceModified; |
| 120 | + watcher.EnumerationCompleted -= Watcher_EnumerationCompleted; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments