Skip to content

Commit

Permalink
Postpone popup show until platform is ready (#934)
Browse files Browse the repository at this point in the history
* If platform is not enabled defer to OnNavigatedTo event

* Sample to prove support for showing Popup in OnAppearing

* Unsubscribe event handlers

* Make use of ContinueWith to avoid awaiting

* Update src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs

Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>

* improved the error handling on TCS

Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>
  • Loading branch information
bijington and pictos authored Jan 27, 2023
1 parent 577db7f commit eb034d4
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 11 deletions.
1 change: 1 addition & 0 deletions samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public partial class AppShell : Shell
CreateViewModelMapping<MultiplePopupPage, MultiplePopupViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<PopupAnchorPage, PopupAnchorViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<PopupPositionPage, PopupPositionViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<ShowPopupInOnAppearingPage, ShowPopupInOnAppearingPageViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
});

public AppShell() => InitializeComponent();
Expand Down
1 change: 1 addition & 0 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services)
services.AddTransientWithShellRoute<MultiplePopupPage, MultiplePopupViewModel>();
services.AddTransientWithShellRoute<PopupAnchorPage, PopupAnchorViewModel>();
services.AddTransientWithShellRoute<PopupPositionPage, PopupPositionViewModel>();
services.AddTransientWithShellRoute<ShowPopupInOnAppearingPage, ShowPopupInOnAppearingPageViewModel>();

// Add Popups
services.AddTransient<CsharpBindingPopup, CsharpBindingPopupViewModel>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CommunityToolkit.Maui.Sample.Pages.Views.ShowPopupInOnAppearingPage"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
Title="ShowPopupInOnAppearingPage"
x:TypeArguments="viewModels:ShowPopupInOnAppearingPageViewModel"
x:DataType="viewModels:ShowPopupInOnAppearingPageViewModel">
<VerticalStackLayout>
<Label
Text="Proves that we now support showing a popup before the platform is even ready."
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CommunityToolkit.Maui.Sample.Models;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Views;

namespace CommunityToolkit.Maui.Sample.Pages.Views;

public partial class ShowPopupInOnAppearingPage : BasePage<ShowPopupInOnAppearingPageViewModel>
{
readonly PopupSizeConstants popupSizeConstants;

public ShowPopupInOnAppearingPage(
PopupSizeConstants popupSizeConstants,
ShowPopupInOnAppearingPageViewModel showPopupInOnAppearingPageViewModel)
: base(showPopupInOnAppearingPageViewModel)
{
InitializeComponent();
this.popupSizeConstants = popupSizeConstants;
}

protected override async void OnAppearing()
{

// Proves that we now support showing a popup before the platform is even ready.
var result = await this.ShowPopupAsync(new ReturnResultPopup(popupSizeConstants));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public class ShowPopupInOnAppearingPageViewModel : BaseViewModel
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public ViewsGalleryViewModel()
SectionModel.Create<MultiplePopupViewModel>("Mutiple Popups Page", Colors.Red, "A page demonstrating multiple different Popups"),
SectionModel.Create<PopupPositionViewModel>("Custom Positioning Popup", Colors.Red, "Displays a basic popup anywhere on the screen using VerticalOptions and HorizontalOptions"),
SectionModel.Create<PopupAnchorViewModel>("Anchor Popup", Colors.Red, "Popups can be anchored to other view's on the screen"),
SectionModel.Create<ShowPopupInOnAppearingPageViewModel>("Show Popup in OnAppearing", Colors.Red, "Proves that we now support showing a popup before the platform is even ready."),
})
{
}
Expand Down
72 changes: 61 additions & 11 deletions src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public static partial class PopupExtensions
/// </param>
public static void ShowPopup<TPopup>(this Page page, TPopup popup) where TPopup : Popup
{
#if WINDOWS
PlatformShowPopup(popup, GetMauiContext(page));
#else
CreatePopup(page, popup);
#endif
if (page.IsPlatformEnabled)
{
CreateAndShowPopup(page, popup);
}
else
{
void handler(object? sender, NavigatedToEventArgs args)
{
page.NavigatedTo -= handler;

CreateAndShowPopup(page, popup);
}

page.NavigatedTo += handler;
}
}

/// <summary>
Expand All @@ -43,13 +52,34 @@ public static void ShowPopup<TPopup>(this Page page, TPopup popup) where TPopup
/// </returns>
public static Task<object?> ShowPopupAsync<TPopup>(this Page page, TPopup popup) where TPopup : Popup
{
#if WINDOWS
return PlatformShowPopupAsync(popup, GetMauiContext(page));
#else
if (page.IsPlatformEnabled)
{
return CreateAndShowPopupAsync(page, popup);
}
else
{
var taskCompletionSource = new TaskCompletionSource<object?>();

CreatePopup(page, popup);
return popup.Result;
#endif
async void handler(object? sender, NavigatedToEventArgs args)
{
page.NavigatedTo -= handler;

try
{
var result = await CreateAndShowPopupAsync(page, popup);

taskCompletionSource.TrySetResult(result);
}
catch (Exception ex)
{
taskCompletionSource.TrySetException(ex);
}
}

page.NavigatedTo += handler;

return taskCompletionSource.Task;
}
}

static void CreatePopup(Page page, Popup popup)
Expand All @@ -65,4 +95,24 @@ static IMauiContext GetMauiContext(Page page)
{
return page.Handler?.MauiContext ?? throw new InvalidOperationException("Could not locate MauiContext.");
}

static void CreateAndShowPopup<TPopup>(Page page, TPopup popup) where TPopup : Popup
{
#if WINDOWS
PlatformShowPopup(popup, GetMauiContext(page));
#else
CreatePopup(page, popup);
#endif
}

static Task<object?> CreateAndShowPopupAsync<TPopup>(this Page page, TPopup popup) where TPopup : Popup
{
#if WINDOWS
return PlatformShowPopupAsync(popup, GetMauiContext(page));
#else
CreatePopup(page, popup);

return popup.Result;
#endif
}
}

0 comments on commit eb034d4

Please sign in to comment.