From c52a50dccef4ecab102632439bceb4b3ba5e4142 Mon Sep 17 00:00:00 2001 From: cinqmilleans Date: Sat, 9 Apr 2022 10:31:59 +0200 Subject: [PATCH 1/2] Clean IStorageItemWithPath --- .../IStorageItemWithPath.cs | 10 +++--- .../StorageFileExtensions.cs | 21 ++++++----- .../StorageFileHelpers/StorageFileWithPath.cs | 35 ++++++------------- .../StorageFolderWithPath.cs | 33 +++++------------ src/Files.Uwp/Helpers/NavigationHelpers.cs | 24 ++++++------- src/Files.Uwp/ViewModels/ItemViewModel.cs | 6 ++-- .../ViewModels/NavToolbarViewModel.cs | 22 ++++++------ 7 files changed, 60 insertions(+), 91 deletions(-) diff --git a/src/Files.Uwp/Filesystem/StorageFileHelpers/IStorageItemWithPath.cs b/src/Files.Uwp/Filesystem/StorageFileHelpers/IStorageItemWithPath.cs index 388e20e398e8..3f6db2e273aa 100644 --- a/src/Files.Uwp/Filesystem/StorageFileHelpers/IStorageItemWithPath.cs +++ b/src/Files.Uwp/Filesystem/StorageFileHelpers/IStorageItemWithPath.cs @@ -2,12 +2,12 @@ namespace Files.Filesystem { - public interface IStorageItemWithPath // TODO: Maybe use here : IStorageItem instead of declaring a variable, - // and keep the Path property for it to override IStorageItem.Path ? + public interface IStorageItemWithPath { public string Name { get; } - public string Path { get; set; } - public IStorageItem Item { get; set; } + public string Path { get; } + + public IStorageItem Item { get; } public FilesystemItemType ItemType { get; } } -} \ No newline at end of file +} diff --git a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileExtensions.cs b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileExtensions.cs index 64d476fea34d..ad1408a86333 100644 --- a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileExtensions.cs +++ b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileExtensions.cs @@ -1,5 +1,4 @@ -using Files.Shared; -using Files.DataModels.NavigationControlItems; +using Files.DataModels.NavigationControlItems; using Files.Extensions; using Files.Filesystem.StorageItems; using Files.Helpers; @@ -106,7 +105,7 @@ public async static Task DangerousGetFolderWithPathFromPa } else if (parentFolder != null && value.IsSubPathOf(parentFolder.Path)) { - var folder = parentFolder.Folder; + var folder = parentFolder.Item; var prevComponents = GetDirectoryPathComponents(parentFolder.Path); var path = parentFolder.Path; foreach (var component in currComponents.ExceptBy(prevComponents, c => c.Path)) @@ -118,7 +117,7 @@ public async static Task DangerousGetFolderWithPathFromPa } else if (value.IsSubPathOf(rootFolder.Path)) { - var folder = rootFolder.Folder; + var folder = rootFolder.Item; var path = rootFolder.Path; foreach (var component in currComponents.Skip(1)) { @@ -145,7 +144,7 @@ public async static Task DangerousGetFolderFromPathAsync(stri StorageFolderWithPath rootFolder = null, StorageFolderWithPath parentFolder = null) { - return (await DangerousGetFolderWithPathFromPathAsync(value, rootFolder, parentFolder)).Folder; + return (await DangerousGetFolderWithPathFromPathAsync(value, rootFolder, parentFolder)).Item; } public async static Task DangerousGetFileWithPathFromPathAsync(string value, @@ -158,7 +157,7 @@ public async static Task DangerousGetFileWithPathFromPathAs if (parentFolder != null && value.IsSubPathOf(parentFolder.Path)) { - var folder = parentFolder.Folder; + var folder = parentFolder.Item; var prevComponents = GetDirectoryPathComponents(parentFolder.Path); var path = parentFolder.Path; foreach (var component in currComponents.ExceptBy(prevComponents, c => c.Path).SkipLast(1)) @@ -172,7 +171,7 @@ public async static Task DangerousGetFileWithPathFromPathAs } else if (value.IsSubPathOf(rootFolder.Path)) { - var folder = rootFolder.Folder; + var folder = rootFolder.Item; var path = rootFolder.Path; foreach (var component in currComponents.Skip(1).SkipLast(1)) { @@ -201,18 +200,18 @@ public async static Task DangerousGetFileFromPathAsync(string v StorageFolderWithPath rootFolder = null, StorageFolderWithPath parentFolder = null) { - return (await DangerousGetFileWithPathFromPathAsync(value, rootFolder, parentFolder)).File; + return (await DangerousGetFileWithPathFromPathAsync(value, rootFolder, parentFolder)).Item; } public async static Task> GetFoldersWithPathAsync(this StorageFolderWithPath parentFolder, uint maxNumberOfItems = uint.MaxValue) { - return (await parentFolder.Folder.GetFoldersAsync(CommonFolderQuery.DefaultQuery, 0, maxNumberOfItems)) + return (await parentFolder.Item.GetFoldersAsync(CommonFolderQuery.DefaultQuery, 0, maxNumberOfItems)) .Select(x => new StorageFolderWithPath(x, PathNormalization.Combine(parentFolder.Path, x.Name))).ToList(); } public async static Task> GetFilesWithPathAsync(this StorageFolderWithPath parentFolder, uint maxNumberOfItems = uint.MaxValue) { - return (await parentFolder.Folder.GetFilesAsync(CommonFileQuery.DefaultQuery, 0, maxNumberOfItems)) + return (await parentFolder.Item.GetFilesAsync(CommonFileQuery.DefaultQuery, 0, maxNumberOfItems)) .Select(x => new StorageFileWithPath(x, PathNormalization.Combine(parentFolder.Path, x.Name))).ToList(); } @@ -225,7 +224,7 @@ public async static Task> GetFoldersWithPathAsync(t var queryOptions = new QueryOptions(); queryOptions.ApplicationSearchFilter = $"System.FileName:{nameFilter}*"; - BaseStorageFolderQueryResult queryResult = parentFolder.Folder.CreateFolderQueryWithOptions(queryOptions); + BaseStorageFolderQueryResult queryResult = parentFolder.Item.CreateFolderQueryWithOptions(queryOptions); return (await queryResult.GetFoldersAsync(0, maxNumberOfItems)).Select(x => new StorageFolderWithPath(x, PathNormalization.Combine(parentFolder.Path, x.Name))).ToList(); } diff --git a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs index 6920a543ef59..c3e849ea49f7 100644 --- a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs +++ b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs @@ -1,37 +1,22 @@ using Files.Filesystem.StorageItems; using Windows.Storage; +using IO = System.IO; namespace Files.Filesystem { public class StorageFileWithPath : IStorageItemWithPath { - public BaseStorageFile File - { - get - { - return (BaseStorageFile)Item; - } - set - { - Item = value; - } - } + public string Path { get; } + public string Name => Item?.Name ?? IO.Path.GetFileName(Path); - public string Path { get; set; } - public string Name => Item?.Name ?? System.IO.Path.GetFileName(Path); - public IStorageItem Item { get; set; } - public FilesystemItemType ItemType => FilesystemItemType.File; + IStorageItem IStorageItemWithPath.Item => Item; + public BaseStorageFile Item { get; } - public StorageFileWithPath(BaseStorageFile file) - { - File = file; - Path = File.Path; - } + public FilesystemItemType ItemType => FilesystemItemType.Directory; + public StorageFileWithPath(BaseStorageFile file) + : this(file, file.Path) {} public StorageFileWithPath(BaseStorageFile file, string path) - { - File = file; - Path = path; - } + => (Item, Path) = (file, path); } -} \ No newline at end of file +} diff --git a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFolderWithPath.cs b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFolderWithPath.cs index d70c42e5eb9d..da63d8fc391e 100644 --- a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFolderWithPath.cs +++ b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFolderWithPath.cs @@ -1,37 +1,22 @@ using Files.Filesystem.StorageItems; using Windows.Storage; +using IO = System.IO; namespace Files.Filesystem { public class StorageFolderWithPath : IStorageItemWithPath { - public BaseStorageFolder Folder - { - get - { - return (BaseStorageFolder)Item; - } - set - { - Item = value; - } - } + public string Path { get; } + public string Name => Item?.Name ?? IO.Path.GetFileName(Path); + + IStorageItem IStorageItemWithPath.Item => Item; + public BaseStorageFolder Item { get; } - public string Path { get; set; } - public string Name => Item?.Name ?? System.IO.Path.GetFileName(Path); - public IStorageItem Item { get; set; } public FilesystemItemType ItemType => FilesystemItemType.Directory; public StorageFolderWithPath(BaseStorageFolder folder) - { - Folder = folder; - Path = folder.Path; - } - + : this(folder, folder.Path) {} public StorageFolderWithPath(BaseStorageFolder folder, string path) - { - Folder = folder; - Path = path; - } + => (Item, Path) = (folder, path); } -} \ No newline at end of file +} diff --git a/src/Files.Uwp/Helpers/NavigationHelpers.cs b/src/Files.Uwp/Helpers/NavigationHelpers.cs index d4a77380d0c4..8f8be50cb2d0 100644 --- a/src/Files.Uwp/Helpers/NavigationHelpers.cs +++ b/src/Files.Uwp/Helpers/NavigationHelpers.cs @@ -1,11 +1,11 @@ -using Files.Shared; -using Files.Shared.Enums; +using CommunityToolkit.Mvvm.DependencyInjection; +using Files.Backend.Services.Settings; using Files.Filesystem; using Files.Filesystem.StorageItems; -using Files.Backend.Services.Settings; +using Files.Shared; +using Files.Shared.Enums; using Files.ViewModels; using Files.Views; -using CommunityToolkit.Mvvm.DependencyInjection; using Microsoft.Toolkit.Uwp; using Newtonsoft.Json; using System; @@ -342,10 +342,10 @@ private static async Task OpenDirectory(string path, IShellPag .OnSuccess(async (childFolder) => { // Add location to MRU List - if (childFolder.Folder is SystemStorageFolder) + if (childFolder.Item is SystemStorageFolder) { var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; - mostRecentlyUsed.Add(await childFolder.Folder.ToStorageFolderAsync(), childFolder.Path); + mostRecentlyUsed.Add(await childFolder.Item.ToStorageFolderAsync(), childFolder.Path); } }); if (!opened) @@ -392,10 +392,10 @@ private static async Task OpenFile(string path, IShellPage ass if (childFile != null) { // Add location to MRU List - if (childFile.File is SystemStorageFile) + if (childFile.Item is SystemStorageFile) { var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; - mostRecentlyUsed.Add(await childFile.File.ToStorageFileAsync(), childFile.Path); + mostRecentlyUsed.Add(await childFile.Item.ToStorageFileAsync(), childFile.Path); } } } @@ -413,10 +413,10 @@ private static async Task OpenFile(string path, IShellPage ass .OnSuccess(async childFile => { // Add location to MRU List - if (childFile.File is SystemStorageFile) + if (childFile.Item is SystemStorageFile) { var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; - mostRecentlyUsed.Add(await childFile.File.ToStorageFileAsync(), childFile.Path); + mostRecentlyUsed.Add(await childFile.Item.ToStorageFileAsync(), childFile.Path); } if (openViaApplicationPicker) @@ -425,7 +425,7 @@ private static async Task OpenFile(string path, IShellPage ass { DisplayApplicationPicker = true }; - if (!await Launcher.LaunchFileAsync(childFile.File, options)) + if (!await Launcher.LaunchFileAsync(childFile.Item, options)) { var connection = await AppServiceConnectionHelper.Instance; if (connection != null) @@ -512,7 +512,7 @@ await connection.SendMessageAsync(new ValueSet() } // Now launch file with options. - launchSuccess = await Launcher.LaunchFileAsync(await childFile.File.ToStorageFileAsync(), options); + launchSuccess = await Launcher.LaunchFileAsync(await childFile.Item.ToStorageFileAsync(), options); } if (!launchSuccess) diff --git a/src/Files.Uwp/ViewModels/ItemViewModel.cs b/src/Files.Uwp/ViewModels/ItemViewModel.cs index 5d606a25353d..8fb4d832fe18 100644 --- a/src/Files.Uwp/ViewModels/ItemViewModel.cs +++ b/src/Files.Uwp/ViewModels/ItemViewModel.cs @@ -1309,7 +1309,7 @@ private async Task RapidAddItemsToCollection(string path, LibraryItem library = case 1: // Enumerated with StorageFolder PageTypeUpdated?.Invoke(this, new PageTypeUpdatedEventArgs() { IsTypeCloudDrive = false }); currentStorageFolder ??= await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderWithPathFromPathAsync(path)); - WatchForStorageFolderChanges(currentStorageFolder?.Folder); + WatchForStorageFolderChanges(currentStorageFolder?.Item); break; case 2: // Watch for changes using FTP in Box Drive folder (#7428) and network drives (#5869) @@ -1527,7 +1527,7 @@ public async Task EnumerateItemsFromStandardFolderAsync(string path, Type s return -1; } currentStorageFolder = res.Result; - rootFolder = currentStorageFolder.Folder; + rootFolder = currentStorageFolder.Item; enumFromStorageFolder = true; } else @@ -1536,7 +1536,7 @@ public async Task EnumerateItemsFromStandardFolderAsync(string path, Type s if (res) { currentStorageFolder = res.Result; - rootFolder = currentStorageFolder.Folder; + rootFolder = currentStorageFolder.Item; } else if (res == FileSystemStatusCode.Unauthorized) { diff --git a/src/Files.Uwp/ViewModels/NavToolbarViewModel.cs b/src/Files.Uwp/ViewModels/NavToolbarViewModel.cs index 351a6778854c..9c4380297ede 100644 --- a/src/Files.Uwp/ViewModels/NavToolbarViewModel.cs +++ b/src/Files.Uwp/ViewModels/NavToolbarViewModel.cs @@ -1,14 +1,16 @@ -using Files.Shared.Extensions; -using Files.Shared.Enums; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.DependencyInjection; +using CommunityToolkit.Mvvm.Input; +using Files.Backend.Services; +using Files.Backend.Services.Settings; using Files.Filesystem; using Files.Filesystem.StorageItems; using Files.Helpers; -using Files.Backend.Services.Settings; +using Files.Shared.Enums; +using Files.Shared.EventArguments; +using Files.Shared.Extensions; using Files.UserControls; using Files.Views; -using CommunityToolkit.Mvvm.ComponentModel; -using CommunityToolkit.Mvvm.DependencyInjection; -using CommunityToolkit.Mvvm.Input; using Microsoft.Toolkit.Uwp; using Microsoft.Toolkit.Uwp.UI; using System; @@ -27,11 +29,9 @@ using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Input; -using Files.Backend.Services; using static Files.UserControls.INavigationToolbar; using SearchBox = Files.UserControls.SearchBox; using SortDirection = Files.Shared.Enums.SortDirection; -using Files.Shared.EventArguments; namespace Files.ViewModels { @@ -1055,7 +1055,7 @@ public async void SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage she suggestions = currPath.Select(x => new ListedItem(null) { ItemPath = x.Path, - ItemNameRaw = x.Folder.DisplayName + ItemNameRaw = x.Item.DisplayName }).ToList(); } else if (currPath.Any()) @@ -1064,12 +1064,12 @@ public async void SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage she suggestions = currPath.Select(x => new ListedItem(null) { ItemPath = x.Path, - ItemNameRaw = x.Folder.DisplayName + ItemNameRaw = x.Item.DisplayName }).Concat( subPath.Select(x => new ListedItem(null) { ItemPath = x.Path, - ItemNameRaw = PathNormalization.Combine(currPath.First().Folder.DisplayName, x.Folder.DisplayName) + ItemNameRaw = PathNormalization.Combine(currPath.First().Item.DisplayName, x.Item.DisplayName) })).ToList(); } else From ccbc3e8c15156d2a784626935e9d284a564c85c6 Mon Sep 17 00:00:00 2001 From: cinqmilleans Date: Sat, 9 Apr 2022 15:28:04 +0200 Subject: [PATCH 2/2] fix file --- .../Filesystem/StorageFileHelpers/StorageFileWithPath.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs index c3e849ea49f7..1ecb1082733a 100644 --- a/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs +++ b/src/Files.Uwp/Filesystem/StorageFileHelpers/StorageFileWithPath.cs @@ -12,7 +12,7 @@ public class StorageFileWithPath : IStorageItemWithPath IStorageItem IStorageItemWithPath.Item => Item; public BaseStorageFile Item { get; } - public FilesystemItemType ItemType => FilesystemItemType.Directory; + public FilesystemItemType ItemType => FilesystemItemType.File; public StorageFileWithPath(BaseStorageFile file) : this(file, file.Path) {}