Skip to content

Commit 496a84e

Browse files
committed
Extract Drives to own class
1 parent 69902d4 commit 496a84e

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

Files/Files.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
<Compile Include="Enums\ThemeStyle.cs" />
173173
<Compile Include="Enums\TimeStyle.cs" />
174174
<Compile Include="Filesystem\DriveItem.cs" />
175+
<Compile Include="Filesystem\Drives.cs" />
175176
<Compile Include="Filesystem\ItemViewModel.cs" />
176177
<Compile Include="Filesystem\ListedItem.cs" />
177178
<Compile Include="Filesystem\SidebarItem.cs" />

Files/Filesystem/Drives.cs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Windows.Storage;
10+
using Windows.UI.Xaml;
11+
12+
namespace Files.Filesystem
13+
{
14+
class Drives
15+
{
16+
public Task<ObservableCollection<DriveItem>> LocalDrives { get; }
17+
18+
public Task<ObservableCollection<DriveItem>> RemovableDrives { get; }
19+
20+
public Task<ObservableCollection<DriveItem>> VirtualDrives { get; }
21+
22+
public Task<ObservableCollection<DriveItem>> AllDrives { get; }
23+
24+
Drives()
25+
{
26+
LocalDrives = GetLocalDrivesListAsync();
27+
RemovableDrives = GetRemovableDrivesListAsync();
28+
VirtualDrives = GetVirtualDrivesListAsync();
29+
30+
InitAllDrivesList();
31+
}
32+
33+
private async void InitAllDrivesList()
34+
{
35+
var localDrives = await LocalDrives;
36+
localDrives.CollectionChanged += (sender, args) => { };
37+
}
38+
39+
private Task<ObservableCollection<DriveItem>> GetLocalDrivesListAsync()
40+
{
41+
return Task.Run(() =>
42+
{
43+
var drives = new ObservableCollection<DriveItem>();
44+
45+
var driveLetters = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).OrderBy(x => x.Root.FullName)
46+
.ToList();
47+
var removableDevicesTask = KnownFolders.RemovableDevices.GetFoldersAsync().AsTask();
48+
removableDevicesTask.Wait();
49+
var removableDevicesPaths = removableDevicesTask.Result.Select(x => x.Path).ToList();
50+
51+
52+
foreach (var driveLetter in driveLetters)
53+
{
54+
try
55+
{
56+
var content = string.Empty;
57+
string icon = null;
58+
59+
if (!removableDevicesPaths.Contains(driveLetter.Name))
60+
{
61+
// TODO: Display Custom Names for Local Disks as well
62+
if (InstanceTabsView.NormalizePath(driveLetter.Name) != InstanceTabsView.NormalizePath("A:")
63+
&& InstanceTabsView.NormalizePath(driveLetter.Name) !=
64+
InstanceTabsView.NormalizePath("B:"))
65+
{
66+
content = $"Local Disk ({driveLetter.Name.TrimEnd('\\')})";
67+
icon = "\uEDA2";
68+
}
69+
else
70+
{
71+
content = $"Floppy Disk ({driveLetter.Name.TrimEnd('\\')})";
72+
icon = "\uE74E";
73+
}
74+
75+
Visibility capacityBarVis = Visibility.Visible;
76+
ulong totalSpaceProg = 0;
77+
ulong freeSpaceProg = 0;
78+
string free_space_text = "Unknown";
79+
string total_space_text = "Unknown";
80+
81+
try
82+
{
83+
84+
var driveTask = StorageFolder.GetFolderFromPathAsync(driveLetter.Name).AsTask();
85+
driveTask.Wait();
86+
var retrivedPropertiesTask = driveTask.Result.Properties
87+
.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" })
88+
.AsTask();
89+
var retrivedProperties = retrivedPropertiesTask.Result;
90+
91+
var sizeAsGBString = ByteSizeLib.ByteSize
92+
.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
93+
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);
94+
95+
sizeAsGBString = ByteSizeLib.ByteSize
96+
.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
97+
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);
98+
99+
free_space_text = ByteSizeLib.ByteSize
100+
.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
101+
total_space_text = ByteSizeLib.ByteSize
102+
.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
103+
}
104+
catch (UnauthorizedAccessException)
105+
{
106+
capacityBarVis = Visibility.Collapsed;
107+
}
108+
catch (NullReferenceException)
109+
{
110+
capacityBarVis = Visibility.Collapsed;
111+
}
112+
113+
drives.Add(new DriveItem()
114+
{
115+
driveText = content,
116+
glyph = icon,
117+
maxSpace = totalSpaceProg,
118+
spaceUsed = totalSpaceProg - freeSpaceProg,
119+
tag = driveLetter.Name,
120+
progressBarVisibility = capacityBarVis,
121+
spaceText = free_space_text + " free of " + total_space_text,
122+
});
123+
}
124+
}
125+
catch (UnauthorizedAccessException e)
126+
{
127+
Debug.WriteLine(e.Message);
128+
}
129+
}
130+
131+
return drives;
132+
});
133+
}
134+
135+
private Task<ObservableCollection<DriveItem>> GetRemovableDrivesListAsync()
136+
{
137+
return Task.Run(() =>
138+
{
139+
var drives = new ObservableCollection<DriveItem>();
140+
141+
// TODO Get RemovableDrives
142+
143+
return drives;
144+
});
145+
}
146+
147+
private Task<ObservableCollection<DriveItem>> GetVirtualDrivesListAsync()
148+
{
149+
return Task.Run(() =>
150+
{
151+
var drives = new ObservableCollection<DriveItem>();
152+
153+
var oneDriveItem = new DriveItem()
154+
{
155+
driveText = "OneDrive",
156+
tag = "OneDrive",
157+
cloudGlyphVisibility = Visibility.Visible,
158+
driveGlyphVisibility = Visibility.Collapsed
159+
};
160+
161+
drives.Add(oneDriveItem);
162+
163+
return drives;
164+
});
165+
}
166+
}
167+
}

Files/View Models/SettingsViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
using Windows.UI;
1111
using Windows.UI.Xaml;
1212
using Windows.UI.Xaml.Controls;
13+
using Files.Filesystem;
1314

1415
namespace Files.View_Models
1516
{
1617
class SettingsViewModel : ViewModelBase
1718
{
1819
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
1920

21+
public Drives Drives { get; }
22+
2023
public SettingsViewModel()
2124
{
2225
DetectCustomLocations();
2326
DetectApplicationTheme();
2427
DetectDateTimeFormat();
2528
DetectSidebarOpacity();
2629

30+
Drives = new Drives();
2731
}
2832

2933
private void DetectSidebarOpacity()

0 commit comments

Comments
 (0)