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>() .GetRequiredService<SettingsService>()
.WatchProperty( .WatchProperty(
o => o.Theme, o => o.Theme,
() => v =>
{ {
RequestedThemeVariant = _services RequestedThemeVariant = v switch
.GetRequiredService<SettingsService>()
.Theme switch
{ {
ThemeVariant.Light => Avalonia.Styling.ThemeVariant.Light, ThemeVariant.Light => Avalonia.Styling.ThemeVariant.Light,
ThemeVariant.Dark => Avalonia.Styling.ThemeVariant.Dark, ThemeVariant.Dark => Avalonia.Styling.ThemeVariant.Dark,

View file

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

View file

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

View file

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