Skip to content

Code Quality: RemovableDrivesService, IStorageDeviceWatcher #11969

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 11 commits into from
Apr 16, 2023
5 changes: 3 additions & 2 deletions src/Files.App/Actions/FileSystem/FormatDriveAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Contexts;
using Files.App.DataModels.NavigationControlItems;
using Files.App.Extensions;
using Files.App.Shell;
using Files.App.ViewModels;
Expand All @@ -13,11 +14,11 @@ namespace Files.App.Actions
internal class FormatDriveAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
public string Label { get; } = "FormatDriveText".GetLocalizedResource();

public string Description { get; } = "FormatDriveDescription".GetLocalizedResource();
public bool IsExecutable => context.HasItem && (App.DrivesManager.Drives.FirstOrDefault(x => string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);
public bool IsExecutable => context.HasItem && (drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x => string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);

public FormatDriveAction()
{
Expand Down
7 changes: 4 additions & 3 deletions src/Files.App/Actions/Navigation/DuplicateCurrentTabAction.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.ViewModels;
using Files.App.Views;
using System.Threading.Tasks;
using static Files.App.ViewModels.MainPageViewModel;

namespace Files.App.Actions
{
internal class DuplicateCurrentTabAction : IAction
{
private readonly IMultitaskingContext context = Ioc.Default.GetRequiredService<IMultitaskingContext>();
private readonly MainPageViewModel mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();

public string Label { get; } = "DuplicateTab".GetLocalizedResource();
public string Description => "DuplicateCurrentTabDescription".GetLocalizedResource();
Expand All @@ -19,11 +20,11 @@ public async Task ExecuteAsync()
var arguments = context.CurrentTabItem.TabItemArguments;
if (arguments is null)
{
await AddNewTabByPathAsync(typeof(PaneHolderPage), "Home");
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), "Home");
}
else
{
await AddNewTabByParam(arguments.InitialPageType, arguments.NavigationArg, context.CurrentTabIndex + 1);
await mainPageViewModel.AddNewTabByParam(arguments.InitialPageType, arguments.NavigationArg, context.CurrentTabIndex + 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.ViewModels;
using Files.App.Views;
using System.Threading.Tasks;
using static Files.App.ViewModels.MainPageViewModel;
Expand All @@ -11,6 +12,7 @@ namespace Files.App.Actions
internal class DuplicateSelectedTabAction : IAction
{
private readonly IMultitaskingContext context = Ioc.Default.GetRequiredService<IMultitaskingContext>();
private readonly MainPageViewModel mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();

public string Label { get; } = "DuplicateTab".GetLocalizedResource();
public string Description => "DuplicateSelectedTabDescription".GetLocalizedResource();
Expand All @@ -22,11 +24,11 @@ public async Task ExecuteAsync()
var arguments = context.SelectedTabItem.TabItemArguments;
if (arguments is null)
{
await AddNewTabByPathAsync(typeof(PaneHolderPage), "Home");
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), "Home");
}
else
{
await AddNewTabByParam(arguments.InitialPageType, arguments.NavigationArg, context.SelectedTabIndex + 1);
await mainPageViewModel.AddNewTabByParam(arguments.InitialPageType, arguments.NavigationArg, context.SelectedTabIndex + 1);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Files.App/Actions/Navigation/NewTabAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Files.App.Commands;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Extensions;
using Files.App.ViewModels;
using System.Threading.Tasks;
Expand All @@ -7,12 +8,14 @@ namespace Files.App.Actions
{
internal class NewTabAction : IAction
{
private readonly MainPageViewModel mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();

public string Label { get; } = "NewTab".GetLocalizedResource();

public string Description => "NewTabDescription".GetLocalizedResource();

public HotKey HotKey { get; } = new(Keys.T, KeyModifiers.Ctrl);

public Task ExecuteAsync() => MainPageViewModel.AddNewTabAsync();
public Task ExecuteAsync() => mainPageViewModel.AddNewTabAsync();
}
}
7 changes: 2 additions & 5 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public partial class App : Application
public static QuickAccessManager QuickAccessManager { get; private set; }
public static CloudDrivesManager CloudDrivesManager { get; private set; }
public static NetworkDrivesManager NetworkDrivesManager { get; private set; }
public static DrivesManager DrivesManager { get; private set; }
public static WSLDistroManager WSLDistroManager { get; private set; }
public static LibraryManager LibraryManager { get; private set; }
public static FileTagsManager FileTagsManager { get; private set; }
Expand Down Expand Up @@ -94,7 +93,6 @@ private static void EnsureSettingsAndConfigurationAreBootstrapped()
RecentItemsManager ??= new RecentItems();
AppModel ??= new AppModel();
LibraryManager ??= new LibraryManager();
DrivesManager ??= new DrivesManager();
NetworkDrivesManager ??= new NetworkDrivesManager();
CloudDrivesManager ??= new CloudDrivesManager();
WSLDistroManager ??= new WSLDistroManager();
Expand Down Expand Up @@ -129,7 +127,6 @@ await Task.Run(async () =>
{
await Task.WhenAll(
StartAppCenter(),
DrivesManager.UpdateDrivesAsync(),
OptionalTask(CloudDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowCloudDrivesSection),
LibraryManager.UpdateLibrariesAsync(),
OptionalTask(NetworkDrivesManager.UpdateDrivesAsync(), generalSettingsService.ShowNetworkDrivesSection),
Expand Down Expand Up @@ -225,10 +222,12 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
.AddSingleton<IQuickAccessService, QuickAccessService>()
.AddSingleton<IResourcesService, ResourcesService>()
.AddSingleton<IJumpListService, JumpListService>()
.AddSingleton<IRemovableDrivesService, RemovableDrivesService>()
.AddSingleton<MainPageViewModel>()
.AddSingleton<PreviewPaneViewModel>()
.AddSingleton<SidebarViewModel>()
.AddSingleton<SettingsViewModel>()
.AddSingleton<DrivesViewModel>()
.AddSingleton<OngoingTasksViewModel>()
.AddSingleton<AppearanceViewModel>()
)
Expand Down Expand Up @@ -313,8 +312,6 @@ await SafetyExtensions.IgnoreExceptions(async () =>
Logger);
}

DrivesManager?.Dispose();

// Try to maintain clipboard data after app close
SafetyExtensions.IgnoreExceptions(() =>
{
Expand Down
51 changes: 44 additions & 7 deletions src/Files.App/DataModels/NavigationControlItems/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Helpers;
using Files.App.Storage.WindowsStorage;
using Files.Sdk.Storage;
using Files.Sdk.Storage.Enums;
using Files.Sdk.Storage.LocatableStorage;
using Files.Shared.Extensions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace Files.App.DataModels.NavigationControlItems
{
public class DriveItem : ObservableObject, INavigationControlItem
public class DriveItem : ObservableObject, INavigationControlItem, ILocatableFolder
{
private BitmapImage icon;
public BitmapImage Icon
Expand All @@ -23,8 +29,6 @@ public BitmapImage Icon
set => SetProperty(ref icon, value);
}

//public Uri IconSource { get; set; }

public byte[] IconData { get; set; }

private string path;
Expand Down Expand Up @@ -150,6 +154,10 @@ public bool ShowStorageSense
set => SetProperty(ref showStorageSense, value);
}

public string Id => DeviceID;

public string Name => Root.DisplayName;

public DriveItem()
{
ItemType = NavigationControlItemType.CloudDrive;
Expand Down Expand Up @@ -235,17 +243,22 @@ public int CompareTo(INavigationControlItem other)
return result == 0 ? Text.CompareTo(other.Text) : result;
}

public async Task LoadDriveIcon()
public async Task LoadThumbnailAsync(bool isSidebar = false)
{
if (IconData is null)
if (!isSidebar)
{
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Root);
IconData ??= await thumbnail.ToByteArrayAsync();
}
else
{
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
IconData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, 24);
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, 24);

IconData ??= UIHelpers.GetSidebarIconResourceInfo(Constants.ImageRes.Folder).IconData;
}

Icon = await IconData.ToBitmapAsync();
Icon ??= await IconData.ToBitmapAsync();
}

private string GetSizeString()
Expand All @@ -255,6 +268,30 @@ private string GetSizeString()
FreeSpace.ToSizeString(),
MaxSpace.ToSizeString());
}

public Task<IFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
{
var folder = new WindowsStorageFolder(Root);
return folder.GetFileAsync(fileName, cancellationToken);
}

public Task<IFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
{
var folder = new WindowsStorageFolder(Root);
return folder.GetFolderAsync(folderName, cancellationToken);
}

public IAsyncEnumerable<IStorable> GetItemsAsync(StorableKind kind = StorableKind.All, CancellationToken cancellationToken = default)
{
var folder = new WindowsStorageFolder(Root);
return folder.GetItemsAsync(kind, cancellationToken);
}

public Task<ILocatableFolder?> GetParentAsync(CancellationToken cancellationToken = default)
{
var folder = new WindowsStorageFolder(Root);
return folder.GetParentAsync(cancellationToken);
}
}

public enum DriveType
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public int IndexOfItem(INavigationControlItem locationItem)

public async Task<LocationItem> CreateLocationItemFromPathAsync(string path)
{
var item = await FilesystemTasks.Wrap(() => DrivesManager.GetRootFromPathAsync(path));
var item = await FilesystemTasks.Wrap(() => DriveHelpers.GetRootFromPathAsync(path));
var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item));
LocationItem locationItem;

Expand Down
Loading