Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix "share sample" on Android by using protocol activation #942

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Expand Down
8 changes: 6 additions & 2 deletions Uno.Gallery/Uno.Gallery.Mobile/iOS/Entitlements.plist
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
<dict>
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:unogallery.app.link</string>
</array>
</dict>
</plist>
26 changes: 25 additions & 1 deletion Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>(design, out var designType))
if (HasValue(design) && Enum.TryParse<Design>(design, ignoreCase: true, out var designType))
{
SamplePageLayout.SetPreferredDesign(designType);
}
Expand Down Expand Up @@ -415,6 +434,11 @@ private void ConfigureXamlDisplay()
XamlDisplay.Init(GetType().Assembly);
}

public static void OpenSample(string pageName, string designName)
{

}

public static IEnumerable<Sample> GetSamples()
{
return _samples = _samples ??
Expand Down
54 changes: 52 additions & 2 deletions Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
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
{
Expand Down Expand Up @@ -166,12 +173,55 @@ 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);
#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

/// <summary>
/// Changes the preferred design.
/// This doesn't change the current UI. It only affects the next created sample.
Expand Down
101 changes: 0 additions & 101 deletions Uno.Gallery/Uno.Gallery.Shared/Deeplinking/BranchService.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Converters\HexToColorConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\RandomColorConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\SecretConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Deeplinking\BranchService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Domain\AnalyticsService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Entities\Data\TreeItem.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Entities\Data\Folder.cs" />
Expand Down
5 changes: 0 additions & 5 deletions Uno.Gallery/Uno.Gallery.Shared/Views/Shell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down