Skip to content

Code Quality: Replpace LibraryManager with IWindowsLibraryService #15219

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

Closed
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
4 changes: 2 additions & 2 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static CommandBarFlyout? LastOpenedFlyout
public static StorageHistoryWrapper HistoryWrapper { get; private set; } = null!;
public static FileTagsManager FileTagsManager { get; private set; } = null!;
public static RecentItems RecentItemsManager { get; private set; } = null!;
public static LibraryManager LibraryManager { get; private set; } = null!;
public static IWindowsLibraryService WindowsLibraryService { get; private set; } = null!;
public static AppModel AppModel { get; private set; } = null!;
public static ILogger Logger { get; private set; } = null!;

Expand Down Expand Up @@ -116,7 +116,7 @@ async Task ActivateAsync()
HistoryWrapper = Ioc.Default.GetRequiredService<StorageHistoryWrapper>();
FileTagsManager = Ioc.Default.GetRequiredService<FileTagsManager>();
RecentItemsManager = Ioc.Default.GetRequiredService<RecentItems>();
LibraryManager = Ioc.Default.GetRequiredService<LibraryManager>();
WindowsLibraryService = Ioc.Default.GetRequiredService<IWindowsLibraryService>();
Logger = Ioc.Default.GetRequiredService<ILogger<App>>();
AppModel = Ioc.Default.GetRequiredService<AppModel>();

Expand Down
45 changes: 45 additions & 0 deletions src/Files.App/Data/Contracts/IWindowsLibraryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using System.Collections.Specialized;

namespace Files.App.Data.Contracts
{
public interface IWindowsLibraryService
{
event EventHandler<NotifyCollectionChangedEventArgs>? DataChanged;

IReadOnlyList<LibraryLocationItem> Libraries { get; }

/// <summary>
/// Get libraries of the current user with the help of the FullTrust process.
/// </summary>
/// <returns>List of library items</returns>
Task<List<LibraryLocationItem>> GetLibrariesAsync();

Task UpdateLibrariesAsync();

/// <summary>
/// Update library details.
/// </summary>
/// <param name="libraryFilePath">Library file path</param>
/// <param name="defaultSaveFolder">Update the default save folder or null to keep current</param>
/// <param name="folders">Update the library folders or null to keep current</param>
/// <param name="isPinned">Update the library pinned status or null to keep current</param>
/// <returns>The new library if successfully updated</returns>
Task<LibraryLocationItem> UpdateLibraryAsync(string libraryPath, string? defaultSaveFolder = null, string[]? folders = null, bool? isPinned = null);

bool TryGetLibrary(string path, out LibraryLocationItem library);

/// <summary>
/// Create a new library with the specified name.
/// </summary>
/// <param name="name">The name of the new library (must be unique)</param>
/// <returns>The new library if successfully created</returns>
Task<bool> CreateNewLibrary(string name);

(bool result, string reason) CanCreateLibrary(string name);

bool IsLibraryPath(string path);
}
}
4 changes: 2 additions & 2 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public async Task SetWorkingDirectoryAsync(string? value)

var isLibrary = false;
string? name = null;
if (App.LibraryManager.TryGetLibrary(value, out LibraryLocationItem library))
if (App.WindowsLibraryService.TryGetLibrary(value, out LibraryLocationItem library))
{
isLibrary = true;
name = library.Text;
Expand Down Expand Up @@ -1384,7 +1384,7 @@ private async Task RapidAddItemsToCollectionAsync(string path, string? previousD

if (path.ToLowerInvariant().EndsWith(ShellLibraryItem.EXTENSION, StringComparison.Ordinal))
{
if (App.LibraryManager.TryGetLibrary(path, out LibraryLocationItem library) && !library.IsEmpty)
if (App.WindowsLibraryService.TryGetLibrary(path, out LibraryLocationItem library) && !library.IsEmpty)
{
var libItem = new LibraryItem(library);
foreach (var folder in library.Folders)
Expand Down
1 change: 0 additions & 1 deletion src/Files.App/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
global using global::Files.App.Utils.Cloud;
global using global::Files.App.Utils.FileTags;
global using global::Files.App.Utils.Git;
global using global::Files.App.Utils.Library;
global using global::Files.App.Utils.RecentItem;
global using global::Files.App.Utils.RecycleBin;
global using global::Files.App.Utils.Serialization;
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static async Task InitializeAppComponentsAsync()
// Start off a list of tasks we need to run before we can continue startup
await Task.WhenAll(
OptionalTaskAsync(CloudDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowCloudDrivesSection),
App.LibraryManager.UpdateLibrariesAsync(),
App.WindowsLibraryService.UpdateLibrariesAsync(),
OptionalTaskAsync(WSLDistroManager.UpdateDrivesAsync(), generalSettingsService.ShowWslSection),
OptionalTaskAsync(App.FileTagsManager.UpdateFileTagsAsync(), generalSettingsService.ShowFileTagsSection),
App.QuickAccessManager.InitializeAsync()
Expand Down Expand Up @@ -190,6 +190,7 @@ public static IHost ConfigureHost()
.AddSingleton<IStartMenuService, StartMenuService>()
.AddSingleton<IStorageCacheService, StorageCacheService>()
.AddSingleton<IWindowsCompatibilityService, WindowsCompatibilityService>()
.AddSingleton<IWindowsLibraryService, WindowsLibraryService>()
// ViewModels
.AddSingleton<MainPageViewModel>()
.AddSingleton<InfoPaneViewModel>()
Expand All @@ -204,7 +205,6 @@ public static IHost ConfigureHost()
.AddSingleton<StorageHistoryWrapper>()
.AddSingleton<FileTagsManager>()
.AddSingleton<RecentItems>()
.AddSingleton<LibraryManager>()
.AddSingleton<AppModel>()
).Build();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/Layout/LayoutPreferencesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public static void SetLayoutPreferencesForPath(string path, LayoutPreferencesIte
DirectoryGroupByDateUnit = GroupByDateUnit.Year
};
}
else if (LibraryManager.IsLibraryPath(path))
else if (App.WindowsLibraryService.IsLibraryPath(path))
{
// Default for libraries is to group by folder path
return new()
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Helpers/Navigation/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static async Task UpdateTabInfoAsync(TabBarItem tabItem, object navigati
tabLocationHeader = "ThisPC".GetLocalizedResource();
else if (currentPath.Equals(Constants.UserEnvironmentPaths.NetworkFolderPath, StringComparison.OrdinalIgnoreCase))
tabLocationHeader = "SidebarNetworkDrives".GetLocalizedResource();
else if (App.LibraryManager.TryGetLibrary(currentPath, out LibraryLocationItem library))
else if (App.WindowsLibraryService.TryGetLibrary(currentPath, out LibraryLocationItem library))
{
var libName = System.IO.Path.GetFileNameWithoutExtension(library.Path).GetLocalizedResource();
// If localized string is empty use the library name.
Expand Down Expand Up @@ -449,7 +449,7 @@ private static async Task<FilesystemResult> OpenLibrary(string path, IShellPage
await OpenPath(forceOpenInNewTab, UserSettingsService.FoldersSettingsService.OpenFoldersInNewTab, path, associatedInstance);
opened = (FilesystemResult)true;
}
else if (App.LibraryManager.TryGetLibrary(path, out LibraryLocationItem library))
else if (App.WindowsLibraryService.TryGetLibrary(path, out LibraryLocationItem library))
{
opened = (FilesystemResult)await library.CheckDefaultSaveFolderAccess();
if (opened)
Expand Down
6 changes: 3 additions & 3 deletions src/Files.App/Helpers/UI/UIFilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static async Task CreateFileFromDialogResultTypeAsync(AddItemDialogItemTy
if (associatedInstance.SlimContentPage is not null)
{
currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;
if (App.LibraryManager.TryGetLibrary(currentPath, out var library) &&
if (App.WindowsLibraryService.TryGetLibrary(currentPath, out var library) &&
!library.IsEmpty &&
library.Folders.Count == 1) // TODO: handle libraries with multiple folders
{
Expand Down Expand Up @@ -375,7 +375,7 @@ public static async Task CreateShortcutAsync(IShellPage? associatedInstance, IRe
{
var currentPath = associatedInstance?.FilesystemViewModel.WorkingDirectory;

if (App.LibraryManager.TryGetLibrary(currentPath ?? string.Empty, out var library) && !library.IsEmpty)
if (App.WindowsLibraryService.TryGetLibrary(currentPath ?? string.Empty, out var library) && !library.IsEmpty)
currentPath = library.DefaultSaveFolder;

foreach (ListedItem selectedItem in selectedItems)
Expand All @@ -394,7 +394,7 @@ public static async Task CreateShortcutAsync(IShellPage? associatedInstance, IRe
public static async Task CreateShortcutFromDialogAsync(IShellPage associatedInstance)
{
var currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;
if (App.LibraryManager.TryGetLibrary(currentPath, out var library) &&
if (App.WindowsLibraryService.TryGetLibrary(currentPath, out var library) &&
!library.IsEmpty)
{
currentPath = library.DefaultSaveFolder;
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Services/WindowsJumpListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void AddFolder(string path, string group, JumpList instance)
displayName = "ThisPC".GetLocalizedResource();
else if (path.Equals(Constants.UserEnvironmentPaths.NetworkFolderPath, StringComparison.OrdinalIgnoreCase))
displayName = "SidebarNetworkDrives".GetLocalizedResource();
else if (App.LibraryManager.TryGetLibrary(path, out LibraryLocationItem library))
else if (App.WindowsLibraryService.TryGetLibrary(path, out LibraryLocationItem library))
{
var libName = Path.GetFileNameWithoutExtension(library.Path);
displayName = libName switch
Expand Down
Loading
Loading