Exemplo n.º 1
0
        private static async Task <bool> ShowShareUIAsync(ShareUIOptions options, DataPackage dataPackage)
        {
            var dataPackageView = dataPackage.GetView();

            var title = dataPackage.Properties.Title != null ? $"\"{WebAssemblyRuntime.EscapeJs(dataPackage.Properties.Title)}\"" : null;

            string?text;

            if (dataPackageView.Contains(StandardDataFormats.Text))
            {
                text = await dataPackageView.GetTextAsync();
            }
            else
            {
                text = dataPackage.Properties.Description;
            }
            text = text != null ? $"\"{WebAssemblyRuntime.EscapeJs(text)}\"" : null;

            var uri = await GetSharedUriAsync(dataPackageView);

            var uriText = uri != null ? $"\"{WebAssemblyRuntime.EscapeJs(uri.OriginalString)}\"" : null;

            var result = await WebAssemblyRuntime.InvokeAsync($"{JsType}.showShareUI({title ?? "null"},{text ?? "null"},{uriText ?? "null"})");

            return(bool.TryParse(result, out var boolResult) && boolResult);
        }
Exemplo n.º 2
0
        private static async void ContinueShowShareUI(ShareUIOptions options, DataPackage dataPackage)
        {
            try
            {
                // Because showing the Share UI is a fire-and-forget operation
                // and retrieving data from DataPackage requires async-await,
                // this method must be async void.
                var result = await ShowShareUIAsync(options, dataPackage);

                if (result)
                {
                    dataPackage.OnShareCompleted();
                }
                else
                {
                    dataPackage.OnShareCanceled();
                }
            }
            catch (Exception ex)
            {
                if (_instance.Value.Log().IsEnabled(LogLevel.Error))
                {
                    _instance.Value.Log().LogError($"Exception occurred trying to show share UI: {ex}");
                }
                dataPackage.OnShareCanceled();
            }
        }
Exemplo n.º 3
0
        private static async Task <bool> ShowShareUIAsync(ShareUIOptions options, DataPackage dataPackage)
        {
            var window = NSApplication.SharedApplication.MainWindow;

            if (window == null)
            {
                throw new InvalidOperationException("Sharing is not possible when no window is active.");
            }

            var view = window.ContentView;

            var dataPackageView = dataPackage.GetView();

            var sharedData = new List <NSObject>();

            if (dataPackageView.Contains(StandardDataFormats.Text))
            {
                var text = await dataPackageView.GetTextAsync();

                sharedData.Add(new NSString(text));
            }

            var uri = await GetSharedUriAsync(dataPackageView);

            if (uri != null)
            {
                sharedData.Add(NSUrl.FromString(uri.OriginalString));
            }

            CGRect targetRect;

            if (options.SelectionRect != null)
            {
                targetRect = options.SelectionRect.Value;
            }
            else
            {
                // Try to center the picker within the window
                targetRect = new CGRect(
                    view.Bounds.Width / 2f - DefaultPickerWidth / 2,
                    view.Bounds.Height / 2 - DefaultPickerHeight / 2,
                    0,
                    0);
            }

            var picker = new NSSharingServicePicker(sharedData.ToArray());

            var completionSource = new TaskCompletionSource <bool>();

            picker.DidChooseSharingService += (s, e) =>
            {
                completionSource.SetResult(e.Service != null);
            };

            picker.ShowRelativeToRect(targetRect, view, NSRectEdge.MinYEdge);

            return(await completionSource.Task);
        }
Exemplo n.º 4
0
        public static void ShowShareUI(ShareUIOptions options)
        {
            var dataTransferManager = _instance.Value;
            var args = new DataRequestedEventArgs((dataRequest) => ContinueShowShareUI(options, dataRequest.Data));

            dataTransferManager.DataRequested?.Invoke(dataTransferManager, args);

            // Data retrieval may be done asynchronously
            args.Request.DeferralManager.EventRaiseCompleted();
        }
        private static async Task <bool> ShowShareUIAsync(ShareUIOptions options, DataPackage dataPackage)
        {
            var context = ContextHelper.Current;

            if (context == null)
            {
                if (_instance.Value.Log().IsEnabled(LogLevel.Error))
                {
                    _instance.Value.Log().LogError("The Share API was called too early in the application lifecycle");
                }
                return(false);
            }

            var dataPackageView = dataPackage.GetView();
            var items           = new List <string>();

            if (dataPackageView.Contains(StandardDataFormats.Text))
            {
                var text = await dataPackageView.GetTextAsync();

                items.Add(text);
            }

            var uri = await GetSharedUriAsync(dataPackageView);

            if (uri != null)
            {
                items.Add(uri.OriginalString);
            }

            var intent = new Intent(Intent.ActionSend);

            intent.SetType("text/plain");
            intent.PutExtra(Intent.ExtraText, string.Join(Environment.NewLine, items));

            var title = dataPackage.Properties.Title;

            if (!string.IsNullOrWhiteSpace(title))
            {
                intent.PutExtra(Intent.ExtraSubject, title);
            }

            var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
            var flags         = ActivityFlags.ClearTop | ActivityFlags.NewTask;

            chooserIntent?.SetFlags(flags);
            ContextHelper.Current.StartActivity(chooserIntent);

            return(true);
        }
Exemplo n.º 6
0
        private static async Task <bool> ShowShareUIAsync(ShareUIOptions options, DataPackage dataPackage)
        {
            var rootViewController = UIApplication.SharedApplication?.KeyWindow?.RootViewController;

            if (rootViewController == null)
            {
                if (_instance.Value.Log().IsEnabled(LogLevel.Error))
                {
                    _instance.Value.Log().LogError("The Share API was called too early in the application lifecycle");
                }
                return(false);
            }

            var dataPackageView = dataPackage.GetView();

            var sharedData = new List <NSObject>();

            var title = dataPackage.Properties.Title ?? string.Empty;

            if (dataPackageView.Contains(StandardDataFormats.Text))
            {
                var text = await dataPackageView.GetTextAsync();

                sharedData.Add(new DataActivityItemSource(new NSString(text), title));
            }

            var uri = await GetSharedUriAsync(dataPackageView);

            if (uri != null)
            {
                sharedData.Add(new DataActivityItemSource(NSUrl.FromString(uri.OriginalString), title));
            }

            var activityViewController = new UIActivityViewController(sharedData.ToArray(), null);

            if (activityViewController.PopoverPresentationController != null && rootViewController.View != null)
            {
                activityViewController.PopoverPresentationController.SourceView = rootViewController.View;

                if (options.SelectionRect != null)
                {
                    activityViewController.PopoverPresentationController.SourceRect = options.SelectionRect.Value.ToCGRect();
                }
                else
                {
                    activityViewController.PopoverPresentationController.SourceRect = new CGRect(rootViewController.View.Bounds.Width / 2, rootViewController.View.Bounds.Height / 2, 0, 0);
                    activityViewController.PopoverPresentationController.PermittedArrowDirections = 0;
                }
            }

            if (options.Theme != ShareUITheme.Default)
            {
                activityViewController.OverrideUserInterfaceStyle = options.Theme == ShareUITheme.Light ? UIUserInterfaceStyle.Light : UIUserInterfaceStyle.Dark;
            }
            else
            {
                // Theme should match the application theme
                activityViewController.OverrideUserInterfaceStyle = CoreApplication.RequestedTheme == SystemTheme.Light ? UIUserInterfaceStyle.Light : UIUserInterfaceStyle.Dark;
            }

            var completionSource = new TaskCompletionSource <bool>();

            activityViewController.CompletionWithItemsHandler = (NSString activityType, bool completed, NSExtensionItem[] returnedItems, NSError error) =>
            {
                completionSource.SetResult(completed);
            };

            await rootViewController.PresentViewControllerAsync(activityViewController, true);

            return(await completionSource.Task);
        }