Skip to content

Commit a7fb38f

Browse files
authored
Transitioned to Json Settings (#6113)
1 parent cda9b7b commit a7fb38f

File tree

109 files changed

+2571
-1382
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+2571
-1382
lines changed

Files/App.xaml.cs

+79-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
using Files.Filesystem;
55
using Files.Filesystem.FilesystemHistory;
66
using Files.Helpers;
7-
using Files.Models.Settings;
8-
using Files.SettingsInterfaces;
7+
using Files.Services;
8+
using Files.Services.Implementation;
99
using Files.UserControls.MultitaskingControl;
1010
using Files.ViewModels;
1111
using Files.Views;
12+
using Microsoft.AppCenter;
13+
using Microsoft.AppCenter.Analytics;
14+
using Microsoft.AppCenter.Crashes;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Toolkit.Mvvm.DependencyInjection;
1217
using Microsoft.Toolkit.Uwp;
1318
using Microsoft.Toolkit.Uwp.Helpers;
1419
using Microsoft.Toolkit.Uwp.Notifications;
@@ -41,7 +46,6 @@ sealed partial class App : Application
4146

4247
public static SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1, 1);
4348
public static StorageHistoryWrapper HistoryWrapper = new StorageHistoryWrapper();
44-
public static IBundlesSettings BundlesSettings = new BundlesSettingsModel();
4549
public static SettingsViewModel AppSettings { get; private set; }
4650
public static MainViewModel MainViewModel { get; private set; }
4751
public static JumpListManager JumpList { get; private set; }
@@ -61,6 +65,10 @@ sealed partial class App : Application
6165
public static OngoingTasksViewModel OngoingTasksViewModel { get; } = new OngoingTasksViewModel();
6266
public static SecondaryTileHelper SecondaryTileHelper { get; private set; } = new SecondaryTileHelper();
6367

68+
public static string AppVersion = $"{Package.Current.Id.Version.Major}.{Package.Current.Id.Version.Minor}.{Package.Current.Id.Version.Build}.{Package.Current.Id.Version.Revision}";
69+
70+
public IServiceProvider Services { get; private set; }
71+
6472
public App()
6573
{
6674
// Initialize logger
@@ -71,12 +79,52 @@ public App()
7179
InitializeComponent();
7280
Suspending += OnSuspending;
7381
LeavingBackground += OnLeavingBackground;
82+
7483
AppServiceConnectionHelper.Register();
84+
85+
this.Services = ConfigureServices();
86+
Ioc.Default.ConfigureServices(Services);
87+
}
88+
89+
private IServiceProvider ConfigureServices()
90+
{
91+
ServiceCollection services = new ServiceCollection();
92+
93+
services
94+
// TODO: Loggers:
95+
96+
// Settings:
97+
// Base IUserSettingsService as parent settings store (to get ISettingsSharingContext from)
98+
.AddSingleton<IUserSettingsService, UserSettingsService>()
99+
// Children settings (from IUserSettingsService)
100+
.AddSingleton<IFilesAndFoldersSettingsService, FilesAndFoldersSettingsService>((sp) => new FilesAndFoldersSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
101+
.AddSingleton<IStartupSettingsService, StartupSettingsService>((sp) => new StartupSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
102+
.AddSingleton<IMultitaskingSettingsService, MultitaskingSettingsService>((sp) => new MultitaskingSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
103+
.AddSingleton<IWidgetsSettingsService, WidgetsSettingsService>((sp) => new WidgetsSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
104+
.AddSingleton<ISidebarSettingsService, SidebarSettingsService>((sp) => new SidebarSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
105+
.AddSingleton<IPreferencesSettingsService, PreferencesSettingsService>((sp) => new PreferencesSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
106+
.AddSingleton<IAppearanceSettingsService, AppearanceSettingsService>((sp) => new AppearanceSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
107+
.AddSingleton<IPreviewPaneSettingsService, PreviewPaneSettingsService>((sp) => new PreviewPaneSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
108+
.AddSingleton<ILayoutSettingsService, LayoutSettingsService>((sp) => new LayoutSettingsService(sp.GetService<IUserSettingsService>().GetSharingContext()))
109+
// Settings not related to IUserSettingsService:
110+
.AddSingleton<IFileTagsSettingsService, FileTagsSettingsService>()
111+
.AddSingleton<IBundlesSettingsService, BundlesSettingsService>()
112+
113+
// TODO: Dialogs:
114+
115+
// TODO: FileSystem operations:
116+
// (IFilesystemHelpersService, IFilesystemOperationsService)
117+
118+
; // End of service configuration
119+
120+
121+
return services.BuildServiceProvider();
75122
}
76123

77124
private static async Task EnsureSettingsAndConfigurationAreBootstrapped()
78125
{
79126
AppSettings ??= new SettingsViewModel();
127+
RegistryToJsonSettingsMerger.MergeSettings();
80128

81129
ExternalResourcesHelper ??= new ExternalResourcesHelper();
82130
await ExternalResourcesHelper.LoadSelectedTheme();
@@ -92,6 +140,24 @@ private static async Task EnsureSettingsAndConfigurationAreBootstrapped()
92140
TerminalController ??= new TerminalController();
93141
}
94142

143+
private static async void StartAppCenter()
144+
{
145+
try
146+
{
147+
if (!AppCenter.Configured)
148+
{
149+
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Resources/AppCenterKey.txt"));
150+
var lines = await FileIO.ReadTextAsync(file);
151+
var obj = Newtonsoft.Json.Linq.JObject.Parse(lines);
152+
AppCenter.Start((string)obj.SelectToken("key"), typeof(Analytics), typeof(Crashes));
153+
}
154+
}
155+
catch (Exception ex)
156+
{
157+
Logger.Warn(ex, "AppCenter could not be started.");
158+
}
159+
}
160+
95161
public static async Task LoadOtherStuffAsync()
96162
{
97163
// Start off a list of tasks we need to run before we can continue startup
@@ -437,9 +503,16 @@ await Common.Extensions.IgnoreExceptions(async () =>
437503

438504
public static void SaveSessionTabs() // Enumerates through all tabs and gets the Path property and saves it to AppSettings.LastSessionPages
439505
{
440-
if (AppSettings != null)
506+
IUserSettingsService userSettingsService = Ioc.Default.GetService<IUserSettingsService>();
507+
IBundlesSettingsService bundlesSettingsService = Ioc.Default.GetService<IBundlesSettingsService>();
508+
509+
if (bundlesSettingsService != null)
510+
{
511+
bundlesSettingsService.FlushSettings();
512+
}
513+
if (userSettingsService?.StartupSettingsService != null)
441514
{
442-
AppSettings.LastSessionPages = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
515+
userSettingsService.StartupSettingsService.LastSessionTabList = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
443516
{
444517
if (tab != null && tab.TabItemArguments != null)
445518
{
@@ -450,7 +523,7 @@ public static void SaveSessionTabs() // Enumerates through all tabs and gets the
450523
var defaultArg = new TabItemArguments() { InitialPageType = typeof(PaneHolderPage), NavigationArg = "NewTab".GetLocalized() };
451524
return defaultArg.Serialize();
452525
}
453-
}).ToArray();
526+
}).ToList();
454527
}
455528
}
456529

Files/BaseLayout.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
using Files.Helpers;
77
using Files.Helpers.ContextFlyouts;
88
using Files.Interacts;
9+
using Files.Services;
910
using Files.UserControls;
1011
using Files.ViewModels;
1112
using Files.ViewModels.Previews;
1213
using Files.Views;
14+
using Microsoft.Toolkit.Mvvm.DependencyInjection;
1315
using Microsoft.Toolkit.Uwp;
1416
using Microsoft.Toolkit.Uwp.UI;
1517
using System;
@@ -42,6 +44,10 @@ public abstract class BaseLayout : Page, IBaseLayout, INotifyPropertyChanged
4244
{
4345
private readonly DispatcherTimer jumpTimer;
4446

47+
protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>();
48+
49+
protected IFileTagsSettingsService FileTagsSettingsService { get; } = Ioc.Default.GetService<IFileTagsSettingsService>();
50+
4551
protected Task<NamedPipeAsAppServiceConnection> Connection => AppServiceConnectionHelper.Instance;
4652

4753
public SelectedItemsPropertiesViewModel SelectedItemsPropertiesViewModel { get; }
@@ -416,7 +422,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
416422
// pathRoot will be empty on recycle bin path
417423
var workingDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory ?? string.Empty;
418424
string pathRoot = GetPathRoot(workingDir);
419-
if (string.IsNullOrEmpty(pathRoot) || workingDir.StartsWith(AppSettings.RecycleBinPath)) // Can't go up from recycle bin
425+
if (string.IsNullOrEmpty(pathRoot) || workingDir.StartsWith(CommonPaths.RecycleBinPath)) // Can't go up from recycle bin
420426
{
421427
ParentShellPageInstance.NavToolbarViewModel.CanNavigateToParent = false;
422428
}
@@ -425,7 +431,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
425431
ParentShellPageInstance.NavToolbarViewModel.CanNavigateToParent = true;
426432
}
427433

428-
ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = workingDir.StartsWith(App.AppSettings.RecycleBinPath);
434+
ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = workingDir.StartsWith(CommonPaths.RecycleBinPath);
429435
ParentShellPageInstance.InstanceViewModel.IsPageTypeMtpDevice = workingDir.StartsWith("\\\\?\\");
430436
ParentShellPageInstance.InstanceViewModel.IsPageTypeFtp = FtpHelpers.IsFtpPath(workingDir);
431437
ParentShellPageInstance.InstanceViewModel.IsPageTypeZipFolder = ZipStorageFolder.IsZipPath(workingDir);
@@ -619,7 +625,7 @@ private async Task LoadMenuItemsAsync()
619625
secondaryElements.OfType<FrameworkElement>().ForEach(i => i.MinWidth = 250); // Set menu min width
620626
secondaryElements.ForEach(i => ItemContextMenuFlyout.SecondaryCommands.Add(i));
621627

622-
if (AppSettings.AreFileTagsEnabled && !InstanceViewModel.IsPageTypeSearchResults && !InstanceViewModel.IsPageTypeRecycleBin && !InstanceViewModel.IsPageTypeFtp && !InstanceViewModel.IsPageTypeZipFolder)
628+
if (UserSettingsService.FilesAndFoldersSettingsService.AreFileTagsEnabled && !InstanceViewModel.IsPageTypeSearchResults && !InstanceViewModel.IsPageTypeRecycleBin && !InstanceViewModel.IsPageTypeFtp && !InstanceViewModel.IsPageTypeZipFolder)
623629
{
624630
AddFileTagsItemToMenu(ItemContextMenuFlyout);
625631
}
@@ -640,7 +646,7 @@ private void AddFileTagsItemToMenu(Microsoft.UI.Xaml.Controls.CommandBarFlyout c
640646
{
641647
var fileTagMenuFlyout = new MenuFlyoutItemFileTag()
642648
{
643-
ItemsSource = AppSettings.FileTagsSettings.FileTagList,
649+
ItemsSource = FileTagsSettingsService.FileTagList,
644650
SelectedItems = SelectedItems
645651
};
646652
var overflowSeparator = contextMenu.SecondaryCommands.FirstOrDefault(x => x is FrameworkElement fe && fe.Tag as string == "OverflowSeparator") as AppBarSeparator;
@@ -656,7 +662,7 @@ private void AddFileTagsItemToMenu(Microsoft.UI.Xaml.Controls.CommandBarFlyout c
656662
private void AddShellItemsToMenu(List<ContextMenuFlyoutItemViewModel> shellMenuItems, Microsoft.UI.Xaml.Controls.CommandBarFlyout contextMenuFlyout, bool shiftPressed)
657663
{
658664
var openWithSubItems = ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(ShellContextmenuHelper.GetOpenWithItems(shellMenuItems));
659-
var mainShellMenuItems = shellMenuItems.RemoveFrom(!App.AppSettings.MoveOverflowMenuItemsToSubMenu ? int.MaxValue : shiftPressed ? 6 : 4);
665+
var mainShellMenuItems = shellMenuItems.RemoveFrom(!UserSettingsService.AppearanceSettingsService.MoveOverflowMenuItemsToSubMenu ? int.MaxValue : shiftPressed ? 6 : 4);
660666
var overflowShellMenuItems = shellMenuItems.Except(mainShellMenuItems).ToList();
661667

662668
var overflowItems = ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(overflowShellMenuItems);

Files/Constants.cs

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public static class AdaptiveLayout
1515
public const float ExtraSmallThreshold = 15.0f;
1616
}
1717

18+
public static class CommonPaths
19+
{
20+
public const string RecycleBinPath = @"Shell:RecycleBinFolder";
21+
22+
public const string NetworkFolderPath = @"Shell:NetworkPlacesFolder";
23+
}
24+
1825
public static class ImageRes
1926
{
2027
// See imageres.dll for more icon indexes to add
@@ -106,6 +113,8 @@ public static class LocalSettings
106113

107114
public const string BundlesSettingsFileName = "bundles.json";
108115

116+
public const string UserSettingsFileName = "user_settings.json";
117+
109118
public const string FileTagSettingsFileName = "filetags.json";
110119
}
111120

Files/DataModels/SidebarPinnedModel.cs

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using Files.DataModels.NavigationControlItems;
44
using Files.Filesystem;
55
using Files.Helpers;
6+
using Files.Services;
67
using Files.UserControls;
78
using Files.ViewModels;
9+
using Microsoft.Toolkit.Mvvm.DependencyInjection;
810
using Microsoft.Toolkit.Uwp;
911
using Newtonsoft.Json;
1012
using System;
@@ -22,13 +24,12 @@ namespace Files.DataModels
2224
{
2325
public class SidebarPinnedModel
2426
{
27+
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>();
28+
2529
private SidebarPinnedController controller;
2630

2731
private LocationItem favoriteSection, homeSection;
2832

29-
[JsonIgnore]
30-
public SettingsViewModel AppSettings => App.AppSettings;
31-
3233
[JsonIgnore]
3334
public MainViewModel MainViewModel => App.MainViewModel;
3435

@@ -51,8 +52,8 @@ public void AddDefaultItems()
5152
{
5253
var udp = UserDataPaths.GetDefault();
5354

54-
FavoriteItems.Add(AppSettings.DesktopPath);
55-
FavoriteItems.Add(AppSettings.DownloadsPath);
55+
FavoriteItems.Add(CommonPaths.DesktopPath);
56+
FavoriteItems.Add(CommonPaths.DownloadsPath);
5657
FavoriteItems.Add(udp.Documents);
5758
}
5859

@@ -61,7 +62,7 @@ private void RemoveFavoritesSideBarSection()
6162
try
6263
{
6364
var item = (from n in SidebarControl.SideBarItems where n.Text.Equals("SidebarFavorites".GetLocalized()) select n).FirstOrDefault();
64-
if (!App.AppSettings.ShowFavoritesSection && item != null)
65+
if (!UserSettingsService.SidebarSettingsService.ShowFavoritesSection && item != null)
6566
{
6667
SidebarControl.SideBarItems.Remove(item);
6768
}
@@ -72,7 +73,7 @@ private void RemoveFavoritesSideBarSection()
7273

7374
public async void UpdateFavoritesSectionVisibility()
7475
{
75-
if (App.AppSettings.ShowFavoritesSection)
76+
if (UserSettingsService.SidebarSettingsService.ShowFavoritesSection)
7677
{
7778
await AddAllItemsToSidebar();
7879
}
@@ -113,11 +114,11 @@ public async Task ShowHideRecycleBinItemAsync(bool show)
113114
Text = ApplicationData.Current.LocalSettings.Values.Get("RecycleBin_Title", "Recycle Bin"),
114115
IsDefaultLocation = true,
115116
Icon = await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => UIHelpers.GetIconResource(Constants.ImageRes.RecycleBin)),
116-
Path = App.AppSettings.RecycleBinPath
117+
Path = CommonPaths.RecycleBinPath
117118
};
118119
// Add recycle bin to sidebar, title is read from LocalSettings (provided by the fulltrust process)
119120
// TODO: the very first time the app is launched localized name not available
120-
if (!favoriteSection.ChildItems.Any(x => x.Path == App.AppSettings.RecycleBinPath))
121+
if (!favoriteSection.ChildItems.Any(x => x.Path == CommonPaths.RecycleBinPath))
121122
{
122123
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Add(recycleBinItem));
123124
}
@@ -126,7 +127,7 @@ public async Task ShowHideRecycleBinItemAsync(bool show)
126127
{
127128
foreach (INavigationControlItem item in favoriteSection.ChildItems.ToList())
128129
{
129-
if (item is LocationItem && item.Path == App.AppSettings.RecycleBinPath)
130+
if (item is LocationItem && item.Path == CommonPaths.RecycleBinPath)
130131
{
131132
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(() => favoriteSection.ChildItems.Remove(item));
132133
}
@@ -251,7 +252,7 @@ public async Task AddItemToSidebarAsync(string path)
251252
var res = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path, item));
252253
if (res || (FilesystemResult)FolderHelpers.CheckFolderAccessWithWin32(path))
253254
{
254-
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(App.AppSettings.RecycleBinPath));
255+
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
255256
int insertIndex = lastItem != null ? favoriteSection.ChildItems.IndexOf(lastItem) + 1 : 0;
256257
var locationItem = new LocationItem
257258
{
@@ -299,7 +300,7 @@ public async Task AddItemToSidebarAsync(string path)
299300
/// <param name="section">The section.</param>
300301
private void AddItemToSidebarAsync(LocationItem section)
301302
{
302-
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(App.AppSettings.RecycleBinPath));
303+
var lastItem = favoriteSection.ChildItems.LastOrDefault(x => x.ItemType == NavigationControlItemType.Location && !x.Path.Equals(CommonPaths.RecycleBinPath));
303304
int insertIndex = lastItem != null ? favoriteSection.ChildItems.IndexOf(lastItem) + 1 : 0;
304305

305306
if (!favoriteSection.ChildItems.Contains(section))
@@ -313,7 +314,7 @@ private void AddItemToSidebarAsync(LocationItem section)
313314
/// </summary>
314315
public async Task AddAllItemsToSidebar()
315316
{
316-
if (!App.AppSettings.ShowFavoritesSection)
317+
if (!UserSettingsService.SidebarSettingsService.ShowFavoritesSection)
317318
{
318319
return;
319320
}
@@ -365,7 +366,7 @@ public async Task AddAllItemsToSidebar()
365366
await AddItemToSidebarAsync(path);
366367
}
367368

368-
await ShowHideRecycleBinItemAsync(App.AppSettings.PinRecycleBinToSideBar);
369+
await ShowHideRecycleBinItemAsync(UserSettingsService.SidebarSettingsService.PinRecycleBinToSidebar);
369370
}
370371

371372
/// <summary>

Files/Enums/SortOption.cs renamed to Files/Enums/SortOptions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ public enum GroupOption : byte
2626
OriginalFolder,
2727
DateDeleted
2828
}
29+
30+
public enum SortDirection : byte // We cannot use Microsoft.Toolkit.Uwp.UI.SortDirection since it's UI-tied and we need Model-tied
31+
{
32+
Ascending = 0,
33+
Descending = 1
34+
}
2935
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Files.EventArguments
4+
{
5+
public sealed class SettingChangedEventArgs : EventArgs
6+
{
7+
public readonly string settingName;
8+
9+
public readonly object newValue;
10+
11+
public SettingChangedEventArgs(string settingName, object newValue)
12+
{
13+
this.settingName = settingName;
14+
this.newValue = newValue;
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)