Skip to content

Commit 5fc5d32

Browse files
authored
Code Quality: Improved app startup routine 2 (files-community#12951)
1 parent 15a2fea commit 5fc5d32

File tree

73 files changed

+875
-805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+875
-805
lines changed

src/Files.App.Storage/Files.App.Storage.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Configurations>Debug;Release;Stable;Preview;Store</Configurations>
1010
<Platforms>x86;x64;arm64</Platforms>
1111
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
12+
<ImplicitUsings>enable</ImplicitUsings>
1213
</PropertyGroup>
1314

1415
<ItemGroup>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// System
2+
global using global::System;
3+
global using global::System.Collections;
4+
global using global::System.Collections.Generic;
5+
global using global::System.Collections.ObjectModel;
6+
global using global::System.Linq;
7+
global using global::System.Threading;
8+
global using global::System.Threading.Tasks;
9+
global using global::System.ComponentModel;
10+
global using global::System.Diagnostics;
11+
global using SystemIO = global::System.IO;

src/Files.App.Storage/NativeStorage/NativeFolder.cs

Lines changed: 81 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,94 +10,89 @@
1010
using Files.Core.Storage.ModifiableStorage;
1111
using Files.Core.Storage.MutableStorage;
1212
using Files.Core.Storage.NestedStorage;
13-
using System;
14-
using System.Collections.Generic;
15-
using System.IO;
1613
using System.Runtime.CompilerServices;
17-
using System.Threading;
18-
using System.Threading.Tasks;
1914

2015
namespace Files.App.Storage.NativeStorage
2116
{
2217
/// <inheritdoc cref="IFolder"/>
2318
public class NativeFolder : NativeStorable<DirectoryInfo>, ILocatableFolder, IModifiableFolder, IMutableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
24-
{
19+
{
2520
public NativeFolder(DirectoryInfo directoryInfo, string? name = null)
26-
: base(directoryInfo, name)
27-
{
28-
}
29-
30-
public NativeFolder(string path, string? name = null)
31-
: this(new DirectoryInfo(path), name)
32-
{
33-
}
34-
35-
/// <inheritdoc/>
36-
public virtual Task<INestedFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
37-
{
38-
var path = System.IO.Path.Combine(Path, fileName);
39-
40-
if (!File.Exists(path))
41-
throw new FileNotFoundException();
42-
43-
return Task.FromResult<INestedFile>(new NativeFile(path));
44-
}
45-
46-
/// <inheritdoc/>
47-
public virtual Task<INestedFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
48-
{
49-
var path = System.IO.Path.Combine(Path, folderName);
50-
if (!Directory.Exists(path))
51-
throw new FileNotFoundException();
52-
53-
return Task.FromResult<INestedFolder>(new NativeFolder(path));
54-
}
55-
56-
/// <inheritdoc/>
57-
public virtual async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind = StorableKind.All, [EnumeratorCancellation] CancellationToken cancellationToken = default)
58-
{
59-
if (kind == StorableKind.Files)
60-
{
61-
foreach (var item in Directory.EnumerateFiles(Path))
62-
yield return new NativeFile(item);
63-
}
64-
else if (kind == StorableKind.Folders)
65-
{
66-
foreach (var item in Directory.EnumerateDirectories(Path))
67-
yield return new NativeFolder(item);
68-
}
69-
else
70-
{
71-
foreach (var item in Directory.EnumerateFileSystemEntries(Path))
72-
{
73-
if (File.Exists(item))
74-
yield return new NativeFile(item);
75-
else
76-
yield return new NativeFolder(item);
77-
}
78-
}
79-
80-
await Task.CompletedTask;
81-
}
21+
: base(directoryInfo, name)
22+
{
23+
}
24+
25+
public NativeFolder(string path, string? name = null)
26+
: this(new DirectoryInfo(path), name)
27+
{
28+
}
29+
30+
/// <inheritdoc/>
31+
public virtual Task<INestedFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
32+
{
33+
var path = SystemIO.Path.Combine(Path, fileName);
34+
35+
if (!File.Exists(path))
36+
throw new FileNotFoundException();
37+
38+
return Task.FromResult<INestedFile>(new NativeFile(path));
39+
}
40+
41+
/// <inheritdoc/>
42+
public virtual Task<INestedFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
43+
{
44+
var path = SystemIO.Path.Combine(Path, folderName);
45+
if (!Directory.Exists(path))
46+
throw new FileNotFoundException();
47+
48+
return Task.FromResult<INestedFolder>(new NativeFolder(path));
49+
}
50+
51+
/// <inheritdoc/>
52+
public virtual async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind = StorableKind.All, [EnumeratorCancellation] CancellationToken cancellationToken = default)
53+
{
54+
if (kind == StorableKind.Files)
55+
{
56+
foreach (var item in Directory.EnumerateFiles(Path))
57+
yield return new NativeFile(item);
58+
}
59+
else if (kind == StorableKind.Folders)
60+
{
61+
foreach (var item in Directory.EnumerateDirectories(Path))
62+
yield return new NativeFolder(item);
63+
}
64+
else
65+
{
66+
foreach (var item in Directory.EnumerateFileSystemEntries(Path))
67+
{
68+
if (File.Exists(item))
69+
yield return new NativeFile(item);
70+
else
71+
yield return new NativeFolder(item);
72+
}
73+
}
74+
75+
await Task.CompletedTask;
76+
}
8277

8378
/// <inheritdoc/>
8479
public virtual Task DeleteAsync(INestedStorable item, bool permanently = false, CancellationToken cancellationToken = default)
85-
{
86-
_ = permanently;
87-
88-
if (item is ILocatableFile locatableFile)
89-
{
90-
File.Delete(locatableFile.Path);
91-
}
92-
else if (item is ILocatableFolder locatableFolder)
93-
{
94-
Directory.Delete(locatableFolder.Path, true);
95-
}
96-
else
97-
throw new ArgumentException($"Could not delete {item}.");
98-
99-
return Task.CompletedTask;
100-
}
80+
{
81+
_ = permanently;
82+
83+
if (item is ILocatableFile locatableFile)
84+
{
85+
File.Delete(locatableFile.Path);
86+
}
87+
else if (item is ILocatableFolder locatableFolder)
88+
{
89+
Directory.Delete(locatableFolder.Path, true);
90+
}
91+
else
92+
throw new ArgumentException($"Could not delete {item}.");
93+
94+
return Task.CompletedTask;
95+
}
10196

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

112107
return new NativeFile(newPath);
@@ -134,7 +129,7 @@ public virtual async Task<INestedStorable> MoveFromAsync(INestedStorable itemToM
134129
{
135130
if (itemToMove is ILocatableFile sourceLocatableFile)
136131
{
137-
var newPath = System.IO.Path.Combine(Path, itemToMove.Name);
132+
var newPath = SystemIO.Path.Combine(Path, itemToMove.Name);
138133
File.Move(sourceLocatableFile.Path, newPath, overwrite);
139134

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

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

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

180175
/// <inheritdoc/>
181176
public Task<IFolderWatcher> GetFolderWatcherAsync(CancellationToken cancellationToken = default)
182-
{
183-
return Task.FromResult<IFolderWatcher>(new NativeFolderWatcher(this));
184-
}
185-
}
177+
{
178+
return Task.FromResult<IFolderWatcher>(new NativeFolderWatcher(this));
179+
}
180+
}
186181
}

src/Files.App.Storage/NativeStorage/NativeStorable.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ protected static string FormatPath(string path)
5252
{
5353
path = path.Replace("file:///", string.Empty);
5454

55-
if ('/' != System.IO.Path.DirectorySeparatorChar)
56-
return path.Replace('/', System.IO.Path.DirectorySeparatorChar);
55+
if ('/' != SystemIO.Path.DirectorySeparatorChar)
56+
return path.Replace('/', SystemIO.Path.DirectorySeparatorChar);
5757

58-
if ('\\' != System.IO.Path.DirectorySeparatorChar)
59-
return path.Replace('\\', System.IO.Path.DirectorySeparatorChar);
58+
if ('\\' != SystemIO.Path.DirectorySeparatorChar)
59+
return path.Replace('\\', SystemIO.Path.DirectorySeparatorChar);
6060

6161
return path;
6262
}

src/Files.App/Actions/Start/PinToStartAction.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Files.Core.Storage;
5+
46
namespace Files.App.Actions
57
{
68
internal class PinToStartAction : IAction
79
{
810
public IContentPageContext context;
911

12+
private IStartMenuService SystemPinService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();
13+
14+
private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();
15+
16+
1017
public string Label
1118
=> "PinItemToStart/Text".GetLocalizedResource();
1219

@@ -26,11 +33,15 @@ public async Task ExecuteAsync()
2633
if (context.SelectedItems.Count > 0)
2734
{
2835
foreach (ListedItem listedItem in context.ShellPage?.SlimContentPage.SelectedItems)
29-
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.Name);
36+
{
37+
var folder = await StorageService.GetFolderAsync(listedItem.ItemPath);
38+
await SystemPinService.PinAsync(folder);
39+
}
3040
}
3141
else
3242
{
33-
await App.SecondaryTileHelper.TryPinFolderAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath, context.ShellPage?.FilesystemViewModel.CurrentFolder.Name);
43+
var folder = await StorageService.GetFolderAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
44+
await SystemPinService.PinAsync(folder);
3445
}
3546
}
3647
}

src/Files.App/Actions/Start/UnpinFromStartAction.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Files.Core.Storage;
5+
46
namespace Files.App.Actions
57
{
68
internal class UnpinFromStartAction : IAction
79
{
810
public IContentPageContext context;
911

12+
private IStartMenuService SystemPinService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();
13+
14+
private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();
15+
1016
public string Label
1117
=> "UnpinItemFromStart/Text".GetLocalizedResource();
1218

@@ -26,11 +32,15 @@ public async Task ExecuteAsync()
2632
if (context.SelectedItems.Count > 0)
2733
{
2834
foreach (ListedItem listedItem in context.ShellPage?.SlimContentPage.SelectedItems)
29-
await App.SecondaryTileHelper.UnpinFromStartAsync(listedItem.ItemPath);
35+
{
36+
var folder = await StorageService.GetFolderAsync(listedItem.ItemPath);
37+
await SystemPinService.UnpinAsync(folder);
38+
}
3039
}
3140
else
3241
{
33-
await App.SecondaryTileHelper.UnpinFromStartAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
42+
var folder = await StorageService.GetFolderAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
43+
await SystemPinService.UnpinAsync(folder);
3444
}
3545
}
3646
}

0 commit comments

Comments
 (0)