Skip to content

Commit

Permalink
ISettingsEditorBase would be a binary breaking change for ISettingsEd…
Browse files Browse the repository at this point in the history
…itor implementations
  • Loading branch information
jetelain committed Jul 24, 2023
1 parent 6942bbb commit 9706108
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/Gemini/Modules/Settings/ISettingsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace Gemini.Modules.Settings
{
public interface ISettingsEditor : ISettingsEditorBase
public interface ISettingsEditor
{
string SettingsPageName { get; }
string SettingsPagePath { get; }

void ApplyChanges();
}
}
5 changes: 4 additions & 1 deletion src/Gemini/Modules/Settings/ISettingsEditorAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Gemini.Modules.Settings
{
public interface ISettingsEditorAsync : ISettingsEditorBase
public interface ISettingsEditorAsync
{
string SettingsPageName { get; }
string SettingsPagePath { get; }

Task ApplyChangesAsync();
}
}
8 changes: 0 additions & 8 deletions src/Gemini/Modules/Settings/ISettingsEditorBase.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/Gemini/Modules/Settings/ViewModels/SettingsEditorWrapper.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class SettingsPageViewModel
public SettingsPageViewModel()
{
Children = new List<SettingsPageViewModel>();
Editors = new List<ISettingsEditorBase>();
Editors = new List<object>();
}

public string Name { get; set; }

public List<ISettingsEditorBase> Editors { get; }
public List<object> Editors { get; } // ISettingsEditor or ISettingsEditorAsync

public List<SettingsPageViewModel> Children { get; }
}
Expand Down
27 changes: 8 additions & 19 deletions src/Gemini/Modules/Settings/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ namespace Gemini.Modules.Settings.ViewModels
[PartCreationPolicy (CreationPolicy.NonShared)]
public class SettingsViewModel : WindowBase
{
private IEnumerable<ISettingsEditor> _settingsEditors;
private IEnumerable<ISettingsEditorAsync> _settingsEditorsAsync;
private IEnumerable<ISettingsEditorAsync> _settingsEditors;
private SettingsPageViewModel _selectedPage;

public SettingsViewModel()
Expand All @@ -40,27 +39,22 @@ protected override async Task OnInitializeAsync(CancellationToken cancellationTo
await base.OnInitializeAsync(cancellationToken);

var pages = new List<SettingsPageViewModel>();
_settingsEditors = IoC.GetAll<ISettingsEditor>();
_settingsEditorsAsync = IoC.GetAll<ISettingsEditorAsync>();

var allSettingsEditor = _settingsEditors.Cast<ISettingsEditorBase>().Concat(_settingsEditorsAsync);
_settingsEditors = IoC.GetAll<ISettingsEditorAsync>().Concat(IoC.GetAll<ISettingsEditor>().Select(e => new SettingsEditorWrapper(e)));

foreach (var settingsEditor in allSettingsEditor)
foreach (var settingsEditor in _settingsEditors)
{
var parentCollection = GetParentCollection(settingsEditor, pages);

var page = parentCollection.FirstOrDefault(m => m.Name == settingsEditor.SettingsPageName);

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;
Expand All @@ -79,7 +73,7 @@ private static SettingsPageViewModel GetFirstLeafPageRecursive(List<SettingsPage
return GetFirstLeafPageRecursive(firstPage.Children);
}

private List<SettingsPageViewModel> GetParentCollection(ISettingsEditorBase settingsEditor,
private List<SettingsPageViewModel> GetParentCollection(ISettingsEditorAsync settingsEditor,
List<SettingsPageViewModel> pages)
{
if (string.IsNullOrEmpty(settingsEditor.SettingsPagePath))
Expand All @@ -95,7 +89,7 @@ private List<SettingsPageViewModel> GetParentCollection(ISettingsEditorBase sett

if (page == null)
{
page = new SettingsPageViewModel {Name = pathElement};
page = new SettingsPageViewModel { Name = pathElement };
pages.Add(page);
}

Expand All @@ -107,14 +101,9 @@ private List<SettingsPageViewModel> 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);
Expand Down

0 comments on commit 9706108

Please sign in to comment.