From 71a086b257e2533a32212df2e4e27a3bc96186d1 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Fri, 1 Sep 2023 14:12:35 +0300 Subject: [PATCH 1/2] fix: Fix "share sample" on Android by using protocol activation --- .../Android/MainActivity.Android.cs | 5 ++++ Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs | 26 ++++++++++++++++++- .../Controls/SamplePageLayout.cs | 14 ++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Uno.Gallery/Uno.Gallery.Mobile/Android/MainActivity.Android.cs b/Uno.Gallery/Uno.Gallery.Mobile/Android/MainActivity.Android.cs index bbf40f82e..f617d252b 100644 --- a/Uno.Gallery/Uno.Gallery.Mobile/Android/MainActivity.Android.cs +++ b/Uno.Gallery/Uno.Gallery.Mobile/Android/MainActivity.Android.cs @@ -11,6 +11,11 @@ namespace Uno.Gallery ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges, WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden )] + [IntentFilter(new[] { Android.Content.Intent.ActionView }, + Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, + DataScheme = "https", + DataHost = "unogallery.app.link", + AutoVerify = true)] public class MainActivity : Microsoft.UI.Xaml.ApplicationActivity { } diff --git a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs index f506b14be..2e12bcadf 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs @@ -75,6 +75,25 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) OnLaunchedOrActivated(); } + protected override void OnActivated(IActivatedEventArgs args) + { + base.OnActivated(args); + OnLaunchedOrActivated(); + + if (args.Kind == ActivationKind.Protocol) + { + var protocolActivatedEventArgs = (ProtocolActivatedEventArgs)args; + var uri = protocolActivatedEventArgs.Uri; + + // Handle uris on the form https://unogallery.app.link/designName/sampleName + if (uri.Host.Equals("unogallery.app.link", StringComparison.OrdinalIgnoreCase)) + { + var urlParts = uri.LocalPath.Split('/', StringSplitOptions.RemoveEmptyEntries); + TryNavigateToLaunchSample(title: urlParts[1], design: urlParts[0]); + } + } + } + private void OnLaunchedOrActivated() { #if WINDOWS && !HAS_UNO @@ -115,7 +134,7 @@ public static void TryNavigateToLaunchSample(string title, string design) var sample = GetSamples().FirstOrDefault(s => s.ViewType.Name.ToLowerInvariant() == title.ToLowerInvariant()); if (sample != null) { - if (HasValue(design) && Enum.TryParse(design, out var designType)) + if (HasValue(design) && Enum.TryParse(design, ignoreCase: true, out var designType)) { SamplePageLayout.SetPreferredDesign(designType); } @@ -415,6 +434,11 @@ private void ConfigureXamlDisplay() XamlDisplay.Init(GetType().Assembly); } + public static void OpenSample(string pageName, string designName) + { + + } + public static IEnumerable GetSamples() { return _samples = _samples ?? diff --git a/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs b/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs index 4ea485da7..687bbcf2f 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs @@ -13,6 +13,10 @@ using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Media; +#if __ANDROID__ +using Android.Content; +#endif + namespace Uno.Gallery { /// @@ -166,9 +170,15 @@ void OnScrolled(object sender, ScrollViewerViewChangedEventArgs e) private void OnShareClicked(Hyperlink sender, HyperlinkClickEventArgs args) { -#if (__IOS__ || __ANDROID__) && !NET6_0_OR_GREATER +#if __ANDROID__ var sample = DataContext as Sample; - _ = Deeplinking.BranchService.Instance.ShareSample(sample, _design); + var intent = new Intent(Intent.ActionSend); + intent.SetType("text/plain"); + intent.PutExtra(Intent.ExtraText, $"Check out this Uno Gallery page!{Environment.NewLine}https://unogallery.app.link/{_design.ToString().ToLowerInvariant()}/{sample.ViewType.Name.ToLowerInvariant()}"); + var chooserIntent = Intent.CreateChooser(intent, "Share Link"); + var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask; + chooserIntent.SetFlags(flags); + global::Android.App.Application.Context.StartActivity(chooserIntent); #endif } From 19c0c5fd2eba033d2ad0e138775e21d477b6bfee Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Fri, 1 Sep 2023 16:20:00 +0300 Subject: [PATCH 2/2] chore: Try for iOS, not working. --- .../Uno.Gallery.Mobile/iOS/Entitlements.plist | 8 +- .../Controls/SamplePageLayout.cs | 42 +++++++- .../Deeplinking/BranchService.cs | 101 ------------------ .../Uno.Gallery.Shared.projitems | 1 - .../Uno.Gallery.Shared/Views/Shell.xaml.cs | 5 - 5 files changed, 47 insertions(+), 110 deletions(-) delete mode 100644 Uno.Gallery/Uno.Gallery.Shared/Deeplinking/BranchService.cs diff --git a/Uno.Gallery/Uno.Gallery.Mobile/iOS/Entitlements.plist b/Uno.Gallery/Uno.Gallery.Mobile/iOS/Entitlements.plist index 24c310368..657bcedc0 100644 --- a/Uno.Gallery/Uno.Gallery.Mobile/iOS/Entitlements.plist +++ b/Uno.Gallery/Uno.Gallery.Mobile/iOS/Entitlements.plist @@ -1,6 +1,10 @@ - - + + com.apple.developer.associated-domains + + applinks:unogallery.app.link + + diff --git a/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs b/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs index 687bbcf2f..80f3a5ab5 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs @@ -12,9 +12,12 @@ using Microsoft.UI.Xaml.Data; using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Media; - #if __ANDROID__ using Android.Content; +#elif __IOS__ +using UIKit; +using Foundation; +using LinkPresentation; #endif namespace Uno.Gallery @@ -179,9 +182,46 @@ private void OnShareClicked(Hyperlink sender, HyperlinkClickEventArgs args) var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask; chooserIntent.SetFlags(flags); global::Android.App.Application.Context.StartActivity(chooserIntent); +#elif false // __IOS__ , not working. + UIResponder responder = this; + + while (responder is UIView nativeView) + { + responder = nativeView.NextResponder; + } + + if (responder is UIViewController vc) + { + var sample = DataContext as Sample; + var activityController = new UIActivityViewController(new NSObject[] { new ShareableSample(_design.ToString(), sample.ViewType.Name) }, null); + activityController.PopoverPresentationController.SourceView = vc.View; + vc.PresentViewController(activityController, animated: true, null); + } #endif } +#if false // __IOS__ , not working. + private sealed class ShareableSample : UIActivityItemSource + { + public ShareableSample(string designName, string sampleName) + { + Subtitle = $"Check out this Uno Gallery page!{Environment.NewLine}https://unogallery.app.link/{designName.ToLowerInvariant()}/{sampleName.ToLowerInvariant()}"; + } + + public string Title => "Share Link"; + public string Subtitle { get; } + + public override NSObject GetPlaceholderData(UIActivityViewController activityViewController) => new NSString(Title); + + public override LPLinkMetadata GetLinkMetadata(UIActivityViewController activityViewController) + { + var metadata = new LPLinkMetadata(); + metadata.Title = Title; + return metadata; + } + } +#endif + /// /// Changes the preferred design. /// This doesn't change the current UI. It only affects the next created sample. diff --git a/Uno.Gallery/Uno.Gallery.Shared/Deeplinking/BranchService.cs b/Uno.Gallery/Uno.Gallery.Shared/Deeplinking/BranchService.cs deleted file mode 100644 index 5e609f0f5..000000000 --- a/Uno.Gallery/Uno.Gallery.Shared/Deeplinking/BranchService.cs +++ /dev/null @@ -1,101 +0,0 @@ -#if (__IOS__ || __ANDROID__) && !NET6_0_OR_GREATER -using BranchXamarinSDK; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Uno.Extensions; -using Uno.Logging; -using Microsoft.UI.Xaml; -using Xamarin.Essentials; - -namespace Uno.Gallery.Deeplinking -{ - public class BranchService : IBranchBUOSessionInterface, IBranchUrlInterface - { - public const string BranchKey = "key_live_eg8jUSrrIb5TEDaviMJkXbccCDi0y7wn"; - - public static BranchService Instance { get; } = new BranchService(); - - private TaskCompletionSource _uriTcs; - private TaskCompletionSource _isAppReadyTcs = new TaskCompletionSource(); - - public async void InitSessionComplete(BranchUniversalObject buo, BranchLinkProperties blp) - { - // Gets the requested sample and design, then navigates to it. - - this.Log().Debug("Branch initialization completed."); - - var pageName = string.Empty; - if (blp?.controlParams?.TryGetValue("$deeplink_path", out pageName) ?? false) - { - var sample = App.GetSamples().FirstOrDefault(s => s.ViewType.Name.ToLowerInvariant() == pageName.ToLowerInvariant()); - if (sample != null) - { - if (blp.controlParams.TryGetValue("$design", out var designName) && Enum.TryParse(designName, out var design)) - { - SamplePageLayout.SetPreferredDesign(design); - } - - await _isAppReadyTcs.Task; - - this.Log().Debug($"Navigating to {sample.ViewType.Name} from deeplink."); - - (Application.Current as App)?.ShellNavigateTo(sample); - - this.Log().Info($"Navigated to {sample.ViewType.Name} from deeplink."); - } - } - } - - public void SessionRequestError(BranchError error) - { - this.Log().Error("Branch error: " + error.ErrorCode + '\n' + error.ErrorMessage); - } - - /// - /// Creates a branch.io link to the specified sample and design, then shares it. - /// - public async Task ShareSample(Sample sample, Design design) - { - var pageName = sample.ViewType.Name.ToLowerInvariant(); - var buo = new BranchUniversalObject() - { - title = $"{design} {sample.Title}", - canonicalIdentifier = Guid.NewGuid().ToString(), - }; - - var blp = new BranchLinkProperties(); - blp.feature = "sharing"; - blp.controlParams["$deeplink_path"] = pageName; - blp.controlParams["$design"] = design.ToString(); - - _uriTcs = new TaskCompletionSource(); - Branch.GetInstance().GetShortURL(this, buo, blp); - var uri = await _uriTcs.Task; - await Share.RequestAsync(new ShareTextRequest - { - Title = "Share Link", - Text = "Check out this Uno Gallery page!", - Uri = uri, - }); - _uriTcs = null; - } - - public void SetIsAppReady() - { - _isAppReadyTcs.TrySetResult(true); - } - - void IBranchUrlInterface.ReceivedUrl(string uri) - { - _uriTcs?.TrySetResult(uri); - } - - void IBranchUrlInterface.UrlRequestError(BranchError error) - { - } - } -} -#endif \ No newline at end of file diff --git a/Uno.Gallery/Uno.Gallery.Shared/Uno.Gallery.Shared.projitems b/Uno.Gallery/Uno.Gallery.Shared/Uno.Gallery.Shared.projitems index 3342b9751..76fff99fa 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Uno.Gallery.Shared.projitems +++ b/Uno.Gallery/Uno.Gallery.Shared/Uno.Gallery.Shared.projitems @@ -157,7 +157,6 @@ - diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/Shell.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/Views/Shell.xaml.cs index 7a7876459..e4cba9c81 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Views/Shell.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/Shell.xaml.cs @@ -45,11 +45,6 @@ public string CurrentSampleBackdoor private void OnLoaded(object sender, RoutedEventArgs e) { SetDarkLightToggleInitialState(); - -#if (__IOS__ || __ANDROID__) && !NET6_0_OR_GREATER - this.Log().Debug("Loaded Shell."); - Uno.Gallery.Deeplinking.BranchService.Instance.SetIsAppReady(); -#endif } private void SetDarkLightToggleInitialState()