Skip to content

Commit 12391aa

Browse files
authored
Code Quality: Improved app startup routine 2 - Checkpoint 1 (#13854)
1 parent 49deb0c commit 12391aa

File tree

23 files changed

+247
-178
lines changed

23 files changed

+247
-178
lines changed

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

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

4+
using CommunityToolkit.Mvvm.DependencyInjection;
5+
using Files.Core.Storage;
6+
47
namespace Files.App.Actions
58
{
69
internal class PinToStartAction : IAction
710
{
11+
private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();
12+
13+
private IStartMenuService StartMenuService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();
14+
815
public IContentPageContext context;
916

1017
public string Label
@@ -29,11 +36,17 @@ public async Task ExecuteAsync()
2936
if (context.SelectedItems.Count > 0 && context.ShellPage?.SlimContentPage?.SelectedItems is not null)
3037
{
3138
foreach (ListedItem listedItem in context.ShellPage.SlimContentPage.SelectedItems)
32-
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.Name);
39+
{
40+
var folder = await StorageService.GetFolderAsync(listedItem.ItemPath);
41+
await StartMenuService.PinAsync(folder, listedItem.Name);
42+
}
3343
}
3444
else if (context.ShellPage?.FilesystemViewModel?.CurrentFolder is not null)
3545
{
36-
await App.SecondaryTileHelper.TryPinFolderAsync(context.ShellPage.FilesystemViewModel.CurrentFolder.ItemPath, context.ShellPage.FilesystemViewModel.CurrentFolder.Name);
46+
var currentFolder = context.ShellPage.FilesystemViewModel.CurrentFolder;
47+
var folder = await StorageService.GetFolderAsync(currentFolder.ItemPath);
48+
49+
await StartMenuService.PinAsync(folder, currentFolder.Name);
3750
}
3851
}
3952
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
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
{
10+
private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();
11+
12+
private IStartMenuService StartMenuService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();
13+
814
public IContentPageContext context;
915

1016
public string Label
@@ -26,11 +32,17 @@ 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 StartMenuService.UnpinAsync(folder);
38+
}
3039
}
3140
else
3241
{
33-
await App.SecondaryTileHelper.UnpinFromStartAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
42+
var currentFolder = context.ShellPage.FilesystemViewModel.CurrentFolder;
43+
var folder = await StorageService.GetFolderAsync(currentFolder.ItemPath);
44+
45+
await StartMenuService.UnpinAsync(folder);
3446
}
3547
}
3648
}

src/Files.App/App.xaml.cs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@ public partial class App : Application
4545
public static AppModel AppModel { get; private set; }
4646
public static RecentItems RecentItemsManager { get; private set; }
4747
public static QuickAccessManager QuickAccessManager { get; private set; }
48-
public static CloudDrivesManager CloudDrivesManager { get; private set; }
49-
public static WSLDistroManager WSLDistroManager { get; private set; }
5048
public static LibraryManager LibraryManager { get; private set; }
5149
public static FileTagsManager FileTagsManager { get; private set; }
5250

5351
public static ILogger Logger { get; private set; }
54-
public static SecondaryTileHelper SecondaryTileHelper { get; private set; } = new();
5552

5653
/// <summary>
5754
/// Initializes the singleton application object. This is the first line of authored code
@@ -138,6 +135,7 @@ private IHost ConfigureHost()
138135
.AddSingleton<IJumpListService, JumpListService>()
139136
.AddSingleton<IRemovableDrivesService, RemovableDrivesService>()
140137
.AddSingleton<INetworkDrivesService, NetworkDrivesService>()
138+
.AddSingleton<IStartMenuService, StartMenuService>()
141139
.AddSingleton<MainPageViewModel>()
142140
.AddSingleton<PreviewPaneViewModel>()
143141
.AddSingleton<SidebarViewModel>()
@@ -156,31 +154,30 @@ private static async Task InitializeAppComponentsAsync()
156154
var generalSettingsService = userSettingsService.GeneralSettingsService;
157155

158156
// Start off a list of tasks we need to run before we can continue startup
159-
await Task.Run(async () =>
160-
{
161-
await Task.WhenAll(
162-
OptionalTaskAsync(CloudDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowCloudDrivesSection),
163-
LibraryManager.UpdateLibrariesAsync(),
164-
OptionalTaskAsync(WSLDistroManager.UpdateDrivesAsync(), generalSettingsService.ShowWslSection),
165-
OptionalTaskAsync(FileTagsManager.UpdateFileTagsAsync(), generalSettingsService.ShowFileTagsSection),
166-
QuickAccessManager.InitializeAsync()
167-
);
168-
169-
await Task.WhenAll(
170-
JumpListHelper.InitializeUpdatesAsync(),
171-
addItemService.InitializeAsync(),
172-
ContextMenu.WarmUpQueryContextMenuAsync()
173-
);
174-
175-
FileTagsHelper.UpdateTagsDb();
176-
});
157+
await Task.WhenAll(
158+
OptionalTaskAsync(CloudDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowCloudDrivesSection),
159+
LibraryManager.UpdateLibrariesAsync(),
160+
OptionalTaskAsync(WSLDistroManager.UpdateDrivesAsync(), generalSettingsService.ShowWslSection),
161+
OptionalTaskAsync(FileTagsManager.UpdateFileTagsAsync(), generalSettingsService.ShowFileTagsSection),
162+
QuickAccessManager.InitializeAsync()
163+
);
164+
165+
await Task.WhenAll(
166+
JumpListHelper.InitializeUpdatesAsync(),
167+
addItemService.InitializeAsync(),
168+
ContextMenu.WarmUpQueryContextMenuAsync()
169+
);
170+
171+
FileTagsHelper.UpdateTagsDb();
177172

178173
await CheckForRequiredUpdatesAsync();
179174

180-
static async Task OptionalTaskAsync(Task task, bool condition)
175+
static Task OptionalTaskAsync(Task task, bool condition)
181176
{
182177
if (condition)
183-
await task;
178+
return task;
179+
180+
return Task.CompletedTask;
184181
}
185182
}
186183

@@ -238,8 +235,7 @@ async Task ActivateAsync()
238235
await SplashScreenLoadingTCS!.Task.WithTimeoutAsync(TimeSpan.FromMilliseconds(500));
239236
SplashScreenLoadingTCS = null;
240237

241-
_ = InitializeAppComponentsAsync().ContinueWith(t => Logger.LogWarning(t.Exception, "Error during InitializeAppComponentsAsync()"), TaskContinuationOptions.OnlyOnFaulted);
242-
238+
_ = InitializeAppComponentsAsync();
243239
_ = MainWindow.Instance.InitializeApplicationAsync(appActivationArguments.Data);
244240
}
245241
}
@@ -251,8 +247,6 @@ private static void EnsureSettingsAndConfigurationAreBootstrapped()
251247
RecentItemsManager ??= new RecentItems();
252248
AppModel ??= new AppModel();
253249
LibraryManager ??= new LibraryManager();
254-
CloudDrivesManager ??= new CloudDrivesManager();
255-
WSLDistroManager ??= new WSLDistroManager();
256250
FileTagsManager ??= new FileTagsManager();
257251
QuickAccessManager ??= new QuickAccessManager();
258252
}
@@ -388,8 +382,7 @@ await SafetyExtensions.IgnoreExceptions(async () =>
388382
/// </summary>
389383
public static void SaveSessionTabs()
390384
{
391-
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
392-
385+
var userSettingsService = Ioc.Default.GetService<IUserSettingsService>() ?? new UserSettingsService();
393386
userSettingsService.GeneralSettingsService.LastSessionTabList = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
394387
{
395388
if (tab is not null && tab.NavigationParameter is not null)

src/Files.App/Data/Items/ListedItem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ListedItem : ObservableObject, IGroupableItem
1818
{
1919
protected static IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
2020

21+
protected static IStartMenuService StartMenuService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();
22+
2123
protected static readonly IFileTagsSettingsService fileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
2224

2325
protected static readonly IDateTimeFormatter dateTimeFormatter = Ioc.Default.GetRequiredService<IDateTimeFormatter>();
@@ -180,7 +182,7 @@ public BitmapImage FileImage
180182
}
181183
}
182184

183-
public bool IsItemPinnedToStart => App.SecondaryTileHelper.CheckFolderPinned(ItemPath);
185+
public bool IsItemPinnedToStart => StartMenuService.IsPinned(ItemPath);
184186

185187
private BitmapImage iconOverlay;
186188
public BitmapImage IconOverlay

src/Files.App/Data/Models/BitmapImageModel.cs

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

4+
using Files.Shared.Utils;
45
using Microsoft.UI.Xaml.Media.Imaging;
56

67
namespace Files.App.Data.Models
78
{
8-
/// <inheritdoc cref="IImageModel"/>
9-
internal sealed class BitmapImageModel : IImageModel
9+
/// <inheritdoc cref="IImage"/>
10+
internal sealed class BitmapImageModel : IImage
1011
{
1112
public BitmapImage Image { get; }
1213

src/Files.App/Data/Models/ItemViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ public void CloseWatcher()
14941494
private async Task<int> EnumerateItemsFromStandardFolderAsync(string path, CancellationToken cancellationToken, LibraryItem? library = null)
14951495
{
14961496
// Flag to use FindFirstFileExFromApp or StorageFolder enumeration - Use storage folder for Box Drive (#4629)
1497-
var isBoxFolder = App.CloudDrivesManager.Drives.FirstOrDefault(x => x.Text == "Box")?.Path?.TrimEnd('\\') is string boxFolder && path.StartsWith(boxFolder);
1497+
var isBoxFolder = CloudDrivesManager.Drives.FirstOrDefault(x => x.Text == "Box")?.Path?.TrimEnd('\\') is string boxFolder && path.StartsWith(boxFolder);
14981498
bool isWslDistro = path.StartsWith(@"\\wsl$\", StringComparison.OrdinalIgnoreCase) || path.StartsWith(@"\\wsl.localhost\", StringComparison.OrdinalIgnoreCase)
14991499
|| path.Equals(@"\\wsl$", StringComparison.OrdinalIgnoreCase) || path.Equals(@"\\wsl.localhost", StringComparison.OrdinalIgnoreCase);
15001500
bool isMtp = path.StartsWith(@"\\?\", StringComparison.Ordinal);

src/Files.App/Helpers/UI/SecondaryTileHelper.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Files.App/Services/ImagingService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
using Files.Core.Storage;
55
using Files.Core.Storage.LocatableStorage;
6+
using Files.Shared.Utils;
67
using Windows.Storage.FileProperties;
78

89
namespace Files.App.Services
910
{
1011
internal sealed class ImagingService : IImageService
1112
{
1213
/// <inheritdoc/>
13-
public async Task<IImageModel?> GetIconAsync(IStorable storable, CancellationToken cancellationToken)
14+
public async Task<IImage?> GetIconAsync(IStorable storable, CancellationToken cancellationToken)
1415
{
1516
if (storable is not ILocatableStorable locatableStorable)
1617
return null;
@@ -23,12 +24,12 @@ internal sealed class ImagingService : IImageService
2324
return new BitmapImageModel(bitmapImage);
2425
}
2526

26-
public async Task<IImageModel?> GetImageModelFromDataAsync(byte[] rawData)
27+
public async Task<IImage?> GetImageModelFromDataAsync(byte[] rawData)
2728
{
2829
return new BitmapImageModel(await BitmapHelper.ToBitmapAsync(rawData));
2930
}
3031

31-
public async Task<IImageModel?> GetImageModelFromPathAsync(string filePath, uint thumbnailSize = 64)
32+
public async Task<IImage?> GetImageModelFromPathAsync(string filePath, uint thumbnailSize = 64)
3233
{
3334
if (await FileThumbnailHelper.LoadIconFromPathAsync(filePath, thumbnailSize, ThumbnailMode.ListView, ThumbnailOptions.ResizeThumbnail) is byte[] imageBuffer)
3435
return await GetImageModelFromDataAsync(imageBuffer);

0 commit comments

Comments
 (0)