From 6942bbb2109c7dbd5330d0bbecc8b1ea77d47ee9 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 24 Jul 2023 08:52:07 +0200 Subject: [PATCH 1/2] Support for ApplyChanges Async in settings editor, in a non breaking way --- src/Gemini/Modules/Settings/ISettingsEditor.cs | 9 +++------ .../Modules/Settings/ISettingsEditorAsync.cs | 9 +++++++++ src/Gemini/Modules/Settings/ISettingsEditorBase.cs | 8 ++++++++ .../Settings/ViewModels/SettingsPageViewModel.cs | 8 +++++--- .../Settings/ViewModels/SettingsViewModel.cs | 14 +++++++++++--- 5 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 src/Gemini/Modules/Settings/ISettingsEditorAsync.cs create mode 100644 src/Gemini/Modules/Settings/ISettingsEditorBase.cs diff --git a/src/Gemini/Modules/Settings/ISettingsEditor.cs b/src/Gemini/Modules/Settings/ISettingsEditor.cs index f6493fa46..dac643e8b 100644 --- a/src/Gemini/Modules/Settings/ISettingsEditor.cs +++ b/src/Gemini/Modules/Settings/ISettingsEditor.cs @@ -1,10 +1,7 @@ -namespace Gemini.Modules.Settings +namespace Gemini.Modules.Settings { - public interface ISettingsEditor + public interface ISettingsEditor : ISettingsEditorBase { - string SettingsPageName { get; } - string SettingsPagePath { get; } - void ApplyChanges(); } -} \ No newline at end of file +} diff --git a/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs b/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs new file mode 100644 index 000000000..1c3a9abd1 --- /dev/null +++ b/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Gemini.Modules.Settings +{ + public interface ISettingsEditorAsync : ISettingsEditorBase + { + Task ApplyChangesAsync(); + } +} diff --git a/src/Gemini/Modules/Settings/ISettingsEditorBase.cs b/src/Gemini/Modules/Settings/ISettingsEditorBase.cs new file mode 100644 index 000000000..07c8e2e3b --- /dev/null +++ b/src/Gemini/Modules/Settings/ISettingsEditorBase.cs @@ -0,0 +1,8 @@ +namespace Gemini.Modules.Settings +{ + public interface ISettingsEditorBase + { + string SettingsPageName { get; } + string SettingsPagePath { get; } + } +} diff --git a/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs b/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs index 4e7268cbc..84cf09242 100644 --- a/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs +++ b/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs @@ -7,11 +7,13 @@ public class SettingsPageViewModel public SettingsPageViewModel() { Children = new List(); - Editors = new List(); + Editors = new List(); } public string Name { get; set; } - public List Editors { get; private set; } - public List Children { get; internal set; } + + public List Editors { get; } + + public List Children { get; } } } diff --git a/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs b/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs index 2ec4c32a6..501d20333 100644 --- a/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs +++ b/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Windows.Input; using Caliburn.Micro; using Gemini.Framework; using Gemini.Properties; @@ -16,6 +15,7 @@ namespace Gemini.Modules.Settings.ViewModels public class SettingsViewModel : WindowBase { private IEnumerable _settingsEditors; + private IEnumerable _settingsEditorsAsync; private SettingsPageViewModel _selectedPage; public SettingsViewModel() @@ -41,8 +41,11 @@ protected override async Task OnInitializeAsync(CancellationToken cancellationTo var pages = new List(); _settingsEditors = IoC.GetAll(); + _settingsEditorsAsync = IoC.GetAll(); - foreach (var settingsEditor in _settingsEditors) + var allSettingsEditor = _settingsEditors.Cast().Concat(_settingsEditorsAsync); + + foreach (var settingsEditor in allSettingsEditor) { var parentCollection = GetParentCollection(settingsEditor, pages); @@ -76,7 +79,7 @@ private static SettingsPageViewModel GetFirstLeafPageRecursive(List GetParentCollection(ISettingsEditor settingsEditor, + private List GetParentCollection(ISettingsEditorBase settingsEditor, List pages) { if (string.IsNullOrEmpty(settingsEditor.SettingsPagePath)) @@ -104,6 +107,11 @@ private List GetParentCollection(ISettingsEditor settings public async Task SaveChanges() { + foreach (var settingsEditor in _settingsEditorsAsync) + { + await settingsEditor.ApplyChangesAsync(); + } + foreach (var settingsEditor in _settingsEditors) { settingsEditor.ApplyChanges(); From 9706108d27f168b3678d9b8e5eec3e3e34d5535a Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 24 Jul 2023 09:08:54 +0200 Subject: [PATCH 2/2] ISettingsEditorBase would be a binary breaking change for ISettingsEditor implementations --- .../Modules/Settings/ISettingsEditor.cs | 5 +++- .../Modules/Settings/ISettingsEditorAsync.cs | 5 +++- .../Modules/Settings/ISettingsEditorBase.cs | 8 ------ .../ViewModels/SettingsEditorWrapper.cs | 26 ++++++++++++++++++ .../ViewModels/SettingsPageViewModel.cs | 4 +-- .../Settings/ViewModels/SettingsViewModel.cs | 27 ++++++------------- 6 files changed, 44 insertions(+), 31 deletions(-) delete mode 100644 src/Gemini/Modules/Settings/ISettingsEditorBase.cs create mode 100644 src/Gemini/Modules/Settings/ViewModels/SettingsEditorWrapper.cs diff --git a/src/Gemini/Modules/Settings/ISettingsEditor.cs b/src/Gemini/Modules/Settings/ISettingsEditor.cs index dac643e8b..e758a882f 100644 --- a/src/Gemini/Modules/Settings/ISettingsEditor.cs +++ b/src/Gemini/Modules/Settings/ISettingsEditor.cs @@ -1,7 +1,10 @@ namespace Gemini.Modules.Settings { - public interface ISettingsEditor : ISettingsEditorBase + public interface ISettingsEditor { + string SettingsPageName { get; } + string SettingsPagePath { get; } + void ApplyChanges(); } } diff --git a/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs b/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs index 1c3a9abd1..9e20162ab 100644 --- a/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs +++ b/src/Gemini/Modules/Settings/ISettingsEditorAsync.cs @@ -2,8 +2,11 @@ namespace Gemini.Modules.Settings { - public interface ISettingsEditorAsync : ISettingsEditorBase + public interface ISettingsEditorAsync { + string SettingsPageName { get; } + string SettingsPagePath { get; } + Task ApplyChangesAsync(); } } diff --git a/src/Gemini/Modules/Settings/ISettingsEditorBase.cs b/src/Gemini/Modules/Settings/ISettingsEditorBase.cs deleted file mode 100644 index 07c8e2e3b..000000000 --- a/src/Gemini/Modules/Settings/ISettingsEditorBase.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Gemini.Modules.Settings -{ - public interface ISettingsEditorBase - { - string SettingsPageName { get; } - string SettingsPagePath { get; } - } -} diff --git a/src/Gemini/Modules/Settings/ViewModels/SettingsEditorWrapper.cs b/src/Gemini/Modules/Settings/ViewModels/SettingsEditorWrapper.cs new file mode 100644 index 000000000..a493b5f5e --- /dev/null +++ b/src/Gemini/Modules/Settings/ViewModels/SettingsEditorWrapper.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; + +namespace Gemini.Modules.Settings.ViewModels +{ + internal sealed class SettingsEditorWrapper : ISettingsEditorAsync + { + private readonly ISettingsEditor _editor; + + public SettingsEditorWrapper(ISettingsEditor editor) + { + _editor = editor; + } + + public string SettingsPageName => _editor.SettingsPageName; + + public string SettingsPagePath => _editor.SettingsPagePath; + + public ISettingsEditor ViewModel => _editor; + + public Task ApplyChangesAsync() + { + _editor.ApplyChanges(); + return Task.CompletedTask; + } + } +} diff --git a/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs b/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs index 84cf09242..1ab2728bc 100644 --- a/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs +++ b/src/Gemini/Modules/Settings/ViewModels/SettingsPageViewModel.cs @@ -7,12 +7,12 @@ public class SettingsPageViewModel public SettingsPageViewModel() { Children = new List(); - Editors = new List(); + Editors = new List(); } public string Name { get; set; } - public List Editors { get; } + public List Editors { get; } // ISettingsEditor or ISettingsEditorAsync public List Children { get; } } diff --git a/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs b/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs index 501d20333..a4288ab55 100644 --- a/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs +++ b/src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs @@ -14,8 +14,7 @@ namespace Gemini.Modules.Settings.ViewModels [PartCreationPolicy (CreationPolicy.NonShared)] public class SettingsViewModel : WindowBase { - private IEnumerable _settingsEditors; - private IEnumerable _settingsEditorsAsync; + private IEnumerable _settingsEditors; private SettingsPageViewModel _selectedPage; public SettingsViewModel() @@ -40,12 +39,10 @@ protected override async Task OnInitializeAsync(CancellationToken cancellationTo await base.OnInitializeAsync(cancellationToken); var pages = new List(); - _settingsEditors = IoC.GetAll(); - _settingsEditorsAsync = IoC.GetAll(); - var allSettingsEditor = _settingsEditors.Cast().Concat(_settingsEditorsAsync); + _settingsEditors = IoC.GetAll().Concat(IoC.GetAll().Select(e => new SettingsEditorWrapper(e))); - foreach (var settingsEditor in allSettingsEditor) + foreach (var settingsEditor in _settingsEditors) { var parentCollection = GetParentCollection(settingsEditor, pages); @@ -53,14 +50,11 @@ protected override async Task OnInitializeAsync(CancellationToken cancellationTo if (page == null) { - page = new SettingsPageViewModel - { - Name = settingsEditor.SettingsPageName, - }; + page = new SettingsPageViewModel { Name = settingsEditor.SettingsPageName }; parentCollection.Add(page); } - page.Editors.Add(settingsEditor); + page.Editors.Add(settingsEditor is SettingsEditorWrapper wrapper ? (object)wrapper.ViewModel : (object)settingsEditor); } Pages = pages; @@ -79,7 +73,7 @@ private static SettingsPageViewModel GetFirstLeafPageRecursive(List GetParentCollection(ISettingsEditorBase settingsEditor, + private List GetParentCollection(ISettingsEditorAsync settingsEditor, List pages) { if (string.IsNullOrEmpty(settingsEditor.SettingsPagePath)) @@ -95,7 +89,7 @@ private List GetParentCollection(ISettingsEditorBase sett if (page == null) { - page = new SettingsPageViewModel {Name = pathElement}; + page = new SettingsPageViewModel { Name = pathElement }; pages.Add(page); } @@ -107,14 +101,9 @@ private List GetParentCollection(ISettingsEditorBase sett public async Task SaveChanges() { - foreach (var settingsEditor in _settingsEditorsAsync) - { - await settingsEditor.ApplyChangesAsync(); - } - foreach (var settingsEditor in _settingsEditors) { - settingsEditor.ApplyChanges(); + await settingsEditor.ApplyChangesAsync(); } await TryCloseAsync(true);