Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Improving the operation of PresentationControllers (#1846)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
dimonovdd and mattleibow authored Jan 13, 2022
1 parent afae843 commit b9ad98b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
8 changes: 8 additions & 0 deletions Xamarin.Essentials/Contacts/Contacts.ios.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ static Task<Contact> PlatformPickContactAsync()
})
};

if (picker.PresentationController != null)
{
picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
{
DismissHandler = () => source?.TrySetResult(null)
};
}

uiView.PresentViewController(picker, true, null);

return source.Task;
Expand Down
13 changes: 11 additions & 2 deletions Xamarin.Essentials/Email/Email.ios.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Threading.Tasks;
using Foundation;
using MessageUI;
Expand Down Expand Up @@ -50,13 +51,21 @@ static Task ComposeWithMailCompose(EmailMessage message)
}
}

// show the controller
var tcs = new TaskCompletionSource<bool>();
controller.Finished += (sender, e) =>
{
controller.DismissViewController(true, null);
tcs.TrySetResult(e.Result == MFMailComposeResult.Sent);
};

if (controller.PresentationController != null)
{
controller.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
{
DismissHandler = () => tcs.TrySetResult(false)
};
}

parentController.PresentViewController(controller, true, null);

return tcs.Task;
Expand Down
12 changes: 2 additions & 10 deletions Xamarin.Essentials/FilePicker/FilePicker.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options

if (documentPicker.PresentationController != null)
{
documentPicker.PresentationController.Delegate = new PickerPresentationControllerDelegate
documentPicker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
{
PickHandler = urls => GetFileResults(urls, tcs)
DismissHandler = () => GetFileResults(null, tcs)
};
}

Expand Down Expand Up @@ -74,14 +74,6 @@ public override void DidPickDocument(UIDocumentPickerViewController controller,
public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)
=> PickHandler?.Invoke(new NSUrl[] { url });
}

class PickerPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
{
public Action<NSUrl[]> PickHandler { get; set; }

public override void DidDismiss(UIPresentationController presentationController) =>
PickHandler?.Invoke(null);
}
}

public partial class FilePickerFileType
Expand Down
12 changes: 2 additions & 10 deletions Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ static async Task<FileResult> PhotoAsync(MediaPickerOptions options, bool photo,

if (picker.PresentationController != null)
{
picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
{
CompletedHandler = info => GetFileResult(info, tcs)
DismissHandler = () => GetFileResult(null, tcs)
};
}

Expand Down Expand Up @@ -167,13 +167,5 @@ public override void FinishedPickingMedia(UIImagePickerController picker, NSDict
public override void Canceled(UIImagePickerController picker) =>
CompletedHandler?.Invoke(null);
}

class PhotoPickerPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
{
public Action<NSDictionary> CompletedHandler { get; set; }

public override void DidDismiss(UIPresentationController presentationController) =>
CompletedHandler?.Invoke(null);
}
}
}
29 changes: 28 additions & 1 deletion Xamarin.Essentials/Platform/Platform.ios.tvos.watchos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,20 @@ internal static bool HasOSVersion(int major, int minor) =>

#if __IOS__ || __TVOS__

static Func<UIViewController> getCurrentController;

public static void Init(Func<UIViewController> getCurrentUIViewController)
=> getCurrentController = getCurrentUIViewController;

public static UIViewController GetCurrentUIViewController() =>
GetCurrentViewController(false);

internal static UIViewController GetCurrentViewController(bool throwIfNull = true)
{
UIViewController viewController = null;
var viewController = getCurrentController?.Invoke();

if (viewController != null)
return viewController;

var window = UIApplication.SharedApplication.KeyWindow;

Expand Down Expand Up @@ -138,5 +146,24 @@ internal static UIWindow GetCurrentWindow(bool throwIfNull = true)

internal static NSOperationQueue GetCurrentQueue() =>
NSOperationQueue.CurrentQueue ?? new NSOperationQueue();

#if __IOS__
internal class UIPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
{
public Action DismissHandler { get; set; }

public override void DidDismiss(UIPresentationController presentationController)
{
DismissHandler?.Invoke();
DismissHandler = null;
}

protected override void Dispose(bool disposing)
{
DismissHandler?.Invoke();
base.Dispose(disposing);
}
}
#endif
}
}
10 changes: 9 additions & 1 deletion Xamarin.Essentials/Sms/Sms.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ static Task PlatformComposeAsync(SmsMessage message)

messageController.Recipients = message?.Recipients?.ToArray() ?? new string[] { };

// show the controller
var tcs = new TaskCompletionSource<bool>();
messageController.Finished += (sender, e) =>
{
messageController.DismissViewController(true, null);
tcs?.TrySetResult(e.Result == MessageComposeResult.Sent);
};

if (controller.PresentationController != null)
{
controller.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
{
DismissHandler = () => tcs.TrySetResult(false)
};
}

controller.PresentViewController(messageController, true, null);

return tcs.Task;
Expand Down

0 comments on commit b9ad98b

Please sign in to comment.