Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the code and speed of ToStorageItem<TOut>() #4346

Merged
merged 6 commits into from
Mar 30, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 62 additions & 12 deletions Files/Helpers/StorageItemHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Files.Enums;
using Files.Filesystem;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Storage;

Expand All @@ -20,35 +22,83 @@ public static async Task<TOut> ToStorageItem<TOut>(string path, IShellPage assoc
FilesystemResult<StorageFile> file = null;
FilesystemResult<StorageFolder> folder = null;

if (associatedInstance == null)
if (path.EndsWith(".lnk") || path.EndsWith(".url"))
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
{
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path));
// TODO: In the future, when IStorageItemWithPath will inherit from IStorageItem,
// we could implement this code here for getting .lnk files
// for now, we can't

return default(TOut);

if (!file)
if (false) // Prevent unnecessary exceptions
d2dyno1 marked this conversation as resolved.
Show resolved Hide resolved
{
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path));
Debugger.Break();
throw new ArgumentException("Function ToStorageItem<TOut>() does not support converting from .lnk and .url files");
}
}
else
{
file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path);

if (!file)
if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut)))
{
await GetFile();
}
else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TOut)))
{
await GetFolder();
}
else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut)))
{
if (System.IO.Path.HasExtension(path)) // Probably a file
{
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
await GetFile();
}
else // Possibly a folder
{
await GetFolder();

if (!folder)
{
// It wasn't a folder, so check file then because it wasn't checked
await GetFile();
}
}
}

if (file)
if (file != null && file)
{
return (TOut)(IStorageItem)file.Result;
}
else if (folder)
else if (folder != null && folder)
{
return (TOut)(IStorageItem)folder.Result;
}

return default(TOut);

// Extensions

async Task GetFile()
{
if (associatedInstance == null)
{
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path));
}
else
{
file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path);
}
}

async Task GetFolder()
{
if (associatedInstance == null)
{
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path));
}
else
{
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
}
}
}

public static async Task<FilesystemResult<IStorageItem>> ToStorageItemResult(this IStorageItemWithPath item, IShellPage associatedInstance = null)
Expand Down Expand Up @@ -114,4 +164,4 @@ public static FilesystemResult<T> ToType<T, V>(FilesystemResult<V> result) where
return new FilesystemResult<T>(result.Result as T, result.ErrorCode);
}
}
}
}