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

[UI] Fix settings observability #12783

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System.ComponentModel;
using WalletWasabi.Fluent.ViewModels.SearchBar.Settings;
using WalletWasabi.Fluent.ViewModels.SearchBar.Sources;

namespace WalletWasabi.Fluent.ViewModels.SearchBar.SearchItems;

public class ContentSearchItemNode
{
public static SearchItemNode<TObject, TProperty> Create<TObject, TProperty>(IEditableSearchSource searchSource, Setting<TObject, TProperty> setting, string name, string category, bool isDefault, IEnumerable<string> keywords, string? icon, int priority, bool isEnabled, params NestedItemConfiguration<TProperty>[] nestedItemConfiguration)
public static SearchItemNode<TObject, TProperty> Create<TObject, TProperty>(IEditableSearchSource searchSource, Setting<TObject, TProperty> setting, string name, string category, bool isDefault, IEnumerable<string> keywords, string? icon, int priority, bool isEnabled, params NestedItemConfiguration<TProperty>[] nestedItemConfiguration) where TObject : class, INotifyPropertyChanged
{
return new SearchItemNode<TObject, TProperty>(searchSource, setting, name, category, keywords, icon, isDefault, isEnabled, nestedItemConfiguration) { Priority = priority };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive.Linq;
using ReactiveUI;
using WalletWasabi.Fluent.ViewModels.SearchBar.Patterns;
Expand All @@ -7,7 +8,7 @@

namespace WalletWasabi.Fluent.ViewModels.SearchBar.SearchItems;

public class SearchItemNode<TObject, TProperty> : ReactiveObject, IContentSearchItem
public class SearchItemNode<TObject, TProperty> : ReactiveObject, IContentSearchItem where TObject : class, INotifyPropertyChanged
{
private readonly IEditableSearchSource _editableSearchSource;
private readonly NestedItemConfiguration<TProperty>[] _nestedItems;
Expand Down

This file was deleted.

44 changes: 14 additions & 30 deletions WalletWasabi.Fluent/ViewModels/SearchBar/Settings/Setting.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
using System.Diagnostics.CodeAnalysis;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Input;
using ReactiveUI;
using WalletWasabi.Fluent.Extensions;

namespace WalletWasabi.Fluent.ViewModels.SearchBar.Settings;

public partial class Setting<TTarget, TProperty> : ReactiveObject
public class Setting<TOwner, TProperty> : ReactiveObject, IDisposable where TOwner : class, INotifyPropertyChanged
{
[AutoNotify] private TProperty? _value;
private readonly CompositeDisposable _disposable = new();
private TProperty _value = default!;

public Setting([DisallowNull] TTarget target, Expression<Func<TTarget, TProperty>> selector)
public Setting(TOwner owner, Expression<Func<TOwner, TProperty?>> propertySelector)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}

if (selector == null)
{
throw new ArgumentNullException(nameof(selector));
}

if (PropertyHelper<TTarget>.GetProperty(selector) is not { } pr)
{
throw new InvalidOperationException($"The expression {selector} is not a valid property selector");
}

Value = (TProperty?)pr.GetValue(target);

SetValueCommand = ReactiveCommand.Create(() => pr.SetValue(target, Value));
owner.WhenAnyValue(propertySelector).BindTo(this, value => value.Value).DisposeWith(_disposable);
this.WhenAnyValue(x => x.Value).Skip(1).BindTo(owner, propertySelector).DisposeWith(_disposable);
}

this.WhenAnyValue(x => x.Value)
.ObserveOn(RxApp.MainThreadScheduler)
.Skip(1)
.ToSignal()
.InvokeCommand(SetValueCommand);
public TProperty Value
{
get => _value;
set => this.RaiseAndSetIfChanged(ref _value, value);
}

public ICommand SetValueCommand { get; }
public void Dispose() => _disposable.Dispose();
}
soosr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private IEnumerable<ISearchItem> GetSettingsItems()
}));
}

private Setting<IApplicationSettings, TProperty> Setting<TProperty>(Expression<Func<IApplicationSettings, TProperty>> selector)
private Setting<ApplicationSettings, TProperty> Setting<TProperty>(Expression<Func<ApplicationSettings, TProperty>> selector)
{
return new Setting<IApplicationSettings, TProperty>(_applicationSettings, selector);
return new Setting<ApplicationSettings, TProperty>((ApplicationSettings)_applicationSettings, selector);
}
}