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

Transitioned to Json Settings #6113

Merged
merged 34 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
82ebfc9
BaseJsonSettingsModel v3 test
d2dyno1 Sep 13, 2021
7cc8a70
Showcase of ISettingsSharingContext
d2dyno1 Sep 13, 2021
707a5db
Transfer more settings into new format
d2dyno1 Sep 15, 2021
57a3342
Transition more settings and added settings merger
d2dyno1 Sep 15, 2021
230239f
Fixed UI not being updated after a setting is changed
d2dyno1 Sep 15, 2021
d0ceec8
Hopefully fix OnSettingChangedEvent not being raised
d2dyno1 Sep 16, 2021
fb0fa9d
Added back caching feature to Bundles
d2dyno1 Sep 16, 2021
7f47567
Fix build
d2dyno1 Sep 16, 2021
5f9fdf1
Remove some stuff from old SettingsViewModel
d2dyno1 Sep 16, 2021
7bfeeba
Added missing service registration StartupSettingsService
gave92 Sep 19, 2021
81ce4fc
Removed ported settings definitions from SettingsViewModel
gave92 Sep 19, 2021
98ea1c9
Add back AppCenter logging for settings
gave92 Sep 19, 2021
004a103
Merged even more settings
d2dyno1 Sep 19, 2021
a416b02
Merge remote-tracking branch 'upstream/main' into iusersettings
d2dyno1 Sep 19, 2021
f419fc8
Fix file tag selection
gave92 Sep 19, 2021
4e042ea
Merge branch 'iusersettings' of https://github.com/d2dyno1/Files into…
gave92 Sep 19, 2021
a82b3db
Use TValue type for defaultValue
d2dyno1 Sep 19, 2021
3355da1
Added IFileTagsSettingsService
d2dyno1 Sep 19, 2021
d92b402
Added IBundlesSettingsService
d2dyno1 Sep 19, 2021
32a40b5
Fix InvalidCastException
gave92 Sep 19, 2021
259af5d
Merge branch 'iusersettings' of https://github.com/d2dyno1/Files into…
gave92 Sep 19, 2021
e95ed78
Resolve conflicts
d2dyno1 Sep 27, 2021
ae9af73
Merge branch 'iusersettings' of https://github.com/d2dyno1/Files into…
d2dyno1 Sep 27, 2021
29c471c
Fix conflicts
d2dyno1 Oct 1, 2021
160a974
Fix 1
d2dyno1 Oct 1, 2021
d23d911
Fix 2
d2dyno1 Oct 1, 2021
f0af6df
Merge branch 'main' into iusersettings
d2dyno1 Oct 5, 2021
b6c6889
Merge remote-tracking branch 'upstream/main' into iusersettings
d2dyno1 Oct 6, 2021
47dcf6d
Fixed changing settings not updating the UI
d2dyno1 Oct 6, 2021
0bce908
Fix build
d2dyno1 Oct 6, 2021
e054723
Fix IsVerticalTabFlyoutEnabled
d2dyno1 Oct 6, 2021
d17e216
Fix NRE
d2dyno1 Oct 6, 2021
d042b20
Fix startup issue
d2dyno1 Oct 6, 2021
d0b2b52
Merge branch 'iusersettings' of https://github.com/d2dyno1/Files into…
d2dyno1 Oct 6, 2021
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
72 changes: 69 additions & 3 deletions Files/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
using Files.Filesystem.FilesystemHistory;
using Files.Helpers;
using Files.Models.Settings;
using Files.Services;
using Files.Services.Implementation;
using Files.SettingsInterfaces;
using Files.UserControls.MultitaskingControl;
using Files.ViewModels;
using Files.Views;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using Microsoft.Toolkit.Uwp;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.Notifications;
Expand Down Expand Up @@ -59,6 +66,10 @@ sealed partial class App : Application
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}";

public IServiceProvider Services { get; private set; }

public App()
{
// Initialize logger
Expand All @@ -69,13 +80,46 @@ public App()
InitializeComponent();
Suspending += OnSuspending;
LeavingBackground += OnLeavingBackground;

this.Services = ConfigureServices();
Ioc.Default.ConfigureServices(Services);
}

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

services
// Loggers:

// Settings:
// Base IUserSettingsService as parent
.AddSingleton<IUserSettingsService, UserSettingsService>()
// Children settings (from IUserSettingsService)
.AddSingleton<IFilesAndFoldersSettingsService, FilesAndFoldersSettingsService>((sp) => new FilesAndFoldersSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
.AddSingleton<IStartupSettingsService, StartupSettingsService>((sp) => new StartupSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
.AddSingleton<IMultitaskingSettingsService, MultitaskingSettingsService>((sp) => new MultitaskingSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
.AddSingleton<IWidgetsSettingsService, WidgetsSettingsService>((sp) => new WidgetsSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
.AddSingleton<ISidebarSettingsService, SidebarSettingsService>((sp) => new SidebarSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
.AddSingleton<IPreferencesSettingsService, PreferencesSettingsService>((sp) => new PreferencesSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
.AddSingleton<IAppearanceSettingsService, AppearanceSettingsService>((sp) => new AppearanceSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()));

// Dialogs:
//
// FileSystem operations:
// TODO: IFilesystemHelpersService, IFilesystemOperationsService

return services.BuildServiceProvider();
}

private static async Task EnsureSettingsAndConfigurationAreBootstrapped()
{
StartAppCenter();

if (AppSettings == null)
{
AppSettings = await SettingsViewModel.CreateInstance();
RegistryToJsonSettingsMerger.MergeSettings();
}

ExternalResourcesHelper ??= new ExternalResourcesHelper();
Expand All @@ -91,6 +135,23 @@ private static async Task EnsureSettingsAndConfigurationAreBootstrapped()
SidebarPinnedController ??= new SidebarPinnedController();
}

private static async void StartAppCenter()
{
try
{
if (!AppCenter.Configured)
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Resources/AppCenterKey.txt"));
var lines = await FileIO.ReadTextAsync(file);
var obj = Newtonsoft.Json.Linq.JObject.Parse(lines);
AppCenter.Start((string)obj.SelectToken("key"), typeof(Analytics), typeof(Crashes));
}
}
catch
{
}
}

public static async Task LoadOtherStuffAsync()
{
// Start off a list of tasks we need to run before we can continue startup
Expand Down Expand Up @@ -405,9 +466,14 @@ private void OnSuspending(object sender, SuspendingEventArgs e)

public static void SaveSessionTabs() // Enumerates through all tabs and gets the Path property and saves it to AppSettings.LastSessionPages
{
if (AppSettings != null)
IUserSettingsService userSettingsService = Ioc.Default.GetService<IUserSettingsService>();
if (App.BundlesSettings != null)
{
App.BundlesSettings.FlushSettings();
}
if (userSettingsService?.StartupSettingsService != null)
{
AppSettings.LastSessionPages = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
userSettingsService.StartupSettingsService.LastSessionTabList = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
{
if (tab != null && tab.TabItemArguments != null)
{
Expand All @@ -418,7 +484,7 @@ public static void SaveSessionTabs() // Enumerates through all tabs and gets the
var defaultArg = new TabItemArguments() { InitialPageType = typeof(PaneHolderPage), NavigationArg = "NewTab".GetLocalized() };
return defaultArg.Serialize();
}
}).ToArray();
}).ToList();
}
}

Expand Down
12 changes: 8 additions & 4 deletions Files/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using Files.Helpers;
using Files.Helpers.ContextFlyouts;
using Files.Interacts;
using Files.Services;
using Files.UserControls;
using Files.ViewModels;
using Files.ViewModels.Previews;
using Files.Views;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using Microsoft.Toolkit.Uwp;
using Microsoft.Toolkit.Uwp.UI;
using System;
Expand Down Expand Up @@ -42,6 +44,8 @@ public abstract class BaseLayout : Page, IBaseLayout, INotifyPropertyChanged
{
private readonly DispatcherTimer jumpTimer;

protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>();

protected Task<NamedPipeAsAppServiceConnection> Connection => AppServiceConnectionHelper.Instance;

public SelectedItemsPropertiesViewModel SelectedItemsPropertiesViewModel { get; }
Expand Down Expand Up @@ -415,7 +419,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
// pathRoot will be empty on recycle bin path
var workingDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory;
string pathRoot = GetPathRoot(workingDir);
if (string.IsNullOrEmpty(pathRoot) || workingDir.StartsWith(AppSettings.RecycleBinPath)) // Can't go up from recycle bin
if (string.IsNullOrEmpty(pathRoot) || workingDir.StartsWith(CommonPaths.RecycleBinPath)) // Can't go up from recycle bin
{
ParentShellPageInstance.NavToolbarViewModel.CanNavigateToParent = false;
}
Expand All @@ -424,7 +428,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
ParentShellPageInstance.NavToolbarViewModel.CanNavigateToParent = true;
}

ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = workingDir.StartsWith(App.AppSettings.RecycleBinPath);
ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = workingDir.StartsWith(CommonPaths.RecycleBinPath);
ParentShellPageInstance.InstanceViewModel.IsPageTypeMtpDevice = workingDir.StartsWith("\\\\?\\");
ParentShellPageInstance.InstanceViewModel.IsPageTypeFtp = FtpHelpers.IsFtpPath(workingDir);
ParentShellPageInstance.InstanceViewModel.IsPageTypeZipFolder = ZipStorageFolder.IsZipPath(workingDir);
Expand Down Expand Up @@ -618,7 +622,7 @@ private async Task LoadMenuItemsAsync()
secondaryElements.OfType<FrameworkElement>().ForEach(i => i.MinWidth = 250); // Set menu min width
secondaryElements.ForEach(i => ItemContextMenuFlyout.SecondaryCommands.Add(i));

if (AppSettings.AreFileTagsEnabled && !InstanceViewModel.IsPageTypeSearchResults && !InstanceViewModel.IsPageTypeRecycleBin && !InstanceViewModel.IsPageTypeFtp && !InstanceViewModel.IsPageTypeZipFolder)
if (UserSettingsService.FilesAndFoldersSettingsService.AreFileTagsEnabled && !InstanceViewModel.IsPageTypeSearchResults && !InstanceViewModel.IsPageTypeRecycleBin && !InstanceViewModel.IsPageTypeFtp && !InstanceViewModel.IsPageTypeZipFolder)
{
AddFileTagsItemToMenu(ItemContextMenuFlyout);
}
Expand Down Expand Up @@ -655,7 +659,7 @@ private void AddFileTagsItemToMenu(Microsoft.UI.Xaml.Controls.CommandBarFlyout c
private void AddShellItemsToMenu(List<ContextMenuFlyoutItemViewModel> shellMenuItems, Microsoft.UI.Xaml.Controls.CommandBarFlyout contextMenuFlyout, bool shiftPressed)
{
var openWithSubItems = ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(ShellContextmenuHelper.GetOpenWithItems(shellMenuItems));
var mainShellMenuItems = shellMenuItems.RemoveFrom(!App.AppSettings.MoveOverflowMenuItemsToSubMenu ? int.MaxValue : shiftPressed ? 6 : 4);
var mainShellMenuItems = shellMenuItems.RemoveFrom(!UserSettingsService.AppearanceSettingsService.MoveOverflowMenuItemsToSubMenu ? int.MaxValue : shiftPressed ? 6 : 4);
var overflowShellMenuItems = shellMenuItems.Except(mainShellMenuItems).ToList();

var overflowItems = ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(overflowShellMenuItems);
Expand Down
9 changes: 9 additions & 0 deletions Files/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public static class AdaptiveLayout
public const float ExtraSmallThreshold = 15.0f;
}

public static class CommonPaths
{
public const string RecycleBinPath = @"Shell:RecycleBinFolder";

public const string NetworkFolderPath = @"Shell:NetworkPlacesFolder";
}

public static class ImageRes
{
// See imageres.dll for more icon indexes to add
Expand Down Expand Up @@ -106,6 +113,8 @@ public static class LocalSettings

public const string BundlesSettingsFileName = "bundles.json";

public const string UserSettingsFileName = "user_settings.json";

public const string FileTagSettingsFileName = "filetags.json";
}

Expand Down
29 changes: 15 additions & 14 deletions Files/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using Files.DataModels.NavigationControlItems;
using Files.Filesystem;
using Files.Helpers;
using Files.Services;
using Files.UserControls;
using Files.ViewModels;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using Microsoft.Toolkit.Uwp;
using Newtonsoft.Json;
using System;
Expand All @@ -22,15 +24,14 @@ namespace Files.DataModels
{
public class SidebarPinnedModel
{
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>();

public static IList<IconFileInfo> IconResources = null;

private SidebarPinnedController controller;

private LocationItem favoriteSection, homeSection;

[JsonIgnore]
public SettingsViewModel AppSettings => App.AppSettings;

[JsonIgnore]
public MainViewModel MainViewModel => App.MainViewModel;

Expand All @@ -53,8 +54,8 @@ public void AddDefaultItems()
{
var udp = UserDataPaths.GetDefault();

FavoriteItems.Add(AppSettings.DesktopPath);
FavoriteItems.Add(AppSettings.DownloadsPath);
FavoriteItems.Add(CommonPaths.DesktopPath);
FavoriteItems.Add(CommonPaths.DownloadsPath);
FavoriteItems.Add(udp.Documents);
}

Expand All @@ -63,7 +64,7 @@ private void RemoveFavoritesSideBarSection()
try
{
var item = (from n in SidebarControl.SideBarItems where n.Text.Equals("SidebarFavorites".GetLocalized()) select n).FirstOrDefault();
if (!App.AppSettings.ShowFavoritesSection && item != null)
if (!UserSettingsService.SidebarSettingsService.ShowFavoritesSection && item != null)
{
SidebarControl.SideBarItems.Remove(item);
}
Expand All @@ -74,7 +75,7 @@ private void RemoveFavoritesSideBarSection()

public async void UpdateFavoritesSectionVisibility()
{
if (App.AppSettings.ShowFavoritesSection)
if (UserSettingsService.SidebarSettingsService.ShowFavoritesSection)
{
await AddAllItemsToSidebar();
}
Expand Down Expand Up @@ -118,11 +119,11 @@ public async Task ShowHideRecycleBinItemAsync(bool show)
Text = ApplicationData.Current.LocalSettings.Values.Get("RecycleBin_Title", "Recycle Bin"),
IsDefaultLocation = true,
Icon = UIHelpers.GetImageForIconOrNull(IconResources?.FirstOrDefault(x => x.Index == Constants.ImageRes.RecycleBin)?.Image),
Path = App.AppSettings.RecycleBinPath
Path = CommonPaths.RecycleBinPath
};
// Add recycle bin to sidebar, title is read from LocalSettings (provided by the fulltrust process)
// TODO: the very first time the app is launched localized name not available
if (!favoriteSection.ChildItems.Any(x => x.Path == App.AppSettings.RecycleBinPath))
if (!favoriteSection.ChildItems.Any(x => x.Path == CommonPaths.RecycleBinPath))
{
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Add(recycleBinItem));
}
Expand All @@ -131,7 +132,7 @@ public async Task ShowHideRecycleBinItemAsync(bool show)
{
foreach (INavigationControlItem item in favoriteSection.ChildItems.ToList())
{
if (item is LocationItem && item.Path == App.AppSettings.RecycleBinPath)
if (item is LocationItem && item.Path == CommonPaths.RecycleBinPath)
{
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Remove(item));
}
Expand Down Expand Up @@ -261,7 +262,7 @@ public async Task AddItemToSidebarAsync(string path)
var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item));
if (res || (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path))
{
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(App.AppSettings.RecycleBinPath));
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
int insertIndex = lastItem != null ? favoriteSection.ChildItems.IndexOf(lastItem) + 1 : 0;
var locationItem = new LocationItem
{
Expand Down Expand Up @@ -309,7 +310,7 @@ public async Task AddItemToSidebarAsync(string path)
/// <param name="section">The section.</param>
private void AddItemToSidebarAsync(LocationItem section)
{
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(App.AppSettings.RecycleBinPath));
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
int insertIndex = lastItem != null ? favoriteSection.ChildItems.IndexOf(lastItem) + 1 : 0;

if (!favoriteSection.ChildItems.Contains(section))
Expand All @@ -330,7 +331,7 @@ public async Task AddAllItemsToSidebar()
IconResources = (await SidebarViewModel.LoadSidebarIconResources())?.ToList();
}

if (!App.AppSettings.ShowFavoritesSection)
if (!UserSettingsService.SidebarSettingsService.ShowFavoritesSection)
{
SidebarControl.SideBarItemsSemaphore.Release();
}
Expand Down Expand Up @@ -382,7 +383,7 @@ public async Task AddAllItemsToSidebar()
SidebarControl.SideBarItemsSemaphore.Release();
}

await ShowHideRecycleBinItemAsync(App.AppSettings.PinRecycleBinToSideBar);
await ShowHideRecycleBinItemAsync(UserSettingsService.SidebarSettingsService.PinRecycleBinToSideBar);
}
}

Expand Down
17 changes: 17 additions & 0 deletions Files/EventArguments/BaseJsonSettingsModelEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Files.EventArguments
{
public sealed class SettingChangedEventArgs : EventArgs
{
public readonly string settingName;

public readonly object newValue;

public SettingChangedEventArgs(string settingName, object newValue)
{
this.settingName = settingName;
this.newValue = newValue;
}
}
}
Loading