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

Commit

Permalink
Redirect exceptions from the pickers into the TCS (#1547)
Browse files Browse the repository at this point in the history
Exceptions thrown in the callbacks will not reach the end user, and the task may not even return.
  • Loading branch information
mattleibow authored Nov 30, 2020
1 parent 439839d commit ba91194
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
50 changes: 24 additions & 26 deletions Xamarin.Essentials/Contacts/Contacts.ios.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ static Task<Contact> PlatformPickContactAsync()
var picker = new CNContactPickerViewController
{
Delegate = new ContactPickerDelegate(phoneContact =>
source?.TrySetResult(ConvertContact(phoneContact)))
{
try
{
source?.TrySetResult(ConvertContact(phoneContact));
}
catch (Exception ex)
{
source?.TrySetException(ex);
}
})
};

uiView.PresentViewController(picker, true, null);
Expand Down Expand Up @@ -80,31 +89,20 @@ internal static Contact ConvertContact(CNContact contact)
if (contact == null)
return default;

try
{
var phones = contact.PhoneNumbers?.Select(
item => new ContactPhone(item?.Value?.StringValue));
var emails = contact.EmailAddresses?.Select(
item => new ContactEmail(item?.Value?.ToString()));

return new Contact(
contact.Identifier,
contact.NamePrefix,
contact.GivenName,
contact.MiddleName,
contact.FamilyName,
contact.NameSuffix,
phones,
emails);
}
catch (Exception ex)
{
throw ex;
}
finally
{
contact.Dispose();
}
var phones = contact.PhoneNumbers?.Select(
item => new ContactPhone(item?.Value?.StringValue));
var emails = contact.EmailAddresses?.Select(
item => new ContactEmail(item?.Value?.ToString()));

return new Contact(
contact.Identifier,
contact.NamePrefix,
contact.GivenName,
contact.MiddleName,
contact.FamilyName,
contact.NameSuffix,
phones,
emails);
}

#if __IOS__
Expand Down
33 changes: 25 additions & 8 deletions Xamarin.Essentials/FilePicker/FilePicker.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ static Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool
try
{
// there was a cancellation
if (urls?.Any() ?? false)
tcs.TrySetResult(urls.Select(url => new UIDocumentFileResult(url)));
else
tcs.TrySetResult(Enumerable.Empty<FileResult>());
tcs.TrySetResult(GetFileResults(urls));
}
catch (Exception ex)
{
Expand All @@ -52,7 +49,19 @@ static Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool
{
documentPicker.PresentationController.Delegate = new PickerPresentationControllerDelegate
{
PickHandler = urls => tcs.TrySetResult(Enumerable.Empty<FileResult>())
PickHandler = urls =>
{
try
{
// there was a cancellation
tcs.TrySetResult(GetFileResults(urls));
}
catch (Exception ex)
{
// pass exception to task so that it doesn't get lost in the UI main loop
tcs.SetException(ex);
}
}
};
}

Expand All @@ -63,9 +72,17 @@ static Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool
return tcs.Task;
}

static IEnumerable<FileResult> GetFileResults(NSUrl[] urls)
{
if (urls?.Length > 0)
return urls.Select(url => new UIDocumentFileResult(url));
else
return Enumerable.Empty<FileResult>();
}

class PickerDelegate : UIDocumentPickerDelegate
{
public Action<IEnumerable<NSUrl>> PickHandler { get; set; }
public Action<NSUrl[]> PickHandler { get; set; }

public override void WasCancelled(UIDocumentPickerViewController controller)
=> PickHandler?.Invoke(null);
Expand All @@ -74,12 +91,12 @@ public override void DidPickDocument(UIDocumentPickerViewController controller,
=> PickHandler?.Invoke(urls);

public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)
=> PickHandler?.Invoke(new List<NSUrl> { url });
=> PickHandler?.Invoke(new NSUrl[] { url });
}

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

public override void DidDismiss(UIPresentationController presentationController) =>
PickHandler?.Invoke(null);
Expand Down
22 changes: 20 additions & 2 deletions Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,33 @@ static async Task<FileResult> PhotoAsync(MediaPickerOptions options, bool photo,
picker.Delegate = new PhotoPickerDelegate
{
CompletedHandler = info =>
tcs.TrySetResult(DictionaryToMediaFile(info))
{
try
{
tcs.TrySetResult(DictionaryToMediaFile(info));
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
};

if (picker.PresentationController != null)
{
picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
{
CompletedHandler = info =>
tcs.TrySetResult(DictionaryToMediaFile(info))
{
try
{
tcs.TrySetResult(DictionaryToMediaFile(info));
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
};
}

Expand Down

0 comments on commit ba91194

Please sign in to comment.