Skip to content

Commit

Permalink
perf: Add ContactPicker bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed May 19, 2023
1 parent b02b729 commit b9571c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ internal partial class ContactPicker
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Windows.ApplicationModel.Contacts.ContactPicker";

[JSImport($"{JsType}.isSupported")]
internal static partial bool IsSupported();

[JSImport($"{JsType}.pickContacts")]
internal static partial Task<string> PickContactsAsync(bool multiple);
}
}
}
#endif
#endif
14 changes: 13 additions & 1 deletion src/Uno.UWP/ApplicationModel/Contacts/ContactPicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,29 @@ namespace Windows.ApplicationModel.Contacts
{
public partial class ContactPicker
{
#if !NET7_0_OR_GREATER
private const string JsType = "Windows.ApplicationModel.Contacts.ContactPicker";
#endif

private static Task<bool> IsSupportedTaskAsync(CancellationToken token)
{
#if NET7_0_OR_GREATER
return Task.FromResult(NativeMethods.IsSupported());
#else
var isSupportedString = WebAssemblyRuntime.InvokeJS($"{JsType}.isSupported()");
return Task.FromResult(bool.TryParse(isSupportedString, out var isSupported) && isSupported);
#endif
}

private async Task<Contact[]> PickContactsAsync(bool multiple, CancellationToken token)
{
var pickResultJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.pickContacts({(multiple ? "true" : "false")})");
var pickResultJson = await
#if NET7_0_OR_GREATER
NativeMethods.PickContactsAsync(multiple);
#else
WebAssemblyRuntime.InvokeAsync($"{JsType}.pickContacts({(multiple ? "true" : "false")})");
#endif

if (string.IsNullOrEmpty(pickResultJson) || token.IsCancellationRequested)
{
return Array.Empty<Contact>();
Expand Down

0 comments on commit b9571c7

Please sign in to comment.