Browse Source

Fix double relay property setting SettingsManager, docs

pull/165/head
Ionite 1 year ago
parent
commit
d342a69a15
No known key found for this signature in database
  1. 15
      StabilityMatrix.Core/Models/RelayPropertyChangedEventArgs.cs
  2. 34
      StabilityMatrix.Core/Services/ISettingsManager.cs
  3. 11
      StabilityMatrix.Core/Services/SettingsManager.cs

15
StabilityMatrix.Core/Models/RelayPropertyChangedEventArgs.cs

@ -0,0 +1,15 @@
using System.ComponentModel;
namespace StabilityMatrix.Core.Models;
public class RelayPropertyChangedEventArgs : PropertyChangedEventArgs
{
public bool IsRelay { get; }
/// <inheritdoc />
public RelayPropertyChangedEventArgs(string? propertyName, bool isRelay = false)
: base(propertyName)
{
IsRelay = isRelay;
}
}

34
StabilityMatrix.Core/Services/ISettingsManager.cs

@ -16,25 +16,47 @@ public interface ISettingsManager
DirectoryPath TagsDirectory { get; }
Settings Settings { get; }
/// <summary>
/// Event fired when the library directory is changed
/// </summary>
event EventHandler<string>? LibraryDirChanged;
event EventHandler<PropertyChangedEventArgs>? SettingsPropertyChanged;
/// <inheritdoc />
/// <summary>
/// Event fired when a property of Settings is changed
/// </summary>
event EventHandler<RelayPropertyChangedEventArgs>? SettingsPropertyChanged;
/// <summary>
/// Return a SettingsTransaction that can be used to modify Settings
/// Saves on Dispose.
/// </summary>
SettingsTransaction BeginTransaction();
/// <inheritdoc />
/// <summary>
/// Execute a function that modifies Settings
/// Commits changes after the function returns.
/// </summary>
/// <param name="func">Function accepting Settings to modify</param>
void Transaction(Action<Settings> func, bool ignoreMissingLibraryDir = false);
/// <inheritdoc />
/// <summary>
/// Modify a settings property by expression and commit changes.
/// This will notify listeners of SettingsPropertyChanged.
/// </summary>
void Transaction<TValue>(Expression<Func<Settings, TValue>> expression, TValue value);
/// <inheritdoc />
/// <summary>
/// Register a source observable object and property to be relayed to Settings
/// </summary>
void RelayPropertyFor<T, TValue>(
T source,
Expression<Func<T, TValue>> sourceProperty,
Expression<Func<Settings, TValue>> settingsProperty) where T : INotifyPropertyChanged;
/// <inheritdoc />
/// <summary>
/// Register an Action to be called on change of the settings property.
/// </summary>
void RegisterPropertyChangedHandler<T>(
Expression<Func<Settings, T>> settingsProperty,
Action<T> onPropertyChanged);

11
StabilityMatrix.Core/Services/SettingsManager.cs

@ -54,7 +54,7 @@ public class SettingsManager : ISettingsManager
public Settings Settings { get; private set; } = new();
public event EventHandler<string>? LibraryDirChanged;
public event EventHandler<PropertyChangedEventArgs>? SettingsPropertyChanged;
public event EventHandler<RelayPropertyChangedEventArgs>? SettingsPropertyChanged;
/// <inheritdoc />
public SettingsTransaction BeginTransaction()
@ -106,7 +106,7 @@ public class SettingsManager : ISettingsManager
propertyInfo.SetValue(transaction.Settings, value);
// Invoke property changed event
SettingsPropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
SettingsPropertyChanged?.Invoke(this, new RelayPropertyChangedEventArgs(name));
}
/// <inheritdoc />
@ -128,7 +128,8 @@ public class SettingsManager : ISettingsManager
// Update source when settings change
SettingsPropertyChanged += (_, args) =>
{
if (args.PropertyName != propertyName) return;
// Skip if event is relay, to avoid double setting
if (args.IsRelay || args.PropertyName != propertyName) return;
Logger.Trace(
"[RelayPropertyFor] " +
@ -149,10 +150,12 @@ public class SettingsManager : ISettingsManager
sourceTypeName, propertyName, targetPropertyName);
settingsSetter(Settings, sourceGetter(source));
// Save settings to file
SaveSettingsAsync().SafeFireAndForget();
// Invoke property changed event
SettingsPropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
SettingsPropertyChanged?.Invoke(this, new RelayPropertyChangedEventArgs(propertyName, true));
};
}

Loading…
Cancel
Save