-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DataTransferManager support on iOS
- Adds support for DataTransferManager on iOS - Includes ShareUIOptions for target rect and dark/light theme
- Loading branch information
1 parent
97ccc3f
commit bfe6ec9
Showing
13 changed files
with
262 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 55 additions & 2 deletions
57
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,68 @@ | ||
#if __WASM__ | ||
#nullable enable | ||
|
||
#if __WASM__ || __IOS__ | ||
using System; | ||
using Windows.Foundation; | ||
using Uno.Logging; | ||
using Microsoft.Extensions.Logging; | ||
using Uno.Extensions; | ||
using System.Threading.Tasks; | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class DataTransferManager | ||
{ | ||
private static Lazy<DataTransferManager> _instance = new Lazy<DataTransferManager>(() => new DataTransferManager()); | ||
|
||
private DataTransferManager() | ||
{ | ||
} | ||
|
||
public event TypedEventHandler<DataTransferManager, DataRequestedEventArgs> DataRequested; | ||
public event TypedEventHandler<DataTransferManager, DataRequestedEventArgs>? DataRequested; | ||
|
||
public static DataTransferManager GetForCurrentView() => _instance.Value; | ||
|
||
public static void ShowShareUI() => ShowShareUI(new ShareUIOptions()); | ||
|
||
public static async void ShowShareUI(ShareUIOptions options) | ||
{ | ||
var dataTransferManager = _instance.Value; | ||
var args = new DataRequestedEventArgs(); | ||
dataTransferManager.DataRequested?.Invoke(dataTransferManager, args); | ||
var dataPackage = args.Request.Data; | ||
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. | ||
await ShowShareUIAsync(options, dataPackage); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (dataTransferManager.Log().IsEnabled(LogLevel.Error)) | ||
{ | ||
dataTransferManager.Log().LogError($"Exception occurred trying to show share UI: {ex}"); | ||
} | ||
} | ||
} | ||
|
||
private static async Task<Uri?> GetSharedUriAsync(DataPackageView view) | ||
{ | ||
if (view.Contains(StandardDataFormats.Uri)) | ||
{ | ||
return await view.GetUriAsync(); | ||
} | ||
else if (view.Contains(StandardDataFormats.WebLink)) | ||
{ | ||
return await view.GetWebLinkAsync(); | ||
} | ||
else if (view.Contains(StandardDataFormats.ApplicationLink)) | ||
{ | ||
return await view.GetApplicationLinkAsync(); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
#endif |
114 changes: 114 additions & 0 deletions
114
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.iOS.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using CoreGraphics; | ||
using Foundation; | ||
using Microsoft.Extensions.Logging; | ||
using UIKit; | ||
using Uno.Extensions; | ||
using Uno.Helpers.Theming; | ||
using Windows.ApplicationModel.Core; | ||
using Windows.Foundation; | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class DataTransferManager | ||
{ | ||
public static bool IsSupported() => true; | ||
|
||
private static async Task 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; | ||
} | ||
|
||
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.ToString()), 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); | ||
|
||
var result = await completionSource.Task; | ||
|
||
if (result) | ||
{ | ||
dataPackage.OnShareCompleted(); | ||
} | ||
else | ||
{ | ||
dataPackage.OnShareCanceled(); | ||
} | ||
} | ||
|
||
internal class DataActivityItemSource : UIActivityItemSource | ||
{ | ||
private NSObject _data; | ||
private string _title; | ||
|
||
internal DataActivityItemSource(NSObject data, string title) => | ||
(_data, _title) = (data, title); | ||
|
||
public override NSObject GetItemForActivity(UIActivityViewController activityViewController, NSString? activityType) => _data; | ||
|
||
public override string GetSubjectForActivity(UIActivityViewController activityViewController, NSString? activityType) => _title; | ||
|
||
public override NSObject GetPlaceholderData(UIActivityViewController activityViewController) => _data; | ||
} | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Uno.UWP/ApplicationModel/DataTransfer/ShareUIOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Windows.Foundation; | ||
|
||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public partial class ShareUIOptions | ||
{ | ||
public ShareUIOptions() | ||
{ | ||
} | ||
|
||
#if __IOS__ | ||
|
||
public ShareUITheme Theme { get; set; } | ||
|
||
public Rect? SelectionRect { get; set; } | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Windows.ApplicationModel.DataTransfer | ||
{ | ||
public enum ShareUITheme | ||
{ | ||
Default, | ||
Light, | ||
Dark, | ||
} | ||
} |
Oops, something went wrong.