Skip to content

Add Support for Shell + IQueryAttributable for PopupV2 #2661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ public PopupsPage(PopupsViewModel multiplePopupViewModel, IPopupService popupSer

async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<SimplePopup>(Navigation, new PopupOptions
var queryAttributes = new Dictionary<string, object>
{
Shape = new RoundRectangle
["DescriptionLabel"] = "This is a popup where this text is being passed in using IQueryAttributable"
};

await popupService.ShowPopupAsync<SimplePopup>(Shell.Current, new PopupOptions
{
CornerRadius = new CornerRadius(4),
Stroke = Colors.White
},
}, CancellationToken.None);
Shape = new RoundRectangle
{
CornerRadius = new CornerRadius(4),
Stroke = Colors.White
}
}, queryAttributes
, CancellationToken.None);
}

async void HandleButtonPopupButtonClicked(object sender, EventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public sealed partial class CsharpBindingPopupViewModel : BaseViewModel
public sealed partial class CsharpBindingPopupViewModel : BaseViewModel, IQueryAttributable
{
[ObservableProperty]
public partial string Title { get; set; } = "C# Binding Popup";
public partial string Title { get; private set; } = string.Empty;

[ObservableProperty]
public partial string Message { get; set; } = "This message uses a ViewModel binding";
public partial string Message { get; private set; } = string.Empty;

public TaskCompletionSource<IPopupResult>? PopupResultManager { get; set; }

void IQueryAttributable.ApplyQueryAttributes(IDictionary<string, object> query)
{
Title = (string)query[nameof(Title)];
Message = (string)query[nameof(Message)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ public partial class PopupsViewModel(IPopupService popupService) : BaseViewModel
[RelayCommand]
void OnCsharpBindingPopup()
{
popupService.ShowPopup<CsharpBindingPopupViewModel>(currentNavigation);
var queryAttributes = new Dictionary<string, object>
{
[nameof(CsharpBindingPopupViewModel.Title)] = "C# Binding Popup",
[nameof(CsharpBindingPopupViewModel.Message)] = "This message uses a ViewModel binding that was set using IQueryAttributable"
};
popupService.ShowPopup<CsharpBindingPopupViewModel>(Shell.Current, null, queryAttributes);
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<Label Style="{StaticResource Title}" Text="Simple Popup"/>
<BoxView Style="{StaticResource Divider}" />
<Label Style="{StaticResource Content}" Text="This is a platform specific popup with a .NET MAUI View being rendered. " />
<Label x:Name="DescriptionLabel" Style="{StaticResource Content}" HorizontalOptions="Center"/>
<Image Source="shield.png"
Margin="0,10,0,10"
WidthRequest="30"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace CommunityToolkit.Maui.Sample.Views.Popups;

public partial class SimplePopup : ContentView
public partial class SimplePopup : ContentView, IQueryAttributable
{
public SimplePopup()
{
InitializeComponent();
}

void IQueryAttributable.ApplyQueryAttributes(IDictionary<string, object> query)
{
DescriptionLabel.Text = (string)query[nameof(DescriptionLabel)];
}
}
17 changes: 12 additions & 5 deletions src/CommunityToolkit.Maui.Core/Interfaces/IPopupResult.shared.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
namespace CommunityToolkit.Maui.Core;

/// <inheritdoc/>
public interface IPopupResult<T> : IPopupResult
public interface IPopupResult<out TResult> : IPopupResult
{
/// <summary>
/// PopupResult
/// The result returned when the popup is closed programmatically.
/// </summary>
T? Result { get; }
/// <remarks>
/// Make sure to check the <see cref="IPopupResult.WasDismissedByTappingOutsideOfPopup"/> value to determine how the popup was closed.
/// This will always return <c>null</c> when <see cref="IPopupResult.WasDismissedByTappingOutsideOfPopup"/> is <c>true</c>.
/// </remarks>
TResult? Result { get; }
}

/// <summary>
/// Represents the result of a popup.
/// Represents a result that can be returned when a popup is closed.
/// </summary>
/// <remarks>
/// Make sure to check the <see cref="IPopupResult.WasDismissedByTappingOutsideOfPopup"/> value to determine how the popup was closed.
/// </remarks>
public interface IPopupResult
{
/// <summary>
/// True if Popup is closed by tapping outside the popup
/// Gets whether the popup was closed by tapping outside the popup.
/// </summary>
bool WasDismissedByTappingOutsideOfPopup { get; }
}
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
#endregion

var mauiApp = appBuilder.Build();
serviceProvider = mauiApp.Services;

var application = (MockApplication)mauiApp.Services.GetRequiredService<IApplication>();
application.AddWindow(new Window { Page = page });
serviceProvider = mauiApp.Services;

IPlatformApplication.Current = application;

Expand Down
Loading