Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Back button support for Dialogs #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion docs/navigation/backbutton.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class MainActivity : FormsAppCompatActivity
PopupPlugin.OnBackPressed();
}
}
```
```

The PopupPlugin.OnBackPressed will check to see if the currently displayed page is PopupDialogContainer. If it is it will call all of the same Dialog API's that you would expect, while using the Navigation Service for all of your other pages and PopupPages.
4 changes: 3 additions & 1 deletion src/Prism.Plugin.Popups/Dialogs/PopupDialogContainer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Rg.Plugins.Popup.Pages;
using System;
using Rg.Plugins.Popup.Pages;

namespace Prism.Plugin.Popups.Dialogs
{
internal class PopupDialogContainer : PopupPage
{
public Action RequestClose { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/Prism.Plugin.Popups/Dialogs/PopupDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ void DialogAware_RequestClose(IDialogParameters outParameters)
popupPage.BackgroundClicked += CloseOnBackgroundClicked;
}

popupPage.RequestClose = () => DialogAware_RequestClose(null);

Action<IDialogParameters> closeCallback = closeOnBackgroundTapped ? DialogAware_RequestClose : p => { };
PushPopupPage(popupPage, view, closeCallback, animated);
}
Expand All @@ -124,7 +126,7 @@ void DialogAware_RequestClose(IDialogParameters outParameters)
}
}

private static PopupPage CreatePopupPageForView(BindableObject view)
private static PopupDialogContainer CreatePopupPageForView(View view)
{
var popupPage = new PopupDialogContainer();

Expand Down
23 changes: 18 additions & 5 deletions src/Prism.Plugin.Popups/PopupPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#if MONOANDROID
using System;
using System;
using System.Linq;
using System.Threading;
using Prism.Common;
using Prism.Ioc;
using Prism.Navigation;
using Prism.Plugin.Popups.Dialogs;
using Rg.Plugins.Popup.Contracts;

namespace Prism.Plugin.Popups
Expand All @@ -23,10 +24,23 @@ public static async void OnBackPressed()
try
{
var container = ContainerLocator.Container;
var popupNavigation = container.Resolve<IPopupNavigation>();
var appProvider = container.Resolve<IApplicationProvider>();
if (container is null)
{
Console.WriteLine("Android back button pressed before Prism has initialized.");
return;
}

var popupNavigation = container.Resolve<IPopupNavigation>();
if (popupNavigation.PopupStack.Count > 0 &&
popupNavigation.PopupStack.LastOrDefault() is PopupDialogContainer dialog)
{
dialog.RequestClose.Invoke();
return;
}

var appProvider = container.Resolve<IApplicationProvider>();
var topPage = PopupUtilities.TopPage(popupNavigation, appProvider);

var navService = container.Resolve<INavigationService>(PrismApplicationBase.NavigationServiceName);
if (navService is IPageAware pa)
{
Expand All @@ -44,4 +58,3 @@ public static async void OnBackPressed()
}
}
}
#endif
7 changes: 7 additions & 0 deletions src/Prism.Plugin.Popups/Prism.Plugin.Popups.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="PopupPlugin.cs" />
<None Include="PopupPlugin.cs" />
<None Include="ReadMe.txt"
Pack="True"
PackagePath="." />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('MonoAndroid'))">
<None Remove="PopupPlugin.cs" />
<Compile Include="PopupPlugin.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Rg.Plugins.Popup" Version="2.0.0.10" />
<PackageReference Include="Prism.Forms" Version="8.0.0.1909" />
Expand Down