|
1 | 1 | using Files.View_Models;
|
2 | 2 | using Files.Views;
|
| 3 | +using GalaSoft.MvvmLight; |
3 | 4 | using NLog;
|
4 | 5 | using System;
|
5 | 6 | using System.Collections.Generic;
|
6 | 7 | using System.Diagnostics;
|
7 | 8 | using System.IO;
|
8 | 9 | using System.Linq;
|
| 10 | +using System.Linq.Expressions; |
9 | 11 | using System.Threading.Tasks;
|
10 | 12 | using Windows.ApplicationModel.Core;
|
11 | 13 | using Windows.Devices.Enumeration;
|
|
15 | 17 |
|
16 | 18 | namespace Files.Filesystem
|
17 | 19 | {
|
18 |
| - public class DrivesManager |
| 20 | + public class DrivesManager : ObservableObject |
19 | 21 | {
|
20 | 22 | private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
21 | 23 | public SettingsViewModel AppSettings => App.AppSettings;
|
22 | 24 | public IList<DriveItem> Drives { get; } = new List<DriveItem>();
|
23 |
| - public bool ShowUserConsentOnInit { get; set; } = false; |
| 25 | + private bool _ShowUserConsentOnInit = false; |
| 26 | + public bool ShowUserConsentOnInit |
| 27 | + { |
| 28 | + get => _ShowUserConsentOnInit; |
| 29 | + set => Set(ref _ShowUserConsentOnInit, value); |
| 30 | + } |
24 | 31 | private DeviceWatcher _deviceWatcher;
|
25 | 32 |
|
26 | 33 | public DrivesManager()
|
27 | 34 | {
|
28 |
| - Task findDrivesTask = null; |
| 35 | + EnumerateDrives(); |
| 36 | + } |
| 37 | + |
| 38 | + private async void EnumerateDrives() |
| 39 | + { |
29 | 40 | try
|
30 | 41 | {
|
31 |
| - findDrivesTask = GetDrives(Drives); |
| 42 | + await GetDrives(Drives); |
| 43 | + GetVirtualDrivesList(Drives); |
| 44 | + StartDeviceWatcher(); |
32 | 45 | }
|
33 | 46 | catch (AggregateException)
|
34 | 47 | {
|
35 | 48 | ShowUserConsentOnInit = true;
|
36 |
| - } |
37 |
| - |
38 |
| - findDrivesTask.ContinueWith((x) => |
39 |
| - { |
40 |
| - GetVirtualDrivesList(Drives); |
| 49 | + }; |
| 50 | + } |
41 | 51 |
|
42 |
| - _deviceWatcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector()); |
43 |
| - _deviceWatcher.Added += DeviceAdded; |
44 |
| - _deviceWatcher.Removed += DeviceRemoved; |
45 |
| - _deviceWatcher.Updated += DeviceUpdated; |
46 |
| - _deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; |
47 |
| - _deviceWatcher.Start(); |
48 |
| - }); |
| 52 | + private void StartDeviceWatcher() |
| 53 | + { |
| 54 | + _deviceWatcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector()); |
| 55 | + _deviceWatcher.Added += DeviceAdded; |
| 56 | + _deviceWatcher.Removed += DeviceRemoved; |
| 57 | + _deviceWatcher.Updated += DeviceUpdated; |
| 58 | + _deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; |
| 59 | + _deviceWatcher.Start(); |
49 | 60 | }
|
50 | 61 |
|
51 | 62 | private async void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
|
@@ -337,9 +348,12 @@ public static async Task<StorageFolderWithPath> GetRootFromPath(string devicePat
|
337 | 348 |
|
338 | 349 | public void Dispose()
|
339 | 350 | {
|
340 |
| - if (_deviceWatcher.Status == DeviceWatcherStatus.Started || _deviceWatcher.Status == DeviceWatcherStatus.EnumerationCompleted) |
| 351 | + if (_deviceWatcher != null) |
341 | 352 | {
|
342 |
| - _deviceWatcher.Stop(); |
| 353 | + if (_deviceWatcher.Status == DeviceWatcherStatus.Started || _deviceWatcher.Status == DeviceWatcherStatus.EnumerationCompleted) |
| 354 | + { |
| 355 | + _deviceWatcher.Stop(); |
| 356 | + } |
343 | 357 | }
|
344 | 358 | }
|
345 | 359 | }
|
|
0 commit comments