Skip to content

Commit 873e9c3

Browse files
authored
refactor: consolidate duplicated reactive plumbing (#4418)
## What kind of change does this PR introduce? Refactor. ## What is the new behavior? Reactive notification initialization, view-contract streams, WinUI host subscriptions, command-binding validation, and observable-property expression validation now reuse shared helpers. Runtime behavior and the public API remain unchanged. ## What is the current behavior? Equivalent implementation blocks are repeated across reactive objects, WPF and WinUI hosts, command-binding overloads, and observable-property helpers. SonarCloud reports those blocks as duplicated new code. ## What might this PR break? None expected. The shared source remains linked into the same WPF and WinUI target-framework paths. ## Checklist - [x] I have read the [Contribute guide](https://www.reactiveui.net/contribute/index.html) - [ ] Tests have been added or updated (for bug fixes / features) - [ ] Docs have been added or updated (for bug fixes / features) - [x] Changes target the `main` branch - [x] PR title follows [Conventional Commits](https://www.conventionalcommits.org/) ## Additional information Verification: - `dotnet build reactiveui.slnx -c Release --no-restore` (0 warnings, 0 errors) - `dotnet test --solution reactiveui.slnx -c Release --no-build` (33,404 passed, 0 failed, 0 skipped)
1 parent 180a40b commit 873e9c3

22 files changed

Lines changed: 821 additions & 479 deletions

File tree

‎src/ReactiveUI.Maui/Common/RoutedViewHost.cs‎

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
#if WINUI_TARGET
77
using System.Diagnostics.CodeAnalysis;
88
using Microsoft.UI.Xaml;
9-
using ReactiveUI.Internal;
109
#if REACTIVE_SHIM
1110
using ReactiveUI.Reactive.Maui.Internal;
1211
#else
1312
using ReactiveUI.Maui.Internal;
1413
#endif
15-
using ReactiveUI.Primitives;
1614
using Splat;
1715

1816
#if REACTIVE_SHIM
@@ -58,78 +56,14 @@ public RoutedViewHost()
5856
HorizontalContentAlignment = HorizontalAlignment.Stretch;
5957
VerticalContentAlignment = VerticalAlignment.Stretch;
6058

61-
var platform = AppLocator.Current.GetService<IPlatformOperations>();
62-
Func<string?> platformGetter = static () => default;
63-
64-
if (platform is null)
65-
{
66-
// NB: This used to be an error but WPF design mode can't read
67-
// good or do other stuff good.
68-
this.Log().Error(
69-
"Couldn't find an IPlatformOperations implementation. Please make sure you have installed the latest "
70-
+ "version of the ReactiveUI packages for your platform. See https://reactiveui.net/docs/getting-started/installation for guidance.");
71-
}
72-
else
73-
{
74-
platformGetter = platform.GetOrientation;
75-
}
76-
77-
ViewContractObservable = ModeDetector.InUnitTestRunner()
78-
? Signal.Silent<string>()
79-
80-
// Replaces FromEvent(SizeChanged).StartWith(platformGetter()).DistinctUntilChanged().
81-
: new StartWithObservable<string?>(
82-
new FromEventObservable<string?>(onNext =>
83-
{
84-
SizeChangedEventHandler handler = (_, _) => onNext(platformGetter());
85-
SizeChanged += handler;
86-
return new ActionDisposable(() => SizeChanged -= handler);
87-
}),
88-
platformGetter())
89-
.DistinctUntilChanged();
90-
91-
// Observe Router property changes using DependencyProperty (AOT-friendly)
92-
var routerChanged = MauiReactiveHelpers.CreatePropertyValueObservable(
93-
this,
94-
nameof(Router),
95-
RouterProperty,
96-
() => Router);
97-
98-
// Observe ViewContractObservable property changes using DependencyProperty (AOT-friendly)
99-
var viewContractObservableChanged = MauiReactiveHelpers.CreatePropertyValueObservable(
100-
this,
101-
nameof(ViewContractObservable),
102-
ViewContractObservableProperty,
103-
() => ViewContractObservable);
104-
105-
// Observe current view model from router. Replaces Where(...).SelectMany(r => r.CurrentViewModel).StartWith(null).
106-
var currentViewModel = new StartWithObservable<IRoutableViewModel?>(
107-
new KeepSignal<RoutingState?>(routerChanged, static router => router is not null)
108-
.SelectMany(static router => router!.CurrentViewModel),
109-
null);
110-
111-
// Flatten the ViewContractObservable observable-of-observable.
112-
// Replaces SelectMany(x => x ?? Return(null)).Do(x => _viewContract = x).StartWith(ViewContract).
113-
var viewContract = new StartWithObservable<string?>(
114-
viewContractObservableChanged
115-
.SelectMany(static x => x ?? Signal.Emit<string?>(null))
116-
.Do(x => _viewContract = x),
117-
ViewContract);
118-
119-
var viewModelAndContract = currentViewModel
120-
.CombineLatest(
121-
viewContract,
122-
static (viewModel, contract) => (viewModel, contract));
123-
124-
// Subscribe directly without WhenActivated
125-
// NB: The DistinctUntilChanged is useful because most views in
126-
// WinRT will end up getting here twice - once for configuring
127-
// the RoutedViewHost's ViewModel, and once on load via SizeChanged
128-
_ = viewModelAndContract.DistinctUntilChanged()
129-
.Subscribe(new DelegateObserver<(IRoutableViewModel? viewModel, string? contract)>(
130-
ResolveViewForViewModel,
131-
RxState.DefaultExceptionHandler.OnNext))
132-
.DisposeWith(_subscriptions);
59+
MauiReactiveHelpers.InitializeRoutedViewHost(
60+
(this, this.Log(), observable => ViewContractObservable = observable),
61+
(nameof(Router), RouterProperty, () => Router),
62+
(nameof(ViewContractObservable), ViewContractObservableProperty, () => ViewContractObservable),
63+
() => ViewContract,
64+
contract => _viewContract = contract,
65+
ResolveViewForViewModel,
66+
_subscriptions);
13367
}
13468

13569
/// <summary>Gets or sets the <see cref="RoutingState"/> of the view model stack.</summary>

‎src/ReactiveUI.Maui/Common/RoutedViewHost{TViewModel}.cs‎

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
#if WINUI_TARGET
77
using System.Diagnostics.CodeAnalysis;
88
using Microsoft.UI.Xaml;
9-
using ReactiveUI.Internal;
109
#if REACTIVE_SHIM
1110
using ReactiveUI.Reactive.Maui.Internal;
1211
#else
1312
using ReactiveUI.Maui.Internal;
1413
#endif
15-
using ReactiveUI.Primitives;
1614
using Splat;
1715

1816
#if REACTIVE_SHIM
@@ -60,80 +58,22 @@ public RoutedViewHost()
6058
HorizontalContentAlignment = HorizontalAlignment.Stretch;
6159
VerticalContentAlignment = VerticalAlignment.Stretch;
6260

63-
var platform = AppLocator.Current.GetService<IPlatformOperations>();
64-
Func<string?> platformGetter = static () => default;
65-
66-
if (platform is null)
67-
{
68-
// NB: This used to be an error but WPF design mode can't read
69-
// good or do other stuff good.
70-
this.Log().Error(
71-
"Couldn't find an IPlatformOperations implementation. Please make sure you have installed the latest "
72-
+ "version of the ReactiveUI packages for your platform. See https://reactiveui.net/docs/getting-started/installation for guidance.");
73-
}
74-
else
75-
{
76-
platformGetter = platform.GetOrientation;
77-
}
78-
79-
ViewContractObservable = ModeDetector.InUnitTestRunner()
80-
? Signal.Silent<string>()
81-
82-
// Replaces FromEvent(SizeChanged).StartWith(platformGetter()).DistinctUntilChanged().
83-
: new StartWithObservable<string?>(
84-
new FromEventObservable<string?>(onNext =>
85-
{
86-
SizeChangedEventHandler handler = (_, _) => onNext(platformGetter());
87-
SizeChanged += handler;
88-
return new ActionDisposable(() => SizeChanged -= handler);
89-
}),
90-
platformGetter())
91-
.DistinctUntilChanged();
92-
93-
// Observe Router property changes using DependencyProperty (AOT-friendly)
94-
var routerChanged = MauiReactiveHelpers.CreatePropertyValueObservable(
95-
this,
96-
nameof(Router),
97-
RouterProperty,
98-
() => Router);
99-
100-
// Observe ViewContractObservable property changes using DependencyProperty (AOT-friendly)
101-
var viewContractObservableChanged = MauiReactiveHelpers.CreatePropertyValueObservable(
102-
this,
103-
nameof(ViewContractObservable),
104-
ViewContractObservableProperty,
105-
() => ViewContractObservable);
106-
107-
// Observe current view model from router. Replaces Where(...).SelectMany(r => r.CurrentViewModel).StartWith(null).
108-
var currentViewModel = new StartWithObservable<IRoutableViewModel?>(
109-
new KeepSignal<RoutingState?>(routerChanged, static router => router is not null)
110-
.SelectMany(static router => router!.CurrentViewModel),
111-
null);
112-
113-
// Flatten the ViewContractObservable observable-of-observable.
114-
// Replaces SelectMany(x => x ?? Return(null)).Do(x => _viewContract = x).StartWith(ViewContract).
115-
var viewContract = new StartWithObservable<string?>(
116-
viewContractObservableChanged
117-
.SelectMany(static x => x ?? Signal.Emit<string?>(null))
118-
.Do(x => _viewContract = x),
119-
ViewContract);
120-
121-
var viewModelAndContract = currentViewModel
122-
.CombineLatest(
123-
viewContract,
124-
static (viewModel, contract) => (viewModel, contract));
125-
126-
// Subscribe directly without WhenActivated
127-
// NB: The DistinctUntilChanged is useful because most views in
128-
// WinRT will end up getting here twice - once for configuring
129-
// the RoutedViewHost's ViewModel, and once on load via SizeChanged
130-
_ = viewModelAndContract.DistinctUntilChanged()
131-
.Subscribe(new DelegateObserver<(IRoutableViewModel? viewModel, string? contract)>(
132-
ResolveViewForViewModel,
133-
RxState.DefaultExceptionHandler.OnNext))
134-
.DisposeWith(_subscriptions);
61+
MauiReactiveHelpers.InitializeRoutedViewHost(
62+
(this, this.Log(), observable => ViewContractObservable = observable),
63+
(nameof(Router), RouterProperty, () => Router),
64+
(nameof(ViewContractObservable), ViewContractObservableProperty, () => ViewContractObservable),
65+
() => ViewContract,
66+
contract => _viewContract = contract,
67+
ResolveViewForViewModel,
68+
_subscriptions);
13569
}
13670

71+
/// <summary>Gets or sets the view locator.</summary>
72+
/// <value>
73+
/// The view locator.
74+
/// </value>
75+
public IViewLocator? ViewLocator { get; set; }
76+
13777
/// <summary>Gets or sets the <see cref="RoutingState"/> of the view model stack.</summary>
13878
public RoutingState Router
13979
{
@@ -169,12 +109,6 @@ public string? ViewContract
169109
}
170110
}
171111

172-
/// <summary>Gets or sets the view locator.</summary>
173-
/// <value>
174-
/// The view locator.
175-
/// </value>
176-
public IViewLocator? ViewLocator { get; set; }
177-
178112
/// <summary>
179113
/// Resolves and displays the view for the given view model and contract.
180114
/// This method uses the generic ViewLocator.ResolveView{TViewModel} which is AOT-safe.

‎src/ReactiveUI.Maui/Common/ViewModelViewHost.cs‎

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55

66
using System.Diagnostics.CodeAnalysis;
77
using Microsoft.UI.Xaml;
8-
using ReactiveUI.Internal;
98
#if REACTIVE_SHIM
109
using ReactiveUI.Reactive.Maui.Internal;
1110
#else
1211
using ReactiveUI.Maui.Internal;
1312
#endif
14-
using ReactiveUI.Primitives;
1513
using Splat;
1614

1715
#if REACTIVE_SHIM
@@ -58,58 +56,12 @@ public partial class ViewModelViewHost : TransitioningContentControl, IViewFor,
5856
Justification = "The single-threaded UI control hands 'this' to MauiReactiveHelpers to observe its own dependency-property changes; it is never published to another thread.")]
5957
public ViewModelViewHost()
6058
{
61-
var platform = AppLocator.Current.GetService<IPlatformOperations>();
62-
Func<string?> platformGetter = static () => default;
63-
64-
if (platform is null)
65-
{
66-
// NB: This used to be an error but WPF design mode can't read
67-
// good or do other stuff good.
68-
this.Log().Error(
69-
"Couldn't find an IPlatformOperations implementation. Please make sure you have installed the latest "
70-
+ "version of the ReactiveUI packages for your platform. See https://reactiveui.net/docs/getting-started/installation for guidance.");
71-
}
72-
else
73-
{
74-
platformGetter = platform.GetOrientation;
75-
}
76-
77-
ViewContractObservable = ModeDetector.InUnitTestRunner()
78-
? Signal.Silent<string>()
79-
80-
// Replaces FromEvent(SizeChanged).StartWith(platformGetter()).DistinctUntilChanged().
81-
: new StartWithObservable<string?>(
82-
new FromEventObservable<string?>(onNext =>
83-
{
84-
SizeChangedEventHandler handler = (_, _) => onNext(platformGetter());
85-
SizeChanged += handler;
86-
return new ActionDisposable(() => SizeChanged -= handler);
87-
}),
88-
platformGetter())
89-
.DistinctUntilChanged();
90-
91-
// Observe ViewModel property changes without expression trees (AOT-friendly)
92-
var viewModelChanged = MauiReactiveHelpers.CreatePropertyValueObservable(
93-
this,
94-
nameof(ViewModel),
95-
ViewModelProperty,
96-
() => ViewModel);
97-
98-
// Combine contract observable (recording the latest contract) with ViewModel changes.
99-
var viewModelAndContract = ViewContractObservable.Do(x => _viewContract = x)
100-
.CombineLatest(
101-
viewModelChanged,
102-
static (contract, vm) => (vm, contract));
103-
104-
// Subscribe directly without WhenActivated
105-
_ = new ObserveOnObservable<string?>(ViewContractObservable, RxSchedulers.MainThreadScheduler)
106-
.Subscribe(new DelegateObserver<string?>(x => _viewContract = x ?? string.Empty))
107-
.DisposeWith(_subscriptions);
108-
109-
_ = viewModelAndContract.DistinctUntilChanged()
110-
.Subscribe(new DelegateObserver<(object? ViewModel, string? Contract)>(
111-
x => ResolveViewForViewModel(x.ViewModel, x.Contract)))
112-
.DisposeWith(_subscriptions);
59+
MauiReactiveHelpers.InitializeViewModelViewHost(
60+
(this, this.Log(), observable => ViewContractObservable = observable),
61+
(nameof(ViewModel), ViewModelProperty, () => ViewModel),
62+
contract => _viewContract = contract,
63+
ResolveViewForViewModel,
64+
_subscriptions);
11365
}
11466

11567
/// <summary>Gets or sets the view contract observable.</summary>

0 commit comments

Comments
 (0)
⌂ 换网址