Skip to content

Commit

Permalink
refactor completed without tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed May 3, 2021
1 parent af5d716 commit 3510905
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 94 deletions.
35 changes: 35 additions & 0 deletions Files/Filesystem/StorageItems/PlaceholderFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.FileProperties;

namespace Files.Filesystem.StorageItems
{
class PlaceholderFile : IStorageItem
{
public IAsyncAction RenameAsync(string desiredName) => throw new NotSupportedException();
public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) => throw new NotSupportedException();
public IAsyncAction DeleteAsync() => throw new NotSupportedException();
public IAsyncAction DeleteAsync(StorageDeleteOption option) => throw new NotSupportedException();
public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync() => throw new NotSupportedException();
public bool IsOfType(StorageItemTypes type) => type == StorageItemTypes.File;

public FileAttributes Attributes => throw new NotSupportedException();

public DateTimeOffset DateCreated => throw new NotSupportedException();

public string Name { get; }

public string Path { get; }

public PlaceholderFile(string path)
{
Path = path;
Name = System.IO.Path.GetFileName(path);
}
}
}
31 changes: 31 additions & 0 deletions Files/Filesystem/StorageItems/PlaceholderFolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.FileProperties;

namespace Files.Filesystem.StorageItems
{
class PlaceholderFolder : IStorageItem
{
public IAsyncAction RenameAsync(string desiredName) => throw new NotSupportedException();
public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) => throw new NotSupportedException();
public IAsyncAction DeleteAsync() => throw new NotSupportedException();
public IAsyncAction DeleteAsync(StorageDeleteOption option) => throw new NotSupportedException();
public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync() => throw new NotSupportedException();
public bool IsOfType(StorageItemTypes type) => type == StorageItemTypes.File;

public FileAttributes Attributes => throw new NotSupportedException();

public DateTimeOffset DateCreated => throw new NotSupportedException();

public string Name { get; }

public string Path { get; }

public PlaceholderFolder(string path)
{
Path = path;
Name = System.IO.Path.GetFileName(path);
}
}
}
12 changes: 11 additions & 1 deletion Files/Helpers/StorageItemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ public static async Task<IStorageItem> PathToStorageItemAsync(string path, Type
return new RecycleBinFolder(path);
}

throw new InvalidOperationException();
if (type == typeof(PlaceholderFile))
{
return new PlaceholderFile(path);
}

if (type == typeof(PlaceholderFolder))
{
return new PlaceholderFolder(path);
}

throw new NotSupportedException("Not supported IStorageItem implementation.");
}

public static async Task<T> PathToStorageItemAsync<T>(string path) where T : IStorageItem
Expand Down
9 changes: 5 additions & 4 deletions Files/Helpers/UIFilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Files.Dialogs;
using Files.Enums;
using Files.Filesystem;
using Files.Filesystem.StorageItems;
using Files.Interacts;
using Microsoft.Toolkit.Uwp;
using System;
Expand Down Expand Up @@ -186,7 +187,7 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string oldNa
ReturnResult renamed = ReturnResult.InProgress;
if (item.PrimaryItemAttribute == StorageItemTypes.Folder)
{
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageItemHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.Directory),
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(item.StorageItem,
newName, NameCollisionOption.FailIfExists, true);
}
else
Expand All @@ -196,7 +197,7 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string oldNa
newName += item.FileExtension;
}

renamed = await associatedInstance.FilesystemHelpers.RenameAsync(StorageItemHelpers.FromPathAndType(item.ItemPath, FilesystemItemType.File),
renamed = await associatedInstance.FilesystemHelpers.RenameAsync(item.StorageItem,
newName, NameCollisionOption.FailIfExists, true);
}

Expand Down Expand Up @@ -238,7 +239,7 @@ public static async void CreateFileFromDialogResultType(AddItemType itemType, Sh
created = await FilesystemTasks.Wrap(async () =>
{
return await associatedInstance.FilesystemHelpers.CreateAsync(
StorageItemHelpers.FromPathAndType(System.IO.Path.Combine(folderRes.Result.Path, userInput), FilesystemItemType.Directory),
await StorageItemHelpers.PathToStorageItemAsync<PlaceholderFolder>(System.IO.Path.Combine(folderRes.Result.Path, userInput)),
true);
});
break;
Expand All @@ -248,7 +249,7 @@ public static async void CreateFileFromDialogResultType(AddItemType itemType, Sh
created = await FilesystemTasks.Wrap(async () =>
{
return await associatedInstance.FilesystemHelpers.CreateAsync(
StorageItemHelpers.FromPathAndType(System.IO.Path.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension), FilesystemItemType.File),
await StorageItemHelpers.PathToStorageItemAsync<PlaceholderFile>(System.IO.Path.Combine(folderRes.Result.Path, userInput + itemInfo?.Extension)),
true);
});
break;
Expand Down
2 changes: 1 addition & 1 deletion Files/Helpers/WallpaperHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static async void SetAsBackground(WallpaperType type, string filePath, IS
if (UserProfilePersonalizationSettings.IsSupported())
{
// Get the path of the selected file
StorageFile sourceFile = await StorageItemHelpers.ToStorageItem<StorageFile>(filePath, associatedInstance);
StorageFile sourceFile = await StorageItemHelpers.PathToStorageItemAsync<StorageFile>(filePath);
if (sourceFile == null)
{
return;
Expand Down
23 changes: 4 additions & 19 deletions Files/Interacts/BaseLayoutCommandImplementationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,7 @@ public virtual async void RestoreItem(RoutedEventArgs e)
{
if (listedItem is RecycleBinItem binItem)
{
FilesystemItemType itemType = binItem.PrimaryItemAttribute == StorageItemTypes.Folder ? FilesystemItemType.Directory : FilesystemItemType.File;
await FilesystemHelpers.RestoreFromTrashAsync(StorageItemHelpers.FromPathAndType(
(listedItem as RecycleBinItem).ItemPath,
itemType), (listedItem as RecycleBinItem).ItemOriginalPath, true);
await FilesystemHelpers.RestoreFromTrashAsync(listedItem.StorageItem, (listedItem as RecycleBinItem).ItemOriginalPath, true);
}
}
}
Expand All @@ -192,9 +189,7 @@ await FilesystemHelpers.RestoreFromTrashAsync(StorageItemHelpers.FromPathAndType
public virtual async void DeleteItem(RoutedEventArgs e)
{
await FilesystemHelpers.DeleteItemsAsync(
SlimContentPage.SelectedItems.Select((item) => StorageItemHelpers.FromPathAndType(
item.ItemPath,
item.PrimaryItemAttribute == StorageItemTypes.File ? FilesystemItemType.File : FilesystemItemType.Directory)).ToList(),
SlimContentPage.SelectedItems.Select((item) => item.StorageItem).ToList(),
true, false, true);
}

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

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

if (destFolder)
{
Expand Down Expand Up @@ -352,19 +347,9 @@ async void Manager_DataRequested(DataTransferManager sender, DataRequestedEventA
return;
}
}
else if (item.PrimaryItemAttribute == StorageItemTypes.Folder)
{
if (await StorageItemHelpers.ToStorageItem<StorageFolder>(item.ItemPath, associatedInstance) is StorageFolder folder)
{
items.Add(folder);
}
}
else
{
if (await StorageItemHelpers.ToStorageItem<StorageFile>(item.ItemPath, associatedInstance) is StorageFile file)
{
items.Add(file);
}
items.Add(item.StorageItem);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Files/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public static async Task UpdateTabInfo(TabItem tabItem, object navigationArg)
fontIconSource.Glyph = "\xE8B7"; // Folder icon
tabLocationHeader = currentPath.TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar).Split('\\', StringSplitOptions.RemoveEmptyEntries).Last();

FilesystemResult<StorageFolderWithPath> rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(currentPath));
FilesystemResult<StorageFolder> rootItem = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(currentPath));
if (rootItem)
{
StorageFolder currentFolder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(currentPath, rootItem));
Expand Down
2 changes: 1 addition & 1 deletion Files/ViewModels/Pages/YourHomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void BundlesViewModel_OpenPathInNewPaneEvent(object sender, string e)

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

#region IDisposable
Expand Down
10 changes: 5 additions & 5 deletions Files/ViewModels/Previews/BasePreviewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public async virtual Task<List<FileProperty>> LoadPreviewAndDetails()
{
Item.FileImage = await IconData.ToBitmapAsync();
}
else
else if (Item.StorageItem is StorageFile file)
{
using var icon = await Item.ItemFile.GetThumbnailAsync(ThumbnailMode.SingleItem, 400);
using var icon = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 400);
Item.FileImage ??= new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await Item.FileImage.SetSourceAsync(icon);
}
Expand All @@ -66,12 +66,12 @@ public async virtual Task<List<FileProperty>> LoadPreviewAndDetails()

private async Task<List<FileProperty>> GetSystemFileProperties()
{
if (Item.IsShortcutItem)
if (Item.IsShortcutItem || Item.StorageItem is not StorageFile file)
{
return null;
}

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

list.Find(x => x.ID == "address").Value = await FileProperties.GetAddressFromCoordinatesAsync((double?)list.Find(x => x.Property == "System.GPS.LatitudeDecimal").Value,
(double?)list.Find(x => x.Property == "System.GPS.LongitudeDecimal").Value);
Expand All @@ -86,7 +86,7 @@ private async Task<List<FileProperty>> GetSystemFileProperties()
public virtual async Task LoadAsync()
{
var detailsFull = new List<FileProperty>();
Item.ItemFile ??= await StorageFile.GetFileFromPathAsync(Item.ItemPath);
Item.StorageItem ??= await StorageFile.GetFileFromPathAsync(Item.ItemPath);
DetailsFromPreview = await LoadPreviewAndDetails();
RaiseLoadedEvent();
var props = await GetSystemFileProperties();
Expand Down
5 changes: 4 additions & 1 deletion Files/ViewModels/Previews/HtmlPreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public string TextValue

public async override Task<List<FileProperty>> LoadPreviewAndDetails()
{
TextValue = await FileIO.ReadTextAsync(Item.ItemFile);
if (Item.StorageItem is StorageFile file)
{
TextValue = await FileIO.ReadTextAsync(file);
}
return new List<FileProperty>();
}
}
Expand Down
29 changes: 16 additions & 13 deletions Files/ViewModels/Previews/ImagePreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ public ImageSource ImageSource

public override async Task<List<FileProperty>> LoadPreviewAndDetails()
{
FileRandomAccessStream stream = (FileRandomAccessStream)await Item.ItemFile.OpenAsync(FileAccessMode.Read);

// svg files require a different type of source
if (!Item.ItemPath.EndsWith(".svg"))
{
var bitmap = new BitmapImage();
ImageSource = bitmap;
await bitmap.SetSourceAsync(stream);
}
else
if (Item.StorageItem is StorageFile file)
{
var bitmap = new SvgImageSource();
ImageSource = bitmap;
await bitmap.SetSourceAsync(stream);
FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

// svg files require a different type of source
if (!Item.ItemPath.EndsWith(".svg"))
{
var bitmap = new BitmapImage();
ImageSource = bitmap;
await bitmap.SetSourceAsync(stream);
}
else
{
var bitmap = new SvgImageSource();
ImageSource = bitmap;
await bitmap.SetSourceAsync(stream);
}
}

return new List<FileProperty>();
Expand Down
9 changes: 6 additions & 3 deletions Files/ViewModels/Previews/MarkdownPreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public string TextValue

public override async Task<List<FileProperty>> LoadPreviewAndDetails()
{
var text = await FileIO.ReadTextAsync(Item.ItemFile);
var displayText = text.Length < Constants.PreviewPane.TextCharacterLimit ? text : text.Remove(Constants.PreviewPane.TextCharacterLimit);
TextValue = displayText;
if (Item.StorageItem is StorageFile file)
{
var text = await FileIO.ReadTextAsync(file);
var displayText = text.Length < Constants.PreviewPane.TextCharacterLimit ? text : text.Remove(Constants.PreviewPane.TextCharacterLimit);
TextValue = displayText;
}

return new List<FileProperty>();
}
Expand Down
7 changes: 6 additions & 1 deletion Files/ViewModels/Previews/MediaPreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Storage;
using Windows.UI.Xaml;

namespace Files.ViewModels.Previews
Expand Down Expand Up @@ -30,7 +31,11 @@ public MediaSource Source

public override Task<List<FileProperty>> LoadPreviewAndDetails()
{
Source = MediaSource.CreateFromStorageFile(Item.ItemFile);
if (Item.StorageItem is StorageFile file)
{
Source = MediaSource.CreateFromStorageFile(file);
}

return Task.FromResult(new List<FileProperty>());
}

Expand Down
8 changes: 7 additions & 1 deletion Files/ViewModels/Previews/PDFPreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Data.Pdf;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Imaging;
Expand Down Expand Up @@ -35,7 +36,12 @@ public Visibility LoadingBarVisibility

public async override Task<List<FileProperty>> LoadPreviewAndDetails()
{
var pdf = await PdfDocument.LoadFromFileAsync(Item.ItemFile);
if (Item.StorageItem is not StorageFile file)
{
return new List<FileProperty>();
}

var pdf = await PdfDocument.LoadFromFileAsync(file);
TryLoadPagesAsync(pdf);
var details = new List<FileProperty>
{
Expand Down
7 changes: 6 additions & 1 deletion Files/ViewModels/Previews/RichTextPreviewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace Files.ViewModels.Previews
Expand All @@ -22,7 +23,11 @@ public RichTextPreviewViewModel(ListedItem item) : base(item)

public async override Task<List<FileProperty>> LoadPreviewAndDetails()
{
Stream = await Item.ItemFile.OpenReadAsync();
if (Item.StorageItem is StorageFile file)
{
Stream = await file.OpenReadAsync();
}

return new List<FileProperty>();
}
}
Expand Down
Loading

0 comments on commit 3510905

Please sign in to comment.