Skip to content

Commit

Permalink
Merge pull request #60 from lucacivale/#53
Browse files Browse the repository at this point in the history
BottomSheet Closed Event Not Triggered
  • Loading branch information
lucacivale authored Feb 20, 2025
2 parents c737895 + 956a89b commit b8eb768
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Plugin.Maui.BottomSheet.Behaviors.BottomSheetPeekBehavior;

using Microsoft.Maui.Platform;

/// <summary>
/// Android implementation.
/// </summary>
public sealed partial class BottomSheetPeekBehavior
{
private WeakReference<IBottomSheet>? _weakBottomSheet;

/// <inheritdoc/>
protected override void OnAttachedTo(View bindable, Android.Views.View platformView)
{
base.OnAttachedTo(bindable, platformView);

var bottomSheet = bindable.FindBottomSheet();

if (bottomSheet is not null)
{
_weakBottomSheet = new WeakReference<IBottomSheet>(bottomSheet);
}

platformView.LayoutChange += OnLayoutChange;
}

/// <inheritdoc/>
protected override void OnDetachedFrom(View bindable, Android.Views.View platformView)
{
base.OnDetachedFrom(bindable, platformView);

platformView.LayoutChange -= OnLayoutChange;
}

private void OnLayoutChange(object? sender, Android.Views.View.LayoutChangeEventArgs e)
{
if (sender is Android.Views.View view
&& _weakBottomSheet?.TryGetTarget(out var bottomSheet) == true
&& view.Context is not null)
{
bottomSheet.PeekHeight = Convert.ToInt32(view.Context.FromPixels(view.Height));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Plugin.Maui.BottomSheet.Behaviors.BottomSheetPeekBehavior;

/// <inheritdoc />
public sealed partial class BottomSheetPeekBehavior : PlatformBehavior<View>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Plugin.Maui.BottomSheet.Behaviors.BottomSheetPeekBehavior;

using System.Diagnostics.CodeAnalysis;
using UIKit;

/// <summary>
/// iOS MacCatalyst implementation.
/// </summary>
public sealed partial class BottomSheetPeekBehavior
{
private WeakReference<IBottomSheet>? _weakBottomSheet;

/// <inheritdoc/>
// ReSharper disable once RedundantNullableFlowAttribute
protected override void OnAttachedTo([NotNull]View bindable, UIKit.UIView platformView)
{
base.OnAttachedTo(bindable, platformView);
var bottomSheetPage = bindable.FindBottomSheetPage();

if (bottomSheetPage?.BottomSheet is not null)
{
_weakBottomSheet = new WeakReference<IBottomSheet>(bottomSheetPage.BottomSheet);
bottomSheetPage.BottomSheet.PeekHeight = CalculateHeight(bindable);
}

bindable.MeasureInvalidated += OnMeasureInvalidated;
}

/// <inheritdoc/>
protected override void OnDetachedFrom(View bindable, UIView platformView)
{
base.OnDetachedFrom(bindable, platformView);

bindable.MeasureInvalidated -= OnMeasureInvalidated;
}

private static double CalculateHeight(View view)
{
#if NET9_0
var size = view.Measure(double.PositiveInfinity, double.PositiveInfinity);
return size.Height;
#elif NET8_0
var size = view.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
return size.Request.Height;
#endif
}

private void OnMeasureInvalidated(object? sender, EventArgs e)
{
if (sender is View view
&& _weakBottomSheet?.TryGetTarget(out var bottomSheet) == true)
{
bottomSheet.PeekHeight = CalculateHeight(view);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Plugin.Maui.BottomSheet.Behaviors.BottomSheetPeekBehavior;

/// <summary>
/// dotnet implementation.
/// </summary>
public sealed partial class BottomSheetPeekBehavior;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@ internal static class IBottomSheetExtensions
/// </summary>
/// <param name="bottomSheet"><see cref="IBottomSheet"/>.</param>
/// <returns><see cref="ContentPage"/> parent.</returns>
internal static ContentPage? GetPageParent(this IBottomSheet bottomSheet)
internal static Page? GetPageParent(this IBottomSheet bottomSheet)
{
ContentPage? page = null;
Page? page = null;

var parent = bottomSheet.Parent;
while (parent is not null)

if (parent is Shell shell)
{
page = shell.CurrentPage;
}
else
{
if (parent is ContentPage contentPage)
while (parent is not null)
{
page = contentPage;
break;
}
if (parent is ContentPage contentPage)
{
page = contentPage;
break;
}

parent = parent.Parent;
parent = parent.Parent;
}
}

return page;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ public void SetIsOpen()
}
else
{
_virtualView?.OnClosingBottomSheet();
if (_bottomSheet.IsShowing)
{
_virtualView?.OnClosingBottomSheet();
_bottomSheet.Close();
_virtualView?.OnClosedBottomSheet();
}

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet_Sample

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet_Sample

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet_Sample

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet_Sample

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet

Check warning on line 138 in src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/Platform/Android/MauiBottomSheet.android.cs

View workflow job for this annotation

GitHub Actions / Build-Plugin_Maui_BottomSheet

_virtualView?.OnClosedBottomSheet();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public bool Draggable
}
}

/// <summary>
/// Gets or sets a value indicating whether sheet is modal.
/// </summary>
public bool IsModal
{
get => SheetPresentationController?.LargestUndimmedDetentIdentifier == UISheetPresentationControllerDetentIdentifier.Unknown;
Expand Down Expand Up @@ -477,8 +480,8 @@ private void ApplyPeekHeight()
_peekDetentHeight = peekHeight;

if ((OperatingSystem.IsMacCatalyst()
|| (OperatingSystem.IsIOS()
&& OperatingSystem.IsIOSVersionAtLeast(16)))
|| (OperatingSystem.IsIOS()
&& OperatingSystem.IsIOSVersionAtLeast(16)))
#pragma warning disable CA1416
#pragma warning disable S6605
&& SheetPresentationController?.Detents.Any(x => x.Identifier == PeekDetentId) == true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[assembly: XmlnsDefinition("http://pluginmauibottomsheet.com", "Plugin.Maui.BottomSheet")]
[assembly: XmlnsDefinition("http://pluginmauibottomsheet.com", "Plugin.Maui.BottomSheet.Behaviors.BottomSheetPeekBehavior")]
[assembly: XmlnsDefinition("http://pluginmauibottomsheet.com/platformconfiguration/android", "Plugin.Maui.BottomSheet.PlatformConfiguration.AndroidSpecific")]

0 comments on commit b8eb768

Please sign in to comment.