Redirect exceptions from the pickers into the TCS (#1547)
Exceptions thrown in the callbacks will not reach the end user, and the task may not even return.
This commit is contained in:
Родитель
439839d8ae
Коммит
ba911949b6
|
@ -27,7 +27,16 @@ namespace Xamarin.Essentials
|
|||
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);
|
||||
|
@ -80,31 +89,20 @@ namespace Xamarin.Essentials
|
|||
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()));
|
||||
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();
|
||||
}
|
||||
return new Contact(
|
||||
contact.Identifier,
|
||||
contact.NamePrefix,
|
||||
contact.GivenName,
|
||||
contact.MiddleName,
|
||||
contact.FamilyName,
|
||||
contact.NameSuffix,
|
||||
phones,
|
||||
emails);
|
||||
}
|
||||
|
||||
#if __IOS__
|
||||
|
|
|
@ -35,10 +35,7 @@ namespace Xamarin.Essentials
|
|||
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)
|
||||
{
|
||||
|
@ -52,7 +49,19 @@ namespace Xamarin.Essentials
|
|||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -63,9 +72,17 @@ namespace Xamarin.Essentials
|
|||
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);
|
||||
|
@ -74,12 +91,12 @@ namespace Xamarin.Essentials
|
|||
=> 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);
|
||||
|
|
|
@ -66,7 +66,16 @@ namespace Xamarin.Essentials
|
|||
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)
|
||||
|
@ -74,7 +83,16 @@ namespace Xamarin.Essentials
|
|||
picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
|
||||
{
|
||||
CompletedHandler = info =>
|
||||
tcs.TrySetResult(DictionaryToMediaFile(info))
|
||||
{
|
||||
try
|
||||
{
|
||||
tcs.TrySetResult(DictionaryToMediaFile(info));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче