Skip to content

Commit 1645c9a

Browse files
authored
Call WithCoreServices and add platform converters (#4316)
<!-- Please be sure to read the [Contribute](https://github.com/reactiveui/reactiveui#contribute) section of the README --> **What kind of change does this PR introduce?** <!-- Bug fix, feature, docs update, ... --> update **What is the new behavior?** <!-- If this is a feature change --> Ensure core services are initialized before configuring platform-specific builders by calling WithCoreServices() (cast back to IReactiveUIBuilder) in AndroidX, Blazor, MAUI and WinUI builder extensions. Add MAUI and WinUI converter registration helpers (WithMauiConverters and WithWinUIConverters) that register BooleanToVisibilityTypeConverter, VisibilityToBooleanTypeConverter and ComponentModelFallbackConverter via the builder's WithConverter/WithFallbackConverter APIs. Also add required using directives for MAUI and update WinUI scheduler XML docs; null checks use ArgumentExceptionHelper.ThrowIfNull. **What might this PR break?** If a user has called WithCoreServices() already in code they should clean up the existing source to remove this unless a custom configuration has been made. **Please check if the PR fulfills these requirements** - [ ] Tests for the changes have been added (for bug fixes / features) - [ ] Docs have been added / updated (for bug fixes / features) **Other information**:
1 parent 4998207 commit 1645c9a

5 files changed

Lines changed: 54 additions & 8 deletions

File tree

src/ReactiveUI.AndroidX/Builder/AndroidXReactiveUIBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static IReactiveUIBuilder WithAndroidX(this IReactiveUIBuilder builder)
3232
throw new ArgumentNullException(nameof(builder));
3333
}
3434

35-
return builder
35+
return ((IReactiveUIBuilder)builder.WithCoreServices())
3636
.WithMainThreadScheduler(HandlerScheduler.MainThreadScheduler)
3737
.WithTaskPoolScheduler(TaskPoolScheduler.Default)
3838
.WithPlatformModule<AndroidX.Registrations>();

src/ReactiveUI.Blazor/Builder/BlazorReactiveUIBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IReactiveUIBuilder WithBlazor(this IReactiveUIBuilder builder)
3838
throw new ArgumentNullException(nameof(builder));
3939
}
4040

41-
return builder
41+
return ((IReactiveUIBuilder)builder.WithCoreServices())
4242
.WithBlazorScheduler()
4343
.WithTaskPoolScheduler(TaskPoolScheduler.Default)
4444
.WithPlatformModule<Blazor.Registrations>();

src/ReactiveUI.Maui/Builder/MauiReactiveUIBuilderExtensions.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using Microsoft.Maui.Dispatching;
77
using Microsoft.Maui.Hosting;
8+
using ReactiveUI.Helpers;
9+
using ReactiveUI.Maui;
810

911
namespace ReactiveUI.Builder;
1012

@@ -68,10 +70,11 @@ public static IReactiveUIBuilder WithMaui(this IReactiveUIBuilder builder, IDisp
6870
throw new ArgumentNullException(nameof(builder));
6971
}
7072

71-
return builder
73+
return ((IReactiveUIBuilder)builder.WithCoreServices())
7274
.WithMauiScheduler(dispatcher)
7375
.WithTaskPoolScheduler(TaskPoolScheduler.Default)
7476
.WithPlatformModule<Maui.Registrations>()
77+
.WithMauiConverters()
7578
.WithPlatformServices();
7679
}
7780

@@ -131,6 +134,26 @@ public static IReactiveUIBuilder WithMauiScheduler(this IReactiveUIBuilder build
131134
return builder.WithMainThreadScheduler(scheduler);
132135
}
133136

137+
/// <summary>
138+
/// Registers Maui-specific converters to the ConverterService.
139+
/// </summary>
140+
/// <param name="builder">The builder instance.</param>
141+
/// <returns>The builder instance for chaining.</returns>
142+
/// <remarks>
143+
/// This method registers Maui-specific converters (<see cref="BooleanToVisibilityTypeConverter"/>,
144+
/// <see cref="VisibilityToBooleanTypeConverter"/>) and the <see cref="ComponentModelFallbackConverter"/>
145+
/// to the <c>ConverterService</c> so they are available when using the builder pattern.
146+
/// </remarks>
147+
public static IReactiveUIBuilder WithMauiConverters(this IReactiveUIBuilder builder)
148+
{
149+
ArgumentExceptionHelper.ThrowIfNull(builder);
150+
151+
return builder
152+
.WithConverter(new BooleanToVisibilityTypeConverter())
153+
.WithConverter(new VisibilityToBooleanTypeConverter())
154+
.WithFallbackConverter(new ComponentModelFallbackConverter());
155+
}
156+
134157
private static IScheduler ResolveMainThreadScheduler(IDispatcher? dispatcher)
135158
{
136159
if (dispatcher is not null)

src/ReactiveUI.WinUI/Builder/WinUIReactiveUIBuilderExtensions.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,44 @@ public static IReactiveUIBuilder WithWinUI(this IReactiveUIBuilder builder)
2727
{
2828
ArgumentExceptionHelper.ThrowIfNull(builder);
2929

30-
return builder
30+
return ((IReactiveUIBuilder)builder.WithCoreServices())
3131
.WithWinUIScheduler()
3232
.WithTaskPoolScheduler(TaskPoolScheduler.Default)
33+
.WithWinUIConverters()
3334
.WithPlatformModule<WinUI.Registrations>();
3435
}
3536

3637
/// <summary>
37-
/// Withes the win UI scheduler.
38+
/// Configures the builder to use the WinUI main thread scheduler for reactive operations.
3839
/// </summary>
39-
/// <param name="builder">The builder.</param>
40-
/// <returns>The builder instance for chaining.</returns>
40+
/// <remarks>Use this method when building reactive applications targeting WinUI to ensure that main
41+
/// thread operations are scheduled appropriately for the WinUI environment.</remarks>
42+
/// <param name="builder">The builder to configure with the WinUI main thread scheduler. Cannot be null.</param>
43+
/// <returns>The same builder instance configured to use the WinUI main thread scheduler.</returns>
4144
public static IReactiveUIBuilder WithWinUIScheduler(this IReactiveUIBuilder builder)
4245
{
4346
ArgumentExceptionHelper.ThrowIfNull(builder);
4447

4548
return builder.WithMainThreadScheduler(WinUIMainThreadScheduler);
4649
}
50+
51+
/// <summary>
52+
/// Registers WinUI-specific converters to the ConverterService.
53+
/// </summary>
54+
/// <param name="builder">The builder instance.</param>
55+
/// <returns>The builder instance for chaining.</returns>
56+
/// <remarks>
57+
/// This method registers WinUI-specific converters (<see cref="BooleanToVisibilityTypeConverter"/>,
58+
/// <see cref="VisibilityToBooleanTypeConverter"/>) and the <see cref="ComponentModelFallbackConverter"/>
59+
/// to the <c>ConverterService</c> so they are available when using the builder pattern.
60+
/// </remarks>
61+
public static IReactiveUIBuilder WithWinUIConverters(this IReactiveUIBuilder builder)
62+
{
63+
ArgumentExceptionHelper.ThrowIfNull(builder);
64+
65+
return builder
66+
.WithConverter(new BooleanToVisibilityTypeConverter())
67+
.WithConverter(new VisibilityToBooleanTypeConverter())
68+
.WithFallbackConverter(new ComponentModelFallbackConverter());
69+
}
4770
}

src/tests/ReactiveUI.Blazor.Tests/BlazorReactiveUIBuilderExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public IReactiveUIBuilder WithPlatformModule<T>()
165165
public Splat.Builder.IAppBuilder UsingModule<T>(T registrationModule)
166166
where T : Splat.Builder.IModule => throw new NotImplementedException();
167167

168-
public Splat.Builder.IAppBuilder WithCoreServices() => throw new NotImplementedException();
168+
public Splat.Builder.IAppBuilder WithCoreServices() => this;
169169

170170
public Splat.Builder.IAppBuilder WithCustomRegistration(Action<IMutableDependencyResolver> configureAction) => throw new NotImplementedException();
171171

0 commit comments

Comments
 (0)