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

Commit b9ad98b

Browse files
Improving the operation of PresentationControllers (#1846)
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
1 parent afae843 commit b9ad98b

File tree

6 files changed

+60
-24
lines changed

6 files changed

+60
-24
lines changed

Xamarin.Essentials/Contacts/Contacts.ios.macos.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ static Task<Contact> PlatformPickContactAsync()
3939
})
4040
};
4141

42+
if (picker.PresentationController != null)
43+
{
44+
picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
45+
{
46+
DismissHandler = () => source?.TrySetResult(null)
47+
};
48+
}
49+
4250
uiView.PresentViewController(picker, true, null);
4351

4452
return source.Task;

Xamarin.Essentials/Email/Email.ios.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Threading.Tasks;
34
using Foundation;
45
using MessageUI;
@@ -50,13 +51,21 @@ static Task ComposeWithMailCompose(EmailMessage message)
5051
}
5152
}
5253

53-
// show the controller
5454
var tcs = new TaskCompletionSource<bool>();
5555
controller.Finished += (sender, e) =>
5656
{
5757
controller.DismissViewController(true, null);
5858
tcs.TrySetResult(e.Result == MFMailComposeResult.Sent);
5959
};
60+
61+
if (controller.PresentationController != null)
62+
{
63+
controller.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
64+
{
65+
DismissHandler = () => tcs.TrySetResult(false)
66+
};
67+
}
68+
6069
parentController.PresentViewController(controller, true, null);
6170

6271
return tcs.Task;

Xamarin.Essentials/FilePicker/FilePicker.ios.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options
3434

3535
if (documentPicker.PresentationController != null)
3636
{
37-
documentPicker.PresentationController.Delegate = new PickerPresentationControllerDelegate
37+
documentPicker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
3838
{
39-
PickHandler = urls => GetFileResults(urls, tcs)
39+
DismissHandler = () => GetFileResults(null, tcs)
4040
};
4141
}
4242

@@ -74,14 +74,6 @@ public override void DidPickDocument(UIDocumentPickerViewController controller,
7474
public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)
7575
=> PickHandler?.Invoke(new NSUrl[] { url });
7676
}
77-
78-
class PickerPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
79-
{
80-
public Action<NSUrl[]> PickHandler { get; set; }
81-
82-
public override void DidDismiss(UIPresentationController presentationController) =>
83-
PickHandler?.Invoke(null);
84-
}
8577
}
8678

8779
public partial class FilePickerFileType

Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ static async Task<FileResult> PhotoAsync(MediaPickerOptions options, bool photo,
7575

7676
if (picker.PresentationController != null)
7777
{
78-
picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
78+
picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
7979
{
80-
CompletedHandler = info => GetFileResult(info, tcs)
80+
DismissHandler = () => GetFileResult(null, tcs)
8181
};
8282
}
8383

@@ -167,13 +167,5 @@ public override void FinishedPickingMedia(UIImagePickerController picker, NSDict
167167
public override void Canceled(UIImagePickerController picker) =>
168168
CompletedHandler?.Invoke(null);
169169
}
170-
171-
class PhotoPickerPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
172-
{
173-
public Action<NSDictionary> CompletedHandler { get; set; }
174-
175-
public override void DidDismiss(UIPresentationController presentationController) =>
176-
CompletedHandler?.Invoke(null);
177-
}
178170
}
179171
}

Xamarin.Essentials/Platform/Platform.ios.tvos.watchos.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,20 @@ internal static bool HasOSVersion(int major, int minor) =>
7373

7474
#if __IOS__ || __TVOS__
7575

76+
static Func<UIViewController> getCurrentController;
77+
78+
public static void Init(Func<UIViewController> getCurrentUIViewController)
79+
=> getCurrentController = getCurrentUIViewController;
80+
7681
public static UIViewController GetCurrentUIViewController() =>
7782
GetCurrentViewController(false);
7883

7984
internal static UIViewController GetCurrentViewController(bool throwIfNull = true)
8085
{
81-
UIViewController viewController = null;
86+
var viewController = getCurrentController?.Invoke();
87+
88+
if (viewController != null)
89+
return viewController;
8290

8391
var window = UIApplication.SharedApplication.KeyWindow;
8492

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

139147
internal static NSOperationQueue GetCurrentQueue() =>
140148
NSOperationQueue.CurrentQueue ?? new NSOperationQueue();
149+
150+
#if __IOS__
151+
internal class UIPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate
152+
{
153+
public Action DismissHandler { get; set; }
154+
155+
public override void DidDismiss(UIPresentationController presentationController)
156+
{
157+
DismissHandler?.Invoke();
158+
DismissHandler = null;
159+
}
160+
161+
protected override void Dispose(bool disposing)
162+
{
163+
DismissHandler?.Invoke();
164+
base.Dispose(disposing);
165+
}
166+
}
167+
#endif
141168
}
142169
}

Xamarin.Essentials/Sms/Sms.ios.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ static Task PlatformComposeAsync(SmsMessage message)
2121

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

24-
// show the controller
2524
var tcs = new TaskCompletionSource<bool>();
2625
messageController.Finished += (sender, e) =>
2726
{
2827
messageController.DismissViewController(true, null);
2928
tcs?.TrySetResult(e.Result == MessageComposeResult.Sent);
3029
};
30+
31+
if (controller.PresentationController != null)
32+
{
33+
controller.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
34+
{
35+
DismissHandler = () => tcs.TrySetResult(false)
36+
};
37+
}
38+
3139
controller.PresentViewController(messageController, true, null);
3240

3341
return tcs.Task;

0 commit comments

Comments
 (0)