Skip to content

Commit

Permalink
Merge pull request #48 from lucacivale/PeekHeight
Browse files Browse the repository at this point in the history
[Breaking change] Enhanced PeekHeight API
  • Loading branch information
lucacivale authored Feb 8, 2025
2 parents dd35cb3 + b765666 commit 3e717ff
Show file tree
Hide file tree
Showing 20 changed files with 660 additions and 685 deletions.
266 changes: 182 additions & 84 deletions README.md

Large diffs are not rendered by default.

Large diffs are not rendered by default.

52 changes: 7 additions & 45 deletions src/Plugin.Maui.BottomSheet/Plugin.Maui.BottomSheet/BottomSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ public class BottomSheet : View, IBottomSheet, IElementConfiguration<BottomSheet
/// <summary>
/// Bindable property.
/// </summary>
public static readonly BindableProperty PeekProperty =
public static readonly BindableProperty PeekHeightProperty =
BindableProperty.Create(
nameof(Peek),
typeof(BottomSheetPeek),
nameof(PeekHeight),
typeof(double),
typeof(BottomSheet),
propertyChanged: OnPeekChanged);
defaultValue: 0.00);

/// <summary>
/// Bindable property.
Expand All @@ -183,7 +183,7 @@ public class BottomSheet : View, IBottomSheet, IElementConfiguration<BottomSheet
/// </summary>
public static readonly BindableProperty PaddingProperty =
BindableProperty.Create(
nameof(Peek),
nameof(Padding),
typeof(Thickness),
typeof(BottomSheet),
defaultValue: new Thickness(5));
Expand Down Expand Up @@ -329,7 +329,7 @@ public event EventHandler Opened
public Color WindowBackgroundColor { get => (Color)GetValue(WindowBackgroundColorProperty); set => SetValue(WindowBackgroundColorProperty, value); }

/// <inheritdoc/>
public BottomSheetPeek? Peek { get => (BottomSheetPeek?)GetValue(PeekProperty); set => SetValue(PeekProperty, value); }
public double PeekHeight { get => (double)GetValue(PeekHeightProperty); set => SetValue(PeekHeightProperty, value); }

/// <inheritdoc/>
public BottomSheetHeader? Header { get => (BottomSheetHeader?)GetValue(HeaderProperty); set => SetValue(HeaderProperty, value); }
Expand Down Expand Up @@ -375,6 +375,7 @@ public ICollection<BottomSheetState> States
set => SetValue(StatesProperty, value);
}

/// <inheritdoc/>
public IPlatformElementConfiguration<T, BottomSheet> On<T>()
where T : IConfigPlatform
{
Expand Down Expand Up @@ -411,27 +412,6 @@ void IBottomSheet.OnClosedBottomSheet()
}
#pragma warning restore CA1033

/// <inheritdoc/>
protected override void OnParentChanged()
{
base.OnParentChanged();

if (Header is not null)
{
Header.Parent = Parent;
}

if (Peek is not null)
{
Peek.Parent = Parent;
}

if (Content is not null)
{
Content.Parent = Parent;
}
}

/// <inheritdoc/>
protected override void OnBindingContextChanged()
{
Expand All @@ -442,11 +422,6 @@ protected override void OnBindingContextChanged()
Header.BindingContext = BindingContext;
}

if (Peek is not null)
{
Peek.BindingContext = BindingContext;
}

if (Content is not null)
{
Content.BindingContext = BindingContext;
Expand All @@ -467,9 +442,6 @@ private static void OnStatesPropertyChanged(BindableObject bindable, object oldv
private static void OnHeaderChanged(BindableObject bindable, object oldvalue, object newvalue)
=> ((BottomSheet)bindable).OnHeaderChanged((BottomSheetHeader)newvalue);

private static void OnPeekChanged(BindableObject bindable, object oldvalue, object newvalue)
=> ((BottomSheet)bindable).OnPeekChanged((BottomSheetPeek)newvalue);

private static void OnContentChanged(BindableObject bindable, object oldvalue, object newvalue)
=> ((BottomSheet)bindable).OnContentChanged((BottomSheetContent)newvalue);

Expand Down Expand Up @@ -501,16 +473,6 @@ private void OnHeaderChanged(BottomSheetHeader newValue)
}
}

private void OnPeekChanged(BottomSheetPeek newValue)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (newValue is not null)
{
newValue.Parent = this;
newValue.BindingContext = BindingContext;
}
}

private void OnContentChanged(BottomSheetContent newValue)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
namespace Plugin.Maui.BottomSheet;

/// <inheritdoc />
public sealed class BottomSheetPeekBehavior : PlatformBehavior<View>
{
#if ANDROID || IOS || MACCATALYST
private WeakReference<IBottomSheet>? _weakBottomSheet;
#endif

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

#pragma warning disable CA1062
bindable.SizeChanged += ViewOnSizeChanged;
#pragma warning restore CA1062
var bottomSheet = bindable.FindBottomSheet();

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

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

#pragma warning disable CA1062
bindable.SizeChanged -= ViewOnSizeChanged;
#pragma warning restore CA1062
}

#elif MACCATALYST || IOS
/// <inheritdoc/>
protected override void OnAttachedTo(View bindable, UIKit.UIView platformView)
{
base.OnAttachedTo(bindable, platformView);

double height = 0.00;
#pragma warning disable CA1062
#if NET9
var size = bindable.Measure(double.PositiveInfinity, double.PositiveInfinity);
height = size.Height;
#elif NET8
var size = bindable.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
height = size.Request.Height;
#endif
bindable.SizeChanged += ViewOnSizeChanged;
#pragma warning restore CA1062
var bottomSheetPage = bindable.FindBottomSheetPage();

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

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

#pragma warning disable CA1062
bindable.SizeChanged -= ViewOnSizeChanged;
#pragma warning restore CA1062
}
#endif

#if ANDROID || IOS || MACCATALYST
private void ViewOnSizeChanged(object? sender, EventArgs e)
{
if (sender is View view
&& _weakBottomSheet?.TryGetTarget(out var bottomSheet) == true)
{
bottomSheet.PeekHeight = view.Height;
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ private static void MapCurrentState(BottomSheetHandler handler, IBottomSheet bot
handler.PlatformView.SetCurrentState();
}

private static void MapPeek(BottomSheetHandler handler, IBottomSheet bottomSheet)
private static void MapPeekHeight(BottomSheetHandler handler, IBottomSheet bottomSheet)
{
handler.PlatformView.SetPeek();
handler.PlatformView.SetPeekHeight();
}

private static void MapContent(BottomSheetHandler handler, IBottomSheet bottomSheet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed partial class BottomSheetHandler
[nameof(IBottomSheet.Header)] = MapHeader,
[nameof(IBottomSheet.States)] = MapStates,
[nameof(IBottomSheet.CurrentState)] = MapCurrentState,
[nameof(IBottomSheet.Peek)] = MapPeek,
[nameof(IBottomSheet.PeekHeight)] = MapPeekHeight,
[nameof(IBottomSheet.Content)] = MapContent,
[nameof(IBottomSheet.Padding)] = MapPadding,
[nameof(IBottomSheet.BackgroundColor)] = MapBackgroundColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ private static void MapCurrentState(BottomSheetHandler handler, IBottomSheet bot
handler.PlatformView.SetCurrentState();
}

private static void MapPeek(BottomSheetHandler handler, IBottomSheet bottomSheet)
private static void MapPeekHeight(BottomSheetHandler handler, IBottomSheet bottomSheet)
{
handler.PlatformView.SetPeek();
handler.PlatformView.SetPeekHeight();
}

private static void MapContent(BottomSheetHandler handler, IBottomSheet bottomSheet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static void MapCurrentState(BottomSheetHandler handler, IBottomSheet bot
throw new NotImplementedException();
}

private static void MapPeek(BottomSheetHandler handler, IBottomSheet bottomSheet)
private static void MapPeekHeight(BottomSheetHandler handler, IBottomSheet bottomSheet)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Plugin.Maui.BottomSheet.Handlers;
/// <inheritdoc/>
public class CloseButtonHandler : ButtonHandler
{
/// <inheritdoc/>
protected override UIButton CreatePlatformView()
{
return new UIButton(UIButtonType.Close);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public interface IBottomSheet : IView, IPadding, ISafeAreaView
Color WindowBackgroundColor { get; set; }

/// <summary>
/// Gets or sets the <see cref="BottomSheetPeek"/>.
/// Gets or sets the PeekHeight.
/// </summary>
BottomSheetPeek? Peek { get; set; }
double PeekHeight { get; set; }

/// <summary>
/// Gets or sets the executed command when the <see cref="IBottomSheet"/> is closing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Plugin.Maui.BottomSheet;

// ReSharper disable once InconsistentNaming

/// <summary>
/// <see cref="IBottomSheet"/> extension methods.
/// </summary>
internal static class IBottomSheetExtensions
{
/// <summary>
/// Get the <see cref="ContentPage"/> parent of <see cref="IBottomSheet"/>.
/// </summary>
/// <param name="bottomSheet"><see cref="IBottomSheet"/>.</param>
/// <returns><see cref="ContentPage"/> parent.</returns>
internal static ContentPage? GetPageParent(this IBottomSheet bottomSheet)
{
ContentPage? page = null;

var parent = bottomSheet.Parent;
while (parent is not null)
{
if (parent is ContentPage contentPage)
{
page = contentPage;
break;
}

parent = parent.Parent;
}

return page;
}
}
Loading

0 comments on commit 3e717ff

Please sign in to comment.