Improve WatchProperty

This commit is contained in:
tyrrrz 2026-04-03 00:13:40 +03:00
parent 1f189e5206
commit 295f4cf9a2
4 changed files with 11 additions and 17 deletions

View file

@ -56,11 +56,9 @@ public class App : Application, IDisposable
.GetRequiredService<SettingsService>()
.WatchProperty(
o => o.Theme,
() =>
v =>
{
RequestedThemeVariant = _services
.GetRequiredService<SettingsService>()
.Theme switch
RequestedThemeVariant = v switch
{
ThemeVariant.Light => Avalonia.Styling.ThemeVariant.Light,
ThemeVariant.Dark => Avalonia.Styling.ThemeVariant.Dark,

View file

@ -14,18 +14,12 @@ public partial class LocalizationManager : ObservableObject, IDisposable
public LocalizationManager(SettingsService settingsService)
{
_eventRoot.Add(
settingsService.WatchProperty(
o => o.Language,
() => Language = settingsService.Language,
true
)
);
_eventRoot.Add(settingsService.WatchProperty(o => o.Language, v => Language = v, true));
_eventRoot.Add(
this.WatchProperty(
o => o.Language,
() =>
_ =>
{
foreach (var propertyName in EnglishLocalization.Keys)
OnPropertyChanged(propertyName);

View file

@ -12,7 +12,7 @@ internal static class NotifyPropertyChangedExtensions
{
public IDisposable WatchProperty<TProperty>(
Expression<Func<TOwner, TProperty>> propertyExpression,
Action callback,
Action<TProperty> callback,
bool watchInitialValue = false
)
{
@ -20,6 +20,8 @@ internal static class NotifyPropertyChangedExtensions
if (memberExpression?.Member is not PropertyInfo property)
throw new ArgumentException("Provided expression must reference a property.");
var getValue = propertyExpression.Compile();
void OnPropertyChanged(object? sender, PropertyChangedEventArgs args)
{
if (
@ -27,14 +29,14 @@ internal static class NotifyPropertyChangedExtensions
|| string.Equals(args.PropertyName, property.Name, StringComparison.Ordinal)
)
{
callback();
callback(getValue(owner));
}
}
owner.PropertyChanged += OnPropertyChanged;
if (watchInitialValue)
callback();
callback(getValue(owner));
return Disposable.Create(() => owner.PropertyChanged -= OnPropertyChanged);
}

View file

@ -53,14 +53,14 @@ public partial class DashboardViewModel : ViewModelBase
_eventRoot.Add(
Progress.WatchProperty(
o => o.Current,
() => OnPropertyChanged(nameof(IsProgressIndeterminate))
_ => OnPropertyChanged(nameof(IsProgressIndeterminate))
)
);
_eventRoot.Add(
SelectedChannels.WatchProperty(
o => o.Count,
() => ExportCommand.NotifyCanExecuteChanged()
_ => ExportCommand.NotifyCanExecuteChanged()
)
);
}