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

Redo: Improved app startup routine 2 #13532

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/Files.App.Storage/Files.App.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Configurations>Debug;Release;Stable;Preview;Store</Configurations>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions src/Files.App.Storage/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// System
global using global::System;
global using global::System.Collections;
global using global::System.Collections.Generic;
global using global::System.Collections.ObjectModel;
global using global::System.Linq;
global using global::System.Threading;
global using global::System.Threading.Tasks;
global using global::System.ComponentModel;
global using global::System.Diagnostics;
global using SystemIO = global::System.IO;
167 changes: 81 additions & 86 deletions src/Files.App.Storage/NativeStorage/NativeFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,89 @@
using Files.Core.Storage.ModifiableStorage;
using Files.Core.Storage.MutableStorage;
using Files.Core.Storage.NestedStorage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Files.App.Storage.NativeStorage
{
/// <inheritdoc cref="IFolder"/>
public class NativeFolder : NativeStorable<DirectoryInfo>, ILocatableFolder, IModifiableFolder, IMutableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
{
{
public NativeFolder(DirectoryInfo directoryInfo, string? name = null)
: base(directoryInfo, name)
{
}

public NativeFolder(string path, string? name = null)
: this(new DirectoryInfo(path), name)
{
}

/// <inheritdoc/>
public virtual Task<INestedFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
{
var path = System.IO.Path.Combine(Path, fileName);

if (!File.Exists(path))
throw new FileNotFoundException();

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

/// <inheritdoc/>
public virtual Task<INestedFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
{
var path = System.IO.Path.Combine(Path, folderName);
if (!Directory.Exists(path))
throw new FileNotFoundException();

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

/// <inheritdoc/>
public virtual async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind = StorableKind.All, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (kind == StorableKind.Files)
{
foreach (var item in Directory.EnumerateFiles(Path))
yield return new NativeFile(item);
}
else if (kind == StorableKind.Folders)
{
foreach (var item in Directory.EnumerateDirectories(Path))
yield return new NativeFolder(item);
}
else
{
foreach (var item in Directory.EnumerateFileSystemEntries(Path))
{
if (File.Exists(item))
yield return new NativeFile(item);
else
yield return new NativeFolder(item);
}
}

await Task.CompletedTask;
}
: base(directoryInfo, name)
{
}

public NativeFolder(string path, string? name = null)
: this(new DirectoryInfo(path), name)
{
}

/// <inheritdoc/>
public virtual Task<INestedFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
{
var path = SystemIO.Path.Combine(Path, fileName);

if (!File.Exists(path))
throw new FileNotFoundException();

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

/// <inheritdoc/>
public virtual Task<INestedFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
{
var path = SystemIO.Path.Combine(Path, folderName);
if (!Directory.Exists(path))
throw new FileNotFoundException();

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

/// <inheritdoc/>
public virtual async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind = StorableKind.All, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (kind == StorableKind.Files)
{
foreach (var item in Directory.EnumerateFiles(Path))
yield return new NativeFile(item);
}
else if (kind == StorableKind.Folders)
{
foreach (var item in Directory.EnumerateDirectories(Path))
yield return new NativeFolder(item);
}
else
{
foreach (var item in Directory.EnumerateFileSystemEntries(Path))
{
if (File.Exists(item))
yield return new NativeFile(item);
else
yield return new NativeFolder(item);
}
}

await Task.CompletedTask;
}

/// <inheritdoc/>
public virtual Task DeleteAsync(INestedStorable item, bool permanently = false, CancellationToken cancellationToken = default)
{
_ = permanently;

if (item is ILocatableFile locatableFile)
{
File.Delete(locatableFile.Path);
}
else if (item is ILocatableFolder locatableFolder)
{
Directory.Delete(locatableFolder.Path, true);
}
else
throw new ArgumentException($"Could not delete {item}.");

return Task.CompletedTask;
}
{
_ = permanently;

if (item is ILocatableFile locatableFile)
{
File.Delete(locatableFile.Path);
}
else if (item is ILocatableFolder locatableFolder)
{
Directory.Delete(locatableFolder.Path, true);
}
else
throw new ArgumentException($"Could not delete {item}.");

return Task.CompletedTask;
}

/// <inheritdoc/>
public virtual async Task<INestedStorable> CreateCopyOfAsync(INestedStorable itemToCopy, bool overwrite = default, CancellationToken cancellationToken = default)
Expand All @@ -106,7 +101,7 @@ public virtual async Task<INestedStorable> CreateCopyOfAsync(INestedStorable ite
{
if (itemToCopy is ILocatableFile sourceLocatableFile)
{
var newPath = System.IO.Path.Combine(Path, itemToCopy.Name);
var newPath = SystemIO.Path.Combine(Path, itemToCopy.Name);
File.Copy(sourceLocatableFile.Path, newPath, overwrite);

return new NativeFile(newPath);
Expand Down Expand Up @@ -134,7 +129,7 @@ public virtual async Task<INestedStorable> MoveFromAsync(INestedStorable itemToM
{
if (itemToMove is ILocatableFile sourceLocatableFile)
{
var newPath = System.IO.Path.Combine(Path, itemToMove.Name);
var newPath = SystemIO.Path.Combine(Path, itemToMove.Name);
File.Move(sourceLocatableFile.Path, newPath, overwrite);

return new NativeFile(newPath);
Expand All @@ -159,7 +154,7 @@ public virtual async Task<INestedStorable> MoveFromAsync(INestedStorable itemToM
/// <inheritdoc/>
public virtual async Task<INestedFile> CreateFileAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
{
var path = System.IO.Path.Combine(Path, desiredName);
var path = SystemIO.Path.Combine(Path, desiredName);
if (overwrite || !File.Exists(path))
await File.Create(path).DisposeAsync();

Expand All @@ -169,7 +164,7 @@ public virtual async Task<INestedFile> CreateFileAsync(string desiredName, bool
/// <inheritdoc/>
public virtual Task<INestedFolder> CreateFolderAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
{
var path = System.IO.Path.Combine(Path, desiredName);
var path = SystemIO.Path.Combine(Path, desiredName);
if (overwrite)
Directory.Delete(path, true);

Expand All @@ -179,8 +174,8 @@ public virtual Task<INestedFolder> CreateFolderAsync(string desiredName, bool ov

/// <inheritdoc/>
public Task<IFolderWatcher> GetFolderWatcherAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult<IFolderWatcher>(new NativeFolderWatcher(this));
}
}
{
return Task.FromResult<IFolderWatcher>(new NativeFolderWatcher(this));
}
}
}
8 changes: 4 additions & 4 deletions src/Files.App.Storage/NativeStorage/NativeStorable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected static string FormatPath(string path)
{
path = path.Replace("file:///", string.Empty);

if ('/' != System.IO.Path.DirectorySeparatorChar)
return path.Replace('/', System.IO.Path.DirectorySeparatorChar);
if ('/' != SystemIO.Path.DirectorySeparatorChar)
return path.Replace('/', SystemIO.Path.DirectorySeparatorChar);

if ('\\' != System.IO.Path.DirectorySeparatorChar)
return path.Replace('\\', System.IO.Path.DirectorySeparatorChar);
if ('\\' != SystemIO.Path.DirectorySeparatorChar)
return path.Replace('\\', SystemIO.Path.DirectorySeparatorChar);

return path;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Files.App/Actions/Start/PinToStartAction.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Core.Storage;

namespace Files.App.Actions
{
internal class PinToStartAction : IAction
{
public IContentPageContext context;

private IStartMenuService SystemPinService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();

private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();


public string Label
=> "PinItemToStart/Text".GetLocalizedResource();

Expand Down
14 changes: 12 additions & 2 deletions src/Files.App/Actions/Start/UnpinFromStartAction.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Core.Storage;

namespace Files.App.Actions
{
internal class UnpinFromStartAction : IAction
{
public IContentPageContext context;

private IStartMenuService SystemPinService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();

private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();

public string Label
=> "UnpinItemFromStart/Text".GetLocalizedResource();

Expand All @@ -26,11 +32,15 @@ public async Task ExecuteAsync()
if (context.SelectedItems.Count > 0)
{
foreach (ListedItem listedItem in context.ShellPage?.SlimContentPage.SelectedItems)
await App.SecondaryTileHelper.UnpinFromStartAsync(listedItem.ItemPath);
{
var folder = await StorageService.GetFolderAsync(listedItem.ItemPath);
await SystemPinService.UnpinAsync(folder);
}
}
else
{
await App.SecondaryTileHelper.UnpinFromStartAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
var folder = await StorageService.GetFolderAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
await SystemPinService.UnpinAsync(folder);
}
}
}
Expand Down
Loading
Loading