Skip to content

Commit 620e7df

Browse files
authored
Merge pull request #376 from lampenlampen/feat/DrivesRefactoring
Feat/drives refactoring
2 parents 69902d4 + 0e3edc0 commit 620e7df

17 files changed

+497
-403
lines changed

Files/App.xaml.cs

Lines changed: 6 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@
1919
using System.IO;
2020
using System.Linq;
2121
using System.Collections.ObjectModel;
22-
using Windows.Devices.Enumeration;
23-
using Windows.Devices.Portable;
24-
using Windows.ApplicationModel.Core;
2522
using Windows.UI.Core;
2623
using Windows.UI.Xaml.Controls.Primitives;
27-
using Files.Enums;
28-
using Windows.UI.Xaml.Media.Imaging;
29-
using Windows.Management.Deployment;
30-
using Windows.Storage.Streams;
3124
using Windows.System;
32-
using Microsoft.UI.Xaml.Controls;
3325
using Files.View_Models;
3426

3527
namespace Files
@@ -57,14 +49,13 @@ public static ProHome OccupiedInstance
5749
public static Dialogs.PropertiesDialog propertiesDialog { get; set; }
5850
public static Dialogs.LayoutDialog layoutDialog { get; set; }
5951
public static Dialogs.AddItemDialog addItemDialog { get; set; }
60-
private DeviceWatcher watcher;
6152
public static ObservableCollection<SidebarItem> sideBarItems = new ObservableCollection<SidebarItem>();
6253
public static ObservableCollection<WSLDistroItem> linuxDistroItems = new ObservableCollection<WSLDistroItem>();
63-
public static SettingsViewModel AppSettings = new SettingsViewModel();
54+
public static SettingsViewModel AppSettings { get; set; }
6455

6556
public App()
6657
{
67-
this.InitializeComponent();
58+
this.InitializeComponent();
6859
this.Suspending += OnSuspending;
6960
consentDialog = new Dialogs.ConsentDialog();
7061
propertiesDialog = new Dialogs.PropertiesDialog();
@@ -76,8 +67,8 @@ public App()
7667
Clipboard_ContentChanged(null, null);
7768
AppCenter.Start("682666d1-51d3-4e4a-93d0-d028d43baaa0", typeof(Analytics), typeof(Crashes));
7869

70+
AppSettings = new SettingsViewModel();
7971
SetPropertiesFromLocalSettings();
80-
8172
PopulatePinnedSidebarItems();
8273
DetectWSLDistros();
8374
}
@@ -267,217 +258,6 @@ private void SetPropertiesFromLocalSettings()
267258

268259

269260

270-
}
271-
272-
273-
274-
public void PopulateDrivesListWithLocalDisks()
275-
{
276-
var driveLetters = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList();
277-
driveLetters.ForEach(async roots =>
278-
{
279-
try
280-
{
281-
var content = string.Empty;
282-
string icon = null;
283-
if (!(await KnownFolders.RemovableDevices.GetFoldersAsync()).Select(x => x.Path).ToList().Contains(roots.Name))
284-
{
285-
// TODO: Display Custom Names for Local Disks as well
286-
if(InstanceTabsView.NormalizePath(roots.Name) != InstanceTabsView.NormalizePath("A:")
287-
&& InstanceTabsView.NormalizePath(roots.Name) != InstanceTabsView.NormalizePath("B:"))
288-
{
289-
content = $"Local Disk ({roots.Name.TrimEnd('\\')})";
290-
icon = "\uEDA2";
291-
}
292-
else
293-
{
294-
content = $"Floppy Disk ({roots.Name.TrimEnd('\\')})";
295-
icon = "\uE74E";
296-
}
297-
298-
299-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
300-
async () =>
301-
{
302-
Visibility capacityBarVis = Visibility.Visible;
303-
ulong totalSpaceProg = 0;
304-
ulong freeSpaceProg = 0;
305-
string free_space_text = "Unknown";
306-
string total_space_text = "Unknown";
307-
308-
try
309-
{
310-
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(roots.Name);
311-
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });
312-
313-
var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
314-
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);
315-
316-
sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
317-
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);
318-
319-
free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
320-
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
321-
}
322-
catch (UnauthorizedAccessException)
323-
{
324-
capacityBarVis = Visibility.Collapsed;
325-
}
326-
catch (NullReferenceException)
327-
{
328-
capacityBarVis = Visibility.Collapsed;
329-
}
330-
331-
App.foundDrives.Add(new DriveItem()
332-
{
333-
driveText = content,
334-
glyph = icon,
335-
maxSpace = totalSpaceProg,
336-
spaceUsed = totalSpaceProg - freeSpaceProg,
337-
tag = roots.Name,
338-
progressBarVisibility = capacityBarVis,
339-
spaceText = free_space_text + " free of " + total_space_text,
340-
});
341-
});
342-
}
343-
344-
}
345-
catch (UnauthorizedAccessException e)
346-
{
347-
Debug.WriteLine(e.Message);
348-
}
349-
350-
});
351-
}
352-
353-
private async void Watcher_EnumerationCompleted(DeviceWatcher sender, object args)
354-
{
355-
try
356-
{
357-
PopulateDrivesListWithLocalDisks();
358-
}
359-
catch (UnauthorizedAccessException)
360-
{
361-
await consentDialog.ShowAsync();
362-
}
363-
DeviceAdded(sender, null);
364-
365-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
366-
() =>
367-
{
368-
App.foundDrives.Add(new DriveItem()
369-
{
370-
driveText = "OneDrive",
371-
tag = "OneDrive",
372-
cloudGlyphVisibility = Visibility.Visible,
373-
driveGlyphVisibility = Visibility.Collapsed
374-
});
375-
});
376-
}
377-
378-
private void DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
379-
{
380-
Debug.WriteLine("Devices updated");
381-
}
382-
383-
384-
private async void DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate args)
385-
{
386-
var devices = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList();
387-
388-
foreach (DriveItem driveItem in foundDrives)
389-
{
390-
if (!driveItem.tag.Equals("OneDrive"))
391-
{
392-
if (!devices.Any(x => x.Name == driveItem.tag) || devices.Equals(null))
393-
{
394-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
395-
() =>
396-
{
397-
foundDrives.Remove(driveItem);
398-
});
399-
return;
400-
401-
}
402-
}
403-
404-
}
405-
}
406-
407-
private async void DeviceAdded(DeviceWatcher sender, DeviceInformation args)
408-
{
409-
try
410-
{
411-
var devices = (await KnownFolders.RemovableDevices.GetFoldersAsync()).OrderBy(x => x.Path);
412-
foreach (StorageFolder device in devices)
413-
{
414-
var letter = device.Path;
415-
if (!foundDrives.Any(x => x.tag == letter))
416-
{
417-
var content = device.DisplayName;
418-
string icon = null;
419-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
420-
async () =>
421-
{
422-
if (content.Contains("DVD"))
423-
{
424-
icon = "\uE958";
425-
}
426-
else
427-
{
428-
icon = "\uE88E";
429-
}
430-
431-
ulong totalSpaceProg = 0;
432-
ulong freeSpaceProg = 0;
433-
string free_space_text = "Unknown";
434-
string total_space_text = "Unknown";
435-
Visibility capacityBarVis = Visibility.Visible;
436-
try
437-
{
438-
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(letter);
439-
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });
440-
441-
var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
442-
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);
443-
444-
sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
445-
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);
446-
447-
free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
448-
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
449-
}
450-
catch (UnauthorizedAccessException)
451-
{
452-
capacityBarVis = Visibility.Collapsed;
453-
}
454-
catch (NullReferenceException)
455-
{
456-
capacityBarVis = Visibility.Collapsed;
457-
}
458-
459-
if (!foundDrives.Any(x => x.tag == letter))
460-
{
461-
foundDrives.Add(new DriveItem()
462-
{
463-
driveText = content,
464-
glyph = icon,
465-
maxSpace = totalSpaceProg,
466-
spaceUsed = totalSpaceProg - freeSpaceProg,
467-
tag = letter,
468-
progressBarVisibility = capacityBarVis,
469-
spaceText = free_space_text + " free of " + total_space_text,
470-
});
471-
}
472-
});
473-
474-
}
475-
}
476-
}
477-
catch (UnauthorizedAccessException)
478-
{
479-
await consentDialog.ShowAsync();
480-
}
481261
}
482262

483263
public static List<string> LinesToRemoveFromFile = new List<string>();
@@ -662,7 +442,6 @@ public static IReadOnlyList<ContentDialog> FindDisplayedContentDialogs<T>()
662442

663443
public static PasteState PS { get; set; } = new PasteState();
664444
public static List<string> pathsToDeleteAfterPaste = new List<string>();
665-
public static ObservableCollection<DriveItem> foundDrives = new ObservableCollection<DriveItem>();
666445

667446
/// <summary>
668447
/// Invoked when the application is launched normally by the end user. Other entry points
@@ -710,12 +489,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
710489

711490

712491
}
713-
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
714-
watcher.Added += DeviceAdded;
715-
watcher.Removed += DeviceRemoved;
716-
watcher.Updated += DeviceUpdated;
717-
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
718-
watcher.Start();
492+
719493
// Ensure the current window is active
720494
Window.Current.Activate();
721495
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
@@ -776,18 +550,12 @@ protected override void OnActivated(IActivatedEventArgs args)
776550
rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo());
777551
}
778552
// Ensure the current window is active.
779-
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
780-
watcher.Added += DeviceAdded;
781-
watcher.Removed += DeviceRemoved;
782-
watcher.Updated += DeviceUpdated;
783-
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
784-
watcher.Start();
785553
Window.Current.Activate();
786554
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
787555
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
788556
return;
789557
}
790-
558+
791559
rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
792560

793561
// Ensure the current window is active.
@@ -819,10 +587,7 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
819587
{
820588
var deferral = e.SuspendingOperation.GetDeferral();
821589
//TODO: Save application state and stop any background activity
822-
if(watcher.Status == DeviceWatcherStatus.Started || watcher.Status == DeviceWatcherStatus.EnumerationCompleted)
823-
{
824-
watcher.Stop();
825-
}
590+
AppSettings.Dispose();
826591
deferral.Complete();
827592
}
828593
}

Files/BaseLayout.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
8888
// Add item jumping handler
8989
Window.Current.CoreWindow.CharacterReceived += Page_CharacterReceived;
9090
var parameters = (string)eventArgs.Parameter;
91-
if (App.FormFactor == Enums.FormFactorMode.Regular)
91+
if (App.AppSettings.FormFactor == Enums.FormFactorMode.Regular)
9292
{
9393
Frame rootFrame = Window.Current.Content as Frame;
9494
InstanceTabsView instanceTabsView = rootFrame.Content as InstanceTabsView;
@@ -111,31 +111,31 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
111111
App.OccupiedInstance.instanceViewModel.AddItemsToCollectionAsync(App.OccupiedInstance.instanceViewModel.Universal.path);
112112
App.Clipboard_ContentChanged(null, null);
113113

114-
if (parameters.Equals(App.DesktopPath))
114+
if (parameters.Equals(App.AppSettings.DesktopPath))
115115
{
116116
App.OccupiedInstance.PathText.Text = "Desktop";
117117
}
118-
else if (parameters.Equals(App.DocumentsPath))
118+
else if (parameters.Equals(App.AppSettings.DocumentsPath))
119119
{
120120
App.OccupiedInstance.PathText.Text = "Documents";
121121
}
122-
else if (parameters.Equals(App.DownloadsPath))
122+
else if (parameters.Equals(App.AppSettings.DownloadsPath))
123123
{
124124
App.OccupiedInstance.PathText.Text = "Downloads";
125125
}
126-
else if (parameters.Equals(App.PicturesPath))
126+
else if (parameters.Equals(App.AppSettings.PicturesPath))
127127
{
128128
App.OccupiedInstance.PathText.Text = "Pictures";
129129
}
130-
else if (parameters.Equals(App.MusicPath))
130+
else if (parameters.Equals(App.AppSettings.MusicPath))
131131
{
132132
App.OccupiedInstance.PathText.Text = "Music";
133133
}
134-
else if (parameters.Equals(App.OneDrivePath))
134+
else if (parameters.Equals(App.AppSettings.OneDrivePath))
135135
{
136136
App.OccupiedInstance.PathText.Text = "OneDrive";
137137
}
138-
else if (parameters.Equals(App.VideosPath))
138+
else if (parameters.Equals(App.AppSettings.VideosPath))
139139
{
140140
App.OccupiedInstance.PathText.Text = "Videos";
141141
}

0 commit comments

Comments
 (0)