Skip to content

Code Quality: Improved app startup routine 2 - Checkpoint 1 #13854

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

Merged
merged 4 commits into from
Nov 14, 2023
Merged
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
17 changes: 15 additions & 2 deletions src/Files.App/Actions/Start/PinToStartAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.DependencyInjection;
using Files.Core.Storage;

namespace Files.App.Actions
{
internal class PinToStartAction : IAction
{
private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();

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

public IContentPageContext context;

public string Label
Expand All @@ -29,11 +36,17 @@ public async Task ExecuteAsync()
if (context.SelectedItems.Count > 0 && context.ShellPage?.SlimContentPage?.SelectedItems is not null)
{
foreach (ListedItem listedItem in context.ShellPage.SlimContentPage.SelectedItems)
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.Name);
{
var folder = await StorageService.GetFolderAsync(listedItem.ItemPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be having some issues #13947

await StartMenuService.PinAsync(folder, listedItem.Name);
}
}
else if (context.ShellPage?.FilesystemViewModel?.CurrentFolder is not null)
{
await App.SecondaryTileHelper.TryPinFolderAsync(context.ShellPage.FilesystemViewModel.CurrentFolder.ItemPath, context.ShellPage.FilesystemViewModel.CurrentFolder.Name);
var currentFolder = context.ShellPage.FilesystemViewModel.CurrentFolder;
var folder = await StorageService.GetFolderAsync(currentFolder.ItemPath);

await StartMenuService.PinAsync(folder, currentFolder.Name);
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/Files.App/Actions/Start/UnpinFromStartAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// 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
{
private IStorageService StorageService { get; } = Ioc.Default.GetRequiredService<IStorageService>();

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

public IContentPageContext context;

public string Label
Expand All @@ -26,11 +32,17 @@ 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 StartMenuService.UnpinAsync(folder);
}
}
else
{
await App.SecondaryTileHelper.UnpinFromStartAsync(context.ShellPage?.FilesystemViewModel.CurrentFolder.ItemPath);
var currentFolder = context.ShellPage.FilesystemViewModel.CurrentFolder;
var folder = await StorageService.GetFolderAsync(currentFolder.ItemPath);

await StartMenuService.UnpinAsync(folder);
}
}
}
Expand Down
51 changes: 22 additions & 29 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ public partial class App : Application
public static AppModel AppModel { get; private set; }
public static RecentItems RecentItemsManager { get; private set; }
public static QuickAccessManager QuickAccessManager { get; private set; }
public static CloudDrivesManager CloudDrivesManager { get; private set; }
public static WSLDistroManager WSLDistroManager { get; private set; }
public static LibraryManager LibraryManager { get; private set; }
public static FileTagsManager FileTagsManager { get; private set; }

public static ILogger Logger { get; private set; }
public static SecondaryTileHelper SecondaryTileHelper { get; private set; } = new();

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

// Start off a list of tasks we need to run before we can continue startup
await Task.Run(async () =>
{
await Task.WhenAll(
OptionalTaskAsync(CloudDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowCloudDrivesSection),
LibraryManager.UpdateLibrariesAsync(),
OptionalTaskAsync(WSLDistroManager.UpdateDrivesAsync(), generalSettingsService.ShowWslSection),
OptionalTaskAsync(FileTagsManager.UpdateFileTagsAsync(), generalSettingsService.ShowFileTagsSection),
QuickAccessManager.InitializeAsync()
);

await Task.WhenAll(
JumpListHelper.InitializeUpdatesAsync(),
addItemService.InitializeAsync(),
ContextMenu.WarmUpQueryContextMenuAsync()
);

FileTagsHelper.UpdateTagsDb();
});
await Task.WhenAll(
OptionalTaskAsync(CloudDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowCloudDrivesSection),
LibraryManager.UpdateLibrariesAsync(),
OptionalTaskAsync(WSLDistroManager.UpdateDrivesAsync(), generalSettingsService.ShowWslSection),
OptionalTaskAsync(FileTagsManager.UpdateFileTagsAsync(), generalSettingsService.ShowFileTagsSection),
QuickAccessManager.InitializeAsync()
);

await Task.WhenAll(
JumpListHelper.InitializeUpdatesAsync(),
addItemService.InitializeAsync(),
ContextMenu.WarmUpQueryContextMenuAsync()
);

FileTagsHelper.UpdateTagsDb();

await CheckForRequiredUpdatesAsync();

static async Task OptionalTaskAsync(Task task, bool condition)
static Task OptionalTaskAsync(Task task, bool condition)
{
if (condition)
await task;
return task;

return Task.CompletedTask;
}
}

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

_ = InitializeAppComponentsAsync().ContinueWith(t => Logger.LogWarning(t.Exception, "Error during InitializeAppComponentsAsync()"), TaskContinuationOptions.OnlyOnFaulted);

_ = InitializeAppComponentsAsync();
_ = MainWindow.Instance.InitializeApplicationAsync(appActivationArguments.Data);
}
}
Expand All @@ -251,8 +247,6 @@ private static void EnsureSettingsAndConfigurationAreBootstrapped()
RecentItemsManager ??= new RecentItems();
AppModel ??= new AppModel();
LibraryManager ??= new LibraryManager();
CloudDrivesManager ??= new CloudDrivesManager();
WSLDistroManager ??= new WSLDistroManager();
FileTagsManager ??= new FileTagsManager();
QuickAccessManager ??= new QuickAccessManager();
}
Expand Down Expand Up @@ -388,8 +382,7 @@ await SafetyExtensions.IgnoreExceptions(async () =>
/// </summary>
public static void SaveSessionTabs()
{
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();

var userSettingsService = Ioc.Default.GetService<IUserSettingsService>() ?? new UserSettingsService();
userSettingsService.GeneralSettingsService.LastSessionTabList = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
{
if (tab is not null && tab.NavigationParameter is not null)
Expand Down
4 changes: 3 additions & 1 deletion src/Files.App/Data/Items/ListedItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ListedItem : ObservableObject, IGroupableItem
{
protected static IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();

protected static IStartMenuService StartMenuService { get; } = Ioc.Default.GetRequiredService<IStartMenuService>();

protected static readonly IFileTagsSettingsService fileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();

protected static readonly IDateTimeFormatter dateTimeFormatter = Ioc.Default.GetRequiredService<IDateTimeFormatter>();
Expand Down Expand Up @@ -180,7 +182,7 @@ public BitmapImage FileImage
}
}

public bool IsItemPinnedToStart => App.SecondaryTileHelper.CheckFolderPinned(ItemPath);
public bool IsItemPinnedToStart => StartMenuService.IsPinned(ItemPath);

private BitmapImage iconOverlay;
public BitmapImage IconOverlay
Expand Down
5 changes: 3 additions & 2 deletions src/Files.App/Data/Models/BitmapImageModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Shared.Utils;
using Microsoft.UI.Xaml.Media.Imaging;

namespace Files.App.Data.Models
{
/// <inheritdoc cref="IImageModel"/>
internal sealed class BitmapImageModel : IImageModel
/// <inheritdoc cref="IImage"/>
internal sealed class BitmapImageModel : IImage
{
public BitmapImage Image { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ public void CloseWatcher()
private async Task<int> EnumerateItemsFromStandardFolderAsync(string path, CancellationToken cancellationToken, LibraryItem? library = null)
{
// Flag to use FindFirstFileExFromApp or StorageFolder enumeration - Use storage folder for Box Drive (#4629)
var isBoxFolder = App.CloudDrivesManager.Drives.FirstOrDefault(x => x.Text == "Box")?.Path?.TrimEnd('\\') is string boxFolder && path.StartsWith(boxFolder);
var isBoxFolder = CloudDrivesManager.Drives.FirstOrDefault(x => x.Text == "Box")?.Path?.TrimEnd('\\') is string boxFolder && path.StartsWith(boxFolder);
bool isWslDistro = path.StartsWith(@"\\wsl$\", StringComparison.OrdinalIgnoreCase) || path.StartsWith(@"\\wsl.localhost\", StringComparison.OrdinalIgnoreCase)
|| path.Equals(@"\\wsl$", StringComparison.OrdinalIgnoreCase) || path.Equals(@"\\wsl.localhost", StringComparison.OrdinalIgnoreCase);
bool isMtp = path.StartsWith(@"\\?\", StringComparison.Ordinal);
Expand Down
75 changes: 0 additions & 75 deletions src/Files.App/Helpers/UI/SecondaryTileHelper.cs

This file was deleted.

7 changes: 4 additions & 3 deletions src/Files.App/Services/ImagingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

using Files.Core.Storage;
using Files.Core.Storage.LocatableStorage;
using Files.Shared.Utils;
using Windows.Storage.FileProperties;

namespace Files.App.Services
{
internal sealed class ImagingService : IImageService
{
/// <inheritdoc/>
public async Task<IImageModel?> GetIconAsync(IStorable storable, CancellationToken cancellationToken)
public async Task<IImage?> GetIconAsync(IStorable storable, CancellationToken cancellationToken)
{
if (storable is not ILocatableStorable locatableStorable)
return null;
Expand All @@ -23,12 +24,12 @@ internal sealed class ImagingService : IImageService
return new BitmapImageModel(bitmapImage);
}

public async Task<IImageModel?> GetImageModelFromDataAsync(byte[] rawData)
public async Task<IImage?> GetImageModelFromDataAsync(byte[] rawData)
{
return new BitmapImageModel(await BitmapHelper.ToBitmapAsync(rawData));
}

public async Task<IImageModel?> GetImageModelFromPathAsync(string filePath, uint thumbnailSize = 64)
public async Task<IImage?> GetImageModelFromPathAsync(string filePath, uint thumbnailSize = 64)
{
if (await FileThumbnailHelper.LoadIconFromPathAsync(filePath, thumbnailSize, ThumbnailMode.ListView, ThumbnailOptions.ResizeThumbnail) is byte[] imageBuffer)
return await GetImageModelFromDataAsync(imageBuffer);
Expand Down
Loading