Skip to content

Commit 3510905

Browse files
committed
refactor completed without tests
1 parent af5d716 commit 3510905

21 files changed

+183
-94
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Windows.Foundation;
7+
using Windows.Storage;
8+
using Windows.Storage.FileProperties;
9+
10+
namespace Files.Filesystem.StorageItems
11+
{
12+
class PlaceholderFile : IStorageItem
13+
{
14+
public IAsyncAction RenameAsync(string desiredName) => throw new NotSupportedException();
15+
public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) => throw new NotSupportedException();
16+
public IAsyncAction DeleteAsync() => throw new NotSupportedException();
17+
public IAsyncAction DeleteAsync(StorageDeleteOption option) => throw new NotSupportedException();
18+
public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync() => throw new NotSupportedException();
19+
public bool IsOfType(StorageItemTypes type) => type == StorageItemTypes.File;
20+
21+
public FileAttributes Attributes => throw new NotSupportedException();
22+
23+
public DateTimeOffset DateCreated => throw new NotSupportedException();
24+
25+
public string Name { get; }
26+
27+
public string Path { get; }
28+
29+
public PlaceholderFile(string path)
30+
{
31+
Path = path;
32+
Name = System.IO.Path.GetFileName(path);
33+
}
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Windows.Foundation;
3+
using Windows.Storage;
4+
using Windows.Storage.FileProperties;
5+
6+
namespace Files.Filesystem.StorageItems
7+
{
8+
class PlaceholderFolder : IStorageItem
9+
{
10+
public IAsyncAction RenameAsync(string desiredName) => throw new NotSupportedException();
11+
public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) => throw new NotSupportedException();
12+
public IAsyncAction DeleteAsync() => throw new NotSupportedException();
13+
public IAsyncAction DeleteAsync(StorageDeleteOption option) => throw new NotSupportedException();
14+
public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync() => throw new NotSupportedException();
15+
public bool IsOfType(StorageItemTypes type) => type == StorageItemTypes.File;
16+
17+
public FileAttributes Attributes => throw new NotSupportedException();
18+
19+
public DateTimeOffset DateCreated => throw new NotSupportedException();
20+
21+
public string Name { get; }
22+
23+
public string Path { get; }
24+
25+
public PlaceholderFolder(string path)
26+
{
27+
Path = path;
28+
Name = System.IO.Path.GetFileName(path);
29+
}
30+
}
31+
}

Files/Helpers/StorageItemHelpers.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ public static async Task<IStorageItem> PathToStorageItemAsync(string path, Type
3737
return new RecycleBinFolder(path);
3838
}
3939

40-
throw new InvalidOperationException();
40+
if (type == typeof(PlaceholderFile))
41+
{
42+
return new PlaceholderFile(path);
43+
}
44+
45+
if (type == typeof(PlaceholderFolder))
46+
{
47+
return new PlaceholderFolder(path);
48+
}
49+
50+
throw new NotSupportedException("Not supported IStorageItem implementation.");
4151
}
4252

4353
public static async Task<T> PathToStorageItemAsync<T>(string path) where T : IStorageItem

Files/Helpers/UIFilesystemHelpers.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Files.Dialogs;
33
using Files.Enums;
44
using Files.Filesystem;
5+
using Files.Filesystem.StorageItems;
56
using Files.Interacts;
67
using Microsoft.Toolkit.Uwp;
78
using System;
@@ -186,7 +187,7 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string oldNa
186187
ReturnResult renamed = ReturnResult.InProgress;
187188
if (item.PrimaryItemAttribute == StorageItemTypes.Folder)
188189
{
189-
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageItemHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.Directory),
190+
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(item.StorageItem,
190191
newName, NameCollisionOption.FailIfExists, true);
191192
}
192193
else
@@ -196,7 +197,7 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string oldNa
196197
newName += item.FileExtension;
197198
}
198199

199-
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageItemHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.File),
200+
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(item.StorageItem,
200201
newName, NameCollisionOption.FailIfExists, true);
201202
}
202203

@@ -238,7 +239,7 @@ public static async void CreateFileFromDialogResultType(AddItemType itemType, Sh
238239
created = await FilesystemTasks.Wrap(async () =>
239240
{
240241
return await associatedInstance.FilesystemHelpers.CreateAsync(
241-
StorageItemHelpers.FromPathAndType(System.IO.Path.Combine(folderRes.Result.Path, userInput), FilesystemItemType.Directory),
242+
await StorageItemHelpers.PathToStorageItemAsync<PlaceholderFolder>(System.IO.Path.Combine(folderRes.Result.Path, userInput)),
242243
true);
243244
});
244245
break;
@@ -248,7 +249,7 @@ public static async void CreateFileFromDialogResultType(AddItemType itemType, Sh
248249
created = await FilesystemTasks.Wrap(async () =>
249250
{
250251
return await associatedInstance.FilesystemHelpers.CreateAsync(
251-
StorageItemHelpers.FromPathAndType(System.IO.Path.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension), FilesystemItemType.File),
252+
await StorageItemHelpers.PathToStorageItemAsync<PlaceholderFile>(System.IO.Path.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension)),
252253
true);
253254
});
254255
break;

Files/Helpers/WallpaperHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static async void SetAsBackground(WallpaperType type, string filePath, IS
1313
if (UserProfilePersonalizationSettings.IsSupported())
1414
{
1515
// Get the path of the selected file
16-
StorageFile sourceFile = await StorageItemHelpers.ToStorageItem<StorageFile>(filePath, associatedInstance);
16+
StorageFile sourceFile = await StorageItemHelpers.PathToStorageItemAsync<StorageFile>(filePath);
1717
if (sourceFile == null)
1818
{
1919
return;

Files/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,7 @@ public virtual async void RestoreItem(RoutedEventArgs e)
180180
{
181181
if (listedItem is RecycleBinItem binItem)
182182
{
183-
FilesystemItemType itemType = binItem.PrimaryItemAttribute == StorageItemTypes.Folder ? FilesystemItemType.Directory : FilesystemItemType.File;
184-
await FilesystemHelpers.RestoreFromTrashAsync(StorageItemHelpers.FromPathAndType(
185-
(listedItem as RecycleBinItem).ItemPath,
186-
itemType), (listedItem as RecycleBinItem).ItemOriginalPath, true);
183+
await FilesystemHelpers.RestoreFromTrashAsync(listedItem.StorageItem, (listedItem as RecycleBinItem).ItemOriginalPath, true);
187184
}
188185
}
189186
}
@@ -192,9 +189,7 @@ await FilesystemHelpers.RestoreFromTrashAsync(StorageItemHelpers.FromPathAndType
192189
public virtual async void DeleteItem(RoutedEventArgs e)
193190
{
194191
await FilesystemHelpers.DeleteItemsAsync(
195-
SlimContentPage.SelectedItems.Select((item) => StorageItemHelpers.FromPathAndType(
196-
item.ItemPath,
197-
item.PrimaryItemAttribute == StorageItemTypes.File ? FilesystemItemType.File : FilesystemItemType.Directory)).ToList(),
192+
SlimContentPage.SelectedItems.Select((item) => item.StorageItem).ToList(),
198193
true, false, true);
199194
}
200195

@@ -219,7 +214,7 @@ public virtual async void OpenFileLocation(RoutedEventArgs e)
219214

220215
// Check if destination path exists
221216
string folderPath = System.IO.Path.GetDirectoryName(item.TargetPath);
222-
FilesystemResult<StorageFolderWithPath> destFolder = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(folderPath);
217+
FilesystemResult<StorageFolder> destFolder = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(folderPath);
223218

224219
if (destFolder)
225220
{
@@ -352,19 +347,9 @@ async void Manager_DataRequested(DataTransferManager sender, DataRequestedEventA
352347
return;
353348
}
354349
}
355-
else if (item.PrimaryItemAttribute == StorageItemTypes.Folder)
356-
{
357-
if (await StorageItemHelpers.ToStorageItem<StorageFolder>(item.ItemPath, associatedInstance) is StorageFolder folder)
358-
{
359-
items.Add(folder);
360-
}
361-
}
362350
else
363351
{
364-
if (await StorageItemHelpers.ToStorageItem<StorageFile>(item.ItemPath, associatedInstance) is StorageFile file)
365-
{
366-
items.Add(file);
367-
}
352+
items.Add(item.StorageItem);
368353
}
369354
}
370355

Files/ViewModels/MainPageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public static async Task UpdateTabInfo(TabItem tabItem, object navigationArg)
320320
fontIconSource.Glyph = "\xE8B7"; // Folder icon
321321
tabLocationHeader = currentPath.TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar).Split('\\', StringSplitOptions.RemoveEmptyEntries).Last();
322322

323-
FilesystemResult<StorageFolderWithPath> rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(currentPath));
323+
FilesystemResult<StorageFolder> rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(currentPath));
324324
if (rootItem)
325325
{
326326
StorageFolder currentFolder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(currentPath, rootItem));

Files/ViewModels/Pages/YourHomeViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private void BundlesViewModel_OpenPathInNewPaneEvent(object sender, string e)
7070

7171
private async void BundlesViewModel_OpenPathEvent(object sender, BundlesOpenPathEventArgs e)
7272
{
73-
await NavigationHelpers.OpenPath(e.path, associatedInstance, e.itemType, e.openSilent, e.openViaApplicationPicker, e.selectItems);
73+
await NavigationHelpers.OpenPath(e.item, associatedInstance, false /* TODO: ? */, e.openSilent, e.openViaApplicationPicker, e.selectItems);
7474
}
7575

7676
#region IDisposable

Files/ViewModels/Previews/BasePreviewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public async virtual Task<List<FileProperty>> LoadPreviewAndDetails()
5454
{
5555
Item.FileImage = await IconData.ToBitmapAsync();
5656
}
57-
else
57+
else if (Item.StorageItem is StorageFile file)
5858
{
59-
using var icon = await Item.ItemFile.GetThumbnailAsync(ThumbnailMode.SingleItem, 400);
59+
using var icon = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 400);
6060
Item.FileImage ??= new Windows.UI.Xaml.Media.Imaging.BitmapImage();
6161
await Item.FileImage.SetSourceAsync(icon);
6262
}
@@ -66,12 +66,12 @@ public async virtual Task<List<FileProperty>> LoadPreviewAndDetails()
6666

6767
private async Task<List<FileProperty>> GetSystemFileProperties()
6868
{
69-
if (Item.IsShortcutItem)
69+
if (Item.IsShortcutItem || Item.StorageItem is not StorageFile file)
7070
{
7171
return null;
7272
}
7373

74-
var list = await FileProperty.RetrieveAndInitializePropertiesAsync(Item.ItemFile, Constants.ResourceFilePaths.PreviewPaneDetailsPropertiesJsonPath);
74+
var list = await FileProperty.RetrieveAndInitializePropertiesAsync(file, Constants.ResourceFilePaths.PreviewPaneDetailsPropertiesJsonPath);
7575

7676
list.Find(x => x.ID == "address").Value = await FileProperties.GetAddressFromCoordinatesAsync((double?)list.Find(x => x.Property == "System.GPS.LatitudeDecimal").Value,
7777
(double?)list.Find(x => x.Property == "System.GPS.LongitudeDecimal").Value);
@@ -86,7 +86,7 @@ private async Task<List<FileProperty>> GetSystemFileProperties()
8686
public virtual async Task LoadAsync()
8787
{
8888
var detailsFull = new List<FileProperty>();
89-
Item.ItemFile ??= await StorageFile.GetFileFromPathAsync(Item.ItemPath);
89+
Item.StorageItem ??= await StorageFile.GetFileFromPathAsync(Item.ItemPath);
9090
DetailsFromPreview = await LoadPreviewAndDetails();
9191
RaiseLoadedEvent();
9292
var props = await GetSystemFileProperties();

Files/ViewModels/Previews/HtmlPreviewViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public string TextValue
3030

3131
public async override Task<List<FileProperty>> LoadPreviewAndDetails()
3232
{
33-
TextValue = await FileIO.ReadTextAsync(Item.ItemFile);
33+
if (Item.StorageItem is StorageFile file)
34+
{
35+
TextValue = await FileIO.ReadTextAsync(file);
36+
}
3437
return new List<FileProperty>();
3538
}
3639
}

0 commit comments

Comments
 (0)