Skip to content

Commit bcf36ed

Browse files
committed
Improve WatchProperty
1 parent d8c632d commit bcf36ed

File tree

6 files changed

+13
-19
lines changed

6 files changed

+13
-19
lines changed

YoutubeDownloader/App.axaml.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ public App()
6060
.GetRequiredService<SettingsService>()
6161
.WatchProperty(
6262
o => o.Theme,
63-
() =>
63+
theme =>
6464
{
65-
RequestedThemeVariant = _services
66-
.GetRequiredService<SettingsService>()
67-
.Theme switch
65+
RequestedThemeVariant = theme switch
6866
{
6967
ThemeVariant.Light => Avalonia.Styling.ThemeVariant.Light,
7068
ThemeVariant.Dark => Avalonia.Styling.ThemeVariant.Dark,

YoutubeDownloader/Localization/LocalizationManager.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@ public partial class LocalizationManager : ObservableObject, IDisposable
1414

1515
public LocalizationManager(SettingsService settingsService)
1616
{
17-
_eventRoot.Add(
18-
settingsService.WatchProperty(
19-
o => o.Language,
20-
() => Language = settingsService.Language,
21-
true
22-
)
23-
);
17+
_eventRoot.Add(settingsService.WatchProperty(o => o.Language, v => Language = v, true));
2418

2519
_eventRoot.Add(
2620
this.WatchProperty(
2721
o => o.Language,
28-
() =>
22+
_ =>
2923
{
3024
foreach (var propertyName in EnglishLocalization.Keys)
3125
OnPropertyChanged(propertyName);

YoutubeDownloader/Utils/Extensions/NotifyPropertyChangedExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,31 @@ internal static class NotifyPropertyChangedExtensions
1212
{
1313
public IDisposable WatchProperty<TProperty>(
1414
Expression<Func<TOwner, TProperty>> propertyExpression,
15-
Action callback,
15+
Action<TProperty> callback,
1616
bool watchInitialValue = false
1717
)
1818
{
1919
var memberExpression = propertyExpression.Body as MemberExpression;
2020
if (memberExpression?.Member is not PropertyInfo property)
2121
throw new ArgumentException("Provided expression must reference a property.");
2222

23+
var getValue = propertyExpression.Compile();
24+
2325
void OnPropertyChanged(object? sender, PropertyChangedEventArgs args)
2426
{
2527
if (
2628
string.IsNullOrWhiteSpace(args.PropertyName)
2729
|| string.Equals(args.PropertyName, property.Name, StringComparison.Ordinal)
2830
)
2931
{
30-
callback();
32+
callback(getValue(owner));
3133
}
3234
}
3335

3436
owner.PropertyChanged += OnPropertyChanged;
3537

3638
if (watchInitialValue)
37-
callback();
39+
callback(getValue(owner));
3840

3941
return Disposable.Create(() => owner.PropertyChanged -= OnPropertyChanged);
4042
}

YoutubeDownloader/ViewModels/Components/DashboardViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ SettingsService settingsService
5353
_eventRoot.Add(
5454
_settingsService.WatchProperty(
5555
o => o.ParallelLimit,
56-
() => _downloadSemaphore.MaxCount = _settingsService.ParallelLimit,
56+
v => _downloadSemaphore.MaxCount = v,
5757
true
5858
)
5959
);
6060

6161
_eventRoot.Add(
6262
Progress.WatchProperty(
6363
o => o.Current,
64-
() => OnPropertyChanged(nameof(IsProgressIndeterminate))
64+
_ => OnPropertyChanged(nameof(IsProgressIndeterminate))
6565
)
6666
);
6767
}

YoutubeDownloader/ViewModels/Components/DownloadViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ LocalizationManager localizationManager
3939
_eventRoot.Add(
4040
Progress.WatchProperty(
4141
o => o.Current,
42-
() => OnPropertyChanged(nameof(IsProgressIndeterminate))
42+
_ => OnPropertyChanged(nameof(IsProgressIndeterminate))
4343
)
4444
);
4545
}

YoutubeDownloader/ViewModels/Dialogs/AuthSetupViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SettingsService settingsService
2626
_eventRoot.Add(
2727
_settingsService.WatchProperty(
2828
o => o.LastAuthCookies,
29-
() =>
29+
_ =>
3030
{
3131
OnPropertyChanged(nameof(Cookies));
3232
OnPropertyChanged(nameof(IsAuthenticated));

0 commit comments

Comments
 (0)