diff --git a/src/Files.App/Data/Contracts/IGeneralSettingsService.cs b/src/Files.App/Data/Contracts/IGeneralSettingsService.cs index 275819589cbd..f1c4370d6fbd 100644 --- a/src/Files.App/Data/Contracts/IGeneralSettingsService.cs +++ b/src/Files.App/Data/Contracts/IGeneralSettingsService.cs @@ -114,37 +114,72 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty /// Gets or sets a value indicating if the favorites section should be visible. /// bool ShowPinnedSection { get; set; } + + /// + /// Gets or sets a value indicating if the favorites section should be expanded. + /// + bool IsPinnedSectionExpanded { get; set; } /// /// Gets or sets a value indicating if the library section should be visible. /// bool ShowLibrarySection { get; set; } + /// + /// Gets or sets a value indicating if the library section should be expanded. + /// + bool IsLibrarySectionExpanded { get; set; } + /// /// Gets or sets a value indicating if the drive section should be visible. /// bool ShowDrivesSection { get; set; } + /// + /// Gets or sets a value indicating if the drive section should be expanded. + /// + bool IsDriveSectionExpanded { get; set; } + /// /// Gets or sets a value indicating if the cloud drive section should be visible. /// bool ShowCloudDrivesSection { get; set; } + /// + /// Gets or sets a value indicating if the cloud drive section should be expanded. + /// + bool IsCloudDriveSectionExpanded { get; set; } + /// /// Gets or sets a value indicating if the network section should be visible. /// bool ShowNetworkSection { get; set; } + /// + /// Gets or sets a value indicating if the network section should be expanded. + /// + bool IsNetworkSectionExpanded { get; set; } + /// /// Gets or sets a value indicating if the wsl section should be visible. /// bool ShowWslSection { get; set; } + /// + /// Gets or sets a value indicating if the wsl section should be expanded. + /// + bool IsWslSectionExpanded { get; set; } + /// /// Gets or sets a value indicating if the tags section should be visible. /// bool ShowFileTagsSection { get; set; } + /// + /// Gets or sets a value indicating if the file tags section should be expanded. + /// + bool IsFileTagsSectionExpanded { get; set; } + /// /// Gets or sets a value indicating whether or not to move shell extensions into a sub menu. /// diff --git a/src/Files.App/Helpers/Application/AppLifecycleHelper.cs b/src/Files.App/Helpers/Application/AppLifecycleHelper.cs index 84c4979b5385..59af809138c7 100644 --- a/src/Files.App/Helpers/Application/AppLifecycleHelper.cs +++ b/src/Files.App/Helpers/Application/AppLifecycleHelper.cs @@ -205,7 +205,6 @@ public static IHost ConfigureHost() .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/src/Files.App/Services/Settings/GeneralSettingsService.cs b/src/Files.App/Services/Settings/GeneralSettingsService.cs index bb5067887556..62febf805697 100644 --- a/src/Files.App/Services/Settings/GeneralSettingsService.cs +++ b/src/Files.App/Services/Settings/GeneralSettingsService.cs @@ -143,42 +143,84 @@ public bool ShowPinnedSection set => Set(value); } + public bool IsPinnedSectionExpanded + { + get => Get(true); + set => Set(value); + } + public bool ShowLibrarySection { get => Get(false); set => Set(value); } + public bool IsLibrarySectionExpanded + { + get => Get(false); + set => Set(value); + } + public bool ShowDrivesSection { get => Get(true); set => Set(value); } + public bool IsDriveSectionExpanded + { + get => Get(false); + set => Set(value); + } + public bool ShowCloudDrivesSection { get => Get(true); set => Set(value); } + public bool IsCloudDriveSectionExpanded + { + get => Get(false); + set => Set(value); + } + public bool ShowNetworkSection { get => Get(true); set => Set(value); } + public bool IsNetworkSectionExpanded + { + get => Get(false); + set => Set(value); + } + public bool ShowWslSection { get => Get(true); set => Set(value); } + public bool IsWslSectionExpanded + { + get => Get(false); + set => Set(value); + + } public bool ShowFileTagsSection { get => Get(true); set => Set(value); } + public bool IsFileTagsSectionExpanded + { + get => Get(false); + set => Set(value); + } + public bool MoveShellExtensionsToSubMenu { get => Get(true); diff --git a/src/Files.App/ViewModels/SettingsViewModel.cs b/src/Files.App/ViewModels/SettingsViewModel.cs deleted file mode 100644 index 916fc9f92ab5..000000000000 --- a/src/Files.App/ViewModels/SettingsViewModel.cs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) 2024 Files Community -// Licensed under the MIT License. See the LICENSE. - -using System.Diagnostics.CodeAnalysis; -using System.Reflection; -using System.Runtime.CompilerServices; -using Windows.Storage; - -namespace Files.App.ViewModels -{ - [Obsolete("Do not use this class as Settings store anymore, settings have been merged to IUserSettingsService.")] - public sealed class SettingsViewModel : ObservableObject - { - private readonly ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; - - #region ReadAndSaveSettings - - public bool Set(TValue value, [CallerMemberName] string propertyName = null) - { - propertyName = - propertyName is not null && propertyName.StartsWith("set_", StringComparison.OrdinalIgnoreCase) ? - propertyName.Substring(4) : - propertyName; - - TValue originalValue = default; - - if (localSettings.Values.ContainsKey(propertyName)) - { - originalValue = Get(originalValue, propertyName); - - localSettings.Values[propertyName] = value; - if (!SetProperty(ref originalValue, value, propertyName)) - { - return false; - } - } - else - { - localSettings.Values[propertyName] = value; - } - - return true; - } - - public TValue Get<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TValue>(TValue defaultValue, [CallerMemberName] string propertyName = null) - { - var name = propertyName ?? throw new ArgumentNullException(nameof(propertyName), "Cannot store property of unnamed."); - - name = - name.StartsWith("get_", StringComparison.OrdinalIgnoreCase) ? - propertyName.Substring(4) : - propertyName; - - if (localSettings.Values.ContainsKey(name)) - { - var value = localSettings.Values[name]; - - if (value is not TValue tValue) - { - if (value is IConvertible) - { - tValue = (TValue)Convert.ChangeType(value, typeof(TValue)); - } - else - { - var valueType = value.GetType(); - var tryParse = typeof(TValue).GetMethod("TryParse", BindingFlags.Instance | BindingFlags.Public); - - if (tryParse is null) - { - return default; - } - - var stringValue = value.ToString(); - tValue = default; - - var tryParseDelegate = - (TryParseDelegate)Delegate.CreateDelegate(valueType, tryParse, false); - - tValue = (tryParseDelegate?.Invoke(stringValue, out tValue) ?? false) ? tValue : default; - } - - // Put the corrected value in settings - Set(tValue, propertyName); - - return tValue; - } - return tValue; - } - - localSettings.Values[propertyName] = defaultValue; - - return defaultValue; - } - - private delegate bool TryParseDelegate(string inValue, out TValue parsedValue); - - #endregion ReadAndSaveSettings - } -} diff --git a/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs b/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs index 815b2d5119c8..39cf627be4d8 100644 --- a/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs @@ -397,7 +397,6 @@ await lib.CheckDefaultSaveFolderAccess() && } } - section.IsExpanded = Ioc.Default.GetRequiredService().Get(section.Text == "Pinned".GetLocalizedResource(), $"section:{section.Text.Replace('\\', '_')}"); section.PropertyChanged += Section_PropertyChanged; } @@ -405,7 +404,30 @@ private void Section_PropertyChanged(object? sender, PropertyChangedEventArgs e) { if (sender is LocationItem section && e.PropertyName == nameof(section.IsExpanded)) { - Ioc.Default.GetRequiredService().Set(section.IsExpanded, $"section:{section.Text.Replace('\\', '_')}"); + switch (section.Text) + { + case var text when text == "Pinned".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsPinnedSectionExpanded = section.IsExpanded; + break; + case var text when text == "SidebarLibraries".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsLibrarySectionExpanded = section.IsExpanded; + break; + case var text when text == "Drives".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsDriveSectionExpanded = section.IsExpanded; + break; + case var text when text == "SidebarCloudDrives".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsCloudDriveSectionExpanded = section.IsExpanded; + break; + case var text when text == "Network".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsNetworkSectionExpanded = section.IsExpanded; + break; + case var text when text == "WSL".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsWslSectionExpanded = section.IsExpanded; + break; + case var text when text == "FileTags".GetLocalizedResource(): + UserSettingsService.GeneralSettingsService.IsFileTagsSectionExpanded = section.IsExpanded; + break; + } } } @@ -448,6 +470,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("Pinned".GetLocalizedResource(), sectionType, new ContextMenuOptions { ShowHideSection = true }, false); icon = new BitmapImage(new Uri(Constants.FluentIconsPaths.StarIcon)); section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsPinnedSectionExpanded; break; } @@ -461,6 +484,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("SidebarLibraries".GetLocalizedResource(), sectionType, new ContextMenuOptions { IsLibrariesHeader = true, ShowHideSection = true }, false); iconIdex = Constants.ImageRes.Libraries; section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsLibrarySectionExpanded; break; } @@ -474,6 +498,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("Drives".GetLocalizedResource(), sectionType, new ContextMenuOptions { ShowHideSection = true }, false); iconIdex = Constants.ImageRes.ThisPC; section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsDriveSectionExpanded; break; } @@ -487,6 +512,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("SidebarCloudDrives".GetLocalizedResource(), sectionType, new ContextMenuOptions { ShowHideSection = true }, false); icon = new BitmapImage(new Uri(Constants.FluentIconsPaths.CloudDriveIcon)); section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsCloudDriveSectionExpanded; break; } @@ -500,6 +526,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("Network".GetLocalizedResource(), sectionType, new ContextMenuOptions { ShowHideSection = true }, false); iconIdex = Constants.ImageRes.Network; section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsNetworkSectionExpanded; break; } @@ -513,6 +540,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("WSL".GetLocalizedResource(), sectionType, new ContextMenuOptions { ShowHideSection = true }, false); icon = new BitmapImage(new Uri(Constants.WslIconsPaths.GenericIcon)); section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsWslSectionExpanded; break; } @@ -526,6 +554,7 @@ private async Task CreateSectionAsync(SectionType sectionType) section = BuildSection("FileTags".GetLocalizedResource(), sectionType, new ContextMenuOptions { ShowHideSection = true }, false); icon = new BitmapImage(new Uri(Constants.FluentIconsPaths.FileTagsIcon)); section.IsHeader = true; + section.IsExpanded = UserSettingsService.GeneralSettingsService.IsFileTagsSectionExpanded; break; }