ThomasBernard03 / MauiBottomSheet

A library to display native bottom sheets
13 stars 0 forks source link

Dismiss Event #1

Open barisertekin opened 1 year ago

barisertekin commented 1 year ago

Hello,

I'm facing difficulty in identifying the active sheet due to the use of nested sheets.

When we close the sheet programmatically, the close event works. However, when the sheet is closed by tapping outside or by dragging it downwards, the event is not triggered.

After examining the source code, I found how to achieve this for Android, but I couldn't find a similar solution for iOS.

For Android, both of the following events work successfully: bottomSheetDialog.CancelEvent += BottomSheetDialog_CancelEvent; bottomSheetDialog.DismissEvent += BottomSheetDialog_DismissEvent;

How can I achieve this for iOS? Could you provide any guidance on capturing these events?

Thank you.

barisertekin commented 1 year ago

Platforms/iOS/MyBottomSheetViewController.cs (New Class for override)

namespace Simple.MauiBottomSheet.Platforms.iOS
{
    public class MyBottomSheetViewController : UIKit.UIViewController
    {
        public event EventHandler ViewWillDisappearEvent;

        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);

            ViewWillDisappearEvent?.Invoke(this, EventArgs.Empty);
        }
    }
}

BottomSheet.cs

#if IOS
using BottomSheetView = Simple.MauiBottomSheet.Platforms.iOS.MyBottomSheetViewController; //UIKit.UIViewController;
#elif ANDROID
using BottomSheetView = Google.Android.Material.BottomSheet.BottomSheetDialog;
#endif

...

Platforms/iOS/BottomSheet.cs

namespace MauiBottomSheet;

public partial class BottomSheet
{
    private Action<object> _onCloseCallback;

    public BottomSheet()
    {
        View.ViewWillDisappearEvent += View_ViewWillDisappearEvent;
    }

    private void View_ViewWillDisappearEvent(object sender, EventArgs e)
    {
        Close();
    }

...

Platform/iOS/iOSBottomSheetService.cs

private BottomSheet CreateBottomSheetInstance<T>(UIViewController viewController, T bottomSheetRef, object parameters = null) where T : IBottomSheetRef
{
    var instance = new BottomSheet()
    {
        View = (MyBottomSheetViewController)viewController
    };

    bottomSheetRef.BottomSheetRef = instance;
    bottomSheetRef.OnAppearing(parameters);

    return instance;
}

I haven't had a chance to test it on iOS. But I hope it can work.

ThomasBernard03 commented 1 year ago

Hello try to implement the delegate : UISheetPresentationControllerDelegate

public override void DidDismiss(UIPresentationController presentationController)
{
    OnSheetDismissed?.Invoke();
}

By implementing this delegate you can access to all lifecycle of bottom sheet

ThomasBernard03 commented 1 year ago

If you wan't I can try your code on iOS