Skip to content
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

Feature: Inject view models #11820

Merged
merged 10 commits into from
Mar 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using Files.App.ViewModels;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -35,7 +36,7 @@ public async Task ExecuteAsync()
await BitmapHelper.Rotate(PathNormalization.NormalizePath(image.ItemPath), BitmapRotation.Clockwise270Degrees);

context.ShellPage?.SlimContentPage?.ItemManipulationModel?.RefreshItemsThumbnail();
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
Ioc.Default.GetRequiredService<PreviewPaneViewModel>().UpdateSelectedItemPreview();
}

private bool IsContextPageTypeAdaptedToCommand()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using Files.App.ViewModels;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -35,7 +36,7 @@ public async Task ExecuteAsync()
await BitmapHelper.Rotate(PathNormalization.NormalizePath(image.ItemPath), BitmapRotation.Clockwise90Degrees);

context.ShellPage?.SlimContentPage?.ItemManipulationModel?.RefreshItemsThumbnail();
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
Ioc.Default.GetRequiredService<PreviewPaneViewModel>().UpdateSelectedItemPreview();
}

private bool IsContextPageTypeAdaptedToCommand()
Expand Down
4 changes: 3 additions & 1 deletion src/Files.App/Actions/Show/TogglePreviewPaneAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Extensions;
using Files.App.ViewModels;
Expand All @@ -10,7 +11,7 @@ namespace Files.App.Actions
{
internal class TogglePreviewPaneAction : ObservableObject, IToggleAction
{
private readonly PreviewPaneViewModel viewModel = App.PreviewPaneViewModel;
private readonly PreviewPaneViewModel viewModel;

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

Expand All @@ -23,6 +24,7 @@ internal class TogglePreviewPaneAction : ObservableObject, IToggleAction

public TogglePreviewPaneAction()
{
viewModel = Ioc.Default.GetRequiredService<PreviewPaneViewModel>();
viewModel.PropertyChanged += ViewModel_PropertyChanged;
}

Expand Down
137 changes: 58 additions & 79 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Files.App.Storage.NativeStorage;
using Files.App.UserControls.MultitaskingControl;
using Files.App.ViewModels;
using Files.App.ViewModels.Settings;
using Files.App.Views;
using Files.Backend.Services;
using Files.Backend.Services.Settings;
Expand All @@ -24,11 +25,13 @@
using Files.Shared;
using Files.Shared.Cloud;
using Files.Shared.Extensions;
using Files.Shared.Services;
using Files.Shared.Services.DateTimeFormatter;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand All @@ -54,14 +57,11 @@ namespace Files.App
public partial class App : Application
{
private static bool ShowErrorNotification = false;

private IHost host { get; set; }
public static string OutputPath { get; set; }
public static CommandBarFlyout? LastOpenedFlyout { get; set; }
public static StorageHistoryWrapper HistoryWrapper = new StorageHistoryWrapper();
public static SettingsViewModel AppSettings { get; private set; }
public static AppModel AppModel { get; private set; }
public static PreviewPaneViewModel PreviewPaneViewModel { get; private set; }
public static JumpListManager JumpList { get; private set; }
public static RecentItems RecentItemsManager { get; private set; }
public static QuickAccessManager QuickAccessManager { get; private set; }
public static CloudDrivesManager CloudDrivesManager { get; private set; }
Expand All @@ -70,12 +70,9 @@ public partial class App : Application
public static WSLDistroManager WSLDistroManager { get; private set; }
public static LibraryManager LibraryManager { get; private set; }
public static FileTagsManager FileTagsManager { get; private set; }
public static AppThemeResourcesHelper AppThemeResourcesHelper { get; private set; }

public static ILogger Logger { get; private set; }
private static readonly UniversalLogWriter logWriter = new UniversalLogWriter();

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

public static string AppVersion = $"{Package.Current.Id.Version.Major}.{Package.Current.Id.Version.Minor}.{Package.Current.Id.Version.Build}.{Package.Current.Id.Version.Revision}";
Expand All @@ -95,84 +92,14 @@ public App()
UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedException;
InitializeComponent();
Services = ConfigureServices();
Ioc.Default.ConfigureServices(Services);
LogoPath = Package.Current.DisplayName == "Files - Dev" ? Constants.AssetPaths.DevLogo
: (Package.Current.DisplayName == "Files (Preview)" ? Constants.AssetPaths.PreviewLogo : Constants.AssetPaths.StableLogo);
}

private IServiceProvider ConfigureServices()
{
ServiceCollection services = new ServiceCollection();

services
// TODO: Loggers:

// Settings:
// Base IUserSettingsService as parent settings store (to get ISettingsSharingContext from)
.AddSingleton<IUserSettingsService, UserSettingsService>()
// Children settings (from IUserSettingsService)
.AddSingleton<IAppearanceSettingsService, AppearanceSettingsService>((sp) => new AppearanceSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IPreferencesSettingsService, PreferencesSettingsService>((sp) => new PreferencesSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IFoldersSettingsService, FoldersSettingsService>((sp) => new FoldersSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IApplicationSettingsService, ApplicationSettingsService>((sp) => new ApplicationSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IPreviewPaneSettingsService, PreviewPaneSettingsService>((sp) => new PreviewPaneSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<ILayoutSettingsService, LayoutSettingsService>((sp) => new LayoutSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IAppSettingsService, AppSettingsService>((sp) => new AppSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
// Settings not related to IUserSettingsService:
.AddSingleton<IFileTagsSettingsService, FileTagsSettingsService>()
.AddSingleton<IBundlesSettingsService, BundlesSettingsService>()

// Contexts
.AddSingleton<IPageContext, PageContext>()
.AddSingleton<IContentPageContext, ContentPageContext>()
.AddSingleton<IDisplayPageContext, DisplayPageContext>()
.AddSingleton<IWindowContext, WindowContext>()
.AddSingleton<IMultitaskingContext, MultitaskingContext>()

// Other services
.AddSingleton(Logger)
.AddSingleton<IDialogService, DialogService>()
.AddSingleton<IImageService, ImagingService>()
.AddSingleton<IThreadingService, ThreadingService>()
.AddSingleton<ILocalizationService, LocalizationService>()
.AddSingleton<ICloudDetector, CloudDetector>()
.AddSingleton<IFileTagsService, FileTagsService>()
.AddSingleton<ICommandManager, CommandManager>()
#if UWP
.AddSingleton<IStorageService, WindowsStorageService>()
#else
.AddSingleton<IStorageService, NativeStorageService>()
#endif
.AddSingleton<IAddItemService, AddItemService>()
#if SIDELOAD
.AddSingleton<IUpdateService, SideloadUpdateService>()
#else
.AddSingleton<IUpdateService, UpdateService>()
#endif
.AddSingleton<IDateTimeFormatterFactory, DateTimeFormatterFactory>()
.AddSingleton<IDateTimeFormatter, UserDateTimeFormatter>()
.AddSingleton<IVolumeInfoFactory, VolumeInfoFactory>()

// TODO(i): FileSystem operations:
// (IFilesystemHelpersService, IFilesystemOperationsService)
// (IStorageEnumerator, IFallbackStorageEnumerator)
.AddSingleton<ISizeProvider, UserSizeProvider>()
.AddSingleton<IQuickAccessService, QuickAccessService>()

; // End of service configuration

return services.BuildServiceProvider();
}

private static void EnsureSettingsAndConfigurationAreBootstrapped()
{
AppSettings ??= new SettingsViewModel();
AppThemeResourcesHelper ??= new AppThemeResourcesHelper();
JumpList ??= new JumpListManager();
RecentItemsManager ??= new RecentItems();
AppModel ??= new AppModel();
PreviewPaneViewModel ??= new PreviewPaneViewModel();
LibraryManager ??= new LibraryManager();
DrivesManager ??= new DrivesManager();
NetworkDrivesManager ??= new NetworkDrivesManager();
Expand Down Expand Up @@ -218,7 +145,7 @@ await Task.WhenAll(
QuickAccessManager.InitializeAsync()
);
await Task.WhenAll(
JumpList.InitializeAsync(),
JumpListHelper.InitializeUpdatesAsync(),
addItemService.GetNewEntriesAsync()
);
FileTagsHelper.UpdateTagsDb();
Expand Down Expand Up @@ -257,6 +184,59 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
// Initialize MainWindow here
EnsureWindowIsInitialized();

host = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
services
.AddSingleton<IUserSettingsService, UserSettingsService>()
.AddSingleton<IAppearanceSettingsService, AppearanceSettingsService>((sp) => new AppearanceSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IPreferencesSettingsService, PreferencesSettingsService>((sp) => new PreferencesSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IFoldersSettingsService, FoldersSettingsService>((sp) => new FoldersSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IApplicationSettingsService, ApplicationSettingsService>((sp) => new ApplicationSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IPreviewPaneSettingsService, PreviewPaneSettingsService>((sp) => new PreviewPaneSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<ILayoutSettingsService, LayoutSettingsService>((sp) => new LayoutSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IAppSettingsService, AppSettingsService>((sp) => new AppSettingsService((sp.GetService<IUserSettingsService>() as UserSettingsService).GetSharingContext()))
.AddSingleton<IFileTagsSettingsService, FileTagsSettingsService>()
.AddSingleton<IBundlesSettingsService, BundlesSettingsService>()
.AddSingleton<IPageContext, PageContext>()
.AddSingleton<IContentPageContext, ContentPageContext>()
.AddSingleton<IDisplayPageContext, DisplayPageContext>()
.AddSingleton<IWindowContext, WindowContext>()
.AddSingleton<IMultitaskingContext, MultitaskingContext>()
.AddSingleton(Logger)
.AddSingleton<IDialogService, DialogService>()
.AddSingleton<IImageService, ImagingService>()
.AddSingleton<IThreadingService, ThreadingService>()
.AddSingleton<ILocalizationService, LocalizationService>()
.AddSingleton<ICloudDetector, CloudDetector>()
.AddSingleton<IFileTagsService, FileTagsService>()
.AddSingleton<ICommandManager, CommandManager>()
#if UWP
.AddSingleton<IStorageService, WindowsStorageService>()
#else
.AddSingleton<IStorageService, NativeStorageService>()
#endif
.AddSingleton<IAddItemService, AddItemService>()
#if SIDELOAD
.AddSingleton<IUpdateService, SideloadUpdateService>()
#else
.AddSingleton<IUpdateService, UpdateService>()
#endif
.AddSingleton<IDateTimeFormatterFactory, DateTimeFormatterFactory>()
.AddSingleton<IDateTimeFormatter, UserDateTimeFormatter>()
.AddSingleton<IVolumeInfoFactory, VolumeInfoFactory>()
.AddSingleton<ISizeProvider, UserSizeProvider>()
.AddSingleton<IQuickAccessService, QuickAccessService>()
.AddSingleton<IResourcesService, ResourcesService>()
.AddSingleton<IJumpListService, JumpListService>()
.AddScoped<MainPageViewModel>()
.AddScoped<PreviewPaneViewModel>()
.AddScoped<SettingsViewModel>()
.AddScoped<OngoingTasksViewModel>()
.AddScoped<AppearanceViewModel>()
)
.Build();
Ioc.Default.ConfigureServices(host.Services);

EnsureSettingsAndConfigurationAreBootstrapped();

_ = InitializeAppComponentsAsync().ContinueWith(t => Logger.Warn(t.Exception, "Error during InitializeAppComponentsAsync()"), TaskContinuationOptions.OnlyOnFaulted);
Expand Down Expand Up @@ -327,7 +307,6 @@ await SafetyExtensions.IgnoreExceptions(async () =>
}

DrivesManager?.Dispose();
PreviewPaneViewModel?.Dispose();

// Try to maintain clipboard data after app close
SafetyExtensions.IgnoreExceptions(() =>
Expand Down
10 changes: 5 additions & 5 deletions src/Files.App/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public FolderSettingsViewModel? FolderSettings
public CurrentInstanceViewModel? InstanceViewModel
=> ParentShellPageInstance?.InstanceViewModel;

public PreviewPaneViewModel PreviewPaneViewModel
=> App.PreviewPaneViewModel;
public PreviewPaneViewModel PreviewPaneViewModel { get; private set; }

public AppModel AppModel
=> App.AppModel;
Expand Down Expand Up @@ -221,15 +220,15 @@ internal set
if (value?.FirstOrDefault() != selectedItems?.FirstOrDefault())
{
// Update preview pane properties
App.PreviewPaneViewModel.IsItemSelected = value?.Count > 0;
App.PreviewPaneViewModel.SelectedItem = value?.Count == 1 ? value.First() : null;
PreviewPaneViewModel.IsItemSelected = value?.Count > 0;
PreviewPaneViewModel.SelectedItem = value?.Count == 1 ? value.First() : null;

// Check if the preview pane is open before updating the model
if (PreviewPaneViewModel.IsEnabled)
{
var isPaneEnabled = ((App.Window.Content as Frame)?.Content as MainPage)?.ShouldPreviewPaneBeActive ?? false;
if (isPaneEnabled)
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
PreviewPaneViewModel.UpdateSelectedItemPreview();
}
}

Expand Down Expand Up @@ -288,6 +287,7 @@ internal set

public BaseLayout()
{
PreviewPaneViewModel = Ioc.Default.GetRequiredService<PreviewPaneViewModel>();
ItemManipulationModel = new ItemManipulationModel();

HookBaseEvents();
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.1" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageReference Include="SevenZipSharp" Version="1.0.0" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Files.Shared;
using Files.Shared.Enums;
using Files.Shared.Extensions;
using Files.Shared.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -31,7 +32,7 @@ public class FilesystemHelpers : IFilesystemHelpers
#region Private Members

private IShellPage associatedInstance;

private readonly IJumpListService jumpListService;
private IFilesystemOperations filesystemOperations;

private ItemManipulationModel itemManipulationModel => associatedInstance.SlimContentPage?.ItemManipulationModel;
Expand Down Expand Up @@ -78,6 +79,7 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel
{
this.associatedInstance = associatedInstance;
this.cancellationToken = cancellationToken;
jumpListService = Ioc.Default.GetRequiredService<IJumpListService>();
filesystemOperations = new ShellFilesystemOperations(this.associatedInstance);
}

Expand Down Expand Up @@ -184,7 +186,7 @@ public async Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItemWithPat
App.HistoryWrapper.AddHistory(history);
var itemsDeleted = history?.Source.Count ?? 0;

source.ForEach(x => App.JumpList.RemoveFolder(x.Path)); // Remove items from jump list
source.ForEach(async x => await jumpListService.RemoveFolderAsync(x.Path)); // Remove items from jump list

banner.Remove();
sw.Stop();
Expand Down Expand Up @@ -481,7 +483,7 @@ public async Task<ReturnResult> MoveItemsAsync(IEnumerable<IStorageItemWithPath>
}
int itemsMoved = history?.Source.Count ?? 0;

source.ForEach(x => App.JumpList.RemoveFolder(x.Path)); // Remove items from jump list
source.ForEach(async x => await jumpListService.RemoveFolderAsync(x.Path)); // Remove items from jump list

banner.Remove();
sw.Stop();
Expand Down Expand Up @@ -586,7 +588,7 @@ await DialogDisplayHelper.ShowDialogAsync(
App.HistoryWrapper.AddHistory(history);
}

App.JumpList.RemoveFolder(source.Path); // Remove items from jump list
await jumpListService.RemoveFolderAsync(source.Path); // Remove items from jump list

await Task.Yield();
return returnStatus;
Expand Down
5 changes: 0 additions & 5 deletions src/Files.App/Filesystem/QuickAccessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
using Files.App.DataModels;
using Files.App.ServicesImplementation;
using Files.App.UserControls.Widgets;
using Files.Shared.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Windows.Storage;
using static Files.App.Constants;

namespace Files.App.Filesystem
Expand Down
Loading