Skip to content

Commit aa101b7

Browse files
authored
Show consent dialog on app start (#1750)
1 parent e9081d6 commit aa101b7

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

Files/Filesystem/Drives.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Files.View_Models;
22
using Files.Views;
3+
using GalaSoft.MvvmLight;
34
using NLog;
45
using System;
56
using System.Collections.Generic;
67
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
10+
using System.Linq.Expressions;
911
using System.Threading.Tasks;
1012
using Windows.ApplicationModel.Core;
1113
using Windows.Devices.Enumeration;
@@ -15,37 +17,46 @@
1517

1618
namespace Files.Filesystem
1719
{
18-
public class DrivesManager
20+
public class DrivesManager : ObservableObject
1921
{
2022
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2123
public SettingsViewModel AppSettings => App.AppSettings;
2224
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+
}
2431
private DeviceWatcher _deviceWatcher;
2532

2633
public DrivesManager()
2734
{
28-
Task findDrivesTask = null;
35+
EnumerateDrives();
36+
}
37+
38+
private async void EnumerateDrives()
39+
{
2940
try
3041
{
31-
findDrivesTask = GetDrives(Drives);
42+
await GetDrives(Drives);
43+
GetVirtualDrivesList(Drives);
44+
StartDeviceWatcher();
3245
}
3346
catch (AggregateException)
3447
{
3548
ShowUserConsentOnInit = true;
36-
}
37-
38-
findDrivesTask.ContinueWith((x) =>
39-
{
40-
GetVirtualDrivesList(Drives);
49+
};
50+
}
4151

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();
4960
}
5061

5162
private async void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
@@ -337,9 +348,12 @@ public static async Task<StorageFolderWithPath> GetRootFromPath(string devicePat
337348

338349
public void Dispose()
339350
{
340-
if (_deviceWatcher.Status == DeviceWatcherStatus.Started || _deviceWatcher.Status == DeviceWatcherStatus.EnumerationCompleted)
351+
if (_deviceWatcher != null)
341352
{
342-
_deviceWatcher.Stop();
353+
if (_deviceWatcher.Status == DeviceWatcherStatus.Started || _deviceWatcher.Status == DeviceWatcherStatus.EnumerationCompleted)
354+
{
355+
_deviceWatcher.Stop();
356+
}
343357
}
344358
}
345359
}

Files/View Models/SettingsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ public int GridViewSize
636636

637637
public void Dispose()
638638
{
639-
DrivesManager.Dispose();
639+
DrivesManager?.Dispose();
640640
}
641641

642642
public bool Set<TValue>(TValue value, [CallerMemberName] string propertyName = null)

Files/Views/ModernShellPage.xaml.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ public sealed partial class ModernShellPage : Page, IShellPage
2929
public ModernShellPage()
3030
{
3131
this.InitializeComponent();
32-
if (AppSettings.DrivesManager.ShowUserConsentOnInit)
33-
{
34-
AppSettings.DrivesManager.ShowUserConsentOnInit = false;
35-
DisplayFilesystemConsentDialog();
36-
}
32+
AppSettings.DrivesManager.PropertyChanged += DrivesManager_PropertyChanged;
33+
DisplayFilesystemConsentDialog();
3734

3835
var flowDirectionSetting = ResourceContext.GetForCurrentView().QualifierValues["LayoutDirection"];
3936

@@ -48,6 +45,14 @@ public ModernShellPage()
4845
App.CurrentInstance.NavigationToolbar.CanGoForward = false;
4946
}
5047

48+
private void DrivesManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
49+
{
50+
if (e.PropertyName == "ShowUserConsentOnInit")
51+
{
52+
DisplayFilesystemConsentDialog();
53+
}
54+
}
55+
5156
Type IShellPage.CurrentPageType => ItemDisplayFrame.SourcePageType;
5257

5358
INavigationToolbar IShellPage.NavigationToolbar => NavToolbar;
@@ -83,8 +88,12 @@ private BaseLayout GetContentOrNull()
8388

8489
private async void DisplayFilesystemConsentDialog()
8590
{
86-
var consentDialogDisplay = new ConsentDialog();
87-
await consentDialogDisplay.ShowAsync(ContentDialogPlacement.Popup);
91+
if (AppSettings.DrivesManager.ShowUserConsentOnInit)
92+
{
93+
AppSettings.DrivesManager.ShowUserConsentOnInit = false;
94+
var consentDialogDisplay = new ConsentDialog();
95+
await consentDialogDisplay.ShowAsync(ContentDialogPlacement.Popup);
96+
}
8897
}
8998

9099
private string NavParams = null;

0 commit comments

Comments
 (0)