Skip to content

Commit

Permalink
Merge pull request #358 from jetelain/settings-async
Browse files Browse the repository at this point in the history
Settings async
  • Loading branch information
tgjones committed Jul 30, 2023
2 parents 779c5dc + 9706108 commit 3099ce3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Gemini/Modules/Settings/ISettingsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Gemini.Modules.Settings
namespace Gemini.Modules.Settings
{
public interface ISettingsEditor
{
Expand All @@ -7,4 +7,4 @@ public interface ISettingsEditor

void ApplyChanges();
}
}
}
12 changes: 12 additions & 0 deletions src/Gemini/Modules/Settings/ISettingsEditorAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;

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

Task ApplyChangesAsync();
}
}
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,11 +7,13 @@ public class SettingsPageViewModel
public SettingsPageViewModel()
{
Children = new List<SettingsPageViewModel>();
Editors = new List<ISettingsEditor>();
Editors = new List<object>();
}

public string Name { get; set; }
public List<ISettingsEditor> Editors { get; private set; }
public List<SettingsPageViewModel> Children { get; internal set; }

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

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

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

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

_settingsEditors = IoC.GetAll<ISettingsEditorAsync>().Concat(IoC.GetAll<ISettingsEditor>().Select(e => new SettingsEditorWrapper(e)));

foreach (var settingsEditor in _settingsEditors)
{
Expand All @@ -50,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;
Expand All @@ -76,7 +73,7 @@ private static SettingsPageViewModel GetFirstLeafPageRecursive(List<SettingsPage
return GetFirstLeafPageRecursive(firstPage.Children);
}

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

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

Expand All @@ -106,7 +103,7 @@ public async Task SaveChanges()
{
foreach (var settingsEditor in _settingsEditors)
{
settingsEditor.ApplyChanges();
await settingsEditor.ApplyChangesAsync();
}

await TryCloseAsync(true);
Expand Down

0 comments on commit 3099ce3

Please sign in to comment.