From 295f4cf9a28c83385b8b6c9a332246c67729d016 Mon Sep 17 00:00:00 2001 From: tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Fri, 3 Apr 2026 00:13:40 +0300 Subject: [PATCH] Improve `WatchProperty` --- DiscordChatExporter.Gui/App.axaml.cs | 6 ++---- .../Localization/LocalizationManager.cs | 10 ++-------- .../Extensions/NotifyPropertyChangedExtensions.cs | 8 +++++--- .../ViewModels/Components/DashboardViewModel.cs | 4 ++-- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/DiscordChatExporter.Gui/App.axaml.cs b/DiscordChatExporter.Gui/App.axaml.cs index f695c404..b4f624d3 100644 --- a/DiscordChatExporter.Gui/App.axaml.cs +++ b/DiscordChatExporter.Gui/App.axaml.cs @@ -56,11 +56,9 @@ public class App : Application, IDisposable .GetRequiredService() .WatchProperty( o => o.Theme, - () => + v => { - RequestedThemeVariant = _services - .GetRequiredService() - .Theme switch + RequestedThemeVariant = v switch { ThemeVariant.Light => Avalonia.Styling.ThemeVariant.Light, ThemeVariant.Dark => Avalonia.Styling.ThemeVariant.Dark, diff --git a/DiscordChatExporter.Gui/Localization/LocalizationManager.cs b/DiscordChatExporter.Gui/Localization/LocalizationManager.cs index 71974f21..de917565 100644 --- a/DiscordChatExporter.Gui/Localization/LocalizationManager.cs +++ b/DiscordChatExporter.Gui/Localization/LocalizationManager.cs @@ -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); diff --git a/DiscordChatExporter.Gui/Utils/Extensions/NotifyPropertyChangedExtensions.cs b/DiscordChatExporter.Gui/Utils/Extensions/NotifyPropertyChangedExtensions.cs index 027bccef..eef1583f 100644 --- a/DiscordChatExporter.Gui/Utils/Extensions/NotifyPropertyChangedExtensions.cs +++ b/DiscordChatExporter.Gui/Utils/Extensions/NotifyPropertyChangedExtensions.cs @@ -12,7 +12,7 @@ internal static class NotifyPropertyChangedExtensions { public IDisposable WatchProperty( Expression> propertyExpression, - Action callback, + Action 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); } diff --git a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs index ad6411ae..dd7a8454 100644 --- a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs @@ -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() ) ); }