Skip to content

Commit

Permalink
Fixes PopupService scopes problem (#2333)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdovaz authored Nov 11, 2024
1 parent d42bd2a commit e46c39e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/CommunityToolkit.Maui.UnitTests/PopupServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static PopupServiceTests()
[Fact]
public async Task ShowPopupAsyncWithNullViewModelShouldThrowArgumentNullException()
{
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcher());
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcherProvider());

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
await Assert.ThrowsAsync<ArgumentNullException>(() =>
Expand All @@ -40,7 +40,7 @@ await Assert.ThrowsAsync<ArgumentNullException>(() =>
[Fact]
public async Task ShowPopupAsyncWithNullOnPresentingShouldThrowArgumentNullException()
{
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcher());
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcherProvider());

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
await Assert.ThrowsAsync<ArgumentNullException>(() =>
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task ShowPopupAsync_CancellationTokenCanceled()
[Fact]
public async Task ShowPopupAsyncWithMismatchedViewModelTypeShouldThrowInvalidOperationException()
{
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcher());
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcherProvider());

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
await Assert.ThrowsAsync<ArgumentNullException>(() =>
Expand Down Expand Up @@ -199,7 +199,7 @@ public async Task ShowPopupAsyncShouldReturnResultOnceClosed()
[Fact]
public void ShowPopupWithNullViewModelShouldThrowArgumentNullException()
{
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcher());
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcherProvider());

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => popupService.ShowPopup<INotifyPropertyChanged>(viewModel: null));
Expand All @@ -209,7 +209,7 @@ public void ShowPopupWithNullViewModelShouldThrowArgumentNullException()
[Fact]
public void ShowPopupWithNullOnPresentingShouldThrowArgumentNullException()
{
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcher());
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcherProvider());

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => popupService.ShowPopup<INotifyPropertyChanged>(onPresenting: null));
Expand All @@ -219,7 +219,7 @@ public void ShowPopupWithNullOnPresentingShouldThrowArgumentNullException()
[Fact]
public void ShowPopupWithMismatchedViewModelTypeShouldThrowInvalidOperationException()
{
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcher());
var popupService = new PopupService(new MockServiceProvider(), new MockDispatcherProvider());

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => popupService.ShowPopup<INotifyPropertyChanged>(viewModel: null));
Expand Down Expand Up @@ -300,7 +300,7 @@ static void SetupTest(
MockServiceProvider.ThatProvides(
(implementation: popup, forType: typeof(MockPopup)),
(implementation: createViewModelInstance.Invoke(), forType: typeof(MockPageViewModel))),
new MockDispatcher());
new MockDispatcherProvider());

var app = Application.Current ?? throw new NullReferenceException();

Expand Down
7 changes: 4 additions & 3 deletions src/CommunityToolkit.Maui/PopupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public class PopupService : IPopupService
/// Creates a new instance of <see cref="PopupService"/>.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> implementation.</param>
/// <param name="dispatcher"></param>
/// <param name="dispatcherProvider"></param>
[ActivatorUtilitiesConstructor]
public PopupService(IServiceProvider serviceProvider, IDispatcher dispatcher)
public PopupService(IServiceProvider serviceProvider, IDispatcherProvider dispatcherProvider)
{
this.serviceProvider = serviceProvider;
this.dispatcher = dispatcher;
dispatcher = dispatcherProvider.GetForCurrentThread()
?? throw new InvalidOperationException("Could not locate IDispatcher");
}

/// <summary>
Expand Down

0 comments on commit e46c39e

Please sign in to comment.