Skip to content

Commit

Permalink
Code Quality: Appended Legacy to the existing storable implementations (
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa authored Nov 24, 2024
1 parent 195be1f commit 3b31e72
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IFile"/>
public class NativeFile : NativeStorable<FileInfo>, ILocatableFile, IModifiableFile, IFileExtended, INestedFile
[Obsolete("Use the new WindowsStorable")]
public class NativeFileLegacy : NativeStorableLegacy<FileInfo>, ILocatableFile, IModifiableFile, IFileExtended, INestedFile
{
public NativeFile(FileInfo fileInfo, string? name = null)
public NativeFileLegacy(FileInfo fileInfo, string? name = null)
: base(fileInfo, name)
{
}

public NativeFile(string path, string? name = null)
public NativeFileLegacy(string path, string? name = null)
: this(new FileInfo(path), name)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IFolder"/>
public class NativeFolder : NativeStorable<DirectoryInfo>, ILocatableFolder, IModifiableFolder, IMutableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
[Obsolete("Use the new WindowsStorable")]
public class NativeFolderLegacy : NativeStorableLegacy<DirectoryInfo>, ILocatableFolder, IModifiableFolder, IMutableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
{
public NativeFolder(DirectoryInfo directoryInfo, string? name = null)
public NativeFolderLegacy(DirectoryInfo directoryInfo, string? name = null)
: base(directoryInfo, name)
{
}

public NativeFolder(string path, string? name = null)
public NativeFolderLegacy(string path, string? name = null)
: this(new DirectoryInfo(path), name)
{
}
Expand All @@ -27,7 +28,7 @@ public virtual Task<INestedFile> GetFileAsync(string fileName, CancellationToken
if (!File.Exists(path))
throw new FileNotFoundException();

return Task.FromResult<INestedFile>(new NativeFile(path));
return Task.FromResult<INestedFile>(new NativeFileLegacy(path));
}

/// <inheritdoc/>
Expand All @@ -37,7 +38,7 @@ public virtual Task<INestedFolder> GetFolderAsync(string folderName, Cancellatio
if (!Directory.Exists(path))
throw new FileNotFoundException();

return Task.FromResult<INestedFolder>(new NativeFolder(path));
return Task.FromResult<INestedFolder>(new NativeFolderLegacy(path));
}

/// <inheritdoc/>
Expand All @@ -46,21 +47,21 @@ public virtual async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKin
if (kind == StorableKind.Files)
{
foreach (var item in Directory.EnumerateFiles(Path))
yield return new NativeFile(item);
yield return new NativeFileLegacy(item);
}
else if (kind == StorableKind.Folders)
{
foreach (var item in Directory.EnumerateDirectories(Path))
yield return new NativeFolder(item);
yield return new NativeFolderLegacy(item);
}
else
{
foreach (var item in Directory.EnumerateFileSystemEntries(Path))
{
if (File.Exists(item))
yield return new NativeFile(item);
yield return new NativeFileLegacy(item);
else
yield return new NativeFolder(item);
yield return new NativeFolderLegacy(item);
}
}

Expand Down Expand Up @@ -96,7 +97,7 @@ public virtual async Task<INestedStorable> CreateCopyOfAsync(INestedStorable ite
var newPath = System.IO.Path.Combine(Path, itemToCopy.Name);
File.Copy(sourceLocatableFile.Path, newPath, overwrite);

return new NativeFile(newPath);
return new NativeFileLegacy(newPath);
}

var copiedFile = await CreateFileAsync(itemToCopy.Name, overwrite, cancellationToken);
Expand Down Expand Up @@ -124,7 +125,7 @@ public virtual async Task<INestedStorable> MoveFromAsync(INestedStorable itemToM
var newPath = System.IO.Path.Combine(Path, itemToMove.Name);
File.Move(sourceLocatableFile.Path, newPath, overwrite);

return new NativeFile(newPath);
return new NativeFileLegacy(newPath);
}
else
{
Expand All @@ -150,7 +151,7 @@ public virtual async Task<INestedFile> CreateFileAsync(string desiredName, bool
if (overwrite || !File.Exists(path))
await File.Create(path).DisposeAsync();

return new NativeFile(path);
return new NativeFileLegacy(path);
}

/// <inheritdoc/>
Expand All @@ -161,7 +162,7 @@ public virtual Task<INestedFolder> CreateFolderAsync(string desiredName, bool ov
Directory.Delete(path, true);

_ = Directory.CreateDirectory(path);
return Task.FromResult<INestedFolder>(new NativeFolder(path));
return Task.FromResult<INestedFolder>(new NativeFolderLegacy(path));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IStorable"/>
public abstract class NativeStorable<TStorage> : ILocatableStorable, INestedStorable
[Obsolete("Use the new WindowsStorable")]
public abstract class NativeStorableLegacy<TStorage> : ILocatableStorable, INestedStorable
where TStorage : FileSystemInfo
{
protected readonly TStorage storage;
Expand All @@ -20,7 +21,7 @@ public abstract class NativeStorable<TStorage> : ILocatableStorable, INestedStor
/// <inheritdoc/>
public virtual string Id { get; }

protected NativeStorable(TStorage storage, string? name = null)
protected NativeStorableLegacy(TStorage storage, string? name = null)
{
this.storage = storage;
Path = storage.FullName;
Expand All @@ -35,7 +36,7 @@ protected NativeStorable(TStorage storage, string? name = null)
if (parent is null)
return Task.FromResult<IFolder?>(null);

return Task.FromResult<IFolder?>(new NativeFolder(parent));
return Task.FromResult<IFolder?>(new NativeFolderLegacy(parent));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IStorageService"/>
public sealed class NativeStorageService : IStorageService
[Obsolete("Use the new WindowsStorable")]
public sealed class NativeStorageLegacyService : IStorageService
{
/// <inheritdoc/>
public Task<IFile> GetFileAsync(string id, CancellationToken cancellationToken = default)
{
if (!File.Exists(id))
throw new FileNotFoundException();

return Task.FromResult<IFile>(new NativeFile(id));
return Task.FromResult<IFile>(new NativeFileLegacy(id));
}

/// <inheritdoc/>
Expand All @@ -29,10 +30,10 @@ public async Task<IFolder> GetFolderAsync(string id, CancellationToken cancellat
if (PathHelpers.IsSpecialFolder(id))
{
var storageFolder = await TryGetStorageFolderAsync(id);
return new NativeFolder(id, storageFolder?.DisplayName);
return new NativeFolderLegacy(id, storageFolder?.DisplayName);
}

return new NativeFolder(id);
return new NativeFolderLegacy(id);

async Task<StorageFolder?> TryGetStorageFolderAsync(string path)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IStorable"/>
public abstract class WindowsStorable<TStorage> : ILocatableStorable, INestedStorable
[Obsolete("Use the new WindowsStorable")]
public abstract class WindowsStorableLegacy<TStorage> : ILocatableStorable, INestedStorable
where TStorage : class, IStorageItem
{
private string? _computedId;
Expand All @@ -22,7 +23,7 @@ public abstract class WindowsStorable<TStorage> : ILocatableStorable, INestedSto
/// <inheritdoc/>
public virtual string Id => _computedId ??= ChecksumHelpers.CalculateChecksumForPath(Path);

protected internal WindowsStorable(TStorage storage)
protected internal WindowsStorableLegacy(TStorage storage)
{
this.storage = storage;
Path = storage.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IFile"/>
public sealed class WindowsStorageFile : WindowsStorable<StorageFile>, ILocatableFile, IModifiableFile, IFileExtended, INestedFile
[Obsolete("Use the new WindowsStorable")]
public sealed class WindowsStorageFileLegacy : WindowsStorableLegacy<StorageFile>, ILocatableFile, IModifiableFile, IFileExtended, INestedFile
{
public WindowsStorageFile(StorageFile storage)
public WindowsStorageFileLegacy(StorageFile storage)
: base(storage)
{
}
Expand Down Expand Up @@ -38,7 +39,7 @@ public async Task<Stream> OpenStreamAsync(FileAccess access, FileShare share = F
var parentFolderTask = storage.GetParentAsync().AsTask(cancellationToken);
var parentFolder = await parentFolderTask;

return new WindowsStorageFolder(parentFolder);
return new WindowsStorageFolderLegacy(parentFolder);
}

private static FileAccessMode GetFileAccessMode(FileAccess access)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
namespace Files.App.Storage.Storables
{
/// <inheritdoc cref="IFolder"/>
public sealed class WindowsStorageFolder : WindowsStorable<StorageFolder>, ILocatableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
[Obsolete("Use the new WindowsStorable")]
public sealed class WindowsStorageFolderLegacy : WindowsStorableLegacy<StorageFolder>, ILocatableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
{
// TODO: Implement IMutableFolder

public WindowsStorageFolder(StorageFolder storage)
public WindowsStorageFolderLegacy(StorageFolder storage)
: base(storage)
{
}
Expand All @@ -20,14 +21,14 @@ public WindowsStorageFolder(StorageFolder storage)
public async Task<INestedFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
{
var file = await storage.GetFileAsync(fileName).AsTask(cancellationToken);
return new WindowsStorageFile(file);
return new WindowsStorageFileLegacy(file);
}

/// <inheritdoc/>
public async Task<INestedFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
{
var folder = await storage.GetFolderAsync(folderName).AsTask(cancellationToken);
return new WindowsStorageFolder(folder);
return new WindowsStorageFolderLegacy(folder);
}

/// <inheritdoc/>
Expand All @@ -40,7 +41,7 @@ public async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind =
var files = await storage.GetFilesAsync().AsTask(cancellationToken);
foreach (var item in files)
{
yield return new WindowsStorageFile(item);
yield return new WindowsStorageFileLegacy(item);
}

break;
Expand All @@ -51,7 +52,7 @@ public async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind =
var folders = await storage.GetFoldersAsync().AsTask(cancellationToken);
foreach (var item in folders)
{
yield return new WindowsStorageFolder(item);
yield return new WindowsStorageFolderLegacy(item);
}

break;
Expand All @@ -63,10 +64,10 @@ public async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind =
foreach (var item in items)
{
if (item is StorageFile storageFile)
yield return new WindowsStorageFile(storageFile);
yield return new WindowsStorageFileLegacy(storageFile);

if (item is StorageFolder storageFolder)
yield return new WindowsStorageFolder(storageFolder);
yield return new WindowsStorageFolderLegacy(storageFolder);
}

break;
Expand All @@ -82,11 +83,11 @@ public Task DeleteAsync(INestedStorable item, bool permanently = default, Cancel
{
return item switch
{
WindowsStorable<StorageFile> storageFile => storageFile.storage
WindowsStorableLegacy<StorageFile> storageFile => storageFile.storage
.DeleteAsync(GetWindowsStorageDeleteOption(permanently))
.AsTask(cancellationToken),

WindowsStorable<StorageFolder> storageFolder => storageFolder.storage
WindowsStorableLegacy<StorageFolder> storageFolder => storageFolder.storage
.DeleteAsync(GetWindowsStorageDeleteOption(permanently))
.AsTask(cancellationToken),

Expand All @@ -97,10 +98,10 @@ public Task DeleteAsync(INestedStorable item, bool permanently = default, Cancel
/// <inheritdoc/>
public async Task<INestedStorable> CreateCopyOfAsync(INestedStorable itemToCopy, bool overwrite = default, CancellationToken cancellationToken = default)
{
if (itemToCopy is WindowsStorable<StorageFile> sourceFile)
if (itemToCopy is WindowsStorableLegacy<StorageFile> sourceFile)
{
var copiedFile = await sourceFile.storage.CopyAsync(storage, itemToCopy.Name, GetWindowsNameCollisionOption(overwrite)).AsTask(cancellationToken);
return new WindowsStorageFile(copiedFile);
return new WindowsStorageFileLegacy(copiedFile);
}

throw new ArgumentException($"Could not copy type {itemToCopy.GetType()}");
Expand All @@ -109,10 +110,10 @@ public async Task<INestedStorable> CreateCopyOfAsync(INestedStorable itemToCopy,
/// <inheritdoc/>
public async Task<INestedStorable> MoveFromAsync(INestedStorable itemToMove, IModifiableFolder source, bool overwrite = default, CancellationToken cancellationToken = default)
{
if (itemToMove is WindowsStorable<StorageFile> sourceFile)
if (itemToMove is WindowsStorableLegacy<StorageFile> sourceFile)
{
await sourceFile.storage.MoveAsync(storage, itemToMove.Name, GetWindowsNameCollisionOption(overwrite)).AsTask(cancellationToken);
return new WindowsStorageFile(sourceFile.storage);
return new WindowsStorageFileLegacy(sourceFile.storage);
}

throw new ArgumentException($"Could not copy type {itemToMove.GetType()}");
Expand All @@ -122,21 +123,21 @@ public async Task<INestedStorable> MoveFromAsync(INestedStorable itemToMove, IMo
public async Task<INestedFile> CreateFileAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
{
var file = await storage.CreateFileAsync(desiredName, GetWindowsCreationCollisionOption(overwrite)).AsTask(cancellationToken);
return new WindowsStorageFile(file);
return new WindowsStorageFileLegacy(file);
}

/// <inheritdoc/>
public async Task<INestedFolder> CreateFolderAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
{
var folder = await storage.CreateFolderAsync(desiredName, GetWindowsCreationCollisionOption(overwrite)).AsTask(cancellationToken);
return new WindowsStorageFolder(folder);
return new WindowsStorageFolderLegacy(folder);
}

/// <inheritdoc/>
public override async Task<IFolder?> GetParentAsync(CancellationToken cancellationToken = default)
{
var parentFolder = await storage.GetParentAsync().AsTask(cancellationToken);
return new WindowsStorageFolder(parentFolder);
return new WindowsStorageFolderLegacy(parentFolder);
}

private static StorageDeleteOption GetWindowsStorageDeleteOption(bool permanently)
Expand Down
Loading

0 comments on commit 3b31e72

Please sign in to comment.