Skip to content

Commit ad02a83

Browse files
committed
Refactor Drives
1 parent 496a84e commit ad02a83

File tree

14 files changed

+368
-486
lines changed

14 files changed

+368
-486
lines changed

Files/App.xaml.cs

Lines changed: 3 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public static ProHome OccupiedInstance
5757
public static Dialogs.PropertiesDialog propertiesDialog { get; set; }
5858
public static Dialogs.LayoutDialog layoutDialog { get; set; }
5959
public static Dialogs.AddItemDialog addItemDialog { get; set; }
60-
private DeviceWatcher watcher;
6160
public static ObservableCollection<SidebarItem> sideBarItems = new ObservableCollection<SidebarItem>();
6261
public static ObservableCollection<WSLDistroItem> linuxDistroItems = new ObservableCollection<WSLDistroItem>();
6362
public static SettingsViewModel AppSettings = new SettingsViewModel();
@@ -267,217 +266,6 @@ private void SetPropertiesFromLocalSettings()
267266

268267

269268

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-
}
481269
}
482270

483271
public static List<string> LinesToRemoveFromFile = new List<string>();
@@ -662,7 +450,6 @@ public static IReadOnlyList<ContentDialog> FindDisplayedContentDialogs<T>()
662450

663451
public static PasteState PS { get; set; } = new PasteState();
664452
public static List<string> pathsToDeleteAfterPaste = new List<string>();
665-
public static ObservableCollection<DriveItem> foundDrives = new ObservableCollection<DriveItem>();
666453

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

711498

712499
}
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();
500+
719501
// Ensure the current window is active
720502
Window.Current.Activate();
721503
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
@@ -776,18 +558,12 @@ protected override void OnActivated(IActivatedEventArgs args)
776558
rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo());
777559
}
778560
// 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();
785561
Window.Current.Activate();
786562
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
787563
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
788564
return;
789565
}
790-
566+
791567
rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());
792568

793569
// Ensure the current window is active.
@@ -819,10 +595,7 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
819595
{
820596
var deferral = e.SuspendingOperation.GetDeferral();
821597
//TODO: Save application state and stop any background activity
822-
if(watcher.Status == DeviceWatcherStatus.Started || watcher.Status == DeviceWatcherStatus.EnumerationCompleted)
823-
{
824-
watcher.Stop();
825-
}
598+
AppSettings.Dispose();
826599
deferral.Complete();
827600
}
828601
}

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
}

Files/Controls/RibbonArea.xaml.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,43 +105,43 @@ public async void CheckPathInput(ItemViewModel instance, string CurrentInput)
105105
}
106106
else if (CurrentInput.Equals("Desktop", StringComparison.OrdinalIgnoreCase))
107107
{
108-
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.DesktopPath);
108+
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.AppSettings.DesktopPath);
109109
parentPage.PathText.Text = "Desktop";
110110
parentPage.LayoutItems.isEnabled = true;
111111
}
112112
else if (CurrentInput.Equals("Documents", StringComparison.OrdinalIgnoreCase))
113113
{
114-
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.DocumentsPath);
114+
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.AppSettings.DocumentsPath);
115115
parentPage.PathText.Text = "Documents";
116116
parentPage.LayoutItems.isEnabled = true;
117117
}
118118
else if (CurrentInput.Equals("Downloads", StringComparison.OrdinalIgnoreCase))
119119
{
120-
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.DownloadsPath);
120+
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.AppSettings.DownloadsPath);
121121
parentPage.PathText.Text = "Downloads";
122122
parentPage.LayoutItems.isEnabled = true;
123123
}
124124
else if (CurrentInput.Equals("Pictures", StringComparison.OrdinalIgnoreCase))
125125
{
126-
parentPage.ItemDisplayFrame.Navigate(typeof(PhotoAlbum), App.PicturesPath);
126+
parentPage.ItemDisplayFrame.Navigate(typeof(PhotoAlbum), App.AppSettings.PicturesPath);
127127
parentPage.PathText.Text = "Pictures";
128128
parentPage.LayoutItems.isEnabled = true;
129129
}
130130
else if (CurrentInput.Equals("Music", StringComparison.OrdinalIgnoreCase))
131131
{
132-
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.MusicPath);
132+
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.AppSettings.MusicPath);
133133
parentPage.PathText.Text = "Music";
134134
parentPage.LayoutItems.isEnabled = true;
135135
}
136136
else if (CurrentInput.Equals("Videos", StringComparison.OrdinalIgnoreCase))
137137
{
138-
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.VideosPath);
138+
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.AppSettings.VideosPath);
139139
parentPage.PathText.Text = "Videos";
140140
parentPage.LayoutItems.isEnabled = true;
141141
}
142142
else if (CurrentInput.Equals("OneDrive", StringComparison.OrdinalIgnoreCase))
143143
{
144-
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.OneDrivePath);
144+
parentPage.ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), App.AppSettings.OneDrivePath);
145145
parentPage.PathText.Text = "OneDrive";
146146
parentPage.LayoutItems.isEnabled = true;
147147
}

0 commit comments

Comments
 (0)